-
Notifications
You must be signed in to change notification settings - Fork 14
/
Date_span_specs.cs
294 lines (259 loc) · 9.69 KB
/
Date_span_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
using FluentAssertions.Extensions;
namespace Date_span_specs;
public class Is_comparable
{
[Test]
public void to_null_is_1() => Svo.DateSpan.CompareTo(Nil.Object).Should().Be(1);
}
public class Is_valid
{
[TestCase("23Y+0M+0D", "Without starting sign")]
[TestCase("+9998Y+0M+0D", "A lot of years")]
[TestCase("-9998Y+0M+0D", "A lot of years")]
[TestCase("0Y+100000M+1D", "A lot of months")]
[TestCase("0Y-100000M+1D", "A lot of months")]
[TestCase("0Y+0M+3650000D", "A lot of days")]
[TestCase("0Y+0M-3650000D", "A lot of days")]
public void For(string str, string because)
=> DateSpan.TryParse(str).Should().NotBeNull(because);
}
public class Is_invalid
{
[Test]
public void for_empty_string() => DateSpan.TryParse(string.Empty).Should().BeNull();
[Test]
public void for_null() => DateSpan.TryParse(Nil.String).Should().BeNull();
[TestCase("+9999Y+0M+0D", "Years out of reach")]
[TestCase("-9999Y+0M+0D", "Years out of reach")]
[TestCase("0Y+0M+4650000D", "Days out of reach")]
[TestCase("0Y+0M-4650000D", "Days out of reach")]
[TestCase("Not a date span", "Garbage")]
public void For(string str, string because)
=> DateSpan.TryParse(str).Should().BeNull(because);
}
public class Supports_type_conversion
{
[Test]
public void via_TypeConverter_registered_with_attribute()
=> typeof(DateSpan).Should().HaveTypeConverterDefined();
[Test]
public void from_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.From("10Y+3M-5D").To<DateSpan>().Should().Be(Svo.DateSpan);
}
}
[Test]
public void to_string()
{
using (TestCultures.en_GB.Scoped())
{
Converting.ToString().From(Svo.DateSpan).Should().Be("10Y+3M-5D");
}
}
}
public class Supports_JSON_serialization
{
#if NET6_0_OR_GREATER
[TestCase(null, "0Y+0M+0D")]
[TestCase(0L, "0Y+0M+0D")]
[TestCase("0Y+0M+0D", "0Y+0M+0D")]
[TestCase("1Y+8M+3D", "1Y+8M+3D")]
public void System_Text_JSON_deserialization(object json, DateSpan svo)
=> JsonTester.Read_System_Text_JSON<DateSpan>(json).Should().Be(svo);
[TestCase("1Y+8M+3D", "1Y+8M+3D")]
public void System_Text_JSON_serialization(DateSpan svo, object json)
=> JsonTester.Write_System_Text_JSON(svo).Should().Be(json);
#endif
[TestCase(0L, "0Y+0M+0D")]
[TestCase("0Y+0M+0D", "0Y+0M+0D")]
[TestCase("1Y+8M+3D", "1Y+8M+3D")]
public void convention_based_deserialization(object json, DateSpan svo)
=> JsonTester.Read<DateSpan>(json).Should().Be(svo);
[TestCase("1Y+8M+3D", "1Y+8M+3D")]
public void convention_based_serialization(DateSpan svo, object json)
=> JsonTester.Write(svo).Should().Be(json);
[TestCase("Invalid input", typeof(FormatException))]
[TestCase("2017-06-11", typeof(FormatException))]
[TestCase(true, typeof(InvalidOperationException))]
public void throws_for_invalid_json(object json, Type exceptionType)
=> json
.Invoking(JsonTester.Read<DateSpan>)
.Should().Throw<Exception>()
.And.Should().BeOfType(exceptionType);
}
#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()
=> SerializeDeserialize.Binary(Svo.DateSpan).Should().Be(Svo.DateSpan);
[Test]
public void storing_string_in_SerializationInfo()
=> Serialize.GetInfo(Svo.DateSpan).GetUInt64("Value").Should().Be(532575944699UL);
}
#endif
public class Is_Open_API_data_type
{
[Test]
public void with_info()
=> OpenApiDataType.FromType(typeof(DateSpan))
.Should().Be(new OpenApiDataType(
dataType: typeof(DateSpan),
description: "Date span, specified in years, months and days.",
example: "1Y+10M+16D",
type: "string",
format: "date-span",
pattern: @"[+-]?[0-9]+Y[+-][0-9]+M[+-][0-9]+D"));
}
public class Can_be_operated
{
[Test]
public void negate_negates_values()
{
var negated = -Svo.DateSpan;
negated.Should().Be(new DateSpan(-10, -3, +5));
}
[Test]
public void plus_does_nothing()
{
var plussed = +Svo.DateSpan;
plussed.Should().Be(Svo.DateSpan);
}
}
public class Can_be_added_to
{
[Test]
public void Date_times()
{
var date = 30.January(1999).At(13, 42).AsUtc();
date.Add(DateSpan.FromMonths(1)).Should().Be(28.February(1999).At(13, 42).AsUtc());
}
}
public class Can_create
{
[Test]
public void Age_form_date_without_months()
{
using (Clock.SetTimeForCurrentContext(() => new Date(2019, 10, 10)))
{
var age = DateSpan.Age(new Date(2017, 06, 11));
age.Should().Be(new DateSpan(years: 2, months: 0, days: 121));
}
}
#if NET6_0_OR_GREATER
[Test]
public void Age_form_date_only_without_months()
{
using (Clock.SetTimeForCurrentContext(() => new Date(2019, 10, 10)))
{
var age = DateSpan.Age(new DateOnly(2017, 06, 11));
age.Should().Be(new DateSpan(years: 2, months: 0, days: 121));
}
}
[Test]
public void Age_for_reference_date()
{
var age = DateSpan.Age(new DateOnly(2017, 06, 11), new DateOnly(2023, 11, 17));
age.Should().Be(new DateSpan(years: 6, months: 0, days: 159));
}
#endif
}
public class Can_add
{
[Test]
public void two_date_spans()
{
var l = new DateSpan(12, 3, 4);
var r = new DateSpan(-2, 2, 7);
(l + r).Should().Be(new DateSpan(10, 5, 11));
}
[Test]
public void days_to_date_span()
{
var span = new DateSpan(12, 3, 4);
span.AddDays(17).Should().Be(new DateSpan(12, 3, 21));
}
[Test]
public void months_to_date_span()
{
var span = new DateSpan(12, 3, 4);
span.AddMonths(17).Should().Be(new DateSpan(12, 20, 4));
}
[Test]
public void years_to_date_span()
{
var span = new DateSpan(12, 3, 4);
span.AddYears(17).Should().Be(new DateSpan(29, 3, 4));
}
}
public class Can_subtract
{
[Test]
public void two_date_spans()
{
var l = new DateSpan(12, 3, 4);
var r = new DateSpan(-2, 2, 7);
(l - r).Should().Be(new DateSpan(14, 1, -3));
}
[Test]
public void two_dates()
=> DateSpan.Subtract(new Date(2023, 11, 17), Svo.Date)
.Should().Be(new DateSpan(years: 6, months: 5, days: 6));
[TestCase(+0, +364, "2018-06-10", "2017-06-11", DateSpanSettings.DaysOnly)]
[TestCase(+0, -364, "2017-06-11", "2018-06-10", DateSpanSettings.DaysOnly)]
[TestCase(+11, +30, "2018-06-10", "2017-06-11", DateSpanSettings.Default)]
[TestCase(+12, -01, "2018-06-10", "2017-06-11", DateSpanSettings.MixedSigns)]
[TestCase(+15, +14, "2018-06-10", "2017-02-27", DateSpanSettings.Default)]
[TestCase(+15, +11, "2018-06-10", "2017-02-27", DateSpanSettings.DaysFirst)]
[TestCase(+24, +119, "2019-10-08", "2017-06-11", DateSpanSettings.WithoutMonths)]
[TestCase(+36, +120, "2020-10-08", "2017-06-11", DateSpanSettings.WithoutMonths)]
[TestCase(+12, +331, "2019-05-08", "2017-06-11", DateSpanSettings.WithoutMonths)]
[TestCase(+24, +332, "2020-05-08", "2017-06-11", DateSpanSettings.WithoutMonths)]
[TestCase(-11, -30, "2017-06-11", "2018-06-10", DateSpanSettings.Default)]
[TestCase(-12, +01, "2017-06-11", "2018-06-10", DateSpanSettings.MixedSigns)]
public void two_dates_based_on_settings(int months, int days, Date d1, Date d2, DateSpanSettings settings)
{
var expected = new DateSpan(0, months, days);
DateSpan.Subtract(d1, d2, settings).Should().Be(expected);
}
#if NET6_0_OR_GREATER
[Test]
public void two_date_onlys()
=> DateSpan.Subtract(new DateOnly(2023, 11, 17), Svo.DateOnly)
.Should().Be(new DateSpan(years: 6, months: 5, days: 6));
[TestCase(+0, +364, "2018-06-10", "2017-06-11", DateSpanSettings.DaysOnly)]
[TestCase(+0, -364, "2017-06-11", "2018-06-10", DateSpanSettings.DaysOnly)]
[TestCase(+11, +30, "2018-06-10", "2017-06-11", DateSpanSettings.Default)]
[TestCase(+12, -01, "2018-06-10", "2017-06-11", DateSpanSettings.MixedSigns)]
[TestCase(+15, +14, "2018-06-10", "2017-02-27", DateSpanSettings.Default)]
[TestCase(+15, +11, "2018-06-10", "2017-02-27", DateSpanSettings.DaysFirst)]
[TestCase(+24, +119, "2019-10-08", "2017-06-11", DateSpanSettings.WithoutMonths)]
[TestCase(+36, +120, "2020-10-08", "2017-06-11", DateSpanSettings.WithoutMonths)]
[TestCase(+12, +331, "2019-05-08", "2017-06-11", DateSpanSettings.WithoutMonths)]
[TestCase(+24, +332, "2020-05-08", "2017-06-11", DateSpanSettings.WithoutMonths)]
[TestCase(-11, -30, "2017-06-11", "2018-06-10", DateSpanSettings.Default)]
[TestCase(-12, +01, "2017-06-11", "2018-06-10", DateSpanSettings.MixedSigns)]
public void two_date_onlys_based_on_settings(int months, int days, Date d1, Date d2, DateSpanSettings settings)
{
var expected = new DateSpan(0, months, days);
DateSpan.Subtract((DateOnly)d1, (DateOnly)d2, settings).Should().Be(expected);
}
#endif
}
public class Throws_when
{
[Test]
public void Mutatation_overflows()
=> 1.Invoking(DateSpan.MaxValue.AddDays)
.Should().Throw<OverflowException>()
.WithMessage("DateSpan overflowed because the resulting duration is too long.");
[Test]
public void Ctor_arguments_are_out_of_range()
=> int.MaxValue.Invoking(n => new DateSpan(n, n))
.Should().Throw<ArgumentOutOfRangeException>()
.WithMessage("The specified years, months and days results in an un-representable DateSpan.");
}