summaryrefslogtreecommitdiff
path: root/src/SMAPI.Tests/Core
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-04-12 20:52:01 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2022-04-12 20:52:01 -0400
commit5f7a92a74592a53529890eebb1ee9fe519afd92f (patch)
tree67c515794b8dcc7d4721adc3b2f239edd68f9009 /src/SMAPI.Tests/Core
parentc3851ae2e6c8fb286d4744612fbfea039d1baf7f (diff)
downloadSMAPI-5f7a92a74592a53529890eebb1ee9fe519afd92f.tar.gz
SMAPI-5f7a92a74592a53529890eebb1ee9fe519afd92f.tar.bz2
SMAPI-5f7a92a74592a53529890eebb1ee9fe519afd92f.zip
enable nullable annotations in unit tests (#837)
Diffstat (limited to 'src/SMAPI.Tests/Core')
-rw-r--r--src/SMAPI.Tests/Core/AssetNameTests.cs10
-rw-r--r--src/SMAPI.Tests/Core/InterfaceProxyTests.cs12
-rw-r--r--src/SMAPI.Tests/Core/ModResolverTests.cs2
-rw-r--r--src/SMAPI.Tests/Core/TranslationTests.cs47
4 files changed, 43 insertions, 28 deletions
diff --git a/src/SMAPI.Tests/Core/AssetNameTests.cs b/src/SMAPI.Tests/Core/AssetNameTests.cs
index ef8a08ef..8018442c 100644
--- a/src/SMAPI.Tests/Core/AssetNameTests.cs
+++ b/src/SMAPI.Tests/Core/AssetNameTests.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using System.Collections.Generic;
using FluentAssertions;
@@ -28,7 +26,7 @@ namespace SMAPI.Tests.Core
[TestCase("Characters/Dialogue/Abigail.fr-FR", "Characters/Dialogue/Abigail", "fr-FR", LocalizedContentManager.LanguageCode.fr)]
[TestCase("Characters/Dialogue\\Abigail.fr-FR", "Characters/Dialogue/Abigail.fr-FR", null, null)]
[TestCase("Characters/Dialogue/Abigail.fr-FR", "Characters/Dialogue/Abigail", "fr-FR", LocalizedContentManager.LanguageCode.fr)]
- public void Constructor_Valid(string name, string expectedBaseName, string expectedLocale, LocalizedContentManager.LanguageCode? expectedLanguageCode)
+ public void Constructor_Valid(string name, string expectedBaseName, string? expectedLocale, LocalizedContentManager.LanguageCode? expectedLanguageCode)
{
// arrange
name = PathUtilities.NormalizeAssetName(name);
@@ -55,13 +53,13 @@ namespace SMAPI.Tests.Core
[TestCase(" ")]
[TestCase("\t")]
[TestCase(" \t ")]
- public void Constructor_NullOrWhitespace(string name)
+ public void Constructor_NullOrWhitespace(string? name)
{
// act
- ArgumentException exception = Assert.Throws<ArgumentException>(() => _ = AssetName.Parse(name, null));
+ ArgumentException exception = Assert.Throws<ArgumentException>(() => _ = AssetName.Parse(name!, null))!;
// assert
- exception!.ParamName.Should().Be("rawName");
+ exception.ParamName.Should().Be("rawName");
exception.Message.Should().Be("The asset name can't be null or empty. (Parameter 'rawName')");
}
diff --git a/src/SMAPI.Tests/Core/InterfaceProxyTests.cs b/src/SMAPI.Tests/Core/InterfaceProxyTests.cs
index 1bf2ed68..0b4919ed 100644
--- a/src/SMAPI.Tests/Core/InterfaceProxyTests.cs
+++ b/src/SMAPI.Tests/Core/InterfaceProxyTests.cs
@@ -1,5 +1,3 @@
-#nullable disable
-
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
@@ -41,7 +39,7 @@ namespace SMAPI.Tests.Core
public void CanProxy_EventField()
{
// arrange
- var providerMod = new ProviderMod();
+ ProviderMod providerMod = new();
object implementation = providerMod.GetModApi();
int expectedValue = this.Random.Next();
@@ -61,7 +59,7 @@ namespace SMAPI.Tests.Core
public void CanProxy_EventProperty()
{
// arrange
- var providerMod = new ProviderMod();
+ ProviderMod providerMod = new();
object implementation = providerMod.GetModApi();
int expectedValue = this.Random.Next();
@@ -86,7 +84,7 @@ namespace SMAPI.Tests.Core
public void CanProxy_Properties(string setVia)
{
// arrange
- var providerMod = new ProviderMod();
+ ProviderMod providerMod = new();
object implementation = providerMod.GetModApi();
int expectedNumber = this.Random.Next();
int expectedObject = this.Random.Next();
@@ -317,13 +315,13 @@ namespace SMAPI.Tests.Core
/// <summary>Get a property value from an instance.</summary>
/// <param name="parent">The instance whose property to read.</param>
/// <param name="name">The property name.</param>
- private object GetPropertyValue(object parent, string name)
+ private object? GetPropertyValue(object parent, string name)
{
if (parent is null)
throw new ArgumentNullException(nameof(parent));
Type type = parent.GetType();
- PropertyInfo property = type.GetProperty(name);
+ PropertyInfo? property = type.GetProperty(name);
if (property is null)
throw new InvalidOperationException($"The '{type.FullName}' type has no public property named '{name}'.");
diff --git a/src/SMAPI.Tests/Core/ModResolverTests.cs b/src/SMAPI.Tests/Core/ModResolverTests.cs
index b8649a7d..bd621bbf 100644
--- a/src/SMAPI.Tests/Core/ModResolverTests.cs
+++ b/src/SMAPI.Tests/Core/ModResolverTests.cs
@@ -507,7 +507,7 @@ namespace SMAPI.Tests.Core
/// <param name="allowStatusChange">Whether the code being tested is allowed to change the mod status.</param>
private Mock<IModMetadata> GetMetadata(string uniqueID, string[] dependencies, bool allowStatusChange = false)
{
- IManifest manifest = this.GetManifest(id: uniqueID, version: "1.0", dependencies: dependencies?.Select(dependencyID => (IManifestDependency)new ManifestDependency(dependencyID, null as ISemanticVersion)).ToArray());
+ IManifest manifest = this.GetManifest(id: uniqueID, version: "1.0", dependencies: dependencies.Select(dependencyID => (IManifestDependency)new ManifestDependency(dependencyID, null as ISemanticVersion)).ToArray());
return this.GetMetadata(manifest, allowStatusChange);
}
diff --git a/src/SMAPI.Tests/Core/TranslationTests.cs b/src/SMAPI.Tests/Core/TranslationTests.cs
index f8f0e315..a65bf772 100644
--- a/src/SMAPI.Tests/Core/TranslationTests.cs
+++ b/src/SMAPI.Tests/Core/TranslationTests.cs
@@ -1,7 +1,6 @@
-#nullable disable
-
using System;
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using System.Linq;
using NUnit.Framework;
using StardewModdingAPI;
@@ -18,7 +17,7 @@ namespace SMAPI.Tests.Core
** Data
*********/
/// <summary>Sample translation text for unit tests.</summary>
- public static string[] Samples = { null, "", " ", "boop", " boop " };
+ public static string?[] Samples = { null, "", " ", "boop", " boop " };
/*********
@@ -36,13 +35,13 @@ namespace SMAPI.Tests.Core
// act
ITranslationHelper helper = new TranslationHelper("ModID", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data);
Translation translation = helper.Get("key");
- Translation[] translationList = helper.GetTranslations()?.ToArray();
+ Translation[]? translationList = helper.GetTranslations()?.ToArray();
// assert
Assert.AreEqual("en", helper.Locale, "The locale doesn't match the input value.");
Assert.AreEqual(LocalizedContentManager.LanguageCode.en, helper.LocaleEnum, "The locale enum doesn't match the input value.");
Assert.IsNotNull(translationList, "The full list of translations is unexpectedly null.");
- Assert.AreEqual(0, translationList.Length, "The full list of translations is unexpectedly not empty.");
+ Assert.AreEqual(0, translationList!.Length, "The full list of translations is unexpectedly not empty.");
Assert.IsNotNull(translation, "The translation helper unexpectedly returned a null translation.");
Assert.AreEqual(this.GetPlaceholderText("key"), translation.ToString(), "The translation returned an unexpected value.");
@@ -56,7 +55,7 @@ namespace SMAPI.Tests.Core
var expected = this.GetExpectedTranslations();
// act
- var actual = new Dictionary<string, Translation[]>();
+ var actual = new Dictionary<string, Translation[]?>();
TranslationHelper helper = new TranslationHelper("ModID", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data);
foreach (string locale in expected.Keys)
{
@@ -109,13 +108,13 @@ namespace SMAPI.Tests.Core
[TestCase(" ", ExpectedResult = true)]
[TestCase("boop", ExpectedResult = true)]
[TestCase(" boop ", ExpectedResult = true)]
- public bool Translation_HasValue(string text)
+ public bool Translation_HasValue(string? text)
{
return new Translation("pt-BR", "key", text).HasValue();
}
[Test(Description = "Assert that the translation's ToString method returns the expected text for various inputs.")]
- public void Translation_ToString([ValueSource(nameof(TranslationTests.Samples))] string text)
+ public void Translation_ToString([ValueSource(nameof(TranslationTests.Samples))] string? text)
{
// act
Translation translation = new("pt-BR", "key", text);
@@ -128,7 +127,7 @@ namespace SMAPI.Tests.Core
}
[Test(Description = "Assert that the translation's implicit string conversion returns the expected text for various inputs.")]
- public void Translation_ImplicitStringConversion([ValueSource(nameof(TranslationTests.Samples))] string text)
+ public void Translation_ImplicitStringConversion([ValueSource(nameof(TranslationTests.Samples))] string? text)
{
// act
Translation translation = new("pt-BR", "key", text);
@@ -141,7 +140,7 @@ namespace SMAPI.Tests.Core
}
[Test(Description = "Assert that the translation returns the expected text given a use-placeholder setting.")]
- public void Translation_UsePlaceholder([Values(true, false)] bool value, [ValueSource(nameof(TranslationTests.Samples))] string text)
+ public void Translation_UsePlaceholder([Values(true, false)] bool value, [ValueSource(nameof(TranslationTests.Samples))] string? text)
{
// act
Translation translation = new Translation("pt-BR", "key", text).UsePlaceholder(value);
@@ -156,7 +155,7 @@ namespace SMAPI.Tests.Core
}
[Test(Description = "Assert that the translation returns the expected text after setting the default.")]
- public void Translation_Default([ValueSource(nameof(TranslationTests.Samples))] string text, [ValueSource(nameof(TranslationTests.Samples))] string @default)
+ public void Translation_Default([ValueSource(nameof(TranslationTests.Samples))] string? text, [ValueSource(nameof(TranslationTests.Samples))] string? @default)
{
// act
Translation translation = new Translation("pt-BR", "key", text).Default(@default);
@@ -192,7 +191,7 @@ namespace SMAPI.Tests.Core
break;
case "class":
- translation = translation.Tokens(new TokenModel { Start = start, Middle = middle, End = end });
+ translation = translation.Tokens(new TokenModel(start, middle, end));
break;
case "IDictionary<string, object>":
@@ -331,16 +330,36 @@ namespace SMAPI.Tests.Core
** Test models
*********/
/// <summary>A model used to test token support.</summary>
+ [SuppressMessage("ReSharper", "NotAccessedField.Local", Justification = "Used dynamically via translation helper.")]
+ [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local", Justification = "Used dynamically via translation helper.")]
private class TokenModel
{
+ /*********
+ ** Accessors
+ *********/
/// <summary>A sample token property.</summary>
- public string Start { get; set; }
+ public string Start { get; }
/// <summary>A sample token property.</summary>
- public string Middle { get; set; }
+ public string Middle { get; }
/// <summary>A sample token field.</summary>
public string End;
+
+
+ /*********
+ ** public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="start">A sample token property.</param>
+ /// <param name="middle">A sample token field.</param>
+ /// <param name="end">A sample token property.</param>
+ public TokenModel(string start, string middle, string end)
+ {
+ this.Start = start;
+ this.Middle = middle;
+ this.End = end;
+ }
}
}
}