/
Energy_label_specs.cs
578 lines (491 loc) · 14.1 KB
/
Energy_label_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
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
using Qowaiv.Sustainability;
namespace Energy_label_specs;
public class With_domain_logic
{
[TestCase(true, "A++")]
[TestCase(true, "?")]
[TestCase(false, "")]
public void HasValue_is(bool result, EnergyLabel svo) => svo.HasValue.Should().Be(result);
[TestCase(true, "A++")]
[TestCase(false, "?")]
[TestCase(false, "")]
public void IsKnown_is(bool result, EnergyLabel svo) => svo.IsKnown.Should().Be(result);
[TestCase(false, "A++")]
[TestCase(false, "?")]
[TestCase(true, "")]
public void IsEmpty_returns(bool result, EnergyLabel svo)
=> svo.IsEmpty().Should().Be(result);
[TestCase(false, "A++")]
[TestCase(true, "?")]
[TestCase(true, "")]
public void IsEmptyOrUnknown_returns(bool result, EnergyLabel svo)
=> svo.IsEmptyOrUnknown().Should().Be(result);
[TestCase(false, "A++")]
[TestCase(true, "?")]
[TestCase(false, "")]
public void IsUnknown_returns(bool result, EnergyLabel svo)
=> svo.IsUnknown().Should().Be(result);
[TestCase(-1)]
[TestCase(5)]
[TestCase(6)]
public void with_zero_to_four_plusses(int plusses)
=> plusses.Invoking(_ => EnergyLabel.A(plusses))
.Should().Throw<ArgumentOutOfRangeException>();
}
public class Is_valid_for
{
[Test]
public void whitespace()
=> EnergyLabel.TryParse(" ").Should().NotBeNull();
[Test]
public void string_empty()
=> EnergyLabel.TryParse(string.Empty).Should().NotBeNull();
[Test]
public void string_null()
=> EnergyLabel.TryParse(null).Should().NotBeNull();
[TestCase("?")]
[TestCase("unknown")]
public void strings_representing_unknown(string input)
=> EnergyLabel.TryParse(input).Should().NotBeNull();
[TestCase("g", "de")]
[TestCase("G", "de")]
[TestCase("F", "fr")]
[TestCase("E", "es")]
[TestCase("D", "nl")]
[TestCase("C", "nl")]
[TestCase("B", "nl")]
[TestCase("A", "nl")]
[TestCase("A+", "nl")]
[TestCase("A++", "nl")]
[TestCase("A+++", "nl")]
[TestCase("A++++", "nl")]
[TestCase("a++++", "pt")]
public void @string(string input, CultureInfo culture)
=> EnergyLabel.TryParse(input, culture).Should().NotBeNull();
}
public class Is_not_valid_for
{
[TestCase("H")]
[TestCase("I")]
public void H_and_lower(string label)
=> EnergyLabel.TryParse(label).Should().BeNull();
[TestCase("G+")]
[TestCase("A+++++")]
[TestCase("A++++++")]
public void Plus_overload(string label)
=> EnergyLabel.TryParse(label).Should().BeNull();
[Test]
public void garbage()
=> EnergyLabel.TryParse("garbage").Should().BeNull();
}
public class Has_constant
{
[Test]
public void Empty() => EnergyLabel.Empty.Should().Be(default);
[Test]
public void A() => EnergyLabel.A().Should().Be(EnergyLabel.Parse("A"));
[Test]
public void A_plus([Range(1, 4)] int plus) => EnergyLabel.A(plus).Should().Be(EnergyLabel.Parse("A" + new string('+', plus)));
[Test]
public void B() => EnergyLabel.B.Should().Be(EnergyLabel.Parse("B"));
[Test]
public void C() => EnergyLabel.C.Should().Be(EnergyLabel.Parse("C"));
[Test]
public void D() => EnergyLabel.D.Should().Be(EnergyLabel.Parse("D"));
[Test]
public void E() => EnergyLabel.E.Should().Be(EnergyLabel.Parse("E"));
[Test]
public void F() => EnergyLabel.F.Should().Be(EnergyLabel.Parse("F"));
[Test]
public void G() => EnergyLabel.G.Should().Be(EnergyLabel.Parse("G"));
[Test]
public void Unknown() => EnergyLabel.Unknown.Should().Be(EnergyLabel.Parse("?"));
}
public class Is_equal_by_value
{
[Test]
public void not_equal_to_null()
=> Svo.EnergyLabel.Equals(null).Should().BeFalse();
[Test]
public void not_equal_to_other_type()
=> Svo.EnergyLabel.Equals(new object()).Should().BeFalse();
[Test]
public void not_equal_to_different_value()
=> Svo.EnergyLabel.Equals(EnergyLabel.G).Should().BeFalse();
[Test]
public void equal_to_same_value()
=> Svo.EnergyLabel.Equals(EnergyLabel.A(2)).Should().BeTrue();
[Test]
public void equal_operator_returns_true_for_same_values()
=> (Svo.EnergyLabel == EnergyLabel.A(2)).Should().BeTrue();
[Test]
public void equal_operator_returns_false_for_different_values()
=> (Svo.EnergyLabel == EnergyLabel.G).Should().BeFalse();
[Test]
public void not_equal_operator_returns_false_for_same_values()
=> (Svo.EnergyLabel != EnergyLabel.A(2)).Should().BeFalse();
[Test]
public void not_equal_operator_returns_true_for_different_values()
=> (Svo.EnergyLabel != EnergyLabel.G).Should().BeTrue();
[TestCase("", 0)]
[TestCase("A++", 665630170)]
public void hash_code_is_value_based(EnergyLabel svo, int hash)
{
using (Hash.WithoutRandomizer())
{
svo.GetHashCode().Should().Be(hash);
}
}
}
public class Can_be_parsed
{
[Test]
public void from_null_string_represents_Empty()
=> EnergyLabel.Parse(null).Should().Be(EnergyLabel.Empty);
[Test]
public void from_empty_string_represents_Empty()
=> EnergyLabel.Parse(string.Empty).Should().Be(EnergyLabel.Empty);
[Test]
public void from_question_mark_represents_Unknown()
=> EnergyLabel.Parse("?").Should().Be(EnergyLabel.Unknown);
[TestCase("en", "a++")]
public void from_string_with_different_formatting_and_cultures(CultureInfo culture, string input)
{
using (culture.Scoped())
{
EnergyLabel.Parse(input).Should().Be(Svo.EnergyLabel);
}
}
[Test]
public void from_valid_input_only_otherwise_throws_on_Parse()
{
using (TestCultures.en_GB.Scoped())
{
Func<EnergyLabel> parse = () => EnergyLabel.Parse("invalid input");
parse.Should().Throw<FormatException>()
.WithMessage("Not a valid energy label");
}
}
[Test]
public void from_valid_input_only_otherwise_return_false_on_TryParse()
=> (EnergyLabel.TryParse("invalid input", out _)).Should().BeFalse();
[Test]
public void from_invalid_as_null_with_TryParse()
=> EnergyLabel.TryParse("invalid input").Should().BeNull();
[Test]
public void with_TryParse_returns_SVO()
=> EnergyLabel.TryParse("A++").Should().Be(Svo.EnergyLabel);
}
public class Has_custom_formatting
{
[TestCase("G")]
[TestCase("F")]
[TestCase("E")]
[TestCase("D")]
[TestCase("C")]
[TestCase("B")]
[TestCase("A")]
[TestCase("A+")]
[TestCase("A++")]
[TestCase("A+++")]
[TestCase("A++++")]
public void _default(string label)
=> EnergyLabel.Parse(label).ToString().Should().Be(label);
[Test]
public void with_null_pattern_equal_to_default()
{
using (TestCultures.en_GB.Scoped())
{
Svo.EnergyLabel.ToString().Should().Be(Svo.EnergyLabel.ToString(default(string)));
}
}
[Test]
public void with_string_empty_pattern_equal_to_default()
{
using (TestCultures.en_GB.Scoped())
{
Svo.EnergyLabel.ToString().Should().Be(Svo.EnergyLabel.ToString(string.Empty));
}
}
[Test]
public void default_value_is_represented_as_string_empty()
=> default(EnergyLabel).ToString().Should().BeEmpty();
[Test]
public void unknown_value_is_represented_as_unknown()
=> EnergyLabel.Unknown.ToString().Should().Be("?");
[Test]
public void with_empty_format_provider()
{
using (TestCultures.es_EC.Scoped())
{
Svo.EnergyLabel.ToString(FormatProvider.Empty).Should().Be("A++");
}
}
[Test]
public void custom_format_provider_is_applied()
{
var formatted = Svo.EnergyLabel.ToString("SomeFormat", FormatProvider.CustomFormatter);
formatted.Should().Be("Unit Test Formatter, value: 'A++', format: 'SomeFormat'");
}
[TestCase(null, "A++", "A++")]
[TestCase("U", "B", "B")]
[TestCase("l", "G", "g")]
[TestCase("l", "A++", "a++")]
public void format_dependent(string format, EnergyLabel svo, string formatted)
=> svo.ToString(format).Should().Be(formatted);
#if NET6_0_OR_GREATER
public class Span_formattable
{
[Test]
public void Skips_custom_formatters()
{
Span<char> span = stackalloc char[128];
Svo.EnergyLabel.TryFormat(span, out int charsWritten, default, FormatProvider.CustomFormatter).Should().BeFalse();
charsWritten.Should().Be(0);
}
[Test]
public void formats_empty() => $"{EnergyLabel.Empty}".Should().BeEmpty();
[Test]
public void formats_unknown() => $"{EnergyLabel.Unknown}".Should().Be("?");
[Test]
public void formats_known() => $"{Svo.EnergyLabel:l}".Should().Be("a++");
[Test]
public void Skips_insufficient_span_sizes()
{
Span<char> span = stackalloc char[2];
Svo.EnergyLabel.TryFormat(span, out int charsWritten, default, TestCultures.nl_NL).Should().BeFalse();
charsWritten.Should().Be(0);
}
}
#endif
}
public class Is_comparable
{
[Test]
public void to_null_is_1() => Svo.EnergyLabel.CompareTo(Nil.Object).Should().Be(1);
[Test]
public void to_EnergyLabel_as_object()
{
object obj = Svo.EnergyLabel;
Svo.EnergyLabel.CompareTo(obj).Should().Be(0);
}
[Test]
public void to_EnergyLabel_only()
=> new object().Invoking(Svo.EnergyLabel.CompareTo).Should().Throw<ArgumentException>();
[Test]
public void can_be_sorted_using_compare()
{
var sorted = new[]
{
default,
default,
EnergyLabel.F,
EnergyLabel.C,
EnergyLabel.A(),
EnergyLabel.Unknown,
};
var list = new List<EnergyLabel> { sorted[3], sorted[4], sorted[5], sorted[2], sorted[0], sorted[1] };
list.Sort();
list.Should().BeEquivalentTo(sorted);
}
[Test]
public void by_operators_for_different_values()
{
var smaller = EnergyLabel.B;
var bigger = EnergyLabel.A(2);
(smaller < bigger).Should().BeTrue();
(smaller <= bigger).Should().BeTrue();
(smaller > bigger).Should().BeFalse();
(smaller >= bigger).Should().BeFalse();
}
[Test]
public void by_operators_for_equal_values()
{
var left = EnergyLabel.A(2);
var right = EnergyLabel.A(2);
(left < right).Should().BeFalse();
(left <= right).Should().BeTrue();
(left > right).Should().BeFalse();
(left >= right).Should().BeTrue();
}
[TestCase("?", "A++")]
[TestCase("?", "")]
[TestCase("A++", "?")]
[TestCase("", "?")]
[TestCase("?", "?")]
public void by_operators_unknown_always_false(EnergyLabel l, EnergyLabel r)
{
(l < r).Should().BeFalse();
(l <= r).Should().BeFalse();
(l > r).Should().BeFalse();
(l >= r).Should().BeFalse();
}
}
public class Casts
{
[Test]
public void explicitly_from_string()
{
var casted = (EnergyLabel)"A++";
casted.Should().Be(Svo.EnergyLabel);
}
[Test]
public void explicitly_to_string()
{
var casted = (string)Svo.EnergyLabel;
casted.Should().Be("A++");
}
}
public class Supports_type_conversion
{
[Test]
public void via_TypeConverter_registered_with_attribute()
=> typeof(EnergyLabel).Should().HaveTypeConverterDefined();
[Test]
public void from_null_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.FromNull<string>().To<EnergyLabel>().Should().Be(EnergyLabel.Empty);
}
}
[Test]
public void from_empty_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.From(string.Empty).To<EnergyLabel>().Should().Be(EnergyLabel.Empty);
}
}
[Test]
public void from_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.From("A++").To<EnergyLabel>().Should().Be(Svo.EnergyLabel);
}
}
[Test]
public void to_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.ToString().From(Svo.EnergyLabel).Should().Be("A++");
}
}
}
public class Supports_JSON_serialization
{
#if NET6_0_OR_GREATER
[TestCase("?", "?")]
[TestCase("C", "C")]
[TestCase("A++", "A++")]
public void System_Text_JSON_deserialization(object json, EnergyLabel svo)
=> JsonTester.Read_System_Text_JSON<EnergyLabel>(json).Should().Be(svo);
[TestCase("?", "?")]
[TestCase("C", "C")]
[TestCase("A++", "A++")]
public void System_Text_JSON_serialization(EnergyLabel svo, object json)
=> JsonTester.Write_System_Text_JSON(svo).Should().Be(json);
#endif
[TestCase("?", "unknown")]
[TestCase("C", "C")]
[TestCase("A++", "A++")]
public void convention_based_deserialization(EnergyLabel svo, object json)
=> JsonTester.Read<EnergyLabel>(json).Should().Be(svo);
[TestCase(null, "")]
[TestCase("C", "C")]
[TestCase("A++", "A++")]
public void convention_based_serialization(object json, EnergyLabel svo)
=> JsonTester.Write(svo).Should().Be(json);
[TestCase("Invalid input", typeof(FormatException))]
[TestCase("2017-06-11", typeof(FormatException))]
[TestCase(5L, typeof(InvalidOperationException))]
public void throws_for_invalid_json(object json, Type exceptionType)
{
Func<EnergyLabel> read = () => JsonTester.Read<EnergyLabel>(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.EnergyLabel);
xml.Should().Be("A++");
}
[Test]
public void using_XmlSerializer_to_deserialize()
{
var svo = Deserialize.Xml<EnergyLabel>("A++");
svo.Should().Be(Svo.EnergyLabel);
}
[Test]
public void using_DataContractSerializer()
{
var round_tripped = SerializeDeserialize.DataContract(Svo.EnergyLabel);
Svo.EnergyLabel.Should().Be(round_tripped);
}
[Test]
public void as_part_of_a_structure()
{
var structure = XmlStructure.New(Svo.EnergyLabel);
var round_tripped = SerializeDeserialize.Xml(structure);
structure.Should().Be(round_tripped);
}
[Test]
public void has_no_custom_XML_schema()
{
IXmlSerializable obj = Svo.EnergyLabel;
obj.GetSchema().Should().BeNull();
}
}
public class Is_Open_API_data_type
{
[Test]
public void with_info()
=> OpenApiDataType.FromType(typeof(EnergyLabel))
.Should().Be(new OpenApiDataType(
dataType: typeof(EnergyLabel),
description: "EU energy label",
example: "A++",
type: "string",
format: "energy-label",
pattern: @"[A-H]|A\+{1,4}",
nullable: true));
[TestCase("G")]
[TestCase("B")]
[TestCase("A")]
[TestCase("A+")]
[TestCase("A++++")]
public void pattern_matches(string input)
=> OpenApiDataType.FromType(typeof(EnergyLabel))!.Matches(input).Should().BeTrue();
}
#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.EnergyLabel);
round_tripped.Should().Be(Svo.EnergyLabel);
}
[Test]
public void storing_int_in_SerializationInfo()
{
var info = Serialize.GetInfo(Svo.EnergyLabel);
info.GetInt32("Value").Should().Be(9);
}
}
#endif
public class Debugger
{
[TestCase("{empty}", "")]
[TestCase("{unknown}", "?")]
[TestCase("A++", "A++")]
public void has_custom_display(object display, EnergyLabel svo)
=> svo.Should().HaveDebuggerDisplay(display);
}