Skip to content

Commit acfc7d3

Browse files
authored
Migrate unit tests to DataRow pattern (#256)
***NO_CI***
1 parent a67553f commit acfc7d3

File tree

10 files changed

+331
-278
lines changed

10 files changed

+331
-278
lines changed

Tests/NFUnitTestBitConverter/BitConverter.cs

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,26 @@ public void BitConverterTest_DoubleToInt64Bits()
4747
}
4848

4949
[TestMethod]
50-
public void BitConverterTest_GetBytesBool()
50+
[DataRow(true, new byte[] { 1 })]
51+
[DataRow(false, new byte[] { 0 })]
52+
public void BitConverterTest_GetBytesBool(bool value, byte[] expected)
5153
{
52-
Helper.GetBytesBool(true, new byte[] { 1 });
53-
Helper.GetBytesBool(false, new byte[] { 0 });
54+
Helper.GetBytesBool(value, expected);
5455
}
5556

5657
[TestMethod]
57-
public void BitConverterTest_GetBytesChar()
58+
[DataRow('\0', new byte[] { 0x00, 0x00 })]
59+
[DataRow(' ', new byte[] { 0x20, 0x00 })]
60+
[DataRow('*', new byte[] { 0x2A, 0x00 })]
61+
[DataRow('3', new byte[] { 0x33, 0x00 })]
62+
[DataRow('A', new byte[] { 0x41, 0x00 })]
63+
[DataRow('[', new byte[] { 0x5B, 0x00 })]
64+
[DataRow('a', new byte[] { 0x61, 0x00 })]
65+
[DataRow('{', new byte[] { 0x7B, 0x00 })]
66+
[DataRow('测', new byte[] { 0x4B, 0x6D })]
67+
public void BitConverterTest_GetBytesChar(char value, byte[] expected)
5868
{
59-
Helper.GetBytesChar('\0', new byte[] { 0x00, 0x00 });
60-
Helper.GetBytesChar(' ', new byte[] { 0x20, 0x00 });
61-
Helper.GetBytesChar('*', new byte[] { 0x2A, 0x00 });
62-
Helper.GetBytesChar('3', new byte[] { 0x33, 0x00 });
63-
Helper.GetBytesChar('A', new byte[] { 0x41, 0x00 });
64-
Helper.GetBytesChar('[', new byte[] { 0x5B, 0x00 });
65-
Helper.GetBytesChar('a', new byte[] { 0x61, 0x00 });
66-
Helper.GetBytesChar('{', new byte[] { 0x7B, 0x00 });
67-
Helper.GetBytesChar('测', new byte[] { 0x4B, 0x6D });
69+
Helper.GetBytesChar(value, expected);
6870
}
6971

7072
[TestMethod]
@@ -90,27 +92,39 @@ public void BitConverterTest_GetBytesDouble()
9092
}
9193

9294
[TestMethod]
93-
public void BitConverterTest_GetBytesInt16()
95+
[DataRow((short)0, new byte[] { 0x00, 0x00 })]
96+
[DataRow((short)15, new byte[] { 0x0F, 0x00 })]
97+
[DataRow((short)-15, new byte[] { 0xF1, 0xFF })]
98+
[DataRow((short)10000, new byte[] { 0x10, 0x27 })]
99+
[DataRow((short)-10000, new byte[] { 0xF0, 0xD8 })]
100+
public void BitConverterTest_GetBytesInt16(short value, byte[] expected)
101+
{
102+
Helper.GetBytesInt16(value, expected);
103+
}
104+
105+
[TestMethod]
106+
public void BitConverterTest_GetBytesInt16_MinMax()
94107
{
95-
Helper.GetBytesInt16(0, new byte[] { 0x00, 0x00 });
96-
Helper.GetBytesInt16(15, new byte[] { 0x0F, 0x00 });
97-
Helper.GetBytesInt16(-15, new byte[] { 0xF1, 0xFF });
98-
Helper.GetBytesInt16(10000, new byte[] { 0x10, 0x27 });
99-
Helper.GetBytesInt16(-10000, new byte[] { 0xF0, 0xD8 });
100108
Helper.GetBytesInt16(short.MinValue, new byte[] { 0x00, 0x80 });
101109
Helper.GetBytesInt16(short.MaxValue, new byte[] { 0xFF, 0x7F });
102110
}
103111

