Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AssertConversion multiples assert but with right feedback :P
public class AssertProperties<T>
{
private readonly ICollection<string> _errors = new List<string>();
public class AssertValue<TResult>
{
private readonly object _value;
private readonly string _propertName;
private readonly AssertProperties<T> _parent;
public AssertValue(string propertName, object value, AssertProperties<T> parent)
{
_propertName = propertName;
_value = value;
_parent = parent;
}
public AssertProperties<T> ShouldBeNull()
{
if (_value != null)
{
_parent.AddError(string.Format("{0} expected to be NULL but was {1}", _propertName, _value));
}
return _parent;
}
public AssertProperties<T> ShouldBe(TResult expectedValue)
{
if (expectedValue == null && _value == null)
{
return _parent;
}
if (expectedValue == null || _value == null || !expectedValue.Equals(_value))
{
_parent.AddError(string.Format("{0} expected to be {1} but was {2}", _propertName, expectedValue, _value));
}
return _parent;
}
}
private void AddError(string error)
{
_errors.Add(error);
}
private readonly T _object;
public AssertProperties(T @object)
{
_object = @object;
}
public AssertValue<TResult> EnsureThat<TResult>(Expression<Func<T, TResult>> expression)
{
MemberExpression memberExpression = null;
if (expression.Body.NodeType == ExpressionType.Convert)
memberExpression = ((UnaryExpression)expression.Body).Operand as MemberExpression;
else if (expression.Body.NodeType == ExpressionType.MemberAccess)
memberExpression = expression.Body as MemberExpression;
if (memberExpression == null || memberExpression.Member == null)
throw new ArgumentNullException("expression", "Not a member access!");
var propertyInfo = memberExpression.Member as PropertyInfo;
if (propertyInfo == null)
{
throw new InvalidOperationException("Could not found property name");
}
var function = expression.Compile();
var value = function.Invoke(_object);
return new AssertValue<TResult>(propertyInfo.Name, value, this);
}
public AssertValue<TResult> And<TResult>(Expression<Func<T, TResult>> expression)
{
return EnsureThat(expression);
}
public void Assert()
{
if (!_errors.Any())
{
return;
}
var sb = new StringBuilder();
foreach (var error in _errors)
{
sb.AppendLine(error);
}
throw new AssertExpcetion(sb.ToString());
}
public class AssertExpcetion : Exception
{
public AssertExpcetion(string message)
: base(message)
{
}
}
}
public static class AssertProperties
{
public static AssertProperties<T> From<T>(T objectValue)
{
return new AssertProperties<T>(objectValue);
}
}
public static class AssertPropertyExtensions
{
public static AssertProperties<T> AssertProperties<T>(this T objectValue)
{
return new AssertProperties<T>(objectValue);
}
}
/*
new ObjectA().AsserProperties()
.EnsureThat(o=>o.Property).ShouldBe("value")
.And(o=>o.PropertyInt).ShouldBe(1)
.And(o=>o.Name).ShouldBeNull()
.Assert();
*/
@wildbrain72

This comment has been minimized.

@wildbrain72

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.