summaryrefslogtreecommitdiff
path: root/src/SMAPI.Tests/Utilities/SDateTests.cs
blob: b9c3d202a6e9c71b31283f37df8041fef7946f77 (plain)
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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.RegularExpressions;
using NUnit.Framework;
using StardewModdingAPI.Utilities;
using StardewValley;

namespace SMAPI.Tests.Utilities
{
    /// <summary>Unit tests for <see cref="SDate"/>.</summary>
    [TestFixture]
    internal class SDateTests
    {
        /*********
        ** Fields
        *********/
        /// <summary>The valid seasons.</summary>
        private static readonly string[] ValidSeasons = { "spring", "summer", "fall", "winter" };

        /// <summary>Sample user inputs for season names.</summary>
        private static readonly string[] SampleSeasonValues = SDateTests.ValidSeasons.Concat(new[] { "  WIntEr  " }).ToArray();

        /// <summary>All valid days of a month.</summary>
        private static readonly int[] ValidDays = Enumerable.Range(1, 28).ToArray();

        /// <summary>Sample relative dates for test cases.</summary>
        private static class Dates
        {
            /// <summary>The base date to which other dates are relative.</summary>
            public const string Now = "02 summer Y2";

            /// <summary>The day before <see cref="Now"/>.</summary>
            public const string PrevDay = "01 summer Y2";

            /// <summary>The month before <see cref="Now"/>.</summary>
            public const string PrevMonth = "02 spring Y2";

            /// <summary>The year before <see cref="Now"/>.</summary>
            public const string PrevYear = "02 summer Y1";

            /// <summary>The day after <see cref="Now"/>.</summary>
            public const string NextDay = "03 summer Y2";

            /// <summary>The month after <see cref="Now"/>.</summary>
            public const string NextMonth = "02 fall Y2";

            /// <summary>The year after <see cref="Now"/>.</summary>
            public const string NextYear = "02 summer Y3";
        }


        /*********
        ** Unit tests
        *********/
        /****
        ** Constructor
        ****/
        [Test(Description = "Assert that the constructor sets the expected values for all valid dates.")]
        public void Constructor_SetsExpectedValues([ValueSource(nameof(SDateTests.SampleSeasonValues))] string season, [ValueSource(nameof(SDateTests.ValidDays))] int day, [Values(1, 2, 100)] int year)
        {
            // act
            SDate date = new(day, season, year);

            // assert
            Assert.AreEqual(day, date.Day);
            Assert.AreEqual(season.Trim().ToLowerInvariant(), date.Season);
            Assert.AreEqual(year, date.Year);
        }

        [Test(Description = "Assert that the constructor throws an exception if the values are invalid.")]
        [TestCase(01, "springs", 1)] // invalid season name
        [TestCase(-1, "spring", 1)] // day < 0
        [TestCase(0, "spring", 1)] // day zero
        [TestCase(0, "spring", 2)] // day zero
        [TestCase(29, "spring", 1)] // day > 28
        [TestCase(01, "spring", -1)] // year < 1
        [TestCase(01, "spring", 0)] // year < 1
        [SuppressMessage("ReSharper", "AssignmentIsFullyDiscarded", Justification = "Deliberate for unit test.")]
        public void Constructor_RejectsInvalidValues(int day, string season, int year)
        {
            // act & assert
            Assert.Throws<ArgumentException>(() => _ = new SDate(day, season, year), "Constructing the invalid date didn't throw the expected exception.");
        }

        /****
        ** FromDaysSinceStart
        ****/
        [Test(Description = "Assert that FromDaysSinceStart returns the expected date.")]
        [TestCase(1, ExpectedResult = "01 spring Y1")]
        [TestCase(2, ExpectedResult = "02 spring Y1")]
        [TestCase(28, ExpectedResult = "28 spring Y1")]
        [TestCase(29, ExpectedResult = "01 summer Y1")]
        [TestCase(141, ExpectedResult = "01 summer Y2")]
        public string FromDaysSinceStart(int daysSinceStart)
        {
            // act
            return SDate.FromDaysSinceStart(daysSinceStart).ToString();
        }

        [Test(Description = "Assert that FromDaysSinceStart throws an exception if the number of days is invalid.")]
        [TestCase(-1)] // day < 0
        [TestCase(0)] // day == 0
        [SuppressMessage("ReSharper", "AssignmentIsFullyDiscarded", Justification = "Deliberate for unit test.")]
        public void FromDaysSinceStart_RejectsInvalidValues(int daysSinceStart)
        {
            // act & assert
            Assert.Throws<ArgumentException>(() => _ = SDate.FromDaysSinceStart(daysSinceStart), "Passing the invalid number of days didn't throw the expected exception.");
        }

