Skip to content

Commit

Permalink
Add collection dictionary tests for IEquatable<> objects in the value…
Browse files Browse the repository at this point in the history
… slot
  • Loading branch information
bradwilson committed Dec 20, 2023
1 parent 0eb76d2 commit c729d7f
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions test/test.xunit.assert/Asserts/CollectionAssertsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,40 @@ public static void WithCollectionValues_NotEqual()
);
}

[Fact]
public void EquatableValues_Equal()
{
var expected = new Dictionary<string, EquatableObject> { { "Key1", new() { Char = 'a' } } };
var actual = new Dictionary<string, EquatableObject> { { "Key1", new() { Char = 'a' } } };

Assert.Equal(expected, actual);
}

[Fact]
public void EquatableValues_NotEqual()
{
var expected = new Dictionary<string, EquatableObject> { { "Key1", new() { Char = 'a' } } };
var actual = new Dictionary<string, EquatableObject> { { "Key1", new() { Char = 'b' } } };

var ex = Record.Exception(() => Assert.Equal(expected, actual));

Assert.IsType<EqualException>(ex);
Assert.Equal(
"Assert.Equal() Failure: Dictionaries differ" + Environment.NewLine +
"Expected: [[\"Key1\"] = EquatableObject { Char = 'a' }]" + Environment.NewLine +
"Actual: [[\"Key1\"] = EquatableObject { Char = 'b' }]",
ex.Message
);
}

public class EquatableObject : IEquatable<EquatableObject>
{
public char Char { get; set; }

public bool Equals(EquatableObject? other) =>
other != null && other.Char == Char;
}

[Fact]
public void ComplexEmbeddedValues_Equal()
{
Expand Down Expand Up @@ -1957,6 +1991,40 @@ public static void WithCollectionValues_NotEqual()
Assert.NotEqual(expected, actual);
}

[Fact]
public void EquatableValues_Equal()
{
var expected = new Dictionary<string, EquatableObject> { { "Key1", new() { Char = 'a' } } };
var actual = new Dictionary<string, EquatableObject> { { "Key1", new() { Char = 'a' } } };

var ex = Record.Exception(() => Assert.NotEqual(expected, actual));

Assert.IsType<NotEqualException>(ex);
Assert.Equal(
"Assert.NotEqual() Failure: Dictionaries are equal" + Environment.NewLine +
"Expected: Not [[\"Key1\"] = EquatableObject { Char = 'a' }]" + Environment.NewLine +
"Actual: [[\"Key1\"] = EquatableObject { Char = 'a' }]",
ex.Message
);
}

[Fact]
public void EquatableValues_NotEqual()
{
var expected = new Dictionary<string, EquatableObject> { { "Key1", new() { Char = 'a' } } };
var actual = new Dictionary<string, EquatableObject> { { "Key1", new() { Char = 'b' } } };

Assert.NotEqual(expected, actual);
}

public class EquatableObject : IEquatable<EquatableObject>
{
public char Char { get; set; }

public bool Equals(EquatableObject? other) =>
other != null && other.Char == Char;
}

[Fact]
public void ComplexEmbeddedValues_Equal()
{
Expand Down

0 comments on commit c729d7f

Please sign in to comment.