/
FormatterSpecs.cs
1256 lines (1021 loc) · 32.9 KB
/
FormatterSpecs.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using FluentAssertions.Common;
using FluentAssertions.Extensions;
using FluentAssertions.Formatting;
using FluentAssertions.Specs.Common;
using Xunit;
using Xunit.Sdk;
namespace FluentAssertions.Specs.Formatting;
[Collection("FormatterSpecs")]
public class FormatterSpecs
{
[Fact]
public void When_value_contains_cyclic_reference_it_should_create_descriptive_error_message()
{
// Arrange
var parent = new Node();
parent.Children.Add(new Node());
parent.Children.Add(parent);
// Act
string result = Formatter.ToString(parent);
// Assert
result.Should().ContainEquivalentOf("cyclic reference");
}
[Fact]
public void When_the_same_object_appears_twice_in_the_graph_at_different_paths()
{
// Arrange
var a = new A();
var b = new B
{
X = a,
Y = a
};
// Act
Action act = () => b.Should().BeNull();
// Assert
var exception = act.Should().Throw<XunitException>().Which;
exception.Message.Should().NotContainEquivalentOf("cyclic");
}
private class A;
private class B
{
public A X { get; set; }
public A Y { get; set; }
}
[Fact]
public void When_the_subject_or_expectation_contains_reserved_symbols_it_should_escape_then()
{
// Arrange
string result = "{ a : [{ b : \"2016-05-23T10:45:12Z\" } ]}";
string expectedJson = "{ a : [{ b : \"2016-05-23T10:45:12Z\" }] }";
// Act
Action act = () => result.Should().Be(expectedJson);
// Assert
act.Should().Throw<XunitException>().WithMessage("*at*index 37*");
}
[Fact]
public void When_a_timespan_is_one_tick_it_should_be_formatted_as_positive()
{
// Arrange
var time = TimeSpan.FromTicks(1);
// Act
string result = Formatter.ToString(time);
// Assert
result.Should().NotStartWith("-");
}
[Fact]
public void When_a_timespan_is_minus_one_tick_it_should_be_formatted_as_negative()
{
// Arrange
var time = TimeSpan.FromTicks(-1);
// Act
string result = Formatter.ToString(time);
// Assert
result.Should().StartWith("-");
}
[Fact]
public void When_a_datetime_is_very_close_to_the_edges_of_a_datetimeoffset_it_should_not_crash()
{
// Arrange
var dateTime = DateTime.MinValue + 1.Minutes();
// Act
string result = Formatter.ToString(dateTime);
// Assert
result.Should().Be("<00:01:00>");
}
[Fact]
public void When_the_minimum_value_of_a_datetime_is_provided_it_should_return_a_clear_representation()
{
// Arrange
var dateTime = DateTime.MinValue;
// Act
string result = Formatter.ToString(dateTime);
// Assert
result.Should().Be("<0001-01-01 00:00:00.000>");
}
[Fact]
public void When_the_maximum_value_of_a_datetime_is_provided_it_should_return_a_clear_representation()
{
// Arrange
var dateTime = DateTime.MaxValue;
// Act
string result = Formatter.ToString(dateTime);
// Assert
result.Should().Be("<9999-12-31 23:59:59.9999999>");
}
[Fact]
public void When_a_property_throws_an_exception_it_should_ignore_that_and_still_create_a_descriptive_error_message()
{
// Arrange
var subject = new ExceptionThrowingClass();
// Act
string result = Formatter.ToString(subject);
// Assert
result.Should().Contain("Member 'ThrowingProperty' threw an exception: 'CustomMessage'");
}
[Fact]
public void When_an_exception_contains_an_inner_exception_they_should_both_appear_in_the_error_message()
{
// Arrange
Exception subject = new("OuterExceptionMessage", new InvalidOperationException("InnerExceptionMessage"));
// Act
string result = Formatter.ToString(subject);
// Assert
result.Should().Contain("OuterExceptionMessage")
.And.Contain("InnerExceptionMessage");
}
[Fact]
public void When_the_object_is_a_generic_type_without_custom_string_representation_it_should_show_the_properties()
{
// Arrange
var stuff = new List<Stuff<int>>
{
new()
{
StuffId = 1,
Description = "Stuff_1",
Children = [1, 2, 3, 4]
},
new()
{
StuffId = 2,
Description = "Stuff_2",
Children = [1, 2, 3, 4]
}
};
var expectedStuff = new List<Stuff<int>>
{
new()
{
StuffId = 1,
Description = "Stuff_1",
Children = [1, 2, 3, 4]
},
new()
{
StuffId = 2,
Description = "WRONG_DESCRIPTION",
Children = [1, 2, 3, 4]
}
};
// Act
Action act = () => stuff.Should().NotBeNull()
.And.Equal(expectedStuff, (t1, t2) => t1.StuffId == t2.StuffId && t1.Description == t2.Description);
// Assert
act.Should().Throw<XunitException>()
.WithMessage(
"""
Expected stuff to be equal to
{
FluentAssertions.Specs.Formatting.FormatterSpecs+Stuff`1[[System.Int32*]]
{
Children = {1, 2, 3, 4},
Description = "Stuff_1",
StuffId = 1
},
FluentAssertions.Specs.Formatting.FormatterSpecs+Stuff`1[[System.Int32*]]
{
Children = {1, 2, 3, 4},
Description = "WRONG_DESCRIPTION",
StuffId = 2
}
}, but
{
FluentAssertions.Specs.Formatting.FormatterSpecs+Stuff`1[[System.Int32*]]
{
Children = {1, 2, 3, 4},
Description = "Stuff_1",
StuffId = 1
},
FluentAssertions.Specs.Formatting.FormatterSpecs+Stuff`1[[System.Int32*]]
{
Children = {1, 2, 3, 4},
Description = "Stuff_2",
StuffId = 2
}
} differs at index 1.
""");
}
[Fact]
public void When_the_object_is_a_user_defined_type_it_should_show_the_name_on_the_initial_line()
{
// Arrange
var stuff = new StuffRecord(42, "description", new(24), [10, 20, 30, 40]);
// Act
Action act = () => stuff.Should().BeNull();
// Assert
act.Should().Throw<XunitException>()
.Which.Message.Should().Be(
"""
Expected stuff to be <null>, but found FluentAssertions.Specs.Formatting.FormatterSpecs+StuffRecord
{
RecordChildren = {10, 20, 30, 40},
RecordDescription = "description",
RecordId = 42,
SingleChild = FluentAssertions.Specs.Formatting.FormatterSpecs+ChildRecord
{
ChildRecordId = 24
}
}.
""");
}
[Fact]
public void When_the_object_is_an_anonymous_type_it_should_show_the_properties_recursively()
{
// Arrange
var stuff = new
{
Description = "absent",
SingleChild = new { ChildId = 8 },
Children = new[] { 1, 2, 3, 4 },
};
var expectedStuff = new
{
SingleChild = new { ChildId = 4 },
Children = new[] { 10, 20, 30, 40 },
};
// Act
Action act = () => stuff.Should().Be(expectedStuff);
// Assert
act.Should().Throw<XunitException>()
.Which.Message.Should().Be(
"""
Expected stuff to be
{
Children = {10, 20, 30, 40},
SingleChild =
{
ChildId = 4
}
}, but found
{
Children = {1, 2, 3, 4},
Description = "absent",
SingleChild =
{
ChildId = 8
}
}.
""");
}
[Fact]
public void When_the_object_is_a_list_of_anonymous_type_it_should_show_the_properties_recursively_with_newlines_and_indentation()
{
// Arrange
var stuff = new[]
{
new
{
Description = "absent",
},
new
{
Description = "absent",
},
};
var expectedStuff = new[]
{
new
{
ComplexChildren = new[]
{
new { Property = "hello" },
new { Property = "goodbye" },
},
},
};
// Act
Action act = () => stuff.Should().BeEquivalentTo(expectedStuff);
// Assert
act.Should().Throw<XunitException>()
.Which.Message.Should().StartWith(
"""
Expected stuff to be a collection with 1 item(s), but
{
{
Description = "absent"
},
{
Description = "absent"
}
}
contains 1 item(s) more than
{
{
ComplexChildren =
{
{
Property = "hello"
},
{
Property = "goodbye"
}
}
}
}.
""");
}
[Fact]
public void When_the_object_is_an_empty_anonymous_type_it_should_show_braces_on_the_same_line()
{
// Arrange
var stuff = new
{
};
// Act
Action act = () => stuff.Should().BeNull();
// Assert
act.Should().Throw<XunitException>()
.Which.Message.Should().Match("*but found { }*");
}
[Fact]
public void When_the_object_is_a_tuple_it_should_show_the_properties_recursively()
{
// Arrange
(int TupleId, string Description, List<int> Children) stuff = (1, "description", [1, 2, 3, 4]);
(int, string, List<int>) expectedStuff = (2, "WRONG_DESCRIPTION", new List<int> { 4, 5, 6, 7 });
// Act
Action act = () => stuff.Should().Be(expectedStuff);
// Assert
act.Should().Throw<XunitException>()
.Which.Message.Should().Be(
"""
Expected stuff to be equal to
{
Item1 = 2,
Item2 = "WRONG_DESCRIPTION",
Item3 = {4, 5, 6, 7}
}, but found
{
Item1 = 1,
Item2 = "description",
Item3 = {1, 2, 3, 4}
}.
""");
}
[Fact]
public void When_the_object_is_a_record_it_should_show_the_properties_recursively()
{
// Arrange
var stuff = new StuffRecord(
RecordId: 9,
RecordDescription: "descriptive",
SingleChild: new(ChildRecordId: 80),
RecordChildren: [4, 5, 6, 7]);
var expectedStuff = new
{
RecordDescription = "WRONG_DESCRIPTION",
};
// Act
Action act = () => stuff.Should().Be(expectedStuff);
// Assert
act.Should().Throw<XunitException>()
.Which.Message.Should().Be(
"""
Expected stuff to be
{
RecordDescription = "WRONG_DESCRIPTION"
}, but found FluentAssertions.Specs.Formatting.FormatterSpecs+StuffRecord
{
RecordChildren = {4, 5, 6, 7},
RecordDescription = "descriptive",
RecordId = 9,
SingleChild = FluentAssertions.Specs.Formatting.FormatterSpecs+ChildRecord
{
ChildRecordId = 80
}
}.
""");
}
[Fact]
public void When_the_to_string_override_throws_it_should_use_the_default_behavior()
{
// Arrange
var subject = new NullThrowingToStringImplementation();
// Act
string result = Formatter.ToString(subject);
// Assert
result.Should().Contain("SomeProperty");
}
[Fact]
public void
When_the_maximum_recursion_depth_is_met_it_should_give_a_descriptive_message()
{
// Arrange
var head = new Node();
var node = head;
int maxDepth = 10;
int iterations = (maxDepth / 2) + 1; // Each iteration adds two levels of depth to the graph
foreach (int i in Enumerable.Range(0, iterations))
{
var newHead = new Node();
node.Children.Add(newHead);
node = newHead;
}
// Act
string result = Formatter.ToString(head, new FormattingOptions
{
MaxDepth = maxDepth
});
// Assert
result.Should().ContainEquivalentOf($"maximum recursion depth of {maxDepth}");
}
[Fact]
public void When_the_maximum_recursion_depth_is_never_reached_it_should_render_the_entire_graph()
{
// Arrange
var head = new Node();
var node = head;
int iterations = 10;
foreach (int i in Enumerable.Range(0, iterations))
{
var newHead = new Node();
node.Children.Add(newHead);
node = newHead;
}
// Act
string result = Formatter.ToString(head, new FormattingOptions
{
// Each iteration adds two levels of depth to the graph
MaxDepth = (iterations * 2) + 1
});
// Assert
result.Should().NotContainEquivalentOf("maximum recursion depth");
}
[Fact]
public void When_formatting_a_collection_exceeds_the_max_line_count_it_should_cut_off_the_result()
{
// Arrange
var collection = Enumerable.Range(0, 20)
.Select(i => new StuffWithAField
{
Description = $"Property {i}",
Field = $"Field {i}",
StuffId = i
})
.ToArray();
// Act
string result = Formatter.ToString(collection, new FormattingOptions
{
MaxLines = 50
});
// Assert
result.Should().Match("*Output has exceeded*50*line*");
}
[Fact]
public void When_formatting_a_byte_array_it_should_limit_the_items()
{
// Arrange
byte[] value = new byte[1000];
new Random().NextBytes(value);
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().Match("{0x*, …968 more…}");
}
[Fact]
public void When_formatting_with_default_behavior_it_should_include_non_private_fields()
{
// Arrange
var stuffWithAField = new StuffWithAField { Field = "Some Text" };
// Act
string result = Formatter.ToString(stuffWithAField);
// Assert
result.Should().Contain("Field").And.Contain("Some Text");
result.Should().NotContain("privateField");
}
[Fact]
public void When_formatting_unsigned_integer_it_should_have_c_sharp_postfix()
{
// Arrange
uint value = 12U;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().Be("12u");
}
[Fact]
public void When_formatting_long_integer_it_should_have_c_sharp_postfix()
{
// Arrange
long value = 12L;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().Be("12L");
}
[Fact]
public void When_formatting_unsigned_long_integer_it_should_have_c_sharp_postfix()
{
// Arrange
ulong value = 12UL;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().Be("12UL");
}
[Fact]
public void When_formatting_short_integer_it_should_have_f_sharp_postfix()
{
// Arrange
short value = 12;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().Be("12s");
}
[Fact]
public void When_formatting_unsigned_short_integer_it_should_have_f_sharp_postfix()
{
// Arrange
ushort value = 12;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().Be("12us");
}
[Fact]
public void When_formatting_byte_it_should_use_hexadecimal_notation()
{
// Arrange
byte value = 12;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().Be("0x0C");
}
[Fact]
public void When_formatting_signed_byte_it_should_have_f_sharp_postfix()
{
// Arrange
sbyte value = 12;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().Be("12y");
}
[Fact]
public void When_formatting_single_it_should_have_c_sharp_postfix()
{
// Arrange
float value = 12;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().Be("12F");
}
[Fact]
public void When_formatting_single_positive_infinity_it_should_be_property_reference()
{
// Arrange
float value = float.PositiveInfinity;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().Be("Single.PositiveInfinity");
}
[Fact]
public void When_formatting_single_negative_infinity_it_should_be_property_reference()
{
// Arrange
float value = float.NegativeInfinity;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().Be("Single.NegativeInfinity");
}
[Fact]
public void When_formatting_single_it_should_have_max_precision()
{
// Arrange
float value = 1 / 3F;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().BeOneOf("0.33333334F", "0.333333343F");
}
[Fact]
public void When_formatting_single_not_a_number_it_should_just_say_nan()
{
// Arrange
float value = float.NaN;
// Act
string result = Formatter.ToString(value);
// Assert
// NaN is not even equal to itself so its type does not matter.
result.Should().Be("NaN");
}
[Fact]
public void When_formatting_double_integer_it_should_have_decimal_point()
{
// Arrange
double value = 12;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().Be("12.0");
}
[Fact]
public void When_formatting_double_with_big_exponent_it_should_have_exponent()
{
// Arrange
double value = 1E+30;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().Be("1E+30");
}
[Fact]
public void When_formatting_double_positive_infinity_it_should_be_property_reference()
{
// Arrange
double value = double.PositiveInfinity;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().Be("Double.PositiveInfinity");
}
[Fact]
public void When_formatting_double_negative_infinity_it_should_be_property_reference()
{
// Arrange
double value = double.NegativeInfinity;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().Be("Double.NegativeInfinity");
}
[Fact]
public void When_formatting_double_not_a_number_it_should_just_say_nan()
{
// Arrange
double value = double.NaN;
// Act
string result = Formatter.ToString(value);
// Assert
// NaN is not even equal to itself so its type does not matter.
result.Should().Be("NaN");
}
[Fact]
public void When_formatting_double_it_should_have_max_precision()
{
// Arrange
double value = 1 / 3D;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().BeOneOf("0.3333333333333333", "0.33333333333333331");
}
[Fact]
public void When_formatting_decimal_it_should_have_c_sharp_postfix()
{
// Arrange
decimal value = 12;
// Act
string result = Formatter.ToString(value);
// Assert
result.Should().Be("12M");
}
[Fact]
public void When_formatting_a_pending_task_it_should_return_the_task_status()
{
// Arrange
Task<int> bar = new TaskCompletionSource<int>().Task;
// Act
string result = Formatter.ToString(bar);
// Assert
result.Should().Be("System.Threading.Tasks.Task`1[System.Int32] {Status=WaitingForActivation}");
}
[Fact]
public void When_formatting_a_completion_source_it_should_include_the_underlying_task()
{
// Arrange
var completionSource = new TaskCompletionSource<int>();
// Act
string result = Formatter.ToString(completionSource);
// Assert
result.Should().Match("*TaskCompletionSource*Task*System.Int32*Status=WaitingForActivation*");
}
private class MyKey
{
public int KeyProp { get; set; }
}
private class MyValue
{
public int ValueProp { get; set; }
}
[Fact]
public void When_formatting_a_dictionary_it_should_format_keys_and_values()
{
// Arrange
var subject = new Dictionary<MyKey, MyValue>
{
[new MyKey { KeyProp = 13 }] = new() { ValueProp = 37 }
};
// Act
string result = Formatter.ToString(subject);
// Assert
result.Should().Match("*{*[*MyKey*KeyProp = 13*] = *MyValue*ValueProp = 37*}*");
}
[Fact]
public void When_formatting_an_empty_dictionary_it_should_be_clear_from_the_message()
{
// Arrange
var subject = new Dictionary<int, int>();
// Act
string result = Formatter.ToString(subject);
// Assert
result.Should().Match("{empty}");
}
[Fact]
public void When_formatting_a_large_dictionary_it_should_limit_the_number_of_formatted_entries()
{
// Arrange
var subject = Enumerable.Range(0, 50).ToDictionary(e => e, e => e);
// Act
string result = Formatter.ToString(subject);
// Assert
result.Should().Match("*…18 more…*");
}
[Fact]
public void
When_formatting_multiple_items_with_a_custom_string_representation_using_line_breaks_it_should_end_lines_with_a_comma()
{
// Arrange
Type[] subject = [typeof(A), typeof(B)];
// Act
string result = Formatter.ToString(subject, new FormattingOptions { UseLineBreaks = true });
// Assert
result.Should().Contain($"FluentAssertions.Specs.Formatting.FormatterSpecs+A, {Environment.NewLine}");
result.Should().Contain($"FluentAssertions.Specs.Formatting.FormatterSpecs+B{Environment.NewLine}");
}
public class BaseStuff
{
public int StuffId { get; set; }
public string Description { get; set; }
}
public class StuffWithAField
{
public int StuffId { get; set; }
public string Description { get; set; }
public string Field;
#pragma warning disable 169, CA1823, IDE0044, RCS1169
private string privateField;
#pragma warning restore 169, CA1823, IDE0044, RCS1169
}
public class Stuff<TChild> : BaseStuff
{
public List<TChild> Children { get; set; }
}
private record StuffRecord(int RecordId, string RecordDescription, ChildRecord SingleChild, List<int> RecordChildren);
private record ChildRecord(int ChildRecordId);
[Fact]
public void When_a_custom_formatter_exists_in_any_loaded_assembly_it_should_override_the_default_formatters()
{
// Arrange
Configuration.Current.ValueFormatterDetectionMode = ValueFormatterDetectionMode.Scan;
var subject = new SomeClassWithCustomFormatter
{
Property = "SomeValue"
};
// Act
string result = Formatter.ToString(subject);
// Assert
result.Should().Be("Property = SomeValue", "it should use my custom formatter");
}
[Fact]
public void When_a_base_class_has_a_custom_formatter_it_should_override_the_default_formatters()
{
// Arrange
Configuration.Current.ValueFormatterDetectionMode = ValueFormatterDetectionMode.Scan;
var subject = new SomeClassInheritedFromClassWithCustomFormatterLvl1
{
Property = "SomeValue"
};
// Act
string result = Formatter.ToString(subject);
// Assert
result.Should().Be("Property = SomeValue", "it should use my custom formatter");
}
[Fact]
public void When_there_are_multiple_custom_formatters_it_should_select_a_more_specific_one()
{
// Arrange
Configuration.Current.ValueFormatterDetectionMode = ValueFormatterDetectionMode.Scan;
var subject = new SomeClassInheritedFromClassWithCustomFormatterLvl2
{
Property = "SomeValue"
};
// Act
string result = Formatter.ToString(subject);
// Assert
result.Should().Be("Property is SomeValue", "it should use my custom formatter");