104112
[TestMethod]
105-
public void BitConverterTest_GetBytesInt32()
113+
[DataRow(0, new byte[] { 0x00, 0x00, 0x00, 0x00 })]
114+
[DataRow(15, new byte[] { 0x0F, 0x00, 0x00, 0x00 })]
115+
[DataRow(-15, new byte[] { 0xF1, 0xFF, 0xFF, 0xFF })]
116+
[DataRow(1048576, new byte[] { 0x00, 0x00, 0x10, 0x00 })]
117+
[DataRow(-1048576, new byte[] { 0x00, 0x00, 0xF0, 0xFF })]
118+
[DataRow(1000000000, new byte[] { 0x00, 0xCA, 0x9A, 0x3B })]
119+
[DataRow(-1000000000, new byte[] { 0x00, 0x36, 0x65, 0xC4 })]
120+
public void BitConverterTest_GetBytesInt32(int value, byte[] expected)
121+
{
122+
Helper.GetBytesInt32(value, expected);
123+
}
124+
125+
[TestMethod]
126+
public void BitConverterTest_GetBytesInt32_MinMax()
106127
{
107-
Helper.GetBytesInt32(0, new byte[] { 0x00, 0x00, 0x00, 0x00 });
108-
Helper.GetBytesInt32(15, new byte[] { 0x0F, 0x00, 0x00, 0x00 });
109-
Helper.GetBytesInt32(-15, new byte[] { 0xF1, 0xFF, 0xFF, 0xFF });
110-
Helper.GetBytesInt32(1048576, new byte[] { 0x00, 0x00, 0x10, 0x00 });
111-
Helper.GetBytesInt32(-1048576, new byte[] { 0x00, 0x00, 0xF0, 0xFF });
112-
Helper.GetBytesInt32(1000000000, new byte[] { 0x00, 0xCA, 0x9A, 0x3B });
113-
Helper.GetBytesInt32(-1000000000, new byte[] { 0x00, 0x36, 0x65, 0xC4 });
114128
Helper.GetBytesInt32(int.MinValue, new byte[] { 0x00, 0x00, 0x00, 0x80 });
115129
Helper.GetBytesInt32(int.MaxValue, new byte[] { 0xFF, 0xFF, 0xFF, 0x7F });
116130
}

Tests/NFUnitTestSystemLib/UnitTestBoolean.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ public void FalseString_Get_ReturnsFalse()
2121
}
2222

2323
[TestMethod]
24-
public void GetHashCode_Invoke_ReturnsExpected()
24+
[DataRow(true, 1)]
25+
[DataRow(false, 0)]
26+
public void GetHashCode_Invoke_ReturnsExpected(bool value, int expected)
2527
{
26-
bool _true = true;
27-
bool _false = false;
28-
29-
Assert.AreEqual(_true.GetHashCode(), 1);
30-
Assert.AreEqual(_false.GetHashCode(), 0);
28+
Assert.AreEqual(value.GetHashCode(), expected);
3129
}
3230
}
3331
}

Tests/NFUnitTestSystemLib/UnitTestByte.cs

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,45 +35,57 @@ public void MinValue()
3535
}
3636

3737
[TestMethod]
38-
public void Equals()
38+
[DataRow((byte)78, (byte)78, true)]
39+
[DataRow((byte)78, (byte)0, false)]
40+
[DataRow((byte)0, (byte)0, true)]
41+
public void Equals_ByteToByte(byte b, byte obj, bool expected)
3942
{
40-
ByteTestData[] testData = new ByteTestData[]
43+
OutputHelper.WriteLine($"Testing combination {b} and {obj}");
44+
45+
if (expected)
4146
{
42-
new ByteTestData((byte)78, (byte)78, true),
43-
new ByteTestData((byte)78, (byte)0, false),
44-
new ByteTestData((byte)0, (byte)0, true),
45-
new ByteTestData((byte)78, null, false),
46-
new ByteTestData((byte)78, "78", false),
47-
new ByteTestData((byte)78, 78, false)
48-
};
49-
50-
foreach (var test in testData)
47+
Assert.AreEqual(b, obj);
48+
Assert.IsTrue(b.GetHashCode().Equals(obj.GetHashCode()));
49+
}
50+
else
5151
{
52-
OutputHelper.WriteLine($"Testing combination {test.B} and {test.Obj}");
52+
Assert.AreNotEqual(b, obj);
53+
Assert.IsFalse(b.GetHashCode().Equals(obj.GetHashCode()));
54+
}
55+
Assert.AreEqual(b, b.GetHashCode());
56+
}
5357