        /****
        ** From
        ****/
        [Test(Description = "Assert that SDate.From constructs the correct instance for a given date.")]
        [TestCase(0, ExpectedResult = "01 spring Y1")]
        [TestCase(1, ExpectedResult = "02 spring Y1")]
        [TestCase(27, ExpectedResult = "28 spring Y1")]
        [TestCase(28, ExpectedResult = "01 summer Y1")]
        [TestCase(140, ExpectedResult = "01 summer Y2")]
        public string From_WorldDate(int totalDays)
        {
            return SDate.From(new WorldDate { TotalDays = totalDays }).ToString();
        }


        /****
        ** SeasonIndex
        ****/
        [Test(Description = "Assert the numeric index of the season.")]
        [TestCase("01 spring Y1", ExpectedResult = 0)]
        [TestCase("02 summer Y1", ExpectedResult = 1)]
        [TestCase("28 fall Y1", ExpectedResult = 2)]
        [TestCase("01 winter Y1", ExpectedResult = 3)]
        [TestCase("01 winter Y2", ExpectedResult = 3)]
        public int SeasonIndex(string dateStr)
        {
            // act
            return this.GetDate(dateStr).SeasonIndex;
        }


        /****
        ** DayOfWeek
        ****/
        [Test(Description = "Assert the day of week.")]
        [TestCase("01 spring Y1", ExpectedResult = System.DayOfWeek.Monday)]
        [TestCase("02 spring Y2", ExpectedResult = System.DayOfWeek.Tuesday)]
        [TestCase("03 spring Y3", ExpectedResult = System.DayOfWeek.Wednesday)]
        [TestCase("04 spring Y4", ExpectedResult = System.DayOfWeek.Thursday)]
        [TestCase("05 spring Y5", ExpectedResult = System.DayOfWeek.Friday)]
        [TestCase("06 spring Y6", ExpectedResult = System.DayOfWeek.Saturday)]
        [TestCase("07 spring Y7", ExpectedResult = System.DayOfWeek.Sunday)]
        [TestCase("08 summer Y8", ExpectedResult = System.DayOfWeek.Monday)]
        [TestCase("09 summer Y9", ExpectedResult = System.DayOfWeek.Tuesday)]
        [TestCase("10 summer Y10", ExpectedResult = System.DayOfWeek.Wednesday)]
        [TestCase("11 summer Y11", ExpectedResult = System.DayOfWeek.Thursday)]
        [TestCase("12 summer Y12", ExpectedResult = System.DayOfWeek.Friday)]
        [TestCase("13 summer Y13", ExpectedResult = System.DayOfWeek.Saturday)]
        [TestCase("14 summer Y14", ExpectedResult = System.DayOfWeek.Sunday)]
        [TestCase("15 fall Y15", ExpectedResult = System.DayOfWeek.Monday)]
        [TestCase("16 fall Y16", ExpectedResult = System.DayOfWeek.Tuesday)]
        [TestCase("17 fall Y17", ExpectedResult = System.DayOfWeek.Wednesday)]
        [TestCase("18 fall Y18", ExpectedResult = System.DayOfWeek.Thursday)]
        [TestCase("19 fall Y19", ExpectedResult = System.DayOfWeek.Friday)]
        [TestCase("20 fall Y20", ExpectedResult = System.DayOfWeek.Saturday)]
        [TestCase("21 fall Y21", ExpectedResult = System.DayOfWeek.Sunday)]
        [TestCase("22 winter Y22", ExpectedResult = System.DayOfWeek.Monday)]
        [TestCase("23 winter Y23", ExpectedResult = System.DayOfWeek.Tuesday)]
        [TestCase("24 winter Y24", ExpectedResult = System.DayOfWeek.Wednesday)]
        [TestCase("25 winter Y25", ExpectedResult = System.DayOfWeek.Thursday)]
        [TestCase("26 winter Y26", ExpectedResult = System.DayOfWeek.Friday)]
        [TestCase("27 winter Y27", ExpectedResult = System.DayOfWeek.Saturday)]
        [TestCase("28 winter Y28" + "", ExpectedResult = System.DayOfWeek.Sunday)]
        public DayOfWeek DayOfWeek(string dateStr)
        {
            // act
            return this.GetDate(dateStr).DayOfWeek;
        }


