-
Notifications
You must be signed in to change notification settings - Fork 732
Expand file tree
/
Copy pathAssertionScopeSpecs.cs
More file actions
393 lines (326 loc) · 11.5 KB
/
AssertionScopeSpecs.cs
File metadata and controls
393 lines (326 loc) · 11.5 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using FluentAssertions.Execution;
using Xunit;
using Xunit.Sdk;
namespace FluentAssertions.Specs.Execution
{
/// <summary>
/// Type <see cref="AssertionScope"/> specs.
/// </summary>
public partial class AssertionScopeSpecs
{
[Fact]
public void When_disposed_it_should_throw_any_failures()
{
// Arrange
var scope = new AssertionScope();
AssertionChain.GetOrCreate().FailWith("Failure1");
// Act
Action act = scope.Dispose;
// Assert
try
{
act();
}
catch (Exception exception)
{
exception.Message.Should().StartWith("Failure1");
}
}
[Fact]
public void When_disposed_it_should_throw_any_failures_and_properly_format_using_args()
{
// Arrange
var scope = new AssertionScope();
AssertionChain.GetOrCreate().FailWith("Failure{0}", 1);
// Act
Action act = scope.Dispose;
// Assert
try
{
act();
}
catch (Exception exception)
{
exception.Message.Should().StartWith("Failure1");
}
}
[Fact]
public void When_lazy_version_is_not_disposed_it_should_not_execute_fail_reason_function()
{
// Arrange
var scope = new AssertionScope();
bool failReasonCalled = false;
AssertionChain.GetOrCreate()
.ForCondition(true)
.FailWith(() =>
{
failReasonCalled = true;
return new FailReason("Failure");
});
// Act
Action act = scope.Dispose;
// Assert
act();
failReasonCalled.Should().BeFalse(" fail reason function cannot be called for scope that successful");
}
[Fact]
public void When_lazy_version_is_disposed_it_should_throw_any_failures_and_properly_format_using_args()
{
// Arrange
var scope = new AssertionScope();
AssertionChain
.GetOrCreate()
.FailWith(() => new FailReason("Failure{0}", 1));
// Act
Action act = scope.Dispose;
// Assert
try
{
act();
}
catch (Exception exception)
{
exception.Message.Should().StartWith("Failure1");
}
}
[Fact]
public void When_multiple_scopes_are_nested_it_should_throw_all_failures_from_the_outer_scope()
{
// Arrange
var scope = new AssertionScope();
AssertionChain.GetOrCreate().FailWith("Failure1");
using (new AssertionScope())
{
AssertionChain.GetOrCreate().FailWith("Failure2");
using var deeplyNestedScope = new AssertionScope();
AssertionChain.GetOrCreate().FailWith("Failure3");
}
// Act
Action act = scope.Dispose;
// Assert
try
{
act();
}
catch (Exception exception)
{
exception.Message.Should().ContainAll("Failure1", "Failure2", "Failure3");
}
}
[Fact]
public void When_a_nested_scope_is_discarded_its_failures_should_also_be_discarded()
{
// Arrange
var scope = new AssertionScope();
AssertionChain.GetOrCreate().FailWith("Failure1");
using (new AssertionScope())
{
AssertionChain.GetOrCreate().FailWith("Failure2");
using var deeplyNestedScope = new AssertionScope();
AssertionChain.GetOrCreate().FailWith("Failure3");
deeplyNestedScope.Discard();
}
// Act
Action act = scope.Dispose;
// Assert
try
{
act();
}
catch (Exception exception)
{
exception.Message.Should().ContainAll("Failure1", "Failure2")
.And.NotContain("Failure3");
}
}
[Fact]
public async Task When_using_a_scope_across_thread_boundaries_it_should_work()
{
using var semaphore = new SemaphoreSlim(0, 1);
await Task.WhenAll(SemaphoreYieldAndWait(semaphore), SemaphoreYieldAndRelease(semaphore));
}
private static async Task SemaphoreYieldAndWait(SemaphoreSlim semaphore)
{
await Task.Yield();
var scope = new AssertionScope();
await semaphore.WaitAsync();
scope.Should().BeSameAs(AssertionScope.Current);
}
private static async Task SemaphoreYieldAndRelease(SemaphoreSlim semaphore)
{
await Task.Yield();
var scope = new AssertionScope();
semaphore.Release();
scope.Should().BeSameAs(AssertionScope.Current);
}
[Fact]
public void When_custom_strategy_used_respect_its_behavior()
{
// Arrange
using var _ = new AssertionScope(new FailWithStupidMessageAssertionStrategy());
// Act
Action act = () => AssertionChain.GetOrCreate().FailWith("Failure 1");
// Assert
act.Should().ThrowExactly<XunitException>()
.WithMessage("Good luck with understanding what's going on!");
}
[Fact]
public void When_custom_strategy_is_null_it_should_throw()
{
// Arrange
IAssertionStrategy strategy = null;
// Arrange / Act
Func<AssertionScope> act = () => new AssertionScope(strategy);
// Assert
act.Should().ThrowExactly<ArgumentNullException>()
.WithParameterName("assertionStrategy");
}
[Fact]
public void When_using_a_custom_strategy_it_should_include_failure_messages_of_all_failing_assertions()
{
// Arrange
var scope = new AssertionScope(new CustomAssertionStrategy());
false.Should().BeTrue();
true.Should().BeFalse();
// Act
Action act = scope.Dispose;
// Assert
act.Should().ThrowExactly<XunitException>()
.WithMessage("*but found false*but found true*");
}
[Fact]
public void When_nested_scope_is_disposed_it_passes_reports_to_parent_scope()
{
// Arrange/Act
var outerScope = new AssertionScope();
outerScope.AddReportable("outerReportable", "foo");
using (var innerScope = new AssertionScope())
{
innerScope.AddReportable("innerReportable", "bar");
}
AssertionChain.GetOrCreate().FailWith("whatever reason");
Action act = () => outerScope.Dispose();
// Assert
act.Should().Throw<XunitException>()
.Which.Message.Should().Match("Whatever reason*outerReportable*foo*innerReportable*bar*");
}
[Fact]
public void Formatting_options_passed_to_inner_assertion_scopes()
{
// Arrange
var subject = new[]
{
new
{
Value = 42
}
};
var expected = new[]
{
new
{
Value = 42
},
new
{
Value = 42
}
};
// Act
using var scope = new AssertionScope();
scope.FormattingOptions.MaxDepth = 1;
subject.Should().BeEquivalentTo(expected);
// Assert
scope.Discard().Should().ContainSingle()
.Which.Should().Contain("Maximum recursion depth of 1 was reached");
}
[Fact]
public void Multiple_named_scopes_will_prefix_the_caller_identifier()
{
// Arrange
List<int> nonEmptyList = [1, 2];
// Act
Action act = () =>
{
using var scope1 = new AssertionScope("Test1");
using var scope2 = new AssertionScope("Test2");
nonEmptyList.Should().BeEmpty();
};
// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected Test1/Test2/nonEmptyList to be empty*");
}
public class CustomAssertionStrategy : IAssertionStrategy
{
private readonly List<string> failureMessages = [];
public IEnumerable<string> FailureMessages => failureMessages;
public IEnumerable<string> DiscardFailures()
{
var discardedFailures = failureMessages.ToArray();
failureMessages.Clear();
return discardedFailures;
}
public void ThrowIfAny(IDictionary<string, object> context)
{
if (failureMessages.Count > 0)
{
var builder = new StringBuilder();
builder.AppendJoin(Environment.NewLine, failureMessages).AppendLine();
if (context.Any())
{
foreach (KeyValuePair<string, object> pair in context)
{
builder.AppendFormat(CultureInfo.InvariantCulture, "\nWith {0}:\n{1}", pair.Key, pair.Value);
}
}
AssertionEngine.TestFramework.Throw(builder.ToString());
}
}
public void HandleFailure(string message)
{
failureMessages.Add(message);
}
}
internal class FailWithStupidMessageAssertionStrategy : IAssertionStrategy
{
public IEnumerable<string> FailureMessages => [];
public void HandleFailure(string message) =>
AssertionEngine.TestFramework.Throw("Good luck with understanding what's going on!");
public IEnumerable<string> DiscardFailures() => [];
public void ThrowIfAny(IDictionary<string, object> context)
{
// do nothing
}
}
}
}
#pragma warning disable MA0047
public class AssertionScopeSpecsWithoutNamespace
#pragma warning restore MA0047
{
[Fact]
public void This_class_should_not_be_inside_a_namespace()
{
// Arrange
Type type = typeof(AssertionScopeSpecsWithoutNamespace);
// Act / Assert
type.Assembly.Should().DefineType(null, type.Name, "this class should not be inside a namespace");
}
[Fact]
public void When_the_test_method_is_not_inside_a_namespace_it_should_not_throw_a_NullReferenceException()
{
// Act
Action act = () => 1.Should().Be(2, "we don't want a NullReferenceException");
// Assert
act.Should().ThrowExactly<XunitException>()
.WithMessage("*we don't want a NullReferenceException*");
}
}