combine Translation::Tokens overloads, support dictionaries with any generic types (#296)

This commit is contained in:
Jesse Plamondon-Willard 2017-05-28 11:23:42 -04:00
parent af28b87660
commit 423a2f5012
1 changed files with 6 additions and 19 deletions

View File

@ -16,19 +16,6 @@ namespace StardewModdingAPI.Tests
/// <summary>Sample translation text for unit tests.</summary> /// <summary>Sample translation text for unit tests.</summary>
public static string[] Samples = { null, "", " ", "boop", " boop " }; public static string[] Samples = { null, "", " ", "boop", " boop " };
/// <summary>A token structure type.</summary>
public enum TokenType
{
/// <summary>The tokens are passed in a string/object dictionary.</summary>
DictionaryStringObject,
/// <summary>The tokens are passed in a string/string dictionary.</summary>
DictionaryStringString,
/// <summary>The tokens are passed in an anonymous object.</summary>
AnonymousObject
}
/********* /*********
** Unit tests ** Unit tests
@ -193,7 +180,7 @@ namespace StardewModdingAPI.Tests
** Translation tokens ** Translation tokens
****/ ****/
[Test(Description = "Assert that multiple translation tokens are replaced correctly regardless of the token structure.")] [Test(Description = "Assert that multiple translation tokens are replaced correctly regardless of the token structure.")]
public void Translation_Tokens([Values(TokenType.AnonymousObject, TokenType.DictionaryStringObject, TokenType.DictionaryStringString)] TokenType tokenType) public void Translation_Tokens([Values("anonymous object", "IDictionary<string, object>", "IDictionary<string, string>")] string structure)
{ {
// arrange // arrange
string start = Guid.NewGuid().ToString("N"); string start = Guid.NewGuid().ToString("N");
@ -204,22 +191,22 @@ namespace StardewModdingAPI.Tests
// act // act
Translation translation = new Translation("ModName", "pt-BR", "key", input); Translation translation = new Translation("ModName", "pt-BR", "key", input);
switch (tokenType) switch (structure)
{ {
case TokenType.AnonymousObject: case "anonymous object":
translation = translation.Tokens(new { start, middle, end }); translation = translation.Tokens(new { start, middle, end });
break; break;
case TokenType.DictionaryStringObject: case "IDictionary<string, object>":
translation = translation.Tokens(new Dictionary<string, object> { ["start"] = start, ["middle"] = middle, ["end"] = end }); translation = translation.Tokens(new Dictionary<string, object> { ["start"] = start, ["middle"] = middle, ["end"] = end });
break; break;
case TokenType.DictionaryStringString: case "IDictionary<string, string>":
translation = translation.Tokens(new Dictionary<string, string> { ["start"] = start, ["middle"] = middle, ["end"] = end }); translation = translation.Tokens(new Dictionary<string, string> { ["start"] = start, ["middle"] = middle, ["end"] = end });
break; break;
default: default:
throw new NotSupportedException($"Unknown token type {tokenType}."); throw new NotSupportedException($"Unknown structure '{structure}'.");
} }
// assert // assert