Skip to content

Commit bc20f48

Browse files
authored
Restructures Duid and DuidTests classes into multiple concern based class partials (#21)
Refactor Duid and DuidTests Classes - Split Duid class into multiple concern-based partial classes. - Split DuidTests class into multiple concern-based partial classes. - Updated to xUnit v3 and utilized `TheoryData`. - Updated package dependencies. - Implemented proper serialization of `Duid` for xUnit tests using IXunitSerializer.
1 parent c3d8fd6 commit bc20f48

21 files changed

+3301
-1747
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
using System;
2+
using Xunit;
3+
4+
namespace NetDuid.Tests
5+
{
6+
public partial class DuidTests
7+
{
8+
#region TryParse(string, Duid)
9+
10+
public static TheoryData<byte[], string> Parse_String_Success_Test_TestCases()
11+
{
12+
var theoryData = new TheoryData<byte[], string>();
13+
14+
// legal delimiters
15+
var delimiters = new string[] { ":", "-", " ", string.Empty };
16+
17+
// DUIDs should be between 3 and 130 bytes inclusively
18+
for (var byteCount = 3; byteCount <= 130; byteCount++)
19+
{
20+
var duidBytes = StaticTestData.GenerateBytes(byteCount, byteCount); // seed random with the number of bytes so byte arrays aren't similar
21+
22+
foreach (var delimiter in delimiters)
23+
{
24+
// upper case version
25+
var upperCase = duidBytes.BytesAsString(delimiter);
26+
AddTestCase(duidBytes, upperCase);
27+
28+
// lower case version
29+
var lowerCase = upperCase.ToLowerInvariant();
30+
AddTestCase(duidBytes, lowerCase);
31+
32+
if (delimiter != string.Empty)
33+
{
34+
// delimited strings do not require a leading 0
35+
var delimitedUpperCase = duidBytes.BytesAsStringNoLeadingZero(delimiter);
36+
37+
AddTestCase(duidBytes, delimitedUpperCase);
38+
AddTestCase(duidBytes, delimitedUpperCase.ToLowerInvariant());
39+
}
40+
}
41+
}
42+
43+
return theoryData;
44+
45+
void AddTestCase(byte[] expectedBytes, string inputString)
46+
{
47+
theoryData.Add(expectedBytes, inputString);
48+
}
49+
}
50+
51+
[Theory]
52+
[MemberData(nameof(Parse_String_Success_Test_TestCases))]
53+
public void TryParse_String_Success_Test(byte[] expectedBytes, string inputString)
54+
{
55+
// Arrange
56+
// Act
57+
var success = Duid.TryParse(inputString, out var duid);
58+
59+
// Assert
60+
Assert.True(success, "parse failed");
61+
Assert.NotNull(duid);
62+
63+
Assert.IsType<Duid>(duid);
64+
Assert.Equal(expectedBytes, duid.GetBytes());
65+
}
66+
67+
public static TheoryData<string> Parse_String_Invalid_Test_TestCases()
68+
{
69+
var theoryData = new TheoryData<string>();
70+
71+
AddTestCase(null);
72+
AddTestCase(string.Empty);
73+
AddTestCase("potato");
74+
75+
// invalid lengths
76+
foreach (var byteLength in new[] { 1, 2, 131, 200 })
77+
{
78+
AddTestCase(StaticTestData.GenerateBytes(byteLength).BytesAsString());
79+
}
80+
81+
return theoryData;
82+
83+
void AddTestCase(string invalidDuidString)
84+
{
85+
theoryData.Add(invalidDuidString);
86+
}
87+
}
88+
89+
[Theory]
90+
[MemberData(nameof(Parse_String_Invalid_Test_TestCases))]
91+
public void Try_Parse_String_Invalid_Test(string inputString)
92+
{
93+
// Arrange
94+
// Act
95+
var success = Duid.TryParse(inputString, out var duid);
96+
97+
// Assert
98+
Assert.False(success, "unexpectedly succeeded");
99+
Assert.Null(duid);
100+
}
101+
102+
#endregion
103+
104+
#region Parse(string)
105+
[Theory]
106+
[MemberData(nameof(Parse_String_Success_Test_TestCases))]
107+
public void Parse_Success_String_Test(byte[] expectedBytes, string inputString)
108+
{
109+
// Arrange
110+
// Act
111+
var duid = Duid.Parse(inputString);
112+
113+
// Assert
114+
Assert.NotNull(duid);
115+
116+
Assert.IsType<Duid>(duid);
117+
Assert.Equal(expectedBytes, duid.GetBytes());
118+
}
119+
120+
[Theory]
121+
[MemberData(nameof(Parse_String_Invalid_Test_TestCases))]
122+
public void Parse_String_Invalid_Throws_ArgumentException_Test(string inputString)
123+
{
124+
// Arrange
125+
// Act
126+
// Assert
127+
Assert.Throws<ArgumentException>(() => Duid.Parse(inputString));
128+
}
129+
130+
[Theory]
131+
[MemberData(nameof(Parse_String_Success_Test_TestCases))]
132+
public void Parse_Success_IFormatProvider_String_Test(byte[] expectedBytes, string inputString)
133+
{
134+
// Arrange
135+
// Act
136+
var duid = Duid.Parse(inputString, null);
137+
138+
// Assert
139+
Assert.NotNull(duid);
140+
141+
Assert.IsType<Duid>(duid);
142+
Assert.Equal(expectedBytes, duid.GetBytes());
143+
}
144+
145+
[Theory]
146+
[MemberData(nameof(Parse_String_Invalid_Test_TestCases))]
147+
public void Parse_String_IFormatProvider_Invalid_Throws_ArgumentException_Test(string inputString)
148+
{
149+
// Arrange
150+
// Act
151+
// Assert
152+
Assert.Throws<ArgumentException>(() => Duid.Parse(inputString, null));
153+
}
154+
155+
#endregion
156+
}
157+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System;
2+
using Xunit;
3+
4+
namespace NetDuid.Tests
5+
{
6+
public partial class DuidTests
7+
{
8+
#region Compare(Duid)
9+
10+
public static TheoryData<int, Duid, Duid> Compare_Duid_Test_TestCases()
11+
{
12+
var theoryData = new TheoryData<int, Duid, Duid>();
13+
14+
var referenceDuid = new Duid(new byte[] { 0x00, 0x00, 0xff });
15+
16+
AddTestCase(0, referenceDuid, referenceDuid); // by reference
17+
AddTestCase(-1, referenceDuid, null); // null case
18+
19+
// equality
20+
AddCommutativeTestCases(0, new byte[] { 0x00, 0x00, 0x00 }, new byte[] { 0x00, 0x00, 0x00 });
21+
AddCommutativeTestCases(0, new byte[] { 0xFF, 0x00, 0x80 }, new byte[] { 0xFF, 0x00, 0x80 });
22+
23+
// by length
24+
AddCommutativeTestCases(-1, new byte[3], new byte[4]);
25+
AddCommutativeTestCases(-1, new byte[3], new byte[130]);
26+
27+
// by length, then big endian value
28+
AddCommutativeTestCases(-1, new byte[] { 0xFF, 0x00, 0x00 }, new byte[] { 0xFF, 0xFF, 0xFF, 0xFF });
29+
AddCommutativeTestCases(-1, new byte[] { 0xFF, 0xFF, 0xFF }, new byte[] { 0xFF, 0x00, 0x00, 0x00 });
30+
AddCommutativeTestCases(-1, new byte[] { 0x00, 0x00, 0x00 }, new byte[] { 0x00, 0x00, 0x01 });
31+
AddCommutativeTestCases(-1, new byte[] { 0x00, 0x00, 0x00 }, new byte[] { 0x80, 0x00, 0x00 });
32+
33+
return theoryData;
34+
35+
void AddTestCase(int expectedSign, Duid thisDuid, Duid otherDuid)
36+
{
37+
theoryData.Add(expectedSign, thisDuid, otherDuid);
38+
}
39+
40+
void AddCommutativeTestCases(int expectedSign, byte[] thisBytes, byte[] otherBytes)
41+
{
42+
AddTestCase(expectedSign, new Duid(thisBytes), new Duid(otherBytes));
43+
AddTestCase(expectedSign * -1, new Duid(otherBytes), new Duid(thisBytes)); // Commutative test case form
44+
}
45+
}
46+
47+
[Theory]
48+
[MemberData(nameof(Compare_Duid_Test_TestCases))]
49+
public void Compare_Duid_Test(int expectedSign, Duid thisDuid, Duid otherDuid)
50+
{
51+
// Arrange
52+
// Act
53+
var result = thisDuid.CompareTo(otherDuid);
54+
55+
// Assert
56+
if (expectedSign == 0)
57+
{
58+
// expected equivalent
59+
Assert.Equal(0, result);
60+
}
61+
else if (expectedSign > 0)
62+
{
63+
// expected this after other
64+
Assert.True(result > 0);
65+
}
66+
else if (expectedSign < 0)
67+
{
68+
// expected this before other
69+
Assert.True(result < 0);
70+
}
71+
}
72+
73+
[Theory]
74+
[MemberData(nameof(Compare_Duid_Test_TestCases))]
75+
public void Compare_Object_Test(int expectedSign, Duid thisDuid, object other)
76+
{
77+
// Arrange
78+
// Act
79+
var result = thisDuid.CompareTo(other);
80+
81+
// Assert
82+
83+
if (expectedSign == 0)
84+
{
85+
// expected equivalent
86+
Assert.Equal(0, result);
87+
}
88+
else if (expectedSign > 0)
89+
{
90+
// expected this after other
91+
Assert.True(result > 0);
92+
}
93+
else if (expectedSign < 0)
94+
{
95+
// expected this before other
96+
Assert.True(result < 0);
97+
}
98+
}
99+
100+
[Theory]
101+
[InlineData("string")]
102+
[InlineData(42)]
103+
public void Compare_Object_NotDuid_Throws_ArgumentException_Test(object other)
104+
{
105+
// Arrange
106+
var duid = new Duid(new byte[] { 0x00, 0x00, 0xff });
107+
108+
// Act
109+
// Assert
110+
Assert.Throws<ArgumentException>(() => duid.CompareTo(other));
111+
}
112+
113+
#endregion
114+
}
115+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using Xunit;
2+
3+
namespace NetDuid.Tests
4+
{
5+
public partial class DuidTests
6+
{
7+
#region Equal(Duid)
8+
9+
public static TheoryData<bool, Duid, Duid> Equal_Duid_Test_TestCases()
10+
{
11+
var theoryData = new TheoryData<bool, Duid, Duid>();
12+
13+
// reference equality
14+
var referenceDuid = new Duid(new byte[] { 0x00, 0x00, 0xff });
15+
AddTestCase(true, referenceDuid, referenceDuid); // by reference
16+
AddTestCase(false, referenceDuid, null); // null case
17+
18+
// equality
19+
AddCommutativeTestCases(true, new byte[] { 0x00, 0x00, 0x00 }, new byte[] { 0x00, 0x00, 0x00 });
20+
AddCommutativeTestCases(true, new byte[] { 0xFF, 0x00, 0x80 }, new byte[] { 0xFF, 0x00, 0x80 });
21+
22+
// inequality
23+
AddCommutativeTestCases(false, new byte[3], new byte[4]);
24+
AddCommutativeTestCases(false, new byte[3], new byte[130]);
25+
AddCommutativeTestCases(false, new byte[] { 0xFF, 0x00, 0x00 }, new byte[] { 0xFF, 0xFF, 0xFF, 0xFF });
26+
AddCommutativeTestCases(false, new byte[] { 0xFF, 0xFF, 0xFF }, new byte[] { 0xFF, 0x00, 0x00, 0x00 });
27+
AddCommutativeTestCases(false, new byte[] { 0x00, 0x00, 0x00 }, new byte[] { 0x00, 0x00, 0x01 });
28+
AddCommutativeTestCases(false, new byte[] { 0x00, 0x00, 0x00 }, new byte[] { 0x80, 0x00, 0x00 });
29+
30+
return theoryData;
31+
32+
void AddTestCase(bool expectedEqual, Duid thisDuid, Duid otherDuid)
33+
{
34+
theoryData.Add(expectedEqual, thisDuid, otherDuid);
35+
}
36+
37+
void AddCommutativeTestCases(bool expectedEqual, byte[] thisBytes, byte[] otherBytes)
38+
{
39+
AddTestCase(expectedEqual, new Duid(thisBytes), new Duid(otherBytes));
40+
AddTestCase(expectedEqual, new Duid(otherBytes), new Duid(thisBytes)); // Commutative test case form
41+
}
42+
}
43+
44+
[Theory]
45+
[MemberData(nameof(Equal_Duid_Test_TestCases))]
46+
public void Equal_Duid_Test(bool expectedEqual, Duid thisDuid, Duid otherDuid)
47+
{
48+
// Arrange
49+
// Act
50+
var result = thisDuid.Equals(otherDuid);
51+
52+
// Assert
53+
Assert.Equal(expectedEqual, result);
54+
55+
if (expectedEqual)
56+
{
57+
Assert.Equal(thisDuid.GetHashCode(), otherDuid.GetHashCode());
58+
}
59+
}
60+
61+
[Theory]
62+
[MemberData(nameof(Equal_Duid_Test_TestCases))]
63+
public void Equal_Object_Test(bool expectedEqual, Duid thisDuid, Duid otherDuid)
64+
{
65+
// Arrange
66+
// Act
67+
var result = thisDuid.Equals(otherDuid);
68+
69+
// Assert
70+
Assert.Equal(expectedEqual, result);
71+
}
72+
73+
[Theory]
74+
[InlineData("string")]
75+
[InlineData(42)]
76+
[InlineData(null)]
77+
public void Equal_Object_NotDuid_Throws_ArgumentException_Test(object other)
78+
{
79+
// Arrange
80+
var duid = new Duid(new byte[] { 0x00, 0x00, 0xff });
81+
82+
// Act
83+
var result = duid.Equals(other);
84+
85+
// Assert
86+
Assert.False(result);
87+
}
88+
89+
#endregion
90+
91+
#region GetHashCode
92+
public static TheoryData<byte[]> HashCode_Test_TestCases()
93+
{
94+
var theoryData = new TheoryData<byte[]>();
95+
96+
for (var byteCount = 3; byteCount <= 130; byteCount += 5)
97+
{
98+
theoryData.Add(StaticTestData.GenerateBytes(byteCount, byteCount));
99+
}
100+
101+
return theoryData;
102+
}
103+
104+
[Theory]
105+
[MemberData(nameof(HashCode_Test_TestCases))]
106+
public void HashCode_EquivalentDuid_HashEqual_Test(byte[] duidBytes)
107+
{
108+
// Arrange
109+
var duidA = new Duid(duidBytes);
110+
var duidB = new Duid(duidBytes);
111+
112+
// Act
113+
// Assert
114+
Assert.Equal(duidA.GetHashCode(), duidB.GetHashCode());
115+
}
116+
#endregion
117+
}
118+
}

0 commit comments

Comments
 (0)