add token support for instance fields, expand unit test (#296)
This commit is contained in:
parent
423a2f5012
commit
e20db6e8e4
|
@ -180,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("anonymous object", "IDictionary<string, object>", "IDictionary<string, string>")] string structure)
|
public void Translation_Tokens([Values("anonymous object", "class", "IDictionary<string, object>", "IDictionary<string, string>")] string structure)
|
||||||
{
|
{
|
||||||
// arrange
|
// arrange
|
||||||
string start = Guid.NewGuid().ToString("N");
|
string start = Guid.NewGuid().ToString("N");
|
||||||
|
@ -197,6 +197,10 @@ namespace StardewModdingAPI.Tests
|
||||||
translation = translation.Tokens(new { start, middle, end });
|
translation = translation.Tokens(new { start, middle, end });
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case "class":
|
||||||
|
translation = translation.Tokens(new TokenModel { Start = start, Middle = middle, End = end });
|
||||||
|
break;
|
||||||
|
|
||||||
case "IDictionary<string, object>":
|
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;
|
||||||
|
@ -323,5 +327,22 @@ namespace StardewModdingAPI.Tests
|
||||||
{
|
{
|
||||||
return string.Format(Translation.PlaceholderText, key);
|
return string.Format(Translation.PlaceholderText, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*********
|
||||||
|
** Test models
|
||||||
|
*********/
|
||||||
|
/// <summary>A model used to test token support.</summary>
|
||||||
|
private class TokenModel
|
||||||
|
{
|
||||||
|
/// <summary>A sample token property.</summary>
|
||||||
|
public string Start { get; set; }
|
||||||
|
|
||||||
|
/// <summary>A sample token property.</summary>
|
||||||
|
public string Middle { get; set; }
|
||||||
|
|
||||||
|
/// <summary>A sample token field.</summary>
|
||||||
|
public string End;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,8 +107,11 @@ namespace StardewModdingAPI
|
||||||
// from object properties
|
// from object properties
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
foreach (PropertyInfo prop in tokens.GetType().GetProperties())
|
Type type = tokens.GetType();
|
||||||
|
foreach (PropertyInfo prop in type.GetProperties())
|
||||||
tokenLookup[prop.Name] = prop.GetValue(tokens)?.ToString();
|
tokenLookup[prop.Name] = prop.GetValue(tokens)?.ToString();
|
||||||
|
foreach (FieldInfo field in type.GetFields())
|
||||||
|
tokenLookup[field.Name] = field.GetValue(tokens)?.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue