Skip to content

Commit

Permalink
Second revision
Browse files Browse the repository at this point in the history
  • Loading branch information
brantw committed Jan 23, 2015
1 parent fd0717e commit 3a4faf2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ public void then_it_should_work_with_list_objects()
new TestObject
{
Awesomeness = 1000,
Name = "Awesome list item"
Name = "Awesome list item",
TestObjectId = Guid.NewGuid()
}
};

Expand Down Expand Up @@ -484,7 +485,7 @@ public void then_it_should_fail_with_list_objects_that_do_not_match()
}
}));

ex.Message.ShouldEqual("For List`1[0].Awesomeness, expected [1000] but found [10].\r\n");
ex.Message.ShouldEqual("For Object[][0].Awesomeness, expected [1000] but found [10].\r\n");
}

[Test]
Expand Down
62 changes: 38 additions & 24 deletions SpecsFor/ShouldExtensions/ShouldLooksLikeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,48 @@ public static void ShouldLookLike<T>(this T actual, Expression<Func<T>> matchFun
}
}

private static void ShouldMatchList(IEnumerable actual, Expression expression)
{
var expected = (IEnumerable)Expression.Lambda<Func<object>>(expression).Compile()();
actual.ShouldLookLike(expected);
}
private static void ShouldMatchIEnumerable(IEnumerable actual, NewArrayExpression arrayExpression)
{
var array = actual.Cast<object>().ToArray();

private static void ShouldMatchIEnumerable(IEnumerable actual, NewArrayExpression arrayExpression)
if (arrayExpression.Expressions.Any(x => !(x is MemberInitExpression)))
{
var expected = (IEnumerable)Expression.Lambda<Func<object>>(arrayExpression).Compile()();
var expectedArray = expected.Cast<object>().ToArray();

array.ShouldLookLike(expectedArray);

return;
}

for (int i = 0; i < arrayExpression.Expressions.Count; i++)
{
ShouldMatch(array[i], arrayExpression.Expressions[i] as MemberInitExpression);
}
}

private static void ShouldMatchIEnumerable(IEnumerable actual, MemberAssignment memberAssignment)
{
var array = actual.Cast<object>().ToArray();

if (arrayExpression.Expressions.Any(x => !(x is MemberInitExpression)))
{
var expected = (IEnumerable)Expression.Lambda<Func<object>>(arrayExpression).Compile()();
var expectedArray = expected.Cast<object>().ToArray();
var arrayExpression = memberAssignment.Expression as NewArrayExpression;

array.ShouldLookLike(expectedArray);

return;
}
if (arrayExpression != null && memberAssignment.Expression.NodeType == ExpressionType.NewArrayInit)
{
ShouldMatchIEnumerable(array, arrayExpression);
return;
}

for (int i = 0; i < arrayExpression.Expressions.Count; i++)
{
ShouldMatch(array[i], arrayExpression.Expressions[i] as MemberInitExpression);
}
// some check here to enable partial matching?
var expected = (IEnumerable)Expression.Lambda<Func<object>>(memberAssignment.Expression).Compile()();
var expectedArray = expected.Cast<object>().ToArray();

array.ShouldLookLike(expectedArray);

//for (int i = 0; i < arrayExpression.Expressions.Count; i++)
//{
// ShouldMatch(array[i], arrayExpression.Expressions[i] as MemberInitExpression);
//}
}

private static void ShouldMatch(object actual, MemberInitExpression expression)
Expand All @@ -82,10 +100,6 @@ private static void ShouldMatch(object actual, MemberInitExpression expression)
{
ShouldMatch(actualValue, bindingAsAnotherExpression.Expression as MemberInitExpression);
}
else if (bindingAsAnotherExpression != null && bindingAsAnotherExpression.Expression.NodeType == ExpressionType.NewArrayInit)
{
ShouldMatchIEnumerable(actualValue as IEnumerable, bindingAsAnotherExpression.Expression as NewArrayExpression);
}
else if (IsMoqExpression(bindingAsAnotherExpression))
{
Assert.Fail("Moq's matchers cannot be used with SpecsFor's partial matching. Instead, use the Some.Of and Any.Of methods in SpecsFor.");
Expand All @@ -105,9 +119,9 @@ private static void ShouldMatch(object actual, MemberInitExpression expression)
throw new EqualException(Matcher.LastMatcher.ToString(), actualValue);
}
}
else if (bindingAsAnotherExpression != null && actualValue.GetType().IsGenericType && actualValue.GetType().GetGenericTypeDefinition() == typeof(List<>))
else if (bindingAsAnotherExpression != null && actualValue as IEnumerable != null)
{
ShouldMatchList(actualValue as IEnumerable, bindingAsAnotherExpression.Expression);
ShouldMatchIEnumerable(actualValue as IEnumerable, bindingAsAnotherExpression);
}
else
{
Expand Down

0 comments on commit 3a4faf2

Please sign in to comment.