Skip to content

Commit 4f16fca

Browse files
authored
chore: use Shouldly instead of FluentAssertions (#350)
* chore(deps): add Shouldly package * test: migrate to Shouldly * test: migrate to Shouldly on Generator.Tests * chore(deps-dev): remove FluentAssertions package * chore(deps): bump Shouldly to 4.3.0
1 parent 767700e commit 4f16fca

40 files changed

+1101
-1468
lines changed

Directory.Packages.props

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
<Project>
2-
32
<PropertyGroup>
43
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
54
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
65
<NoWarn>$(NoWarn);NU1507</NoWarn>
76
</PropertyGroup>
8-
97
<ItemGroup>
10-
<PackageVersion Include="FluentAssertions" Version="7.0.0" />
118
<PackageVersion Include="Microsoft.Bcl.TimeProvider" Version="9.0.0" />
129
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
1310
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
@@ -16,10 +13,10 @@
1613
<PackageVersion Include="Moq" Version="4.20.72" />
1714
<PackageVersion Include="Moq.Contrib.HttpClient" Version="1.4.0" />
1815
<PackageVersion Include="RandomFixtureKit" Version="1.0.1" />
16+
<PackageVersion Include="Shouldly" Version="4.3.0" />
1917
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
2018
<PackageVersion Include="System.CommandLine.Hosting" Version="0.4.0-alpha.22272.1" />
2119
<PackageVersion Include="System.Net.Http.Json" Version="9.0.0" />
2220
<PackageVersion Include="System.Text.Json" Version="9.0.0" />
2321
</ItemGroup>
24-
2522
</Project>

test/Kaonavi.NET.Generator.Tests/Kaonavi.NET.Generator.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="FluentAssertions" />
109
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
1110
<PackageReference Include="RandomFixtureKit" />
11+
<PackageReference Include="Shouldly" />
1212
<PackageReference Include="System.Text.Json" />
1313
</ItemGroup>
1414

1515
<ItemGroup>
1616
<AssemblyAttribute Include="Microsoft.VisualStudio.TestTools.UnitTesting.ParallelizeAttribute" />
17-
<Using Include="FluentAssertions" />
1817
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
18+
<Using Include="Shouldly" />
1919
</ItemGroup>
2020

2121
<ItemGroup>

test/Kaonavi.NET.Generator.Tests/SheetDataGenerator.Test.cs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void When_Invalid_Code_Compiler_Warns_With_Diagnostic(string code, string
8181
var warnings = CSharpGeneratorRunner.RunGenerator(code);
8282

8383
// Assert
84-
warnings.Select(x => x.Id).Should().Contain(id);
84+
warnings.Select(x => x.Id).ShouldContain(id);
8585
}
8686

8787
/// <summary>
@@ -127,7 +127,7 @@ public void Generates_ToCustomFields_Method(string code, LanguageVersion version
127127
var warnings = CSharpGeneratorRunner.RunGenerator(code, version);
128128

129129
// Assert
130-
warnings.Should().BeEmpty();
130+
warnings.ShouldBeEmpty();
131131
}
132132

133133
/// <summary>
@@ -140,19 +140,17 @@ public void Class_Generates_ToCustomFields_Method()
140140
var values = FixtureFactory.CreateMany<NormalClassSheetData>(10);
141141

142142
// Act - Assert
143-
typeof(NormalClassSheetData).Should().Implement<ISheetData>()
144-
.And.HaveMethod(nameof(ISheetData.ToCustomFields), []);
145143
foreach (var sut in values)
146144
{
147-
sut.ToCustomFields().Should().Equal(
145+
sut.ToCustomFields().ShouldBe([
148146
new(101, sut.Name!),
149147
new(102, sut.Date1.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
150148
new(103, sut.Date2.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
151149
new(104, sut.Date3.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
152150
new(105, sut.Date4.GetValueOrDefault().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
153151
new(106, sut.Date5.GetValueOrDefault().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
154-
new(107, sut.Date6.GetValueOrDefault().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture))
155-
);
152+
new(107, sut.Date6.GetValueOrDefault().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
153+
]);
156154
}
157155
}
158156

@@ -166,19 +164,17 @@ public void Record_Generates_ToCustomFields_Method()
166164
var values = FixtureFactory.CreateMany<NormalRecordSheetData>(10);
167165

168166
// Act - Assert
169-
typeof(NormalRecordSheetData).Should().Implement<ISheetData>()
170-
.And.HaveMethod(nameof(ISheetData.ToCustomFields), []);
171167
foreach (var sut in values)
172168
{
173-
sut.ToCustomFields().Should().Equal(
169+
sut.ToCustomFields().ShouldBe([
174170
new(101, sut.Name!),
175171
new(102, sut.Date1.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
176172
new(103, sut.Date2.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
177173
new(104, sut.Date3.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
178174
new(105, sut.Date4.GetValueOrDefault().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
179175
new(106, sut.Date5.GetValueOrDefault().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
180176
new(107, sut.Date6.GetValueOrDefault().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture))
181-
);
177+
]);
182178
}
183179
}
184180

@@ -192,19 +188,17 @@ public void Class_Without_Namespace_Generates_ToCustomFields_Method()
192188
var values = FixtureFactory.CreateMany<NoNamespaceClassSheetData>(10);
193189

194190
// Act - Assert
195-
typeof(NoNamespaceClassSheetData).Should().Implement<ISheetData>()
196-
.And.HaveMethod(nameof(ISheetData.ToCustomFields), []);
197191
foreach (var sut in values)
198192
{
199-
sut.ToCustomFields().Should().Equal(
193+
sut.ToCustomFields().ShouldBe([
200194
new(101, sut.Name),
201195
new(102, sut.Date1.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
202196
new(103, sut.Date2.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
203197
new(104, sut.Date3.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
204198
new(105, sut.Date4.GetValueOrDefault().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
205199
new(106, sut.Date5.GetValueOrDefault().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
206200
new(107, sut.Date6.GetValueOrDefault().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture))
207-
);
201+
]);
208202
}
209203
}
210204

@@ -218,19 +212,17 @@ public void Record_Without_Namespace_Generates_ToCustomFields_Method()
218212
var values = FixtureFactory.CreateMany<NoNamespaceRecordSheetData>(10);
219213

220214
// Act - Assert
221-
typeof(NoNamespaceRecordSheetData).Should().Implement<ISheetData>()
222-
.And.HaveMethod(nameof(ISheetData.ToCustomFields), []);
223215
foreach (var sut in values)
224216
{
225-
sut.ToCustomFields().Should().Equal(
217+
sut.ToCustomFields().ShouldBe([
226218
new(101, sut.Name),
227219
new(102, sut.Date1.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
228220
new(103, sut.Date2.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
229221
new(104, sut.Date3.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
230222
new(105, sut.Date4.GetValueOrDefault().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
231223
new(106, sut.Date5.GetValueOrDefault().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
232224
new(107, sut.Date6.GetValueOrDefault().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture))
233-
);
225+
]);
234226
}
235227
}
236228
}

test/Kaonavi.NET.Generator.Tests/packages.lock.json

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22
"version": 2,
33
"dependencies": {
44
"net8.0": {
5-
"FluentAssertions": {
6-
"type": "Direct",
7-
"requested": "[7.0.0, )",
8-
"resolved": "7.0.0",
9-
"contentHash": "mTLbcU991EQ1SEmNbVBaGGGJy0YFzvGd1sYJGNZ07nlPKuyHSn1I22aeKzqQXgEiaKyRO6MSCto9eN9VxMwBdA==",
10-
"dependencies": {
11-
"System.Configuration.ConfigurationManager": "6.0.0"
12-
}
13-
},
145
"Microsoft.CodeAnalysis.CSharp": {
156
"type": "Direct",
167
"requested": "[4.11.0, )",
@@ -81,6 +72,16 @@
8172
"resolved": "1.0.1",
8273
"contentHash": "7H6wNY82zWzDJo3ALnp0nSUHaD1QXdKBIHURkk44YjUL8BIV4zkFSdopNETDkdw3oSBIqkoiuaUhwWzrB/kZdg=="
8374
},
75+
"Shouldly": {
76+
"type": "Direct",
77+
"requested": "[4.3.0, )",
78+
"resolved": "4.3.0",
79+
"contentHash": "sDetrWXrl6YXZ4HeLsdBoNk3uIa7K+V4uvIJ+cqdRa5DrFxeTED7VkjoxCuU1kJWpUuBDZz2QXFzSxBtVXLwRQ==",
80+
"dependencies": {
81+
"DiffEngine": "11.3.0",
82+
"EmptyFiles": "4.4.0"
83+
}
84+
},
8485
"System.Text.Json": {
8586
"type": "Direct",
8687
"requested": "[9.0.0, )",
@@ -91,6 +92,20 @@
9192
"System.Text.Encodings.Web": "9.0.0"
9293
}
9394
},
95+
"DiffEngine": {
96+
"type": "Transitive",
97+
"resolved": "11.3.0",
98+
"contentHash": "k0ZgZqd09jLZQjR8FyQbSQE86Q7QZnjEzq1LPHtj1R2AoWO8sjV5x+jlSisL7NZAbUOI4y+7Bog8gkr9WIRBGw==",
99+
"dependencies": {
100+
"EmptyFiles": "4.4.0",
101+
"System.Management": "6.0.1"
102+
}
103+
},
104+
"EmptyFiles": {
105+
"type": "Transitive",
106+
"resolved": "4.4.0",
107+
"contentHash": "gwJEfIGS7FhykvtZoscwXj/XwW+mJY6UbAZk+qtLKFUGWC95kfKXnj8VkxsZQnWBxJemM/q664rGLN5nf+OHZw=="
108+
},
94109
"Microsoft.ApplicationInsights": {
95110
"type": "Transitive",
96111
"resolved": "2.22.0",
@@ -190,11 +205,6 @@
190205
"Newtonsoft.Json": "13.0.1"
191206
}
192207
},
193-
"Microsoft.Win32.SystemEvents": {
194-
"type": "Transitive",
195-
"resolved": "6.0.0",
196-
"contentHash": "hqTM5628jSsQiv+HGpiq3WKBl2c8v1KZfby2J6Pr7pEPlK9waPdgEO6b8A/+/xn/yZ9ulv8HuqK71ONy2tg67A=="
197-
},
198208
"MSTest.Analyzers": {
199209
"type": "Transitive",
200210
"resolved": "3.7.0",
@@ -210,38 +220,34 @@
210220
"resolved": "4.5.1",
211221
"contentHash": "Rw7ijyl1qqRS0YQD/WycNst8hUUMgrMH4FCn1nNm27M4VxchZ1js3fVjQaANHO5f3sN4isvP4a+Met9Y4YomAg=="
212222
},
223+
"System.CodeDom": {
224+
"type": "Transitive",
225+
"resolved": "6.0.0",
226+
"contentHash": "CPc6tWO1LAer3IzfZufDBRL+UZQcj5uS207NHALQzP84Vp/z6wF0Aa0YZImOQY8iStY0A2zI/e3ihKNPfUm8XA=="
227+
},
213228
"System.Collections.Immutable": {
214229
"type": "Transitive",
215230
"resolved": "8.0.0",
216231
"contentHash": "AurL6Y5BA1WotzlEvVaIDpqzpIPvYnnldxru8oXJU2yFxFUy3+pNXjXd1ymO+RA0rq0+590Q8gaz2l3Sr7fmqg=="
217232
},
218-
"System.Configuration.ConfigurationManager": {
219-
"type": "Transitive",
220-
"resolved": "6.0.0",
221-
"contentHash": "7T+m0kDSlIPTHIkPMIu6m6tV6qsMqJpvQWW2jIc2qi7sn40qxFo0q+7mEQAhMPXZHMKnWrnv47ntGlM/ejvw3g==",
222-
"dependencies": {
223-
"System.Security.Cryptography.ProtectedData": "6.0.0",
224-
"System.Security.Permissions": "6.0.0"
225-
}
226-
},
227233
"System.Diagnostics.DiagnosticSource": {
228234
"type": "Transitive",
229235
"resolved": "5.0.0",
230236
"contentHash": "tCQTzPsGZh/A9LhhA6zrqCRV4hOHsK90/G7q3Khxmn6tnB1PuNU0cRaKANP2AWcF9bn0zsuOoZOSrHuJk6oNBA=="
231237
},
232-
"System.Drawing.Common": {
233-
"type": "Transitive",
234-
"resolved": "6.0.0",
235-
"contentHash": "NfuoKUiP2nUWwKZN6twGqXioIe1zVD0RIj2t976A+czLHr2nY454RwwXs6JU9Htc6mwqL6Dn/nEL3dpVf2jOhg==",
236-
"dependencies": {
237-
"Microsoft.Win32.SystemEvents": "6.0.0"
238-
}
239-
},
240238
"System.IO.Pipelines": {
241239
"type": "Transitive",
242240
"resolved": "9.0.0",
243241
"contentHash": "eA3cinogwaNB4jdjQHOP3Z3EuyiDII7MT35jgtnsA4vkn0LUrrSHsU0nzHTzFzmaFYeKV7MYyMxOocFzsBHpTw=="
244242
},
243+
"System.Management": {
244+
"type": "Transitive",
245+
"resolved": "6.0.1",
246+
"contentHash": "10J1D0h/lioojphfJ4Fuh5ZUThT/xOVHdV9roGBittKKNP2PMjrvibEdbVTGZcPra1399Ja3tqIJLyQrc5Wmhg==",
247+
"dependencies": {
248+
"System.CodeDom": "6.0.0"
249+
}
250+
},
245251
"System.Memory": {
246252
"type": "Transitive",
247253
"resolved": "4.5.4",
@@ -260,38 +266,11 @@
260266
"resolved": "6.0.0",
261267
"contentHash": "/iUeP3tq1S0XdNNoMz5C9twLSrM/TH+qElHkXWaPvuNOt+99G75NrV0OS2EqHx5wMN7popYjpc8oTjC1y16DLg=="
262268
},
263-
"System.Security.AccessControl": {
264-
"type": "Transitive",
265-
"resolved": "6.0.0",
266-
"contentHash": "AUADIc0LIEQe7MzC+I0cl0rAT8RrTAKFHl53yHjEUzNVIaUlhFY11vc2ebiVJzVBuOzun6F7FBA+8KAbGTTedQ=="
267-
},
268-
"System.Security.Cryptography.ProtectedData": {
269-
"type": "Transitive",
270-
"resolved": "6.0.0",
271-
"contentHash": "rp1gMNEZpvx9vP0JW0oHLxlf8oSiQgtno77Y4PLUBjSiDYoD77Y8uXHr1Ea5XG4/pIKhqAdxZ8v8OTUtqo9PeQ=="
272-
},
273-
"System.Security.Permissions": {
274-
"type": "Transitive",
275-
"resolved": "6.0.0",
276-
"contentHash": "T/uuc7AklkDoxmcJ7LGkyX1CcSviZuLCa4jg3PekfJ7SU0niF0IVTXwUiNVP9DSpzou2PpxJ+eNY2IfDM90ZCg==",
277-
"dependencies": {
278-
"System.Security.AccessControl": "6.0.0",
279-
"System.Windows.Extensions": "6.0.0"
280-
}
281-
},
282269
"System.Text.Encodings.Web": {
283270
"type": "Transitive",
284271
"resolved": "9.0.0",
285272
"contentHash": "e2hMgAErLbKyUUwt18qSBf9T5Y+SFAL3ZedM8fLupkVj8Rj2PZ9oxQ37XX2LF8fTO1wNIxvKpihD7Of7D/NxZw=="
286273
},
287-
"System.Windows.Extensions": {
288-
"type": "Transitive",
289-
"resolved": "6.0.0",
290-
"contentHash": "IXoJOXIqc39AIe+CIR7koBtRGMiCt/LPM3lI+PELtDIy9XdyeSrwXFdWV9dzJ2Awl0paLWUaknLxFQ5HpHZUog==",
291-
"dependencies": {
292-
"System.Drawing.Common": "6.0.0"
293-
}
294-
},
295274
"kaonavi.net.core": {
296275
"type": "Project",
297276
"dependencies": {
Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,39 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using Moq;
3+
using Moq.Contrib.HttpClient;
4+
15
namespace Kaonavi.Net.Tests.Assertions;
26

7+
[ShouldlyMethods]
38
internal static class AssertExtensions
49
{
5-
public static HttpRequestMessageAssertions Should(this HttpRequestMessage instance) => new(instance);
6-
public static JsonElementAssertions Should(this JsonElement instance) => new(instance);
10+
public static void ShouldBeCalled(this Mock<HttpMessageHandler> handler, Times times, params Action<HttpRequestMessage>[] conditions)
11+
{
12+
if (conditions is null || conditions.Length == 0)
13+
handler.VerifyAnyRequest(times);
14+
else
15+
handler.VerifyRequest(req => { req.ShouldSatisfyAllConditions(conditions); return true; }, times);
16+
}
17+
18+
public static void ShouldBeCalledOnce(this Mock<HttpMessageHandler> handler, params Action<HttpRequestMessage>[] conditions)
19+
=> handler.ShouldBeCalled(Times.Once(), conditions);
20+
21+
public static void ShouldNotBeCalled(this Mock<HttpMessageHandler> handler, params Action<HttpRequestMessage>[] conditions)
22+
=> handler.ShouldBeCalled(Times.Never(), conditions);
23+
24+
public static void ShouldHaveJsonBody(this HttpContent content, [StringSyntax(StringSyntaxAttribute.Json)] string expectedJson, string? customMessage = null)
25+
{
26+
using var actual = JsonDocument.Parse(content.ReadAsStream());
27+
using var expected = JsonDocument.Parse(expectedJson);
28+
if (!JsonElement.DeepEquals(actual.RootElement, expected.RootElement))
29+
throw new ShouldAssertException(new ExpectedActualShouldlyMessage(expectedJson, actual.RootElement, customMessage).ToString());
30+
}
31+
32+
public static void ShouldHaveJsonBody(this HttpContent content, ReadOnlySpan<byte> utf8PropertyName, [StringSyntax(StringSyntaxAttribute.Json)] string expectedJson, string? customMessage = null)
33+
{
34+
using var actual = JsonDocument.Parse(content.ReadAsStream());
35+
using var expected = JsonDocument.Parse(expectedJson);
36+
if (!JsonElement.DeepEquals(actual.RootElement.GetProperty(utf8PropertyName), expected.RootElement))
37+
throw new ShouldAssertException(new ExpectedActualShouldlyMessage(expectedJson, actual.RootElement, customMessage).ToString());
38+
}
739
}

test/Kaonavi.NET.Tests/Assertions/HttpRequestMessageAssertions.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)