Skip to content

Commit da525cb

Browse files
committed
✅ refactor test data sources to use strongly-typed TheoryData and EndToEndTestCase
1 parent c6664f2 commit da525cb

File tree

5 files changed

+30
-46
lines changed

5 files changed

+30
-46
lines changed

QsNet.Tests/DecodeTests.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using QsNet.Enums;
99
using QsNet.Internal;
1010
using QsNet.Models;
11+
using QsNet.Tests.Fixtures.Data;
1112
using Xunit;
1213

1314
namespace QsNet.Tests;
@@ -2210,26 +2211,17 @@ public void Decode_DoesNotInterpretUXXXXSyntaxInIso88591Mode()
22102211
}
22112212

22122213
[Theory]
2213-
[MemberData(nameof(EmptyTestCases))]
2214-
public void Decode_ParsesEmptyKeys_SkipsEmptyStringKey(
2215-
string input,
2216-
Dictionary<string, object?> noEmptyKeys
2217-
)
2214+
[MemberData(nameof(GetEmptyTestCases))]
2215+
public void Decode_ParsesEmptyKeys_SkipsEmptyStringKey(Dictionary<string, object?> testCase)
22182216
{
2219-
Qs.Decode(input).Should().BeEquivalentTo(noEmptyKeys);
2217+
Qs.Decode(testCase["input"]).Should().BeEquivalentTo(testCase["noEmptyKeys"]);
22202218
}
22212219

2222-
public static IEnumerable<object[]> EmptyTestCases()
2220+
public static TheoryData<Dictionary<string, object?>> GetEmptyTestCases()
22232221
{
2224-
// You'll need to define the actual test cases here based on your EmptyTestCases data
2225-
// This is just an example structure
2226-
yield return new object[] { "=value", new Dictionary<string, object?>() };
2227-
yield return new object[]
2228-
{
2229-
"key=",
2230-
new Dictionary<string, object?> { ["key"] = "" }
2231-
};
2232-
// Add more test cases as needed
2222+
var data = new TheoryData<Dictionary<string, object?>>();
2223+
foreach (var testCase in EmptyTestCases.Cases) data.Add(testCase);
2224+
return data;
22332225
}
22342226

22352227
[Fact]

QsNet.Tests/EncodeTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3004,9 +3004,11 @@ public void Encode_MapWithEmptyStringKey(Dictionary<string, object?> testCase)
30043004
.Be((string)stringifyOutput["repeat"]!);
30053005
}
30063006

3007-
public static IEnumerable<object[]> GetEmptyTestCases()
3007+
public static TheoryData<Dictionary<string, object?>> GetEmptyTestCases()
30083008
{
3009-
return EmptyTestCases.Cases.Select(testCase => new object[] { testCase });
3009+
var data = new TheoryData<Dictionary<string, object?>>();
3010+
foreach (var testCase in EmptyTestCases.Cases) data.Add(testCase);
3011+
return data;
30103012
}
30113013

30123014
[Fact]

QsNet.Tests/EndToEndTests.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
31
using FluentAssertions;
42
using JetBrains.Annotations;
53
using QsNet.Models;
@@ -13,19 +11,19 @@ public class EndToEndTests
1311
{
1412
[Theory]
1513
[MemberData(nameof(GetEndToEndTestCases))]
16-
public void EndToEndTest_EncodeDecodeRoundTrip(object data, string encoded)
14+
public void EndToEndTest_EncodeDecodeRoundTrip(EndToEndTestCase testCase)
1715
{
1816
// Encode the data and verify it matches the expected encoded string
19-
Qs.Encode(data, new EncodeOptions { Encode = false }).Should().Be(encoded);
17+
Qs.Encode(testCase.Data, new EncodeOptions { Encode = false }).Should().Be(testCase.Encoded);
2018

2119
// Decode the encoded string and verify it matches the original data
22-
Qs.Decode(encoded).Should().BeEquivalentTo(data);
20+
Qs.Decode(testCase.Encoded).Should().BeEquivalentTo(testCase.Data);
2321
}
2422

25-
public static IEnumerable<object[]> GetEndToEndTestCases()
23+
public static TheoryData<EndToEndTestCase> GetEndToEndTestCases()
2624
{
27-
return EndToEndTestCases.Cases.Select(testCase =>
28-
new object[] { testCase.Data, testCase.Encoded }
29-
);
25+
var data = new TheoryData<EndToEndTestCase>();
26+
foreach (var testCase in EndToEndTestCases.Cases) data.Add(testCase);
27+
return data;
3028
}
3129
}

QsNet.Tests/ExtensionTests.cs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
31
using FluentAssertions;
42
using JetBrains.Annotations;
53
using QsNet.Models;
@@ -13,33 +11,27 @@ public class ExtensionTests
1311
{
1412
[Theory]
1513
[MemberData(nameof(GetEndToEndTestCases))]
16-
public void ToQueryString_ShouldEncodeEndToEndTestCases(
17-
Dictionary<string, object?> data,
18-
string expectedEncoded
19-
)
14+
public void ToQueryString_ShouldEncodeEndToEndTestCases(EndToEndTestCase testCase)
2015
{
21-
data.ToQueryString(new EncodeOptions { Encode = false })
16+
testCase.Data.ToQueryString(new EncodeOptions { Encode = false })
2217
.Should()
23-
.Be(expectedEncoded, $"Failed for test case: {data}");
18+
.Be(testCase.Encoded, $"Failed for test case: {testCase.Data}");
2419
}
2520

2621
[Theory]
2722
[MemberData(nameof(GetEndToEndTestCases))]
28-
public void ToQueryMap_ShouldDecodeEndToEndTestCases(
29-
Dictionary<string, object?> expectedData,
30-
string encoded
31-
)
23+
public void ToQueryMap_ShouldDecodeEndToEndTestCases(EndToEndTestCase testCase)
3224
{
33-
encoded
25+
testCase.Encoded
3426
.ToQueryMap()
3527
.Should()
36-
.BeEquivalentTo(expectedData, $"Failed for test case: {encoded}");
28+
.BeEquivalentTo(testCase.Data, $"Failed for test case: {testCase.Encoded}");
3729
}
3830

39-
public static IEnumerable<object[]> GetEndToEndTestCases()
31+
public static TheoryData<EndToEndTestCase> GetEndToEndTestCases()
4032
{
41-
return EndToEndTestCases.Cases.Select(testCase =>
42-
(object[])[testCase.Data, testCase.Encoded]
43-
);
33+
var data = new TheoryData<EndToEndTestCase>();
34+
foreach (var testCase in EndToEndTestCases.Cases) data.Add(testCase);
35+
return data;
4436
}
4537
}

QsNet.Tests/Fixtures/Data/EndToEndTestCases.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace QsNet.Tests.Fixtures.Data;
44

5-
internal record EndToEndTestCase(Dictionary<string, object?> Data, string Encoded);
5+
public record EndToEndTestCase(Dictionary<string, object?> Data, string Encoded);
66

77
internal static class EndToEndTestCases
88
{

0 commit comments

Comments
 (0)