Skip to content

Commit 5564cd3

Browse files
Copilotthomhurst
andcommitted
Add IsZero/IsNotZero assertions for all numeric types
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
1 parent e1c4544 commit 5564cd3

11 files changed

Lines changed: 525 additions & 0 deletions
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
using TUnit.Assertions.Extensions;
2+
3+
namespace TUnit.Assertions.Tests;
4+
5+
public class NumericAssertionTests
6+
{
7+
// Long tests
8+
[Test]
9+
public async Task Test_Long_IsZero()
10+
{
11+
long value = 0;
12+
await Assert.That(value).IsZero();
13+
}
14+
15+
[Test]
16+
public async Task Test_Long_IsNotZero()
17+
{
18+
long value = 1234567890L;
19+
await Assert.That(value).IsNotZero();
20+
}
21+
22+
[Test]
23+
public async Task Test_Long_IsEven()
24+
{
25+
long value = 2;
26+
await Assert.That(value).IsEven();
27+
}
28+
29+
[Test]
30+
public async Task Test_Long_IsOdd()
31+
{
32+
long value = 3;
33+
await Assert.That(value).IsOdd();
34+
}
35+
36+
// Double tests
37+
[Test]
38+
public async Task Test_Double_IsZero()
39+
{
40+
double value = 0.0;
41+
await Assert.That(value).IsZero();
42+
}
43+
44+
[Test]
45+
public async Task Test_Double_IsNotZero()
46+
{
47+
double value = 1.5;
48+
await Assert.That(value).IsNotZero();
49+
}
50+
51+
[Test]
52+
public async Task Test_Double_IsNotZero_Negative()
53+
{
54+
double value = -3.14;
55+
await Assert.That(value).IsNotZero();
56+
}
57+
58+
// Float tests
59+
[Test]
60+
public async Task Test_Float_IsZero()
61+
{
62+
float value = 0.0f;
63+
await Assert.That(value).IsZero();
64+
}
65+
66+
[Test]
67+
public async Task Test_Float_IsNotZero()
68+
{
69+
float value = 2.5f;
70+
await Assert.That(value).IsNotZero();
71+
}
72+
73+
// Decimal tests
74+
[Test]
75+
public async Task Test_Decimal_IsZero()
76+
{
77+
decimal value = 0m;
78+
await Assert.That(value).IsZero();
79+
}
80+
81+
[Test]
82+
public async Task Test_Decimal_IsNotZero()
83+
{
84+
decimal value = 99.99m;
85+
await Assert.That(value).IsNotZero();
86+
}
87+
88+
// Short tests
89+
[Test]
90+
public async Task Test_Short_IsZero()
91+
{
92+
short value = 0;
93+
await Assert.That(value).IsZero();
94+
}
95+
96+
[Test]
97+
public async Task Test_Short_IsNotZero()
98+
{
99+
short value = 100;
100+
await Assert.That(value).IsNotZero();
101+
}
102+
103+
[Test]
104+
public async Task Test_Short_IsEven()
105+
{
106+
short value = 4;
107+
await Assert.That(value).IsEven();
108+
}
109+
110+
[Test]
111+
public async Task Test_Short_IsOdd()
112+
{
113+
short value = 5;
114+
await Assert.That(value).IsOdd();
115+
}
116+
117+
// Byte tests
118+
[Test]
119+
public async Task Test_Byte_IsZero()
120+
{
121+
byte value = 0;
122+
await Assert.That(value).IsZero();
123+
}
124+
125+
[Test]
126+
public async Task Test_Byte_IsNotZero()
127+
{
128+
byte value = 255;
129+
await Assert.That(value).IsNotZero();
130+
}
131+
132+
[Test]
133+
public async Task Test_Byte_IsEven()
134+
{
135+
byte value = 10;
136+
await Assert.That(value).IsEven();
137+
}
138+
139+
[Test]
140+
public async Task Test_Byte_IsOdd()
141+
{
142+
byte value = 11;
143+
await Assert.That(value).IsOdd();
144+
}
145+
146+
// Uint tests
147+
[Test]
148+
public async Task Test_Uint_IsZero()
149+
{
150+
uint value = 0;
151+
await Assert.That(value).IsZero();
152+
}
153+
154+
[Test]
155+
public async Task Test_Uint_IsNotZero()
156+
{
157+
uint value = 12345;
158+
await Assert.That(value).IsNotZero();
159+
}
160+
161+
[Test]
162+
public async Task Test_Uint_IsEven()
163+
{
164+
uint value = 6;
165+
await Assert.That(value).IsEven();
166+
}
167+
168+
[Test]
169+
public async Task Test_Uint_IsOdd()
170+
{
171+
uint value = 7;
172+
await Assert.That(value).IsOdd();
173+
}
174+
175+
// Ulong tests
176+
[Test]
177+
public async Task Test_Ulong_IsZero()
178+
{
179+
ulong value = 0;
180+
await Assert.That(value).IsZero();
181+
}
182+
183+
[Test]
184+
public async Task Test_Ulong_IsNotZero()
185+
{
186+
ulong value = 9876543210UL;
187+
await Assert.That(value).IsNotZero();
188+
}
189+
190+
[Test]
191+
public async Task Test_Ulong_IsEven()
192+
{
193+
ulong value = 8;
194+
await Assert.That(value).IsEven();
195+
}
196+
197+
[Test]
198+
public async Task Test_Ulong_IsOdd()
199+
{
200+
ulong value = 9;
201+
await Assert.That(value).IsOdd();
202+
}
203+
204+
// Ushort tests
205+
[Test]
206+
public async Task Test_Ushort_IsZero()
207+
{
208+
ushort value = 0;
209+
await Assert.That(value).IsZero();
210+
}
211+
212+
[Test]
213+
public async Task Test_Ushort_IsNotZero()
214+
{
215+
ushort value = 500;
216+
await Assert.That(value).IsNotZero();
217+
}
218+
219+
[Test]
220+
public async Task Test_Ushort_IsEven()
221+
{
222+
ushort value = 12;
223+
await Assert.That(value).IsEven();
224+
}
225+
226+
[Test]
227+
public async Task Test_Ushort_IsOdd()
228+
{
229+
ushort value = 13;
230+
await Assert.That(value).IsOdd();
231+
}
232+
233+
// Sbyte tests
234+
[Test]
235+
public async Task Test_Sbyte_IsZero()
236+
{
237+
sbyte value = 0;
238+
await Assert.That(value).IsZero();
239+
}
240+
241+
[Test]
242+
public async Task Test_Sbyte_IsNotZero()
243+
{
244+
sbyte value = -50;
245+
await Assert.That(value).IsNotZero();
246+
}
247+
248+
[Test]
249+
public async Task Test_Sbyte_IsEven()
250+
{
251+
sbyte value = 14;
252+
await Assert.That(value).IsEven();
253+
}
254+
255+
[Test]
256+
public async Task Test_Sbyte_IsOdd()
257+
{
258+
sbyte value = 15;
259+
await Assert.That(value).IsOdd();
260+
}
261+
}

