From 424708608ad175fca78660870bda21e62afc9a0d Mon Sep 17 00:00:00 2001 From: Dennis Doomen Date: Sun, 18 Nov 2018 22:20:18 +0100 Subject: [PATCH] Allows BeEquivalentTo to handle a non-generic collection as the SUT --- .../GenericEnumerableEquivalencyStep.cs | 16 ++++--- .../CollectionEquivalencySpecs.cs | 44 ++++++++----------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/Src/FluentAssertions/Equivalency/GenericEnumerableEquivalencyStep.cs b/Src/FluentAssertions/Equivalency/GenericEnumerableEquivalencyStep.cs index e584047988..d80929327e 100644 --- a/Src/FluentAssertions/Equivalency/GenericEnumerableEquivalencyStep.cs +++ b/Src/FluentAssertions/Equivalency/GenericEnumerableEquivalencyStep.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; @@ -61,7 +62,7 @@ public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAsserti MethodCallExpression executeExpression = Expression.Call( Expression.Constant(validator), ExpressionExtensions.GetMethodName(() => validator.Execute(null, null)), - new[] { typeOfEnumeration }, + new[] {typeOfEnumeration}, subjectAsArray, expectationAsArray); @@ -82,18 +83,23 @@ private static bool AssertSubjectIsCollection(object expectation, object subject { bool conditionMet = AssertionScope.Current .ForCondition(!(subject is null)) - .FailWith("Expected {context:Subject} not to be {0}.", new object[] { null }); + .FailWith("Expected {context:subject} not to be {0}.", new object[] {null}); if (conditionMet) { conditionMet = AssertionScope.Current - .ForCondition(IsGenericCollection(subject.GetType())) - .FailWith("Expected {context:Subject} to be {0}, but found {1}.", expectation, subject); + .ForCondition(IsCollection(subject.GetType())) + .FailWith("Expected {context:subject} to be a collection, but it was a {0}", subject.GetType()); } return conditionMet; } + private static bool IsCollection(Type type) + { + return !typeof(string).IsAssignableFrom(type) && typeof(IEnumerable).IsAssignableFrom(type); + } + private static bool IsGenericCollection(Type type) { var enumerableInterfaces = GetIEnumerableInterfaces(type); @@ -120,7 +126,7 @@ private static MethodCallExpression ToArray(object value, Type typeOfEnumeration return Expression.Call( typeof(Enumerable), "ToArray", - new[] { typeOfEnumeration }, + new[] {typeOfEnumeration}, Expression.Constant(value, typeof(IEnumerable<>).MakeGenericType(typeOfEnumeration))); } } diff --git a/Tests/Shared.Specs/CollectionEquivalencySpecs.cs b/Tests/Shared.Specs/CollectionEquivalencySpecs.cs index 659be3f88c..cdbce1a0af 100644 --- a/Tests/Shared.Specs/CollectionEquivalencySpecs.cs +++ b/Tests/Shared.Specs/CollectionEquivalencySpecs.cs @@ -649,6 +649,24 @@ public void .WithMessage("*Customers[0].Name*John*Jane*"); } + + [Fact] + public void When_the_subject_is_a_non_generic_collection_it_should_still_work() + { + //----------------------------------------------------------------------------------------------------------- + // Arrange + //----------------------------------------------------------------------------------------------------------- + object item = new object(); + object[] array = new[] { item }; + IList readOnlyList = ArrayList.ReadOnly(array); + + + //----------------------------------------------------------------------------------------------------------- + // Act / Assert + //----------------------------------------------------------------------------------------------------------- + readOnlyList.Should().BeEquivalentTo(array); + } + [Fact] public void When_a_collection_property_was_expected_but_the_property_is_not_a_collection_it_should_throw @@ -685,7 +703,7 @@ public void //----------------------------------------------------------------------------------------------------------- act.Should().Throw() .WithMessage( - "*member Customers to be {*Customer*{*Age*38*, but*Jane, John*"); + "Expected*Customers*collection*String*"); } [Fact] @@ -1598,30 +1616,6 @@ public void When_subject_is_null_and_expectation_is_enumerable_it_should_throw() "Expected subject not to be *"); } - [Fact] - public void When_the_expectation_is_not_a_multi_dimensional_array_it_should_throw() - { - //----------------------------------------------------------------------------------------------------------- - // Arrange - //----------------------------------------------------------------------------------------------------------- - var actual = new[,] - { - { 1, 2, 3 }, - { 4, 5, 6 } - }; - - //----------------------------------------------------------------------------------------------------------- - // Act - //----------------------------------------------------------------------------------------------------------- - Action act = () => actual.Should().BeEquivalentTo("not-a-multi-dimensional-array"); - - //----------------------------------------------------------------------------------------------------------- - // Assert - //----------------------------------------------------------------------------------------------------------- - act.Should().Throw() - .WithMessage("Expected*not-a-multi-dimensional-array*but found {1, 2, 3, 4, 5, 6}*"); - } - [Fact] public void When_the_expectation_is_null_it_should_throw() {