-
Notifications
You must be signed in to change notification settings - Fork 14
/
Generic_SVO_specs.cs
452 lines (388 loc) · 10.9 KB
/
Generic_SVO_specs.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
namespace Customization.SVO_specs;
public class Default_behavior
{
[Test]
public void MinLength_is_0()
=> new WithDefaultBehavior().MinLength.Should().Be(0);
[Test]
public void MaxLength_is_int_MaxValue()
=> new WithDefaultBehavior().MaxLength.Should().Be(int.MaxValue);
[Test]
public void Regex_Pattern_is_null()
=> new WithDefaultBehavior().Pattern.Should().BeNull();
[Test]
public void ToJson_returns_underlying_value()
=> GenericSvo.Empty.ToJson().Should().BeNull();
}
public class With_domain_logic
{
[TestCase(true, "QOWAIV")]
[TestCase(true, "?")]
[TestCase(false, "")]
public void HasValue_is(bool result, CustomSvo svo) => svo.HasValue.Should().Be(result);
[TestCase(true, "QOWAIV")]
[TestCase(false, "?")]
[TestCase(false, "")]
public void IsKnown_is(bool result, CustomSvo svo) => svo.IsKnown.Should().Be(result);
[TestCase("")]
[TestCase("?")]
public void has_length_zero_for_empty_and_unknown(CustomSvo svo)
=> svo.Length.Should().Be(0);
[TestCase(6, "QOWAIV")]
public void has_length(int length, CustomSvo svo)
=> svo.Length.Should().Be(length);
[TestCase(false, "QOWAIV")]
[TestCase(false, "?")]
[TestCase(true, "")]
public void IsEmpty_returns(bool result, CustomSvo svo)
=> svo.IsEmpty().Should().Be(result);
[TestCase(false, "QOWAIV")]
[TestCase(true, "?")]
[TestCase(true, "")]
public void IsEmptyOrUnknown_returns(bool result, CustomSvo svo)
=> svo.IsEmptyOrUnknown().Should().Be(result);
[TestCase(false, "QOWAIV")]
[TestCase(true, "?")]
[TestCase(false, "")]
public void IsUnknown_returns(bool result, CustomSvo svo)
=> svo.IsUnknown().Should().Be(result);
}
public class Has_constant
{
[Test]
public void Empty_represent_default_value()
=> CustomSvo.Empty.Should().Be(default);
}
public class Is_equal_by_value
{
[Test]
public void not_equal_to_null()
=> Svo.CustomSvo.Equals(null).Should().BeFalse();
[Test]
public void not_equal_to_other_type()
=> Svo.CustomSvo.Equals(new object()).Should().BeFalse();
[Test]
public void not_equal_to_different_value()
=> Svo.CustomSvo.Equals(CustomSvo.Parse("different")).Should().BeFalse();
[Test]
public void equal_to_same_value()
=> Svo.CustomSvo.Equals(CustomSvo.Parse("QOWAIV")).Should().BeTrue();
[Test]
public void equal_operator_returns_true_for_same_values()
=> (Svo.CustomSvo == CustomSvo.Parse("QOWAIV")).Should().BeTrue();
[Test]
public void equal_operator_returns_false_for_different_values()
=> (Svo.CustomSvo == CustomSvo.Parse("different")).Should().BeFalse();
[Test]
public void not_equal_operator_returns_false_for_same_values()
=> (Svo.CustomSvo != CustomSvo.Parse("QOWAIV")).Should().BeFalse();
[Test]
public void not_equal_operator_returns_true_for_different_values()
=> (Svo.CustomSvo != CustomSvo.Parse("different")).Should().BeTrue();
[TestCase("", 0)]
[TestCase("QOWAIV", 467820648)]
public void hash_code_is_value_based(CustomSvo svo, int hash)
{
using (Hash.WithoutRandomizer())
{
svo.GetHashCode().Should().Be(hash);
}
}
}
public class Can_be_parsed
{
[Test]
public void from_null_string_represents_Empty()
=> CustomSvo.Parse(null).Should().Be(CustomSvo.Empty);
[Test]
public void from_empty_string_represents_Empty()
=> CustomSvo.Parse(string.Empty).Should().Be(CustomSvo.Empty);
[Test]
public void from_question_mark_represents_Unknown()
=> CustomSvo.Parse("?").Should().Be(CustomSvo.Unknown);
[TestCase("en", "QOWAIV")]
public void from_string_with_different_formatting_and_cultures(CultureInfo culture, string input)
{
using (culture.Scoped())
{
CustomSvo.Parse(input).Should().Be(Svo.CustomSvo);
}
}
[Test]
public void from_valid_input_only_otherwise_throws_on_Parse()
{
using (TestCultures.en_GB.Scoped())
{
Func<CustomSvo> parse = () => CustomSvo.Parse("invalid input!");
parse.Should().Throw<FormatException>()
.WithMessage("Not a valid CustomSvo");
}
}
[Test]
public void from_valid_input_only_otherwise_return_false_on_TryParse()
=> CustomSvo.TryParse("invalid input", out _).Should().BeFalse();
[Test]
public void from_invalid_as_null_with_TryParse()
=> CustomSvo.TryParse("invalid input!").Should().BeNull();
[Test]
public void with_TryParse_returns_SVO()
=> CustomSvo.TryParse("QOWAIV").Should().Be(Svo.CustomSvo);
}
public class Has_custom_formatting
{
[Test]
public void _default()
{
using (TestCultures.en_GB.Scoped())
{
Svo.CustomSvo.ToString().Should().Be("QOWAIV");
}
}
[Test]
public void with_null_pattern_equal_to_default()
{
using (TestCultures.en_GB.Scoped())
{
Svo.CustomSvo.ToString().Should().Be(Svo.CustomSvo.ToString(default(string)));
}
}
[Test]
public void with_string_empty_pattern_equal_to_default()
{
using (TestCultures.en_GB.Scoped())
{
Svo.CustomSvo.ToString().Should().Be(Svo.CustomSvo.ToString(string.Empty));
}
}
[Test]
public void default_value_is_represented_as_string_empty()
=> default(CustomSvo).ToString().Should().BeEmpty();
[Test]
public void unknown_value_is_represented_as_unknown()
=> CustomSvo.Unknown.ToString().Should().Be("?");
[Test]
public void with_empty_format_provider()
{
using (TestCultures.es_EC.Scoped())
{
Svo.CustomSvo.ToString(FormatProvider.Empty).Should().Be("QOWAIV");
}
}
[Test]
public void custom_format_provider_is_applied()
{
var formatted = Svo.CustomSvo.ToString("SomeFormat", FormatProvider.CustomFormatter);
formatted.Should().Be("Unit Test Formatter, value: 'QOWAIV', format: 'SomeFormat'");
}
[TestCase("en-GB", null, "QOWAIV", "QOWAIV")]
[TestCase("nl-BE", "f", "QOWAIV", "QOWAIV")]
public void culture_dependent(CultureInfo culture, string format, CustomSvo svo, string expected)
{
using (culture.Scoped())
{
svo.ToString(format).Should().Be(expected);
}
}
[Test]
public void with_current_thread_culture_as_default()
{
using (new CultureInfoScope(
culture: TestCultures.nl_NL,
cultureUI: TestCultures.en_GB))
{
Svo.CustomSvo.ToString(provider: null).Should().Be("QOWAIV");
}
}
}
public class Is_comparable
{
[Test]
public void to_null_is_1() => Svo.CustomSvo.CompareTo(Nil.Object).Should().Be(1);
[Test]
public void to_Svo_as_object()
{
object obj = Svo.CustomSvo;
Svo.CustomSvo.CompareTo(obj).Should().Be(0);
}
[Test]
public void to_Svo_only()
=> new object().Invoking(Svo.CustomSvo.CompareTo).Should().Throw<ArgumentException>();
[Test]
public void can_be_sorted_using_compare()
{
var sorted = new[]
{
default,
default,
CustomSvo.Parse("ABC"),
CustomSvo.Parse("ABCD"),
CustomSvo.Parse("ABCDE"),
CustomSvo.Unknown,
};
var list = new List<CustomSvo> { sorted[3], sorted[4], sorted[5], sorted[2], sorted[0], sorted[1] };
list.Sort();
list.Should().BeEquivalentTo(sorted);
}
}
public class Casts
{
[Test]
public void explicitly_from_string()
{
var casted = (CustomSvo)"QOWAIV";
casted.Should().Be(Svo.CustomSvo);
}
[Test]
public void explicitly_to_string()
{
var casted = (string)Svo.CustomSvo;
casted.Should().Be("QOWAIV");
}
}
public class Supports_type_conversion
{
[Test]
public void via_TypeConverter_registered_with_attribute()
=> typeof(CustomSvo).Should().HaveTypeConverterDefined();
[Test]
public void from_null_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.FromNull<string>().To<CustomSvo>().Should().Be(CustomSvo.Empty);
}
}
[Test]
public void from_empty_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.From(string.Empty).To<CustomSvo>().Should().Be(CustomSvo.Empty);
}
}
[Test]
public void from_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.From("QOWAIV").To<CustomSvo>().Should().Be(Svo.CustomSvo);
}
}
[Test]
public void to_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.ToString().From(Svo.CustomSvo).Should().Be("QOWAIV");
}
}
}
public class Supports_JSON_serialization
{
#if NET6_0_OR_GREATER
[TestCase(null, null)]
[TestCase("?", "?")]
[TestCase("QOWAIV", "QOWAIV")]
public void System_Text_JSON_deserialization(object json, CustomSvo svo)
=> JsonTester.Read_System_Text_JSON<CustomSvo>(json).Should().Be(svo);
[TestCase(null, null)]
[TestCase("QOWAIV", "QOWAIV")]
public void System_Text_JSON_serialization(CustomSvo svo, object json)
=> JsonTester.Write_System_Text_JSON(svo).Should().Be(json);
[TestCase("{}")]
public void System_Text_JSON_throws_on(string json)
{
json.Invoking(json => System.Text.Json.JsonSerializer.Deserialize<CustomSvo>(json))
.Should().Throw<System.Text.Json.JsonException>();
}
#endif
[TestCase("?", "?")]
[TestCase("QOWAIV", "QOWAIV")]
public void convention_based_deserialization(object json, CustomSvo svo)
=> JsonTester.Read<CustomSvo>(json).Should().Be(svo);
[TestCase(null, null)]
[TestCase("QOWAIV", "QOWAIV")]
public void convention_based_serialization(CustomSvo svo, object json)
=> JsonTester.Write(svo).Should().Be(json);
[TestCase("Invalid input!", typeof(FormatException))]
[TestCase(5L, typeof(InvalidOperationException))]
public void throws_for_invalid_json(object json, Type exceptionType)
{
Func<CustomSvo> read = () => JsonTester.Read<CustomSvo>(json);
read.Should().Throw<Exception>().Subject.Single().Should().BeOfType(exceptionType);
}
}
public class Supports_XML_serialization
{
[Test]
public void using_XmlSerializer_to_serialize()
{
var xml = Serialize.Xml(Svo.CustomSvo);
xml.Should().Be("QOWAIV");
}
[Test]
public void using_XmlSerializer_to_deserialize()
{
var svo = Deserialize.Xml<CustomSvo>("QOWAIV");
svo.Should().Be(Svo.CustomSvo);
}
[Test]
public void using_DataContractSerializer()
{
var round_tripped = SerializeDeserialize.DataContract(Svo.CustomSvo);
Svo.CustomSvo.Should().Be(round_tripped);
}
[Test]
public void as_part_of_a_structure()
{
var structure = XmlStructure.New(Svo.CustomSvo);
var round_tripped = SerializeDeserialize.Xml(structure);
structure.Should().Be(round_tripped);
}
[Test]
public void has_no_custom_XML_schema()
{
IXmlSerializable obj = Svo.CustomSvo;
obj.GetSchema().Should().BeNull();
}
}
public class Is_Open_API_data_type
{
[Test]
public void with_info()
=> OpenApiDataType.FromType(typeof(ForCustomSvo))
.Should().Be(new OpenApiDataType(
dataType: typeof(CustomSvo),
description: "Custom SVO Example",
example: "QOWAIV",
type: "string",
format: "custom",
pattern: null));
}
#if NET8_0_OR_GREATER
#else
public class Supports_binary_serialization
{
[Test]
[Obsolete("Usage of the binary formatter is considered harmful.")]
public void using_BinaryFormatter()
{
var round_tripped = SerializeDeserialize.Binary(Svo.CustomSvo);
round_tripped.Should().Be(Svo.CustomSvo);
}
[Test]
public void storing_string_in_SerializationInfo()
{
var info = Serialize.GetInfo(Svo.CustomSvo);
info.GetString("Value").Should().Be("QOWAIV");
}
}
#endif
public class Debugger
{
[TestCase("{empty}", "")]
[TestCase("{unknown}", "?")]
[TestCase("QOWAIV", "QOWAIV")]
public void has_custom_display(object display, CustomSvo svo)
=> svo.Should().HaveDebuggerDisplay(display);
}