TUnit.Assertions/ByteAssertions.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using TUnit.Assertions.Attributes;
2+
3+
namespace TUnit.Assertions;
4+
5+
public static partial class ByteAssertions
6+
{
7+
[GenerateAssertion(ExpectationMessage = "to be zero")]
8+
public static bool IsZero(this byte value)
9+
{
10+
return value == 0;
11+
}
12+
13+
[GenerateAssertion(ExpectationMessage = "to not be zero")]
14+
public static bool IsNotZero(this byte value)
15+
{
16+
return value != 0;
17+
}
18+
19+
[GenerateAssertion]
20+
public static bool IsEven(this byte value)
21+
{
22+
return value % 2 == 0;
23+
}
24+
25+
[GenerateAssertion]
26+
public static bool IsOdd(this byte value)
27+
{
28+
return value % 2 != 0;
29+
}
30+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using TUnit.Assertions.Attributes;
2+
3+
namespace TUnit.Assertions;
4+
5+
public static partial class DecimalAssertions
6+
{
7+
[GenerateAssertion(ExpectationMessage = "to be zero")]
8+
public static bool IsZero(this decimal value)
9+
{
10+
return value == 0m;
11+
}
12+
13+
[GenerateAssertion(ExpectationMessage = "to not be zero")]
14+
public static bool IsNotZero(this decimal value)
15+
{
16+
return value != 0m;
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using TUnit.Assertions.Attributes;
2+
3+
namespace TUnit.Assertions;
4+
5+
public static partial class DoubleAssertions
6+
{
7+
[GenerateAssertion(ExpectationMessage = "to be zero")]
8+
public static bool IsZero(this double value)
9+
{
10+
return value == 0.0;
11+
}
12+
13+
[GenerateAssertion(ExpectationMessage = "to not be zero")]
14+
public static bool IsNotZero(this double value)
15+
{
16+
return value != 0.0;
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using TUnit.Assertions.Attributes;
2+
3+
namespace TUnit.Assertions;
4+
5+
public static partial class FloatAssertions
6+
{
7+
[GenerateAssertion(ExpectationMessage = "to be zero")]
8+
public static bool IsZero(this float value)
9+
{
10+
return value == 0.0f;
11+
}
12+
13+
[GenerateAssertion(ExpectationMessage = "to not be zero")]
14+
public static bool IsNotZero(this float value)
15+
{
16+
return value != 0.0f;
17+
}
18+
}

TUnit.Assertions/LongAssertions.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using TUnit.Assertions.Attributes;
2+
3+
namespace TUnit.Assertions;
4+
5+
public static partial class LongAssertions
6+
{
7+
[GenerateAssertion(ExpectationMessage = "to be zero")]
8+
public static bool IsZero(this long value)
9+
{
10+
return value == 0;
11+
}
12+
13+
[GenerateAssertion(ExpectationMessage = "to not be zero")]
14+
public static bool IsNotZero(this long value)
15+
{
16+
return value != 0;
17+
}
18+
19+
[GenerateAssertion]
20+
public static bool IsEven(this long value)
21+
{
22+
return value % 2 == 0;
23+
}
24+
25+
[GenerateAssertion]
26+
public static bool IsOdd(this long value)
27+
{
28+
return value % 2 != 0;
29+
}
30+
}

0 commit comments

Comments
 (0)