-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInt64EntityTests.cs
More file actions
278 lines (238 loc) · 7.24 KB
/
Int64EntityTests.cs
File metadata and controls
278 lines (238 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System;
using FluentAssertions;
using Light.SharedCore.Entities;
using Light.Xunit;
using Xunit;
using Xunit.Abstractions;
namespace Light.SharedCore.Tests.Entities;
public sealed class Int64EntityTests
{
public Int64EntityTests(ITestOutputHelper output) => Output = output;
private ITestOutputHelper Output { get; }
public static TheoryData<long> NegativeIds { get; } =
new () { -1L, -58192923421L, long.MinValue };
[Fact]
public static void MustImplementIEntityOfInt64() => typeof(Int64Entity<>).Should().Implement<IEntity<long>>();
[Fact]
public static void TheSameInstanceShouldBeConsideredEqual()
{
var entity = new Entity(42L);
// ReSharper disable EqualExpressionComparison
#pragma warning disable CS1718 // We explicitly want to test the equality operators here
(entity == entity).Should().BeTrue();
(entity != entity).Should().BeFalse();
#pragma warning restore CS1718
// ReSharper restore EqualExpressionComparison
entity.GetHashCode().Should().Be(entity.GetHashCode());
}
[Fact]
public static void TwoInstancesWithTheSameIdShouldBeEqual()
{
var x = new Entity(50_203_040_004L);
var y = new Entity(50_203_040_004L);
(x == y).Should().BeTrue();
(x != y).Should().BeFalse();
x.GetHashCode().Should().Be(y.GetHashCode());
}
[Fact]
public static void TwoInstancesWithDifferentIdsShouldNotBeEqual()
{
var x = new Entity(1L);
var y = new Entity(2L);
(x == y).Should().BeFalse();
(x != y).Should().BeTrue();
x.GetHashCode().Should().NotBe(y.GetHashCode());
}
[Fact]
public static void ComparingWithNullMustReturnFalse()
{
var x = new Entity(3L);
var y = default(Entity);
// ReSharper disable ConditionIsAlwaysTrueOrFalse
(x == y).Should().BeFalse();
(x != y).Should().BeTrue();
(y == x).Should().BeFalse();
(y != x).Should().BeTrue();
// ReSharper restore ConditionIsAlwaysTrueOrFalse
}
[Fact]
public static void TwoNullReferencesAreEqual()
{
var @null = default(Entity);
// ReSharper disable EqualExpressionComparison
// ReSharper disable ConditionIsAlwaysTrueOrFalse
#pragma warning disable CS1718 // We explicitly want to test the equality operators here
(@null == @null).Should().BeTrue();
(@null != @null).Should().BeFalse();
#pragma warning restore CS1718
// ReSharper restore EqualExpressionComparison
// ReSharper restore ConditionIsAlwaysTrueOrFalse
}
[Theory]
[InlineData(5L)]
[InlineData(359_544L)]
[InlineData(10001L)]
public static void ToStringShouldReturnSimpleTypeNameAndId(long id) =>
new Entity(id).ToString().Should().Be("Entity " + id);
[Theory]
[MemberData(nameof(NegativeIds))]
public void ConstructorShouldThrowWhenPassingNegativeId(long negativeId)
{
void Act() => _ = new Entity(negativeId);
ExpectArgumentOutOfRangeException(Act);
}
[Theory]
[MemberData(nameof(NegativeIds))]
public void PropertyInitializerShouldThrowWhenPassingNegativeId(long negativeId)
{
void Act() => _ = new Entity { Id = negativeId };
ExpectArgumentOutOfRangeException(Act, "value");
}
[Theory]
[MemberData(nameof(NegativeIds))]
public static void AllowNegativeIdViaConstructor(long negativeId)
{
try
{
Entity.AllowNegativeIds = true;
var entity = new Entity(negativeId);
entity.Id.Should().Be(negativeId);
}
finally
{
Entity.AllowNegativeIds = false;
}
}
[Theory]
[MemberData(nameof(NegativeIds))]
public static void AllowNegativeIdViaPropertyInitializer(long negativeId)
{
try
{
Entity.AllowNegativeIds = true;
var entity = new Entity { Id = negativeId };
entity.Id.Should().Be(negativeId);
}
finally
{
Entity.AllowNegativeIds = false;
}
}
[Fact]
public void ConstructorShouldThrowWhenZeroIsPassed()
{
void Act() => _ = new Entity(0L);
ExpectArgumentOutOfRangeException(Act);
}
[Fact]
public void PropertyInitializerShouldThrowWhenZeroIsPassed()
{
void Act() => _ = new Entity { Id = 0L };
ExpectArgumentOutOfRangeException(Act, "value");
}
[Fact]
public static void AllowZeroIdViaConstructor()
{
try
{
Entity.AllowIdZero = true;
var entity = new Entity(0L);
entity.Id.Should().Be(0L);
}
finally
{
Entity.AllowIdZero = false;
}
}
[Fact]
public static void AllowZeroViaPropertyInitialization()
{
try
{
Entity.AllowIdZero = true;
var entity = new Entity { Id = 0L };
entity.Id.Should().Be(0L);
}
finally
{
Entity.AllowIdZero = false;
}
}
[Fact]
public static void SetAllowZeroAndAllowNegativeInOneCall()
{
try
{
Entity.AllowZeroAndNegativeIds();
Entity.AllowIdZero.Should().BeTrue();
Entity.AllowNegativeIds.Should().BeTrue();
}
finally
{
Entity.AllowNegativeIds = false;
Entity.AllowIdZero = false;
}
}
[Fact]
public static void SetIdAfterInitialization()
{
var entity = new Entity();
entity.ToMutable().SetId(42L);
entity.Id.Should().Be(42L);
}
[Fact]
public void SetIdAfterInitializationShouldThrowWhenIdIsZero()
{
void Act() => new Entity().ToMutable().SetId(0L);
ExpectArgumentOutOfRangeException(Act);
}
[Theory]
[MemberData(nameof(NegativeIds))]
public void SetIdAfterInitializationShouldThrowWhenIdIsNegative(long negativeId)
{
void Act() => new Entity().ToMutable().SetId(negativeId);
ExpectArgumentOutOfRangeException(Act);
}
[Fact]
public static void AllowZeroOnSetIdAfterInitialization()
{
try
{
Entity.AllowIdZero = true;
var entity = new Entity();
entity.ToMutable().SetId(0L);
entity.Id.Should().Be(0L);
}
finally
{
Entity.AllowIdZero = false;
}
}
[Theory]
[MemberData(nameof(NegativeIds))]
public static void AllowNegativeIdsOnSetIdAfterInitialization(long negativeId)
{
try
{
Entity.AllowNegativeIds = true;
var entity = new Entity();
entity.ToMutable().SetId(negativeId);
entity.Id.Should().Be(negativeId);
}
finally
{
Entity.AllowNegativeIds = false;
}
}
private void ExpectArgumentOutOfRangeException(Action act, string parameterName = "id")
{
var exception = act.Should().Throw<ArgumentOutOfRangeException>().Which;
exception.ShouldBeWrittenTo(Output);
exception.ParamName.Should().Be(parameterName);
}
private sealed class Entity : Int64Entity<Entity>
{
public Entity() { }
public Entity(long id) : base(id) { }
}
}