Skip to content

Commit

Permalink
Unit tests for #2821 (v2)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Nov 17, 2023
1 parent e25a79e commit 9cbad9a
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/xunit.assert/Asserts
76 changes: 76 additions & 0 deletions test/test.xunit.assert/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 test/test.xunit.assert/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
4 changes: 2 additions & 2 deletions test/test.xunit.assert/Asserts/Sdk/ArgumentFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public static void KeyValuePairValue()
public class Enumerables
{
// Both tracked and untracked should be the same
public static TheoryData<IEnumerable?> Collections = new()
public static TheoryData<IEnumerable> Collections = new()
{
new object[] { 1, 2.3M, "Hello, world!" },
new object[] { 1, 2.3M, "Hello, world!" }.AsTracker(),
Expand Down Expand Up @@ -288,7 +288,7 @@ public static void DictionaryValue()
Assert.Equal(expected, ArgumentFormatter.Format(value));
}

public static TheoryData<IEnumerable?> LongCollections = new()
public static TheoryData<IEnumerable> LongCollections = new()
{
new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }.AsTracker(),
Expand Down
4 changes: 2 additions & 2 deletions test/test.xunit.assert/Asserts/StringAssertsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,8 @@ public class Equal
[InlineData("", "\t", false, false, true, true)]
[InlineData("foobar", "foo bar", false, false, true, true)]
public void Success(
string value1,
string value2,
string? value1,
string? value2,
bool ignoreCase,
bool ignoreLineEndingDifferences,
bool ignoreWhiteSpaceDifferences,
Expand Down

0 comments on commit 9cbad9a

Please sign in to comment.