Skip to content

Commit a44852f

Browse files
committed
refactor: diff tests
1 parent d4349fd commit a44852f

8 files changed

Lines changed: 184 additions & 154 deletions

File tree

Generator.Equals.Tests/Diff/Classes/AttributeDiffTests.cs renamed to Generator.Equals.Tests/Classes/Diff/AttributeDiffTests.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
using FluentAssertions;
22

3-
namespace Generator.Equals.Tests.Diff.Classes;
3+
namespace Generator.Equals.Tests.Classes.Diff;
44

55
/// <summary>
66
/// Diff tests for equality attributes: IgnoreEquality, StringEquality, ReferenceEquality.
77
/// </summary>
88
public partial class AttributeDiffTests
99
{
10+
static (string Path, object? Left, object? Right) Diff(string path, object? left, object? right)
11+
=> (path, left, right);
12+
1013
[Equatable]
1114
public partial class IgnoredPropertySample
1215
{
@@ -44,7 +47,7 @@ public void IgnoredProperty_NotReportedInDiff()
4447

4548
var diffs = IgnoredPropertySample.EqualityComparer.Default.Diff(a, b).ToList();
4649

47-
diffs.Should().BeEmpty("Ignored properties should not appear in diff");
50+
diffs.Should().BeEmpty();
4851
}
4952

5053
[Fact]
@@ -55,9 +58,7 @@ public void IgnoredProperty_OnlyReportsNonIgnored()
5558

5659
var diffs = IgnoredPropertySample.EqualityComparer.Default.Diff(a, b).ToList();
5760

58-
diffs.Should().ContainSingle();
59-
diffs[0].Path.Should().Be("Name");
60-
diffs.Should().NotContain(d => d.Path.Contains("IgnoredAge"));
61+
diffs.Should().BeEquivalentTo(new[] { Diff("Name", "Alice", "Bob") });
6162
}
6263

6364
#endregion
@@ -72,7 +73,7 @@ public void CaseInsensitiveString_SameCaseDifferent_NoDiff()
7273

7374
var diffs = StringComparisonSample.EqualityComparer.Default.Diff(a, b).ToList();
7475

75-
diffs.Should().BeEmpty("Case-insensitive comparison should treat 'Alice' and 'ALICE' as equal");
76+
diffs.Should().BeEmpty();
7677
}
7778

7879
[Fact]
@@ -83,8 +84,7 @@ public void CaseSensitiveString_SameCaseDifferent_ReportsDiff()
8384

8485
var diffs = StringComparisonSample.EqualityComparer.Default.Diff(a, b).ToList();
8586

86-
diffs.Should().ContainSingle();
87-
diffs[0].Path.Should().Be("CaseSensitiveName");
87+
diffs.Should().BeEquivalentTo(new[] { Diff("CaseSensitiveName", "Test", "TEST") });
8888
}
8989

9090
[Fact]
@@ -95,8 +95,7 @@ public void CaseInsensitiveString_ActuallyDifferent_ReportsDiff()
9595

9696
var diffs = StringComparisonSample.EqualityComparer.Default.Diff(a, b).ToList();
9797

98-
diffs.Should().ContainSingle();
99-
diffs[0].Path.Should().Be("CaseInsensitiveName");
98+
diffs.Should().BeEquivalentTo(new[] { Diff("CaseInsensitiveName", "Alice", "Bob") });
10099
}
101100

102101
#endregion
@@ -118,13 +117,14 @@ public void ReferenceEquality_SameReference_NoDiff()
118117
[Fact]
119118
public void ReferenceEquality_DifferentReference_ReportsDiff()
120119
{
121-
var a = new ReferenceEqualitySample { Reference = new object(), Name = "Test" };
122-
var b = new ReferenceEqualitySample { Reference = new object(), Name = "Test" };
120+
var ref1 = new object();
121+
var ref2 = new object();
122+
var a = new ReferenceEqualitySample { Reference = ref1, Name = "Test" };
123+
var b = new ReferenceEqualitySample { Reference = ref2, Name = "Test" };
123124

124125
var diffs = ReferenceEqualitySample.EqualityComparer.Default.Diff(a, b).ToList();
125126

126-
diffs.Should().ContainSingle();
127-
diffs[0].Path.Should().Be("Reference");
127+
diffs.Should().BeEquivalentTo(new[] { Diff("Reference", ref1, ref2) });
128128
}
129129

