summaryrefslogtreecommitdiff
path: root/src/SMAPI.Tests/Core/TranslationTests.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI.Tests/Core/TranslationTests.cs')
-rw-r--r--src/SMAPI.Tests/Core/TranslationTests.cs87
1 files changed, 67 insertions, 20 deletions
diff --git a/src/SMAPI.Tests/Core/TranslationTests.cs b/src/SMAPI.Tests/Core/TranslationTests.cs
index 457f9fad..a52df607 100644
--- a/src/SMAPI.Tests/Core/TranslationTests.cs
+++ b/src/SMAPI.Tests/Core/TranslationTests.cs
@@ -1,9 +1,14 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.IO;
using System.Linq;
using NUnit.Framework;
using StardewModdingAPI;
+using StardewModdingAPI.Framework;
using StardewModdingAPI.Framework.ModHelpers;
+using StardewModdingAPI.Framework.ModLoading;
+using StardewModdingAPI.Toolkit.Serialization.Models;
using StardewValley;
namespace SMAPI.Tests.Core
@@ -16,7 +21,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 " };
/*********
@@ -32,15 +37,15 @@ namespace SMAPI.Tests.Core
var data = new Dictionary<string, IDictionary<string, string>>();
// act
- ITranslationHelper helper = new TranslationHelper("ModID", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data);
+ ITranslationHelper helper = new TranslationHelper(this.CreateModMetadata(), "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.");
@@ -54,8 +59,8 @@ namespace SMAPI.Tests.Core
var expected = this.GetExpectedTranslations();
// act
- var actual = new Dictionary<string, Translation[]>();
- TranslationHelper helper = new TranslationHelper("ModID", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data);
+ var actual = new Dictionary<string, Translation[]?>();
+ TranslationHelper helper = new TranslationHelper(this.CreateModMetadata(), "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data);
foreach (string locale in expected.Keys)
{
this.AssertSetLocale(helper, locale, LocalizedContentManager.LanguageCode.en);
@@ -79,7 +84,7 @@ namespace SMAPI.Tests.Core
// act
var actual = new Dictionary<string, Translation[]>();
- TranslationHelper helper = new TranslationHelper("ModID", "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data);
+ TranslationHelper helper = new TranslationHelper(this.CreateModMetadata(), "en", LocalizedContentManager.LanguageCode.en).SetTranslations(data);
foreach (string locale in expected.Keys)
{
this.AssertSetLocale(helper, locale, LocalizedContentManager.LanguageCode.en);
@@ -107,16 +112,16 @@ 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 Translation("pt-BR", "key", text);
+ Translation translation = new("pt-BR", "key", text);
// assert
if (translation.HasValue())
@@ -126,20 +131,20 @@ 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 Translation("pt-BR", "key", text);
+ Translation translation = new("pt-BR", "key", text);
// assert
if (translation.HasValue())
- Assert.AreEqual(text, (string)translation, "The translation returned an unexpected value given a valid input.");
+ Assert.AreEqual(text, (string?)translation, "The translation returned an unexpected value given a valid input.");
else
- Assert.AreEqual(this.GetPlaceholderText("key"), (string)translation, "The translation returned an unexpected value given a null or empty input.");
+ Assert.AreEqual(this.GetPlaceholderText("key"), (string?)translation, "The translation returned an unexpected value given a null or empty input.");
}
[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);
@@ -154,7 +159,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);
@@ -182,7 +187,7 @@ namespace SMAPI.Tests.Core
string expected = $"{start} tokens are properly replaced (including {middle} {middle}) {end}";
// act
- Translation translation = new Translation("pt-BR", "key", input);
+ Translation translation = new("pt-BR", "key", input);
switch (structure)
{
case "anonymous object":
@@ -190,7 +195,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>":
@@ -324,21 +329,63 @@ namespace SMAPI.Tests.Core
return string.Format(Translation.PlaceholderText, key);
}
+ /// <summary>Create a fake mod manifest.</summary>
+ private IModMetadata CreateModMetadata()
+ {
+ string id = $"smapi.unit-tests.fake-mod-{Guid.NewGuid():N}";
+
+ string tempPath = Path.Combine(Path.GetTempPath(), id);
+ return new ModMetadata(
+ displayName: "Mod Display Name",
+ directoryPath: tempPath,
+ rootPath: tempPath,
+ manifest: new Manifest(
+ uniqueID: id,
+ name: "Mod Name",
+ author: "Mod Author",
+ description: "Mod Description",
+ version: new SemanticVersion(1, 0, 0)
+ ),
+ dataRecord: null,
+ isIgnored: false
+ );
+ }
+
/*********
** 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;
+ }
}
}
}