Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix checking equivalence of null subject to dictionary #933

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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