diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/SMAPI.Tests/Utilities/SDateTests.cs | 117 | ||||
-rw-r--r-- | src/SMAPI/Utilities/SDate.cs | 36 |
2 files changed, 73 insertions, 80 deletions
diff --git a/src/SMAPI.Tests/Utilities/SDateTests.cs b/src/SMAPI.Tests/Utilities/SDateTests.cs index 3fca9b29..0461952e 100644 --- a/src/SMAPI.Tests/Utilities/SDateTests.cs +++ b/src/SMAPI.Tests/Utilities/SDateTests.cs @@ -6,7 +6,6 @@ using System.Text.RegularExpressions; using NUnit.Framework; using StardewModdingAPI.Utilities; using StardewValley; -using LC = StardewValley.LocalizedContentManager.LanguageCode; namespace SMAPI.Tests.Utilities { @@ -84,6 +83,46 @@ namespace SMAPI.Tests.Utilities } /**** + ** 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.")] @@ -98,6 +137,7 @@ namespace SMAPI.Tests.Utilities return this.GetDate(dateStr).SeasonIndex; } + /**** ** DayOfWeek ****/ @@ -136,6 +176,7 @@ namespace SMAPI.Tests.Utilities return this.GetDate(dateStr).DayOfWeek; } + /**** ** DaysSinceStart ****/ @@ -151,6 +192,7 @@ namespace SMAPI.Tests.Utilities return this.GetDate(dateStr).DaysSinceStart; } + /**** ** ToString ****/ @@ -164,57 +206,6 @@ namespace SMAPI.Tests.Utilities return this.GetDate(dateStr).ToString(); } - /**** - ** ToLocaleString - ****/ - // TODO: Provide an appropriate XNA/MonoGame context to run this in, or else remove the test. - // [Test(Description = "Assert that ToLocaleString returns the expected string in various locales.")] - // [TestCase("14 spring Y1", LC.en, ExpectedResult = "Day 14 of Spring, Year 1")] - // [TestCase("01 summer Y16", LC.en, ExpectedResult = "Day 1 of Summer, Year 16")] - // [TestCase("28 fall Y10", LC.en, ExpectedResult = "Day 28 of Fall, Year 10")] - // [TestCase("01 winter Y1", LC.en, ExpectedResult = "Day 1 of Winter, Year 1")] - // [TestCase("14 spring Y1", LC.es, ExpectedResult = "Día 14 de primavera, año 1")] - // [TestCase("01 summer Y16", LC.es, ExpectedResult = "Día 1 de verano, año 16")] - // [TestCase("28 fall Y10", LC.es, ExpectedResult = "Día 28 de otoño, año 10")] - // [TestCase("01 winter Y1", LC.es, ExpectedResult = "Día 1 de invierno, año 1")] - // public string ToLocaleString(string dateStr, LC langCode) - // { - // LC oldCode = LocalizedContentManager.CurrentLanguageCode; - // try - // { - // LocalizedContentManager.CurrentLanguageCode = langCode; - // return this.GetDate(dateStr).ToLocaleString(); - // } - // finally - // { - // LocalizedContentManager.CurrentLanguageCode = oldCode; - // } - // } - - /**** - ** 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."); - } /**** ** AddDays @@ -246,6 +237,7 @@ namespace SMAPI.Tests.Utilities Assert.Throws<ArithmeticException>(() => _ = this.GetDate(dateStr).AddDays(addDays), "Passing the invalid number of days didn't throw the expected exception."); } + /**** ** GetHashCode ****/ @@ -274,28 +266,25 @@ namespace SMAPI.Tests.Utilities } } - [Test(Description = "Assert that the SDate operator for WorldDates returns the corresponding SDate.")] - [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 Operators_SDate_WorldDate(int totalDays) - { - return ((SDate)new WorldDate { TotalDays = totalDays }).ToString(); - } + /**** + ** 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 Operators_WorldDate(string dateStr) + public int ToWorldDate(string dateStr) { - return ((WorldDate)this.GetDate(dateStr)).TotalDays; + 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)] diff --git a/src/SMAPI/Utilities/SDate.cs b/src/SMAPI/Utilities/SDate.cs index 8cb55891..36907714 100644 --- a/src/SMAPI/Utilities/SDate.cs +++ b/src/SMAPI/Utilities/SDate.cs @@ -29,7 +29,8 @@ namespace StardewModdingAPI.Utilities /// <summary>The season name.</summary> public string Season { get; } - /// <summary>The season index.</summary> + /// <summary>The index of the season (where 0 is spring, 1 is summer, 2 is fall, and 3 is winter).</summary> + /// <remarks>This is used in some game calculations (e.g. seasonal game sprites) and methods (e.g. <see cref="Utility.getSeasonNameFromNumber"/>).</remarks> public int SeasonIndex { get; } /// <summary>The year.</summary> @@ -66,7 +67,7 @@ namespace StardewModdingAPI.Utilities return new SDate(Game1.dayOfMonth, Game1.currentSeason, Game1.year, allowDayZero: true); } - /// <summary>Get the date falling the given number of days after 0 spring Y1.</summary> + /// <summary>Get a date from the number of days after 0 spring Y1.</summary> /// <param name="daysSinceStart">The number of days since 0 spring Y1.</param> public static SDate FromDaysSinceStart(int daysSinceStart) { @@ -80,6 +81,16 @@ namespace StardewModdingAPI.Utilities } } + /// <summary>Get a date from a game date instance.</summary> + /// <param name="date">The world date.</param> + public static SDate From(WorldDate date) + { + if (date == null) + return null; + + return new SDate(date.DayOfMonth, date.Season, date.Year, allowDayZero: true); + } + /// <summary>Get a new date with the given number of days added.</summary> /// <param name="offset">The number of days to add.</param> /// <returns>Returns the resulting date.</returns> @@ -109,13 +120,19 @@ namespace StardewModdingAPI.Utilities return new SDate(day, this.Seasons[seasonIndex], year); } + /// <summary>Get a game date representation of the date.</summary> + public WorldDate ToWorldDate() + { + return new WorldDate(this.Year, this.Season, this.Day); + } + /// <summary>Get a string representation of the date. This is mainly intended for debugging or console messages.</summary> public override string ToString() { return $"{this.Day:00} {this.Season} Y{this.Year}"; } - /// <summary>Get a string representation of the date in the current game locale.</summary> + /// <summary>Get a translated string representation of the date in the current game locale.</summary> public string ToLocaleString() { return Utility.getDateStringFor(this.Day, this.SeasonIndex, this.Year); @@ -147,19 +164,6 @@ namespace StardewModdingAPI.Utilities /**** ** Operators ****/ - /// <summary>Get the SDate equivalent to the given WorldDate.</summary> - /// <param name="worldDate">A date returned from a core game property or method.</param> - public static explicit operator SDate(WorldDate worldDate) - { - return new SDate(worldDate.DayOfMonth, worldDate.Season, worldDate.Year, allowDayZero: true); - } - - /// <summary>Get the SDate as an instance of the game's WorldDate class. This is intended for passing to core game methods.</summary> - public static explicit operator WorldDate(SDate date) - { - return new WorldDate(date.Year, date.Season, date.Day); - } - /// <summary>Get whether one date is equal to another.</summary> /// <param name="date">The base date to compare.</param> /// <param name="other">The other date to compare.</param> |