130130
[Fact]
@@ -137,11 +137,10 @@ public void ReferenceEquality_EqualButDifferentReference_ReportsDiff()
137137
var a = new ReferenceEqualitySample { Reference = str1, Name = "Test" };
138138
var b = new ReferenceEqualitySample { Reference = str2, Name = "Test" };
139139

140-
// Reference equality should report diff even if content is equal
141140
var diffs = ReferenceEqualitySample.EqualityComparer.Default.Diff(a, b).ToList();
142141

143-
diffs.Should().ContainSingle();
144-
diffs[0].Path.Should().Be("Reference");
142+
// Reference equality should report diff even if content is equal
143+
diffs.Should().BeEquivalentTo(new[] { Diff("Reference", str1, str2) });
145144
}
146145

147146
#endregion

Generator.Equals.Tests/Diff/Classes/BasicDiffTests.cs renamed to Generator.Equals.Tests/Classes/Diff/BasicDiffTests.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
using FluentAssertions;
22

3-
namespace Generator.Equals.Tests.Diff.Classes;
3+
namespace Generator.Equals.Tests.Classes.Diff;
44

55
/// <summary>
66
/// Basic Diff tests for classes: null handling, simple properties, path handling, consistency.
77
/// </summary>
88
public partial class BasicDiffTests
99
{
10+
static (string Path, object? Left, object? Right) Diff(string path, object? left, object? right)
11+
=> (path, left, right);
12+
1013
[Equatable]
1114
public partial class Sample
1215
{
@@ -42,10 +45,7 @@ public void Diff_OneNull_ReturnsEntireObject()
4245

4346
var diffs = Sample.EqualityComparer.Default.Diff(a, null).ToList();
4447

45-
diffs.Should().ContainSingle();
46-
diffs[0].Path.Should().Be("");
47-
diffs[0].Left.Should().Be(a);
48-
diffs[0].Right.Should().BeNull();
48+
diffs.Should().BeEquivalentTo(new[] { Diff("", a, null) });
4949
}
5050

5151
[Fact]
@@ -64,10 +64,7 @@ public void Diff_DifferentName_ReturnsNameDifference()
6464

6565
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
6666

67-
diffs.Should().ContainSingle();
68-
diffs[0].Path.Should().Be("Name");
69-
diffs[0].Left.Should().Be("Alice");
70-
diffs[0].Right.Should().Be("Bob");
67+
diffs.Should().BeEquivalentTo(new[] { Diff("Name", "Alice", "Bob") });
7168
}
7269

7370
[Fact]
@@ -78,10 +75,7 @@ public void Diff_DifferentAge_ReturnsAgeDifference()
7875

7976
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
8077

81-
diffs.Should().ContainSingle();
82-
diffs[0].Path.Should().Be("Age");
83-
diffs[0].Left.Should().Be(30);
84-
diffs[0].Right.Should().Be(35);
78+
diffs.Should().BeEquivalentTo(new[] { Diff("Age", 30, 35) });
8579
}
8680

8781
[Fact]
@@ -92,9 +86,11 @@ public void Diff_MultipleDifferences_ReturnsAllDifferences()
9286

9387
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
9488

95-
diffs.Should().HaveCount(2);
96-
diffs.Should().Contain(d => d.Path == "Name");
97-
diffs.Should().Contain(d => d.Path == "Age");
89+
diffs.Should().BeEquivalentTo(new[]
90+
{
91+
Diff("Name", "Alice", "Bob"),
92+
Diff("Age", 30, 35)
93+
});
9894
}
9995

10096
[Fact]
@@ -105,8 +101,7 @@ public void Diff_WithBasePath_PrependsPath()
105101

106102
var diffs = Sample.EqualityComparer.Default.Diff(a, b, "Root").ToList();
107103

108-
diffs.Should().ContainSingle();
109-
diffs[0].Path.Should().Be("Root.Name");
104+
diffs.Should().BeEquivalentTo(new[] { Diff("Root.Name", "Alice", "Bob") });
110105
}
111106

