Skip to content

Instantly share code, notes, and snippets.

@lfreneda
Forked from wildbrain72/gist:7afc91742858e29e9836
Last active August 29, 2015 14:11
Show Gist options
  • Save lfreneda/a4a724d455e532809ae0 to your computer and use it in GitHub Desktop.
Save lfreneda/a4a724d455e532809ae0 to your computer and use it in GitHub Desktop.
AssertProperties.cs
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> ShouldBe(TResult expectedValue)
{
if (!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)
{
}
}
}
/*
new AssertConvertion<ObjectA>(new ObjectA())
.EnsureThat(o=>o.Property).ShouldBe("value")
.And(o=>o.PropertyInt).ShouldBe(1)
.Assert();
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment