From 3a4faf2893f18d549121b56baf539dd4f67a0991 Mon Sep 17 00:00:00 2001 From: Brant Wheeler Date: Thu, 22 Jan 2015 22:18:15 -0600 Subject: [PATCH] Second revision --- .../ShouldLookLikeExtensionsSpecs.cs | 5 +- .../ShouldLooksLikeExtensions.cs | 62 ++++++++++++------- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/SpecsFor.Tests/ShouldExtensions/ShouldLookLikeExtensionsSpecs.cs b/SpecsFor.Tests/ShouldExtensions/ShouldLookLikeExtensionsSpecs.cs index ec59866..859339d 100644 --- a/SpecsFor.Tests/ShouldExtensions/ShouldLookLikeExtensionsSpecs.cs +++ b/SpecsFor.Tests/ShouldExtensions/ShouldLookLikeExtensionsSpecs.cs @@ -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() } }; @@ -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] diff --git a/SpecsFor/ShouldExtensions/ShouldLooksLikeExtensions.cs b/SpecsFor/ShouldExtensions/ShouldLooksLikeExtensions.cs index ef08da9..fad2b88 100644 --- a/SpecsFor/ShouldExtensions/ShouldLooksLikeExtensions.cs +++ b/SpecsFor/ShouldExtensions/ShouldLooksLikeExtensions.cs @@ -39,30 +39,48 @@ public static void ShouldLookLike(this T actual, Expression> matchFun } } - private static void ShouldMatchList(IEnumerable actual, Expression expression) - { - var expected = (IEnumerable)Expression.Lambda>(expression).Compile()(); - actual.ShouldLookLike(expected); - } + private static void ShouldMatchIEnumerable(IEnumerable actual, NewArrayExpression arrayExpression) + { + var array = actual.Cast().ToArray(); - private static void ShouldMatchIEnumerable(IEnumerable actual, NewArrayExpression arrayExpression) + if (arrayExpression.Expressions.Any(x => !(x is MemberInitExpression))) + { + var expected = (IEnumerable)Expression.Lambda>(arrayExpression).Compile()(); + var expectedArray = expected.Cast().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().ToArray(); - if (arrayExpression.Expressions.Any(x => !(x is MemberInitExpression))) - { - var expected = (IEnumerable)Expression.Lambda>(arrayExpression).Compile()(); - var expectedArray = expected.Cast().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>(memberAssignment.Expression).Compile()(); + var expectedArray = expected.Cast().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) @@ -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."); @@ -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 {