/
Amount_specs.cs
137 lines (116 loc) · 4.07 KB
/
Amount_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
namespace Financial.Amount_specs;
public class Min_and_max
{
[TestCase(17, 17)]
[TestCase(17, 18)]
[TestCase(18, 17)]
public void Min_of_two_returns_minimum(Amount a1, Amount a2)
=> Amount.Min(a1, a2).Should().Be(17.Amount());
[Test]
public void Min_of_collection_returns_minimum()
=> Amount.Min(700.Amount(), 17.Amount(), 48.Amount()).Should().Be(17.Amount());
[TestCase(17, 17)]
[TestCase(17, 16)]
[TestCase(16, 17)]
public void Max_of_two_returns_minimum(Amount a1, Amount a2)
=> Amount.Max(a1, a2).Should().Be(17.Amount());
[Test]
public void Max_of_collection_returns_maximum()
=> Amount.Max(7.Amount(), 17.Amount(), -48.Amount()).Should().Be(17.Amount());
}
public class Is_comparable
{
[Test]
public void to_null_is_1() => Svo.Amount.CompareTo(Nil.Object).Should().Be(1);
}
public class Supports_type_conversion
{
[Test]
public void via_TypeConverter_registered_with_attribute()
=> typeof(Amount).Should().HaveTypeConverterDefined();
[Test]
public void from_null_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.FromNull<string>().To<Amount>().Should().Be(Amount.Zero);
}
}
[Test]
public void from_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.From("42.17").To<Amount>().Should().Be(Svo.Amount);
}
}
[Test]
public void to_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.To<string>().From(Svo.Amount).Should().Be("42.17");
}
}
[Test]
public void from_decimal()
=> Converting.From(42.17m).To<Amount>().Should().Be(Svo.Amount);
[Test]
public void from_double()
=> Converting.From(42.17).To<Amount>().Should().Be(Svo.Amount);
[Test]
public void to_decimal()
=> Converting.To<decimal>().From(Svo.Amount).Should().Be(42.17m);
[Test]
public void to_double()
=> Converting.To<double>().From(Svo.Amount).Should().Be(42.17);
public class Supports_JSON_serialization
{
#if NET6_0_OR_GREATER
[TestCase("1234.56", 1234.56)]
[TestCase(1234.56, 1234.56)]
[TestCase(1234L, 1234.00)]
public void System_Text_JSON_deserialization(object json, Amount svo)
=> JsonTester.Read_System_Text_JSON<Amount>(json).Should().Be(svo);
[Test]
public void System_Text_JSON_deserialization_min_value()
{
var amount = System.Text.Json.JsonSerializer.Deserialize<Amount>("-7.922816251426434E+28");
amount.Should().Be(Amount.MinValue);
}
[Test]
public void System_Text_JSON_deserialization_max_value()
{
var amount = System.Text.Json.JsonSerializer.Deserialize<Amount>("7.922816251426434E+28");
amount.Should().Be(Amount.MaxValue);
}
[TestCase(1234.56, 1234.56)]
public void System_Text_JSON_serialization(Amount svo, object json)
=> JsonTester.Write_System_Text_JSON(svo).Should().Be(json);
#endif
[TestCase("1234.56", 1234.56)]
[TestCase(1234.56, 1234.56)]
[TestCase(1234L, 1234.00)]
public void convention_based_deserialization(object json, Amount svo)
=> JsonTester.Read<Amount>(json).Should().Be(svo);
[TestCase(1234.56, 1234.56)]
public void convention_based_serialization(Amount svo, object json)
=> JsonTester.Write(svo).Should().Be(json);
[TestCase("Invalid input", typeof(FormatException))]
[TestCase("2017-06-11", typeof(FormatException))]
public void throws_for_invalid_json(object json, Type exceptionType)
=> json
.Invoking(JsonTester.Read<Amount>)
.Should().Throw<Exception>()
.And.Should().BeOfType(exceptionType);
}
public class Has_operators
{
[Test]
public void to_divide_amount_by_amount_as_decimal()
{
var ratio = Svo.Amount / 2.Amount();
ratio.Should().Be(21.085m);
}
}
}