54-
if (test.Obj is byte b2)
55-
{
56-
Assert.AreEqual(test.Expected, test.B.Equals(b2), $"Casting Obj wasn't successful for {test.Obj}");
57-
Assert.AreEqual(test.Expected, test.B.GetHashCode().Equals(b2.GetHashCode()), $"HashCode of {test.B}({test.B.GetHashCode()}) differs from the one of {b2}(b2.GetHashCode())");
58-
Assert.AreEqual((byte)test.B, test.B.GetHashCode(), $"HashCode of {(byte)test.B} different from expected, is {test.B.GetHashCode()}");
59-
}
58+
[TestMethod]
59+
public void Equals_ByteToNull()
60+
{
61+
byte b = 78;
62+
object obj = null;
6063

61-
Assert.AreEqual(test.Expected, test.B.Equals(test.Obj), $"Equality test between {test.B} and {test.Obj} failed");
62-
}
64+
OutputHelper.WriteLine($"Testing combination {b} and {obj}");
65+
66+
Assert.AreNotEqual(b, obj);
67+
}
68+
69+
[TestMethod]
70+
public void Equals_ByteToString()
71+
{
72+
byte b = 78;
73+
object obj = "78";
74+
75+
OutputHelper.WriteLine($"Testing combination {b} and {obj}");
76+
77+
Assert.AreNotEqual(b, obj);
6378
}
6479

65-
private sealed class ByteTestData
80+
[TestMethod]
81+
public void Equals_ByteToInt()
6682
{
67-
public object B { get; }
68-
public object Obj { get; }
69-
public bool Expected { get; }
83+
byte b = 78;
84+
object obj = 78;
7085

71-
public ByteTestData(object b, object obj, bool expected)
72-
{
73-
B = b;
74-
Obj = obj;
75-
Expected = expected;
76-
}
86+
OutputHelper.WriteLine($"Testing combination {b} and {obj}");
87+
88+
Assert.AreNotEqual(b, obj);
7789
}
7890
}
7991
}

Tests/NFUnitTestSystemLib/UnitTestInt16.cs

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,46 +35,51 @@ public void MinValue()
3535
}
3636

3737
[TestMethod]
38-
public void Equals()
38+
[DataRow((short)789, (short)789, true)]
39+
[DataRow((short)789, (short)-789, false)]
40+
[DataRow((short)789, (short)0, false)]
41+
[DataRow((short)0, (short)0, true)]
42+
[DataRow((short)-789, (short)-789, true)]
43+
[DataRow((short)-789, (short)789, false)]
44+
public void Equals_Int16ToInt16(short i1, short obj, bool expected)
3945
{
40-
Int16TestData[] testData = new Int16TestData[]
46+
if (expected)
4147
{
42-
new Int16TestData((short)789, (short)789, true),
43-
new Int16TestData((short)789, (short)-789, false),
44-
new Int16TestData((short)789, (short)0, false),
45-
new Int16TestData((short)0, (short)0, true),
46-
new Int16TestData((short)-789, (short)-789, true),
47-
new Int16TestData((short)-789, (short)789, false),
48-
new Int16TestData((short)789, null, false),
49-
new Int16TestData((short)789, "789", false),
50-
new Int16TestData((short)789, 789, false)
51-
};
52-
53-
foreach (var test in testData)
48+
Assert.AreEqual(i1, obj);
49+
Assert.IsTrue(i1.GetHashCode().Equals(obj.GetHashCode()));
50+
}
51+
else
5452
{
55-
if (test.Obj is short)
56-
{
57-
short i2 = (short)test.Obj;
58-
Assert.AreEqual(test.Expected, test.I1.Equals(i2));
59-
Assert.AreEqual(test.Expected, test.I1.GetHashCode().Equals(i2.GetHashCode()));
60-
}
61-
62-
Assert.AreEqual(test.Expected, test.I1.Equals(test.Obj));
53+
Assert.AreNotEqual(i1, obj);
54+
Assert.IsFalse(i1.GetHashCode().Equals(obj.GetHashCode()));
6355
}
6456
}
6557

