Skip to content

Commit

Permalink
Unit tests for #2821 (v3)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Nov 17, 2023
1 parent 29eed58 commit 1e8f90d
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 1 deletion.
76 changes: 76 additions & 0 deletions src/xunit.v3.assert.tests/Asserts/CollectionAssertsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,45 @@ public class ThrowingComparer : IEqualityComparer<int>
}
}

public class CollectionsWithEquatable
{
[Fact]
public void Equal()
{
var expected = new[] { new EquatableObject { Char = 'a' } };
var actual = new[] { new EquatableObject { Char = 'a' } };

Assert.Equal(expected, actual);
}

[Fact]
public void NotEqual()
{
var expected = new[] { new EquatableObject { Char = 'a' } };
var actual = new[] { new EquatableObject { Char = 'b' } };

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

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

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

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

public class CollectionsWithFunc
{
[Fact]
Expand Down Expand Up @@ -1708,6 +1747,43 @@ public class ThrowingComparer : IEqualityComparer<int>
}
}

public class CollectionsWithEquatable
{
[Fact]
public void Equal()
{
var expected = new[] { new EquatableObject { Char = 'a' } };
var actual = new[] { new EquatableObject { Char = 'a' } };

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

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

[Fact]
public void NotEqual()
{
var expected = new[] { new EquatableObject { Char = 'a' } };
var actual = new[] { new EquatableObject { 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;
}
}

public class CollectionsWithFunc
{
[Fact]
Expand Down
68 changes: 68 additions & 0 deletions src/xunit.v3.assert.tests/Asserts/EqualityAssertsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,40 @@ public void CollectionValues_NotEqual()
ex.Message
);
}

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

Assert.Equal(expected, actual);
}

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

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

Assert.IsType<EqualException>(ex);
Assert.Equal(
"Assert.Equal() Failure: Values 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;
}
}

public class DoubleEnumerationPrevention
Expand Down Expand Up @@ -3330,6 +3364,40 @@ public void CollectionValues_NotEqual()

Assert.NotEqual(expected, actual);
}

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

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

Assert.IsType<NotEqualException>(ex);
Assert.Equal(
"Assert.NotEqual() Failure: Values 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 KeyValuePair<string, EquatableObject>("Key1", new() { Char = 'a' });
var actual = new KeyValuePair<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;
}
}

public class DoubleEnumerationPrevention
Expand Down
2 changes: 1 addition & 1 deletion src/xunit.v3.assert/Asserts

0 comments on commit 1e8f90d

Please sign in to comment.