Skip to content

Commit

Permalink
Fix checking equivalence of null subject to dictionary (#933)
Browse files Browse the repository at this point in the history
  • Loading branch information
krajek authored and dennisdoomen committed Oct 1, 2018
1 parent 4982935 commit 8620cbc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Expand Up @@ -44,11 +44,19 @@ private static bool PreconditionsAreMet(IEquivalencyValidationContext context, I
Type expectationType = config.GetExpectationType(context);

return AssertImplementsOnlyOneDictionaryInterface(context.Expectation)
&& AssertSubjectIsNotNull(context.Subject)
&& AssertExpectationIsNotNull(context.Subject, context.Expectation)
&& AssertIsCompatiblyTypedDictionary(expectationType, context.Subject)
&& AssertSameLength(context.Subject, expectationType, context.Expectation);
}

private static bool AssertSubjectIsNotNull(object subject)
{
return AssertionScope.Current
.ForCondition(!(subject is null))
.FailWith("Expected {context:Subject} not to be {0}.", new object[] { null });
}

private static bool AssertExpectationIsNotNull(object subject, object expectation)
{
return AssertionScope.Current
Expand Down
44 changes: 44 additions & 0 deletions Tests/Shared.Specs/DictionaryEquivalencySpecs.cs
Expand Up @@ -377,6 +377,50 @@ public void When_a_read_only_dictionary_does_not_match_the_expectation_it_should
act.Should().Throw<XunitException>().WithMessage("Expected item[0]*Value3*Value2*");
}

[Fact]
// #930
public void When_a_dictionary_is_compared_to_null_it_should_not_throw_a_NullReferenceException()
{

//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
Dictionary<int, int> subject = null;
Dictionary<int, int> expectation = new Dictionary<int, int>();

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => subject.Should().BeEquivalentTo(expectation);

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>();
}

[Fact]
// #930
public void When_a_null_dictionary_is_compared_to_null_it_should_not_throw()
{

//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
Dictionary<int, int> subject = null;
Dictionary<int, int> expectation = null;

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => subject.Should().BeEquivalentTo(expectation);

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().NotThrow();
}

[Fact]
public void
When_a_dictionary_property_is_detected_it_should_ignore_the_order_of_the_pairs
Expand Down

0 comments on commit 8620cbc

Please sign in to comment.