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

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

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


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

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

        [Test(Description = "Assert that the constructor throws an exception if the values are invalid.")]
        [TestCase(01, "Spring", 1)] // seasons are case-sensitive
        [TestCase(01, "springs", 1)] // invalid season name
        [TestCase(-1, "spring", 1)] // day < 0
        [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.");
        }

        /****
        ** 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.ParseDate(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
        public string AddDays(string dateStr, int addDays)
        {
            return this.ParseDate(dateStr).AddDays(addDays).ToString();
        }


        /*********
        ** 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>
        private SDate ParseDate(string dateStr)
        {
            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);
        }
    }
}