normalize season names in SDate constructor
This commit is contained in:
parent
6dd4a8a12b
commit
3431f486a2
|
@ -5,6 +5,9 @@
|
||||||
* For players:
|
* For players:
|
||||||
* Improved translations. Thanks to ChulkyBow (updated Ukrainian)!
|
* Improved translations. Thanks to ChulkyBow (updated Ukrainian)!
|
||||||
|
|
||||||
|
* For mod authors:
|
||||||
|
* The `SDate` constructor is no longer case-sensitive for season names.
|
||||||
|
|
||||||
* For console commands:
|
* For console commands:
|
||||||
* Fixed `player_add` with Journal Scraps and Secret Notes.
|
* Fixed `player_add` with Journal Scraps and Secret Notes.
|
||||||
|
|
||||||
|
|
|
@ -16,9 +16,12 @@ namespace SMAPI.Tests.Utilities
|
||||||
/*********
|
/*********
|
||||||
** Fields
|
** Fields
|
||||||
*********/
|
*********/
|
||||||
/// <summary>All valid seasons.</summary>
|
/// <summary>The valid seasons.</summary>
|
||||||
private static readonly string[] ValidSeasons = { "spring", "summer", "fall", "winter" };
|
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>
|
/// <summary>All valid days of a month.</summary>
|
||||||
private static readonly int[] ValidDays = Enumerable.Range(1, 28).ToArray();
|
private static readonly int[] ValidDays = Enumerable.Range(1, 28).ToArray();
|
||||||
|
|
||||||
|
@ -55,19 +58,18 @@ namespace SMAPI.Tests.Utilities
|
||||||
** Constructor
|
** Constructor
|
||||||
****/
|
****/
|
||||||
[Test(Description = "Assert that the constructor sets the expected values for all valid dates.")]
|
[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)
|
public void Constructor_SetsExpectedValues([ValueSource(nameof(SDateTests.SampleSeasonValues))] string season, [ValueSource(nameof(SDateTests.ValidDays))] int day, [Values(1, 2, 100)] int year)
|
||||||
{
|
{
|
||||||
// act
|
// act
|
||||||
SDate date = new SDate(day, season, year);
|
SDate date = new SDate(day, season, year);
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
Assert.AreEqual(day, date.Day);
|
Assert.AreEqual(day, date.Day);
|
||||||
Assert.AreEqual(season, date.Season);
|
Assert.AreEqual(season.Trim().ToLowerInvariant(), date.Season);
|
||||||
Assert.AreEqual(year, date.Year);
|
Assert.AreEqual(year, date.Year);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test(Description = "Assert that the constructor throws an exception if the values are invalid.")]
|
[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(01, "springs", 1)] // invalid season name
|
||||||
[TestCase(-1, "spring", 1)] // day < 0
|
[TestCase(-1, "spring", 1)] // day < 0
|
||||||
[TestCase(0, "spring", 1)] // day zero
|
[TestCase(0, "spring", 1)] // day zero
|
||||||
|
|
|
@ -250,6 +250,8 @@ namespace StardewModdingAPI.Utilities
|
||||||
/// <exception cref="ArgumentException">One of the arguments has an invalid value (like day 35).</exception>
|
/// <exception cref="ArgumentException">One of the arguments has an invalid value (like day 35).</exception>
|
||||||
private SDate(int day, string season, int year, bool allowDayZero)
|
private SDate(int day, string season, int year, bool allowDayZero)
|
||||||
{
|
{
|
||||||
|
season = season?.Trim().ToLowerInvariant();
|
||||||
|
|
||||||
// validate
|
// validate
|
||||||
if (season == null)
|
if (season == null)
|
||||||
throw new ArgumentNullException(nameof(season));
|
throw new ArgumentNullException(nameof(season));
|
||||||
|
@ -277,7 +279,7 @@ namespace StardewModdingAPI.Utilities
|
||||||
/// <param name="year">The year.</param>
|
/// <param name="year">The year.</param>
|
||||||
private bool IsDayZero(int day, string season, int year)
|
private bool IsDayZero(int day, string season, int year)
|
||||||
{
|
{
|
||||||
return day == 0 && season == "spring" && year == 1;
|
return day == 0 && season?.Trim().ToLower() == "spring" && year == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Get the day of week for a given date.</summary>
|
/// <summary>Get the day of week for a given date.</summary>
|
||||||
|
|
Loading…
Reference in New Issue