66-
private sealed class Int16TestData
58+
[TestMethod]
59+
public void Equals_Int16ToNull()
6760
{
68-
public object I1 { get; }
69-
public object Obj { get; }
70-
public bool Expected { get; }
61+
short i1 = 789;
62+
object obj = null;
7163

72-
public Int16TestData(object i1, object obj, bool expected)
73-
{
74-
I1 = i1;
75-
Obj = obj;
76-
Expected = expected;
77-
}
64+
Assert.AreNotEqual(i1, obj);
65+
}
66+
67+
[TestMethod]
68+
public void Equals_Int16ToString()
69+
{
70+
short i1 = 789;
71+
object obj = "789";
72+
73+
Assert.AreNotEqual(i1, obj);
74+
}
75+
76+
[TestMethod]
77+
public void Equals_Int16ToInt()
78+
{
79+
short i1 = 789;
80+
object obj = 789;
81+
82+
Assert.AreNotEqual(i1, obj);
7883
}
7984
}
8085
}

Tests/NFUnitTestSystemLib/UnitTestInt32.cs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,45 +35,43 @@ public void MinValue()
3535
}
3636

3737
[TestMethod]
38-
public void Equals()
38+
[DataRow(789, 789, true)]
39+
[DataRow(789, -789, false)]
40+
[DataRow(789, 0, false)]
41+
[DataRow(0, 0, true)]
42+
[DataRow(-789, -789, true)]
43+
[DataRow(-789, 789, false)]
44+
public void Equals_Int32ToInt32(int i1, int obj, bool expected)
3945
{
40-
Int32TestData[] testData = new Int32TestData[]
46+
if (expected)
4147
{
42-
new Int32TestData((int)789, (int)789, true),
43-
new Int32TestData((int)789, (int)-789, false),
44-
new Int32TestData((int)789, (int)0, false),
45-
new Int32TestData((int)0, (int)0, true),
46-
new Int32TestData((int)-789, (int)-789, true),
47-
new Int32TestData((int)-789, (int)789, false),
48-
new Int32TestData((int)789, null, false),
49-
new Int32TestData((int)89, "789", false),
50-
};
51-
52-
foreach (var test in testData)
48+
Assert.AreEqual(i1, obj);
49+
Assert.IsTrue(i1.GetHashCode().Equals(obj.GetHashCode()));
50+
}
51+
else
5352
{
54-
if (test.Obj is int)
55-
{
56-
int i2 = (int)test.Obj;
57-
Assert.AreEqual(test.Expected, test.I1.Equals(i2));
58-
Assert.AreEqual(test.Expected, test.I1.GetHashCode().Equals(i2.GetHashCode()));
59-
}
60-
61-
Assert.AreEqual(test.Expected, test.I1.Equals(test.Obj));
53+
Assert.AreNotEqual(i1, obj);
54+
Assert.IsFalse(i1.GetHashCode().Equals(obj.GetHashCode()));
6255
}
56+
Assert.AreEqual(i1, i1.GetHashCode());
6357
}
6458

65-
private sealed class Int32TestData
59+
[TestMethod]
60+
public void Equals_Int32ToNull()
6661
{
67-
public object I1 { get; }
68-
public object Obj { get; }
69-
public bool Expected { get; }
62+
int i1 = 789;
63+
object obj = null;
7064

71-
public Int32TestData(object i1, object obj, bool expected)
72-
{
73-
I1 = i1;
74-
Obj = obj;
75-
Expected = expected;
76-
}
65+
Assert.AreNotEqual(i1, obj);
66+
}
67+
68+
[TestMethod]
69+
public void Equals_Int32ToString()
70+
{
71+
int i1 = 89;
72+
object obj = "789";
73+
74+
Assert.AreNotEqual(i1, obj);
7775
}
7876
}
7977
}

0 commit comments

Comments
 (0)