From ab6cf45b03073f324c46f9e93a98e3342a1bdff7 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 7 Apr 2022 00:56:00 -0400 Subject: enable nullable annotations for semantic versions (#837) --- src/SMAPI.Tests/Utilities/SemanticVersionTests.cs | 35 +++++++++++------------ 1 file changed, 16 insertions(+), 19 deletions(-) (limited to 'src/SMAPI.Tests/Utilities') diff --git a/src/SMAPI.Tests/Utilities/SemanticVersionTests.cs b/src/SMAPI.Tests/Utilities/SemanticVersionTests.cs index 599ac839..fedadba6 100644 --- a/src/SMAPI.Tests/Utilities/SemanticVersionTests.cs +++ b/src/SMAPI.Tests/Utilities/SemanticVersionTests.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Diagnostics.CodeAnalysis; using Newtonsoft.Json; @@ -63,10 +61,10 @@ namespace SMAPI.Tests.Utilities [TestCase("apple")] [TestCase("-apple")] [TestCase("-5")] - public void Constructor_FromString_WithInvalidValues(string input) + public void Constructor_FromString_WithInvalidValues(string? input) { if (input == null) - this.AssertAndLogException(() => new SemanticVersion(input)); + this.AssertAndLogException(() => new SemanticVersion(input!)); else this.AssertAndLogException(() => new SemanticVersion(input)); } @@ -93,7 +91,7 @@ namespace SMAPI.Tests.Utilities [TestCase("1.2.3.4-some-tag.4 ")] public void Constructor_FromString_Standard_DisallowsNonStandardVersion(string input) { - Assert.Throws(() => new SemanticVersion(input)); + Assert.Throws(() => _ = new SemanticVersion(input)); } /// Assert the parsed version when constructed from standard parts. @@ -112,7 +110,7 @@ namespace SMAPI.Tests.Utilities [TestCase(1, 2, 3, "some-tag.4 ", null, ExpectedResult = "1.2.3-some-tag.4")] [TestCase(1, 2, 3, "some-tag.4 ", "build.004", ExpectedResult = "1.2.3-some-tag.4+build.004")] [TestCase(1, 2, 0, null, "3.4.5-build.004", ExpectedResult = "1.2.0+3.4.5-build.004")] - public string Constructor_FromParts(int major, int minor, int patch, string prerelease, string build) + public string Constructor_FromParts(int major, int minor, int patch, string? prerelease, string? build) { // act ISemanticVersion version = new SemanticVersion(major, minor, patch, prerelease, build); @@ -222,11 +220,11 @@ namespace SMAPI.Tests.Utilities // null [TestCase("1.0.0", null, ExpectedResult = 1)] // null is always less than any value per CompareTo remarks - public int CompareTo(string versionStrA, string versionStrB) + public int CompareTo(string versionStrA, string? versionStrB) { // arrange ISemanticVersion versionA = new SemanticVersion(versionStrA); - ISemanticVersion versionB = versionStrB != null + ISemanticVersion? versionB = versionStrB != null ? new SemanticVersion(versionStrB) : null; @@ -270,11 +268,11 @@ namespace SMAPI.Tests.Utilities // null [TestCase("1.0.0", null, ExpectedResult = false)] // null is always less than any value per CompareTo remarks - public bool IsOlderThan(string versionStrA, string versionStrB) + public bool IsOlderThan(string versionStrA, string? versionStrB) { // arrange ISemanticVersion versionA = new SemanticVersion(versionStrA); - ISemanticVersion versionB = versionStrB != null + ISemanticVersion? versionB = versionStrB != null ? new SemanticVersion(versionStrB) : null; @@ -319,11 +317,11 @@ namespace SMAPI.Tests.Utilities // null [TestCase("1.0.0", null, ExpectedResult = true)] // null is always less than any value per CompareTo remarks - public bool IsNewerThan(string versionStrA, string versionStrB) + public bool IsNewerThan(string versionStrA, string? versionStrB) { // arrange ISemanticVersion versionA = new SemanticVersion(versionStrA); - ISemanticVersion versionB = versionStrB != null + ISemanticVersion? versionB = versionStrB != null ? new SemanticVersion(versionStrB) : null; @@ -356,13 +354,13 @@ namespace SMAPI.Tests.Utilities [TestCase("1.0-beta.2", "1.0-beta.10", "1.0-beta.3", ExpectedResult = false)] [TestCase("1.0-beta-2", "1.0-beta-10", "1.0-beta-3", ExpectedResult = false)] [TestCase("1.0.0", "1.0.0", null, ExpectedResult = false)] // null is always less than any value per CompareTo remarks - public bool IsBetween(string versionStr, string lowerStr, string upperStr) + public bool IsBetween(string versionStr, string? lowerStr, string? upperStr) { // arrange - ISemanticVersion lower = lowerStr != null + ISemanticVersion? lower = lowerStr != null ? new SemanticVersion(lowerStr) : null; - ISemanticVersion upper = upperStr != null + ISemanticVersion? upper = upperStr != null ? new SemanticVersion(upperStr) : null; ISemanticVersion version = new SemanticVersion(versionStr); @@ -436,7 +434,7 @@ namespace SMAPI.Tests.Utilities /// The prerelease tag. /// The build metadata. /// Whether the version should be marked as non-standard. - private void AssertParts(ISemanticVersion version, int major, int minor, int patch, string prerelease, string build, bool nonStandard) + private void AssertParts(ISemanticVersion version, int major, int minor, int patch, string? prerelease, string? build, bool nonStandard) { Assert.AreEqual(major, version.MajorVersion, "The major version doesn't match."); Assert.AreEqual(minor, version.MinorVersion, "The minor version doesn't match."); @@ -449,9 +447,8 @@ namespace SMAPI.Tests.Utilities /// Assert that the expected exception type is thrown, and log the action output and thrown exception. /// The expected exception type. /// The action which may throw the exception. - /// The message to log if the expected exception isn't thrown. [SuppressMessage("ReSharper", "UnusedParameter.Local", Justification = "The message argument is deliberately only used in precondition checks since this is an assertion method.")] - private void AssertAndLogException(Func action, string message = null) + private void AssertAndLogException(Func action) where T : Exception { this.AssertAndLogException(() => @@ -466,7 +463,7 @@ namespace SMAPI.Tests.Utilities /// The action which may throw the exception. /// The message to log if the expected exception isn't thrown. [SuppressMessage("ReSharper", "UnusedParameter.Local", Justification = "The message argument is deliberately only used in precondition checks since this is an assertion method.")] - private void AssertAndLogException(Action action, string message = null) + private void AssertAndLogException(Action action, string? message = null) where T : Exception { try -- cgit