Skip to content

Commit ca68393

Browse files
committed
Unit tests for #2828 (v2)
1 parent 62505d1 commit ca68393

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

test/test.xunit.assert/Asserts/EqualityAssertsTests.cs

+94
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Collections.ObjectModel;
66
using System.Diagnostics.CodeAnalysis;
7+
using System.Linq;
78
using Xunit;
89
using Xunit.Sdk;
910

@@ -1046,6 +1047,53 @@ public bool Equals(IEnumerable? x, IEnumerable? y)
10461047
public int GetHashCode([DisallowNull] IEnumerable obj) =>
10471048
throw new NotImplementedException();
10481049
}
1050+
1051+
[Fact]
1052+
public void CollectionWithIEquatable_Equal()
1053+
{
1054+
var expected = new EnumerableEquatable<int> { 42, 2112 };
1055+
var actual = new EnumerableEquatable<int> { 2112, 42 };
1056+
1057+
Assert.Equal(expected, actual);
1058+
}
1059+
1060+
[Fact]
1061+
public void CollectionWithIEquatable_NotEqual()
1062+
{
1063+
var expected = new EnumerableEquatable<int> { 42, 2112 };
1064+
var actual = new EnumerableEquatable<int> { 2112, 2600 };
1065+
1066+
var ex = Record.Exception(() => Assert.Equal(expected, actual));
1067+
1068+
Assert.IsType<EqualException>(ex);
1069+
// No pointers because it's relying on IEquatable<>
1070+
Assert.Equal(
1071+
"Assert.Equal() Failure: Collections differ" + Environment.NewLine +
1072+
"Expected: [42, 2112]" + Environment.NewLine +
1073+
"Actual: [2112, 2600]",
1074+
ex.Message
1075+
);
1076+
}
1077+
1078+
public sealed class EnumerableEquatable<T> :
1079+
IEnumerable<T>, IEquatable<EnumerableEquatable<T>>
1080+
{
1081+
List<T> values = new();
1082+
1083+
public void Add(T value) => values.Add(value);
1084+
1085+
public bool Equals(EnumerableEquatable<T>? other)
1086+
{
1087+
if (other == null)
1088+
return false;
1089+
1090+
return !values.Except(other.values).Any() && !other.values.Except(values).Any();
1091+
}
1092+
1093+
public IEnumerator<T> GetEnumerator() => values.GetEnumerator();
1094+
1095+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
1096+
}
10491097
}
10501098

10511099
public class Dictionaries
@@ -3007,6 +3055,52 @@ public void NonZeroBoundedArrays_NotEqual()
30073055

30083056
Assert.NotEqual(expected, actual);
30093057
}
3058+
3059+
[Fact]
3060+
public void CollectionWithIEquatable_Equal()
3061+
{
3062+
var expected = new EnumerableEquatable<int> { 42, 2112 };
3063+
var actual = new EnumerableEquatable<int> { 2112, 42 };
3064+
3065+
var ex = Record.Exception(() => Assert.NotEqual(expected, actual));
3066+
3067+
Assert.IsType<NotEqualException>(ex);
3068+
Assert.Equal(
3069+
"Assert.NotEqual() Failure: Collections are equal" + Environment.NewLine +
3070+
"Expected: Not [42, 2112]" + Environment.NewLine +
3071+
"Actual: [2112, 42]",
3072+
ex.Message
3073+
);
3074+
}
3075+
3076+
[Fact]
3077+
public void CollectionWithIEquatable_NotEqual()
3078+
{
3079+
var expected = new EnumerableEquatable<int> { 42, 2112 };
3080+
var actual = new EnumerableEquatable<int> { 2112, 2600 };
3081+
3082+
Assert.NotEqual(expected, actual);
3083+
}
3084+
3085+
public sealed class EnumerableEquatable<T> :
3086+
IEnumerable<T>, IEquatable<EnumerableEquatable<T>>
3087+
{
3088+
List<T> values = new();
3089+
3090+
public void Add(T value) => values.Add(value);
3091+
3092+
public bool Equals(EnumerableEquatable<T>? other)
3093+
{
3094+
if (other == null)
3095+
return false;
3096+
3097+
return !values.Except(other.values).Any() && !other.values.Except(values).Any();
3098+
}
3099+
3100+
public IEnumerator<T> GetEnumerator() => values.GetEnumerator();
3101+
3102+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
3103+
}
30103104
}
30113105

30123106
public class Dictionaries

0 commit comments

Comments
 (0)