diff options
Diffstat (limited to 'src/SMAPI.Tests/Utilities')
-rw-r--r-- | src/SMAPI.Tests/Utilities/KeybindListTests.cs | 22 | ||||
-rw-r--r-- | src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs | 183 | ||||
-rw-r--r-- | src/SMAPI.Tests/Utilities/SDateTests.cs | 19 |
3 files changed, 106 insertions, 118 deletions
diff --git a/src/SMAPI.Tests/Utilities/KeybindListTests.cs b/src/SMAPI.Tests/Utilities/KeybindListTests.cs index f5c156c4..c4c086de 100644 --- a/src/SMAPI.Tests/Utilities/KeybindListTests.cs +++ b/src/SMAPI.Tests/Utilities/KeybindListTests.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using NUnit.Framework; @@ -23,12 +21,12 @@ namespace SMAPI.Tests.Utilities public void TryParse_SimpleValue(SButton button) { // act - bool success = KeybindList.TryParse($"{button}", out KeybindList parsed, out string[] errors); + bool success = KeybindList.TryParse($"{button}", out KeybindList? parsed, out string[] errors); // assert Assert.IsTrue(success, "Parsing unexpectedly failed."); Assert.IsNotNull(parsed, "The parsed result should not be null."); - Assert.AreEqual(parsed.ToString(), $"{button}"); + Assert.AreEqual(parsed!.ToString(), $"{button}"); Assert.IsNotNull(errors, message: "The errors should never be null."); Assert.IsEmpty(errors, message: "The input bindings incorrectly reported errors."); } @@ -46,17 +44,17 @@ namespace SMAPI.Tests.Utilities [TestCase(",", ExpectedResult = "None")] [TestCase("A,", ExpectedResult = "A")] [TestCase(",A", ExpectedResult = "A")] - public string TryParse_MultiValues(string input) + public string TryParse_MultiValues(string? input) { // act - bool success = KeybindList.TryParse(input, out KeybindList parsed, out string[] errors); + bool success = KeybindList.TryParse(input, out KeybindList? parsed, out string[] errors); // assert Assert.IsTrue(success, "Parsing unexpectedly failed."); Assert.IsNotNull(parsed, "The parsed result should not be null."); Assert.IsNotNull(errors, message: "The errors should never be null."); Assert.IsEmpty(errors, message: "The input bindings incorrectly reported errors."); - return parsed.ToString(); + return parsed!.ToString(); } /// <summary>Assert invalid values are rejected.</summary> @@ -69,7 +67,7 @@ namespace SMAPI.Tests.Utilities public void TryParse_InvalidValues(string input, string expectedError) { // act - bool success = KeybindList.TryParse(input, out KeybindList parsed, out string[] errors); + bool success = KeybindList.TryParse(input, out KeybindList? parsed, out string[] errors); // assert Assert.IsFalse(success, "Parsing unexpectedly succeeded."); @@ -100,13 +98,15 @@ namespace SMAPI.Tests.Utilities public SButtonState GetState(string input, string stateMap) { // act - bool success = KeybindList.TryParse(input, out KeybindList parsed, out string[] errors); + bool success = KeybindList.TryParse(input, out KeybindList? parsed, out string[] errors); if (success && parsed?.Keybinds != null) { - foreach (var keybind in parsed.Keybinds) + foreach (Keybind? keybind in parsed.Keybinds) + { #pragma warning disable 618 // method is marked obsolete because it should only be used in unit tests keybind.GetButtonState = key => this.GetStateFromMap(key, stateMap); #pragma warning restore 618 + } } // assert @@ -114,7 +114,7 @@ namespace SMAPI.Tests.Utilities Assert.IsNotNull(parsed, "The parsed result should not be null."); Assert.IsNotNull(errors, message: "The errors should never be null."); Assert.IsEmpty(errors, message: "The input bindings incorrectly reported errors."); - return parsed.GetState(); + return parsed!.GetState(); } diff --git a/src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs b/src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs index ae2cc6ce..3219d89d 100644 --- a/src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs +++ b/src/SMAPI.Tests/Utilities/PathUtilitiesTests.cs @@ -1,5 +1,4 @@ -#nullable disable - +using System.Diagnostics.CodeAnalysis; using System.IO; using NUnit.Framework; using StardewModdingAPI.Toolkit.Utilities; @@ -8,6 +7,7 @@ namespace SMAPI.Tests.Utilities { /// <summary>Unit tests for <see cref="PathUtilities"/>.</summary> [TestFixture] + [SuppressMessage("ReSharper", "StringLiteralTypo", Justification = "These are standard game install paths.")] internal class PathUtilitiesTests { /********* @@ -16,136 +16,125 @@ namespace SMAPI.Tests.Utilities /// <summary>Sample paths used in unit tests.</summary> public static readonly SamplePath[] SamplePaths = { // Windows absolute path - new() - { - OriginalPath = @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley", + new( + OriginalPath: @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley", - Segments = new[] { "C:", "Program Files (x86)", "Steam", "steamapps", "common", "Stardew Valley" }, - SegmentsLimit3 = new [] { "C:", "Program Files (x86)", @"Steam\steamapps\common\Stardew Valley" }, + Segments: new[] { "C:", "Program Files (x86)", "Steam", "steamapps", "common", "Stardew Valley" }, + SegmentsLimit3: new [] { "C:", "Program Files (x86)", @"Steam\steamapps\common\Stardew Valley" }, - NormalizedOnWindows = @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley", - NormalizedOnUnix = @"C:/Program Files (x86)/Steam/steamapps/common/Stardew Valley" - }, + NormalizedOnWindows: @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley", + NormalizedOnUnix: @"C:/Program Files (x86)/Steam/steamapps/common/Stardew Valley" + ), // Windows absolute path (with trailing slash) - new() - { - OriginalPath = @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\", + new( + OriginalPath: @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\", - Segments = new[] { "C:", "Program Files (x86)", "Steam", "steamapps", "common", "Stardew Valley" }, - SegmentsLimit3 = new [] { "C:", "Program Files (x86)", @"Steam\steamapps\common\Stardew Valley\" }, + Segments: new[] { "C:", "Program Files (x86)", "Steam", "steamapps", "common", "Stardew Valley" }, + SegmentsLimit3: new [] { "C:", "Program Files (x86)", @"Steam\steamapps\common\Stardew Valley\" }, - NormalizedOnWindows = @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\", - NormalizedOnUnix = @"C:/Program Files (x86)/Steam/steamapps/common/Stardew Valley/" - }, + NormalizedOnWindows: @"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley\", + NormalizedOnUnix: @"C:/Program Files (x86)/Steam/steamapps/common/Stardew Valley/" + ), // Windows relative path - new() - { - OriginalPath = @"Content\Characters\Dialogue\Abigail", + new( + OriginalPath: @"Content\Characters\Dialogue\Abigail", - Segments = new [] { "Content", "Characters", "Dialogue", "Abigail" }, - SegmentsLimit3 = new [] { "Content", "Characters", @"Dialogue\Abigail" }, + Segments: new [] { "Content", "Characters", "Dialogue", "Abigail" }, + SegmentsLimit3: new [] { "Content", "Characters", @"Dialogue\Abigail" }, - NormalizedOnWindows = @"Content\Characters\Dialogue\Abigail", - NormalizedOnUnix = @"Content/Characters/Dialogue/Abigail" - }, + NormalizedOnWindows: @"Content\Characters\Dialogue\Abigail", + NormalizedOnUnix: @"Content/Characters/Dialogue/Abigail" + ), // Windows relative path (with directory climbing) - new() - { - OriginalPath = @"..\..\Content", + new( + OriginalPath: @"..\..\Content", - Segments = new [] { "..", "..", "Content" }, - SegmentsLimit3 = new [] { "..", "..", "Content" }, + Segments: new [] { "..", "..", "Content" }, + SegmentsLimit3: new [] { "..", "..", "Content" }, - NormalizedOnWindows = @"..\..\Content", - NormalizedOnUnix = @"../../Content" - }, + NormalizedOnWindows: @"..\..\Content", + NormalizedOnUnix: @"../../Content" + ), // Windows UNC path - new() - { - OriginalPath = @"\\unc\path", + new( + OriginalPath: @"\\unc\path", - Segments = new [] { "unc", "path" }, - SegmentsLimit3 = new [] { "unc", "path" }, + Segments: new [] { "unc", "path" }, + SegmentsLimit3: new [] { "unc", "path" }, - NormalizedOnWindows = @"\\unc\path", - NormalizedOnUnix = "/unc/path" // there's no good way to normalize this on Unix since UNC paths aren't supported; path normalization is meant for asset names anyway, so this test only ensures it returns some sort of sane value - }, + NormalizedOnWindows: @"\\unc\path", + NormalizedOnUnix: "/unc/path" // there's no good way to normalize this on Unix since UNC paths aren't supported; path normalization is meant for asset names anyway, so this test only ensures it returns some sort of sane value + ), // Linux absolute path - new() - { - OriginalPath = @"/home/.steam/steam/steamapps/common/Stardew Valley", + new( + OriginalPath: @"/home/.steam/steam/steamapps/common/Stardew Valley", - Segments = new [] { "home", ".steam", "steam", "steamapps", "common", "Stardew Valley" }, - SegmentsLimit3 = new [] { "home", ".steam", "steam/steamapps/common/Stardew Valley" }, + Segments: new [] { "home", ".steam", "steam", "steamapps", "common", "Stardew Valley" }, + SegmentsLimit3: new [] { "home", ".steam", "steam/steamapps/common/Stardew Valley" }, - NormalizedOnWindows = @"\home\.steam\steam\steamapps\common\Stardew Valley", - NormalizedOnUnix = @"/home/.steam/steam/steamapps/common/Stardew Valley" - }, + NormalizedOnWindows: @"\home\.steam\steam\steamapps\common\Stardew Valley", + NormalizedOnUnix: @"/home/.steam/steam/steamapps/common/Stardew Valley" + ), // Linux absolute path (with trailing slash) - new() - { - OriginalPath = @"/home/.steam/steam/steamapps/common/Stardew Valley/", + new( + OriginalPath: @"/home/.steam/steam/steamapps/common/Stardew Valley/", - Segments = new [] { "home", ".steam", "steam", "steamapps", "common", "Stardew Valley" }, - SegmentsLimit3 = new [] { "home", ".steam", "steam/steamapps/common/Stardew Valley/" }, + Segments: new [] { "home", ".steam", "steam", "steamapps", "common", "Stardew Valley" }, + SegmentsLimit3: new [] { "home", ".steam", "steam/steamapps/common/Stardew Valley/" }, - NormalizedOnWindows = @"\home\.steam\steam\steamapps\common\Stardew Valley\", - NormalizedOnUnix = @"/home/.steam/steam/steamapps/common/Stardew Valley/" - }, + NormalizedOnWindows: @"\home\.steam\steam\steamapps\common\Stardew Valley\", + NormalizedOnUnix: @"/home/.steam/steam/steamapps/common/Stardew Valley/" + ), // Linux absolute path (with ~) - new() - { - OriginalPath = @"~/.steam/steam/steamapps/common/Stardew Valley", + new( + OriginalPath: @"~/.steam/steam/steamapps/common/Stardew Valley", - Segments = new [] { "~", ".steam", "steam", "steamapps", "common", "Stardew Valley" }, - SegmentsLimit3 = new [] { "~", ".steam", "steam/steamapps/common/Stardew Valley" }, + Segments: new [] { "~", ".steam", "steam", "steamapps", "common", "Stardew Valley" }, + SegmentsLimit3: new [] { "~", ".steam", "steam/steamapps/common/Stardew Valley" }, - NormalizedOnWindows = @"~\.steam\steam\steamapps\common\Stardew Valley", - NormalizedOnUnix = @"~/.steam/steam/steamapps/common/Stardew Valley" - }, + NormalizedOnWindows: @"~\.steam\steam\steamapps\common\Stardew Valley", + NormalizedOnUnix: @"~/.steam/steam/steamapps/common/Stardew Valley" + ), // Linux relative path - new() - { - OriginalPath = @"Content/Characters/Dialogue/Abigail", + new( + OriginalPath: @"Content/Characters/Dialogue/Abigail", - Segments = new [] { "Content", "Characters", "Dialogue", "Abigail" }, - SegmentsLimit3 = new [] { "Content", "Characters", "Dialogue/Abigail" }, + Segments: new [] { "Content", "Characters", "Dialogue", "Abigail" }, + SegmentsLimit3: new [] { "Content", "Characters", "Dialogue/Abigail" }, - NormalizedOnWindows = @"Content\Characters\Dialogue\Abigail", - NormalizedOnUnix = @"Content/Characters/Dialogue/Abigail" - }, + NormalizedOnWindows: @"Content\Characters\Dialogue\Abigail", + NormalizedOnUnix: @"Content/Characters/Dialogue/Abigail" + ), // Linux relative path (with directory climbing) - new() - { - OriginalPath = @"../../Content", + new( + OriginalPath: @"../../Content", - Segments = new [] { "..", "..", "Content" }, - SegmentsLimit3 = new [] { "..", "..", "Content" }, + Segments: new [] { "..", "..", "Content" }, + SegmentsLimit3: new [] { "..", "..", "Content" }, - NormalizedOnWindows = @"..\..\Content", - NormalizedOnUnix = @"../../Content" - }, + NormalizedOnWindows: @"..\..\Content", + NormalizedOnUnix: @"../../Content" + ), // Mixed directory separators - new() - { - OriginalPath = @"C:\some/mixed\path/separators", + new( + OriginalPath: @"C:\some/mixed\path/separators", - Segments = new [] { "C:", "some", "mixed", "path", "separators" }, - SegmentsLimit3 = new [] { "C:", "some", @"mixed\path/separators" }, + Segments: new [] { "C:", "some", "mixed", "path", "separators" }, + SegmentsLimit3: new [] { "C:", "some", @"mixed\path/separators" }, - NormalizedOnWindows = @"C:\some\mixed\path\separators", - NormalizedOnUnix = @"C:/some/mixed/path/separators" - }, + NormalizedOnWindows: @"C:\some\mixed\path\separators", + NormalizedOnUnix: @"C:/some/mixed/path/separators" + ) }; @@ -283,14 +272,14 @@ namespace SMAPI.Tests.Utilities /********* ** Private classes *********/ - public class SamplePath + /// <summary>A sample path in multiple formats.</summary> + /// <param name="OriginalPath">The original path to pass to the <see cref="PathUtilities"/>.</param> + /// <param name="Segments">The normalized path segments.</param> + /// <param name="SegmentsLimit3">The normalized path segments, if we stop segmenting after the second one.</param> + /// <param name="NormalizedOnWindows">The normalized form on Windows.</param> + /// <param name="NormalizedOnUnix">The normalized form on Linux or macOS.</param> + public record SamplePath(string OriginalPath, string[] Segments, string[] SegmentsLimit3, string NormalizedOnWindows, string NormalizedOnUnix) { - public string OriginalPath { get; set; } - public string[] Segments { get; set; } - public string[] SegmentsLimit3 { get; set; } - public string NormalizedOnWindows { get; set; } - public string NormalizedOnUnix { get; set; } - public override string ToString() { return this.OriginalPath; diff --git a/src/SMAPI.Tests/Utilities/SDateTests.cs b/src/SMAPI.Tests/Utilities/SDateTests.cs index a4a36828..b9c3d202 100644 --- a/src/SMAPI.Tests/Utilities/SDateTests.cs +++ b/src/SMAPI.Tests/Utilities/SDateTests.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -258,7 +256,7 @@ namespace SMAPI.Tests.Utilities { SDate date = new(day, season, year); int hash = date.GetHashCode(); - if (hashes.TryGetValue(hash, out SDate otherDate)) + 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})."); @@ -298,7 +296,7 @@ namespace SMAPI.Tests.Utilities [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) + public bool Operators_Equals(string? now, string other) { return this.GetDate(now) == this.GetDate(other); } @@ -312,7 +310,7 @@ namespace SMAPI.Tests.Utilities [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) + public bool Operators_NotEquals(string? now, string other) { return this.GetDate(now) != this.GetDate(other); } @@ -326,7 +324,7 @@ namespace SMAPI.Tests.Utilities [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) + public bool Operators_LessThan(string? now, string other) { return this.GetDate(now) < this.GetDate(other); } @@ -340,7 +338,7 @@ namespace SMAPI.Tests.Utilities [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) + public bool Operators_LessThanOrEqual(string? now, string other) { return this.GetDate(now) <= this.GetDate(other); } @@ -354,7 +352,7 @@ namespace SMAPI.Tests.Utilities [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) + public bool Operators_MoreThan(string? now, string other) { return this.GetDate(now) > this.GetDate(other); } @@ -368,7 +366,7 @@ namespace SMAPI.Tests.Utilities [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) + public bool Operators_MoreThanOrEqual(string? now, string other) { return this.GetDate(now) > this.GetDate(other); } @@ -379,7 +377,8 @@ namespace SMAPI.Tests.Utilities *********/ /// <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 GetDate(string dateStr) + [return: NotNullIfNotNull("dateStr")] + private SDate? GetDate(string? dateStr) { if (dateStr == null) return null; |