        /****
        ** DaysSinceStart
        ****/
        [Test(Description = "Assert the number of days since 01 spring Y1 (inclusive).")]
        [TestCase("01 spring Y1", ExpectedResult = 1)]
        [TestCase("02 spring Y1", ExpectedResult = 2)]
        [TestCase("28 spring Y1", ExpectedResult = 28)]
        [TestCase("01 summer Y1", ExpectedResult = 29)]
        [TestCase("01 summer Y2", ExpectedResult = 141)]
        public int DaysSinceStart(string dateStr)
        {
            // act
            return this.GetDate(dateStr).DaysSinceStart;
        }


        /****
        ** ToString
        ****/
        [Test(Description = "Assert that ToString returns the expected string.")]
        [TestCase("14 spring Y1", ExpectedResult = "14 spring Y1")]
        [TestCase("01 summer Y16", ExpectedResult = "01 summer Y16")]
        [TestCase("28 fall Y10", ExpectedResult = "28 fall Y10")]
        [TestCase("01 winter Y1", ExpectedResult = "01 winter Y1")]
        public string ToString(string dateStr)
        {
            return this.GetDate(dateStr).ToString();
        }


        /****
        ** AddDays
        ****/
        [Test(Description = "Assert that AddDays returns the expected date.")]
        [TestCase("01 spring Y1", 15, ExpectedResult = "16 spring Y1")] // day transition
        [TestCase("01 spring Y1", 28, ExpectedResult = "01 summer Y1")] // season transition
        [TestCase("01 spring Y1", 28 * 4, ExpectedResult = "01 spring Y2")] // year transition
        [TestCase("01 spring Y1", 28 * 7 + 17, ExpectedResult = "18 winter Y2")] // year transition
        [TestCase("15 spring Y1", -14, ExpectedResult = "01 spring Y1")] // negative day transition
        [TestCase("15 summer Y1", -28, ExpectedResult = "15 spring Y1")] // negative season transition
        [TestCase("15 summer Y2", -28 * 4, ExpectedResult = "15 summer Y1")] // negative year transition
        [TestCase("01 spring Y3", -(28 * 7 + 17), ExpectedResult = "12 spring Y1")] // negative year transition
        [TestCase("06 fall Y2", 50, ExpectedResult = "28 winter Y2")] // test for zero-index errors
        [TestCase("06 fall Y2", 51, ExpectedResult = "01 spring Y3")] // test for zero-index errors
        public string AddDays(string dateStr, int addDays)
        {
            return this.GetDate(dateStr).AddDays(addDays).ToString();
        }

        [Test(Description = "Assert that AddDays throws an exception if the number of days is invalid.")]
        [TestCase("01 spring Y1", -1)]
        [TestCase("01 summer Y1", -29)]
        [TestCase("01 spring Y2", -113)]
        [SuppressMessage("ReSharper", "AssignmentIsFullyDiscarded", Justification = "Deliberate for unit test.")]
        public void AddDays_RejectsInvalidValues(string dateStr, int addDays)
        {
            // act & assert
            Assert.Throws<ArithmeticException>(() => _ = this.GetDate(dateStr).AddDays(addDays), "Passing the invalid number of days didn't throw the expected exception.");
        }


        /****
        ** GetHashCode
        ****/
        [Test(Description = "Assert that GetHashCode returns a unique ordered value for every date.")]
        public void GetHashCode_ReturnsUniqueOrderedValue()
        {
            IDictionary<int, SDate> hashes = new Dictionary<int, SDate>();
            int lastHash = int.MinValue;
            for (int year = 1; year <= 4; year++)
            {
                foreach (string season in SDateTests.ValidSeasons)
                {
                    foreach (int day in SDateTests.ValidDays)
                    {
                        SDate date = new(day, season, year);
                        int hash = date.GetHashCode();
                        if (hashes.TryGetValue(hash, out SDate? otherDate))
                            Assert.Fail($"Received identical hash code {hash} for dates {otherDate} and {date}.");
                        if (hash < lastHash)
                            Assert.Fail($"Received smaller hash code for date {date} ({hash}) relative to {hashes[lastHash]} ({lastHash}).");

                        lastHash = hash;
                        hashes[hash] = date;
                    }
                }
            }
        }


