/
AssertionScopeSpecs.cs
514 lines (446 loc) · 24.5 KB
/
AssertionScopeSpecs.cs
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using FluentAssertions;
using FluentAssertions.Execution;
using Xunit;
using Xunit.Sdk;
#pragma warning disable RCS1110 // Declare type inside namespace.
public class AssertionScopeSpecsWithoutNamespace
#pragma warning restore RCS1110 // Declare type inside namespace.
{
#if NET45 || NET47 || NETCOREAPP2_0
[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");
}
#endif
[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*");
}
}
namespace FluentAssertions.Specs
{
public class AssertionScopeSpecs
{
[Fact]
public void When_disposed_it_should_throw_any_failures()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var scope = new AssertionScope();
AssertionScope.Current.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();
AssertionScope.Current.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;
AssertionScope.Current
.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();
AssertionScope.Current.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();
AssertionScope.Current.FailWith("Failure1");
using (var nestedScope = new AssertionScope())
{
nestedScope.FailWith("Failure2");
using (var deeplyNestedScope = new AssertionScope())
{
deeplyNestedScope.FailWith("Failure3");
}
}
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = scope.Dispose;
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
try
{
act();
}
catch (Exception exception)
{
exception.Message.Should().Contain("Failure1");
exception.Message.Should().Contain("Failure2");
exception.Message.Should().Contain("Failure3");
}
}
[Fact]
public void When_a_nested_scope_is_discarded_its_failures_should_also_be_discarded()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var scope = new AssertionScope();
AssertionScope.Current.FailWith("Failure1");
using (var nestedScope = new AssertionScope())
{
nestedScope.FailWith("Failure2");
using (var deeplyNestedScope = new AssertionScope())
{
deeplyNestedScope.FailWith("Failure3");
deeplyNestedScope.Discard();
}
}
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = scope.Dispose;
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
try
{
act();
}
catch (Exception exception)
{
exception.Message.Should().Contain("Failure1");
exception.Message.Should().Contain("Failure2");
exception.Message.Should().NotContain("Failure3");
}
}
[Fact]
public void When_the_same_failure_is_handled_twice_or_more_it_should_still_report_it_once()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var scope = new AssertionScope();
AssertionScope.Current.FailWith("Failure");
AssertionScope.Current.FailWith("Failure");
using (var nestedScope = new AssertionScope())
{
nestedScope.FailWith("Failure");
nestedScope.FailWith("Failure");
}
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = scope.Dispose;
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
try
{
act();
}
catch (Exception exception)
{
int matches = new Regex(".*Failure.*").Matches(exception.Message).Count;
matches.Should().Be(4);
}
}
[Fact]
public void When_an_assertion_fails_in_a_named_scope_it_should_use_the_name_as_the_assertion_context()
{
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () =>
{
using (new AssertionScope("foo"))
{
new[] { 1, 2, 3 }.Should().Equal(3, 2, 1);
}
};
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage("Expected foo to be equal to*");
}
[Fact]
public void When_an_assertion_fails_on_ContainKey_succeeding_message_should_be_included()
{
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () =>
{
using (new AssertionScope())
{
var values = new Dictionary<int, int>();
values.Should().ContainKey(0);
values.Should().ContainKey(1);
}
};
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage("Expected*to contain key 0*Expected*to contain key 1*");
}
[Fact]
public void When_an_assertion_fails_on_ContainSingle_succeeding_message_should_be_included()
{
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () =>
{
using (new AssertionScope())
{
var values = new List<int>();
values.Should().ContainSingle();
values.Should().ContainSingle();
}
};
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage("Expected*to contain a single item, but the collection is empty*" +
"Expected*to contain a single item, but the collection is empty*");
}
[Fact]
public void When_parentheses_are_used_in_the_because_arguments_it_should_render_them_correctly()
{
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => 1.Should().Be(2, "can't use these in becauseArgs: {0} {1}", "{", "}");
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage("*because can't use these in becauseArgs: { }*");
}
[Fact]
public void When_becauseArgs_is_null_it_should_render_reason_correctly()
{
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
object[] becauseArgs = null;
Action act = () => 1.Should().Be(2, "it should still work", becauseArgs);
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage("*it should still work*");
}
[Fact]
public void When_invalid_format_is_used_in_because_parameter_without_becauseArgs_it_should_still_render_reason_correctly()
{
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => 1.Should().Be(2, "use of {} is okay if there are no because parameters");
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage("*because use of {} is okay if there are no because parameters*");
}
[Fact]
public void When_invalid_format_is_used_in_because_parameter_along_with_becauseArgs_it_should_render_default_text()
{
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => 1.Should().Be(2, "use of {} is considered invalid in because parameter with becauseArgs", "additional becauseArgs parameter");
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage("*because message 'use of {} is considered invalid in because parameter with becauseArgs' could not be formatted with string.Format*");
}
[Fact]
public void When_an_assertion_fails_in_a_scope_with_braces_it_should_use_the_name_as_the_assertion_context()
{
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () =>
{
using (new AssertionScope("{}"))
{
default(Array).Should().Equal(3, 2, 1);
}
};
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage("Expected {} to be equal to*");
}
[Fact]
public void When_parentheses_are_used_in_literal_values_it_should_render_them_correctly()
{
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = () => "{foo}".Should().Be("{bar}");
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<XunitException>()
.WithMessage("Expected string to be \"{bar}\", but \"{foo}\" differs near*");
}
[Fact]
public void When_message_contains_double_braces_they_should_not_be_replaced_with_context()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var scope = new AssertionScope();
AssertionScope.Current.FailWith("{{empty}}");
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = scope.Dispose;
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().ThrowExactly<XunitException>()
.WithMessage("*empty*");
}
[Fact]
public void When_message_starts_with_single_braces_they_should_be_replaced_with_context()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var scope = new AssertionScope();
scope.AddReportable("MyKey", "MyValue");
AssertionScope.Current.FailWith("{MyKey}");
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = scope.Dispose;
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().ThrowExactly<XunitException>()
.WithMessage("*MyValue*");
}
[Fact]
public void When_message_starts_with_two_single_braces_they_should_be_replaced_with_context()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var scope = new AssertionScope();
scope.AddReportable("SomeKey", "SomeValue");
scope.AddReportable("AnotherKey", "AnotherValue");
AssertionScope.Current.FailWith("{SomeKey}{AnotherKey}");
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
Action act = scope.Dispose;
//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().ThrowExactly<XunitException>()
.WithMessage("*SomeValue*AnotherValue*");
}
}
}