112107
[Fact]
@@ -132,6 +127,10 @@ public void Diff_ConsistentWithEquals_WhenNotEqual()
132127
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
133128

134129
areEqual.Should().BeFalse();
135-
diffs.Should().NotBeEmpty("Diff should return differences when Equals returns false");
130+
diffs.Should().BeEquivalentTo(new[]
131+
{
132+
Diff("Name", "Alice", "Bob"),
133+
Diff("Age", 30, 35)
134+
});
136135
}
137136
}

Generator.Equals.Tests/Diff/Classes/CollectionDiffTests.cs renamed to Generator.Equals.Tests/Classes/Diff/CollectionDiffTests.cs

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
using FluentAssertions;
22

3-
namespace Generator.Equals.Tests.Diff.Classes;
3+
namespace Generator.Equals.Tests.Classes.Diff;
44

55
/// <summary>
66
/// Diff tests for collection properties: ordered, unordered, set, and dictionary.
77
/// </summary>
88
public partial class CollectionDiffTests
99
{
10+
static (string Path, object? Left, object? Right) Diff(string path, object? left, object? right)
11+
=> (path, left, right);
12+
1013
[Equatable]
1114
public partial class Sample
1215
{
@@ -41,10 +44,7 @@ public void OrderedCollection_DifferentItem_ReportsIndex()
4144

4245
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
4346

44-
diffs.Should().ContainSingle();
45-
diffs[0].Path.Should().Be("Items[1]");
46-
diffs[0].Left.Should().Be(2);
47-
diffs[0].Right.Should().Be(99);
47+
diffs.Should().BeEquivalentTo(new[] { Diff("Items[1]", 2, 99) });
4848
}
4949

5050
[Fact]
@@ -55,10 +55,7 @@ public void OrderedCollection_ExtraItemInRight_ReportsNullLeft()
5555

5656
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
5757

58-
diffs.Should().ContainSingle();
59-
diffs[0].Path.Should().Be("Items[2]");
60-
diffs[0].Left.Should().BeNull();
61-
diffs[0].Right.Should().Be(3);
58+
diffs.Should().BeEquivalentTo(new[] { Diff("Items[2]", null, 3) });
6259
}
6360

6461
[Fact]
@@ -69,10 +66,22 @@ public void OrderedCollection_ExtraItemInLeft_ReportsNullRight()
6966

7067
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
7168

72-
diffs.Should().ContainSingle();
73-
diffs[0].Path.Should().Be("Items[2]");
74-
diffs[0].Left.Should().Be(3);
75-
diffs[0].Right.Should().BeNull();
69+
diffs.Should().BeEquivalentTo(new[] { Diff("Items[2]", 3, null) });
70+
}
71+
72+
[Fact]
73+
public void OrderedCollection_MultipleDifferences_ReportsAll()
74+
{
75+
var a = new Sample { Items = [1, 2, 3] };
76+
var b = new Sample { Items = [9, 2, 8] };
77+
78+
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
79+
80+
diffs.Should().BeEquivalentTo(new[]
81+
{
82+
Diff("Items[0]", 1, 9),
83+
Diff("Items[2]", 3, 8)
84+
});
7685
}
7786

7887
#endregion
@@ -98,10 +107,7 @@ public void UnorderedSet_AddedItem_ReportsAddition()
98107

99108
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
100109

101-
diffs.Should().ContainSingle();
102-
diffs[0].Path.Should().Be("Tags[+]");
103-
diffs[0].Left.Should().BeNull();
104-
diffs[0].Right.Should().Be("c");
110+
diffs.Should().BeEquivalentTo(new[] { Diff("Tags[+]", null, "c") });
105111
}
106112

107113
[Fact]
@@ -112,10 +118,7 @@ public void UnorderedSet_RemovedItem_ReportsRemoval()
112118

113119
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
114120

115-
diffs.Should().ContainSingle();
116-
diffs[0].Path.Should().Be("Tags[-]");
117-
diffs[0].Left.Should().Be("c");
118-
diffs[0].Right.Should().BeNull();
121+
diffs.Should().BeEquivalentTo(new[] { Diff("Tags[-]", "c", null) });
119122
}
120123

