|
4 | 4 | using System.Collections.Generic;
|
5 | 5 | using System.Collections.ObjectModel;
|
6 | 6 | using System.Diagnostics.CodeAnalysis;
|
| 7 | +using System.Linq; |
7 | 8 | using Xunit;
|
8 | 9 | using Xunit.Sdk;
|
9 | 10 |
|
@@ -1046,6 +1047,53 @@ public bool Equals(IEnumerable? x, IEnumerable? y)
|
1046 | 1047 | public int GetHashCode([DisallowNull] IEnumerable obj) =>
|
1047 | 1048 | throw new NotImplementedException();
|
1048 | 1049 | }
|
| 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 | + } |
1049 | 1097 | }
|
1050 | 1098 |
|
1051 | 1099 | public class Dictionaries
|
@@ -3007,6 +3055,52 @@ public void NonZeroBoundedArrays_NotEqual()
|
3007 | 3055 |
|
3008 | 3056 | Assert.NotEqual(expected, actual);
|
3009 | 3057 | }
|
| 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 | + } |
3010 | 3104 | }
|
3011 | 3105 |
|
3012 | 3106 | public class Dictionaries
|
|
0 commit comments