        /****
        ** ToWorldDate
        ****/
        [Test(Description = "Assert that the WorldDate operator returns the corresponding WorldDate.")]
        [TestCase("01 spring Y1", ExpectedResult = 0)]
        [TestCase("02 spring Y1", ExpectedResult = 1)]
        [TestCase("28 spring Y1", ExpectedResult = 27)]
        [TestCase("01 summer Y1", ExpectedResult = 28)]
        [TestCase("01 summer Y2", ExpectedResult = 140)]
        public int ToWorldDate(string dateStr)
        {
            return this.GetDate(dateStr).ToWorldDate().TotalDays;
        }


        /****
        ** Operators
        ****/
        [Test(Description = "Assert that the == operator returns the expected values. We only need a few test cases, since it's based on GetHashCode which is tested more thoroughly.")]
        [TestCase(Dates.Now, null, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.PrevDay, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.PrevMonth, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.PrevYear, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.Now, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.NextDay, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.NextYear, ExpectedResult = false)]
        public bool Operators_Equals(string? now, string other)
        {
            return this.GetDate(now) == this.GetDate(other);
        }

        [Test(Description = "Assert that the != operator returns the expected values. We only need a few test cases, since it's based on GetHashCode which is tested more thoroughly.")]
        [TestCase(Dates.Now, null, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.PrevDay, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.PrevMonth, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.PrevYear, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.Now, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.NextDay, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.NextYear, ExpectedResult = true)]
        public bool Operators_NotEquals(string? now, string other)
        {
            return this.GetDate(now) != this.GetDate(other);
        }

        [Test(Description = "Assert that the < operator returns the expected values. We only need a few test cases, since it's based on GetHashCode which is tested more thoroughly.")]
        [TestCase(Dates.Now, null, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.PrevDay, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.PrevMonth, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.PrevYear, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.Now, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.NextDay, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.NextYear, ExpectedResult = true)]
        public bool Operators_LessThan(string? now, string other)
        {
            return this.GetDate(now) < this.GetDate(other);
        }

        [Test(Description = "Assert that the <= operator returns the expected values. We only need a few test cases, since it's based on GetHashCode which is tested more thoroughly.")]
        [TestCase(Dates.Now, null, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.PrevDay, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.PrevMonth, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.PrevYear, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.Now, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.NextDay, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.NextYear, ExpectedResult = true)]
        public bool Operators_LessThanOrEqual(string? now, string other)
        {
            return this.GetDate(now) <= this.GetDate(other);
        }

        [Test(Description = "Assert that the > operator returns the expected values. We only need a few test cases, since it's based on GetHashCode which is tested more thoroughly.")]
        [TestCase(Dates.Now, null, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.PrevDay, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.PrevMonth, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.PrevYear, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.Now, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.NextDay, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.NextYear, ExpectedResult = false)]
        public bool Operators_MoreThan(string? now, string other)
        {
            return this.GetDate(now) > this.GetDate(other);
        }

        [Test(Description = "Assert that the > operator returns the expected values. We only need a few test cases, since it's based on GetHashCode which is tested more thoroughly.")]
        [TestCase(Dates.Now, null, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.PrevDay, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.PrevMonth, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.PrevYear, ExpectedResult = true)]
        [TestCase(Dates.Now, Dates.Now, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.NextDay, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.NextMonth, ExpectedResult = false)]
        [TestCase(Dates.Now, Dates.NextYear, ExpectedResult = false)]
        public bool Operators_MoreThanOrEqual(string? now, string other)
        {
            return this.GetDate(now) > this.GetDate(other);
        }


        /*********
        ** Private methods
        *********/
        /// <summary>Convert a string date into a game date, to make unit tests easier to read.</summary>
        /// <param name="dateStr">The date string like "dd MMMM yy".</param>
        [return: NotNullIfNotNull("dateStr")]
        private SDate? GetDate(string? dateStr)
        {
            if (dateStr == null)
                return null;

            void Fail(string reason) => throw new AssertionException($"Couldn't parse date '{dateStr}' because {reason}.");

            // parse
            Match match = Regex.Match(dateStr, @"^(?<day>\d+) (?<season>\w+) Y(?<year>\d+)$");
            if (!match.Success)
                Fail("it doesn't match expected pattern (should be like 28 spring Y1)");

            // extract parts
            string season = match.Groups["season"].Value;
            if (!int.TryParse(match.Groups["day"].Value, out int day))
                Fail($"'{match.Groups["day"].Value}' couldn't be parsed as a day.");
            if (!int.TryParse(match.Groups["year"].Value, out int year))
                Fail($"'{match.Groups["year"].Value}' couldn't be parsed as a year.");

            // build date
            return new SDate(day, season, year);
        }
    }
}