121124
[Fact]
@@ -126,9 +129,11 @@ public void UnorderedSet_MultipleChanges_ReportsAllChanges()
126129

127130
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
128131

129-
diffs.Should().HaveCount(2);
130-
diffs.Should().Contain(d => d.Path == "Tags[-]" && (string?)d.Left == "a");
131-
diffs.Should().Contain(d => d.Path == "Tags[+]" && (string?)d.Right == "c");
132+
diffs.Should().BeEquivalentTo(new[]
133+
{
134+
Diff("Tags[-]", "a", null),
135+
Diff("Tags[+]", null, "c")
136+
});
132137
}
133138

134139
#endregion
@@ -154,10 +159,7 @@ public void Dictionary_AddedKey_ReportsAddition()
154159

155160
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
156161

157-
diffs.Should().ContainSingle();
158-
diffs[0].Path.Should().Be("Properties[y]");
159-
diffs[0].Left.Should().BeNull();
160-
diffs[0].Right.Should().Be(2);
162+
diffs.Should().BeEquivalentTo(new[] { Diff("Properties[y]", null, 2) });
161163
}
162164

163165
[Fact]
@@ -168,10 +170,7 @@ public void Dictionary_RemovedKey_ReportsRemoval()
168170

169171
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
170172

171-
diffs.Should().ContainSingle();
172-
diffs[0].Path.Should().Be("Properties[y]");
173-
diffs[0].Left.Should().Be(2);
174-
diffs[0].Right.Should().BeNull();
173+
diffs.Should().BeEquivalentTo(new[] { Diff("Properties[y]", 2, null) });
175174
}
176175

177176
[Fact]
@@ -182,10 +181,7 @@ public void Dictionary_ChangedValue_ReportsChange()
182181

183182
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
184183

185-
diffs.Should().ContainSingle();
186-
diffs[0].Path.Should().Be("Properties[x]");
187-
diffs[0].Left.Should().Be(1);
188-
diffs[0].Right.Should().Be(99);
184+
diffs.Should().BeEquivalentTo(new[] { Diff("Properties[x]", 1, 99) });
189185
}
190186

191187
[Fact]
@@ -196,9 +192,11 @@ public void Dictionary_NullVsNonNull_ReportsAllKeysAdded()
196192

197193
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
198194

199-
diffs.Should().HaveCount(2);
200-
diffs.Should().Contain(d => d.Path == "Properties[x]" && d.Left == null && (int?)d.Right == 1);
201-
diffs.Should().Contain(d => d.Path == "Properties[y]" && d.Left == null && (int?)d.Right == 2);
195+
diffs.Should().BeEquivalentTo(new[]
196+
{
197+
Diff("Properties[x]", null, 1),
198+
Diff("Properties[y]", null, 2)
199+
});
202200
}
203201

204202
[Fact]
@@ -209,9 +207,27 @@ public void Dictionary_NonNullVsNull_ReportsAllKeysRemoved()
209207

210208
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
211209

212-
diffs.Should().HaveCount(2);
213-
diffs.Should().Contain(d => d.Path == "Properties[x]" && (int?)d.Left == 1 && d.Right == null);
214-
diffs.Should().Contain(d => d.Path == "Properties[y]" && (int?)d.Left == 2 && d.Right == null);
210+
diffs.Should().BeEquivalentTo(new[]
211+
{
212+
Diff("Properties[x]", 1, null),
213+
Diff("Properties[y]", 2, null)
214+
});
215+
}
216+
217+
[Fact]
218+
public void Dictionary_MultipleChanges_ReportsAll()
219+
{
220+
var a = new Sample { Properties = new Dictionary<string, int> { ["x"] = 1, ["y"] = 2 } };
221+
var b = new Sample { Properties = new Dictionary<string, int> { ["x"] = 99, ["z"] = 3 } };
222+
223+
var diffs = Sample.EqualityComparer.Default.Diff(a, b).ToList();
224+
225+
diffs.Should().BeEquivalentTo(new[]
226+
{
227+
Diff("Properties[x]", 1, 99),
228+
Diff("Properties[y]", 2, null),
229+
Diff("Properties[z]", null, 3)
230+
});
215231
}
216232

217233
#endregion

0 commit comments

Comments
 (0)