Skip to content

Commit

Permalink
added parameterized string tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jukka Snellman committed Aug 19, 2022
1 parent a890d2d commit 24b21a1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class StringLocalizerWithCultureTests
{

private readonly IStringLocalizerWithCultureFactory _factory;
private readonly CultureInfo _en = CultureInfo.GetCultureInfo("en-US");
private readonly CultureInfo _fi = CultureInfo.GetCultureInfo("fi-FI");

public StringLocalizerWithCultureTests() {
var services = new ServiceCollection()
Expand All @@ -33,6 +35,14 @@ private LocalizedString Translate(string key, CultureInfo culture)
return result;
}

private void TestGetString(string expected, string key, string[] arguments, CultureInfo culture)
{
var localizer = _factory.Create(typeof(MyClass), culture);
var actual = localizer[key, arguments];
actual.Should().NotBeNull();
actual.Value.Should().Be(expected);
}

[Fact]
public void GetString_Invariant()
{
Expand All @@ -42,26 +52,26 @@ public void GetString_Invariant()
[Fact]
public void GetString_En()
{
TestGetString("Hello World", "Hello", CultureInfo.GetCultureInfo("en-US"));
TestGetString("Hello World", "Hello", _en);
}

[Fact]
public void GetString_Fi()
{
TestGetString("Hei maailma", "Hello", CultureInfo.GetCultureInfo("fi-FI"));
TestGetString("Hei maailma", "Hello", _fi);
}

[Fact]
public void GetString_Null()
{
this.Invoking(self => self.Translate(null!, CultureInfo.GetCultureInfo("en-US")))
this.Invoking(self => self.Translate(null!, _en))
.Should().Throw<ArgumentException>();
}

[Fact]
public void GetString_Missing()
{
Translate("Missing", CultureInfo.GetCultureInfo("en-US")).ResourceNotFound.Should().BeTrue();
Translate("Missing", _en).ResourceNotFound.Should().BeTrue();
}

[Fact]
Expand All @@ -71,6 +81,17 @@ public void GetString_Typed()
localizer["Hello"].Value.Should().Be("Hello World");
}

[Fact]
public void GetString_Param()
{
TestGetString("Hello Mike", "HelloName", new[] { "Mike" }, CultureInfo.InvariantCulture);
}

[Fact]
public void GetString_Param_Fi()
{
TestGetString("Hei, Mike", "HelloName", new[] { "Mike" }, _fi);
}
}

class MyClass
Expand Down
9 changes: 9 additions & 0 deletions StringLocalizerWithCulture.Tests/TestData/MyClass.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions StringLocalizerWithCulture.Tests/TestData/MyClass.fi.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,7 @@
<data name="Hello" xml:space="preserve">
<value>Hei maailma</value>
</data>
<data name="HelloName" xml:space="preserve">
<value>Hei, {0}</value>
</data>
</root>
3 changes: 3 additions & 0 deletions StringLocalizerWithCulture.Tests/TestData/MyClass.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,7 @@
<data name="Hello" xml:space="preserve">
<value>Hello World</value>
</data>
<data name="HelloName" xml:space="preserve">
<value>Hello {0}</value>
</data>
</root>

0 comments on commit 24b21a1

Please sign in to comment.