Skip to content

Commit

Permalink
Add TestHelper.BackupCultureInfo()
Browse files Browse the repository at this point in the history
  • Loading branch information
y-iihoshi committed Jan 2, 2025
1 parent 1d751eb commit ac3826b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using ThScoreFileConverter.Core.Extensions;
using ThScoreFileConverter.Core.Models;
using ThScoreFileConverter.Core.Tests.UnitTesting;

namespace ThScoreFileConverter.Core.Tests.Extensions;

Expand Down Expand Up @@ -80,7 +81,7 @@ public void ToDisplayShortNameTest()
[TestMethod]
public void ToCharaNameTest()
{
var culture = CultureInfo.CurrentCulture;
using var backup = TestHelper.BackupCultureInfo();

Assert.AreEqual(string.Empty, DayOfWeek.Sunday.ToCharaName());

Expand All @@ -91,14 +92,12 @@ public void ToCharaNameTest()
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
Assert.AreEqual("Reimu", Protagonist.Reimu.ToCharaName());
Assert.AreEqual("Marisa", Protagonist.Marisa.ToCharaName());

CultureInfo.CurrentCulture = culture;
}

[TestMethod]
public void ToCharaFullNameTest()
{
var culture = CultureInfo.CurrentCulture;
using var backup = TestHelper.BackupCultureInfo();

Assert.AreEqual(string.Empty, DayOfWeek.Sunday.ToCharaFullName());

Expand All @@ -109,7 +108,5 @@ public void ToCharaFullNameTest()
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
Assert.AreEqual("Hakurei Reimu", Protagonist.Reimu.ToCharaFullName());
Assert.AreEqual("Kirisame Marisa", Protagonist.Marisa.ToCharaFullName());

CultureInfo.CurrentCulture = culture;
}
}
17 changes: 5 additions & 12 deletions ThScoreFileConverter.Core.Tests/Models/CharacterAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Globalization;
using ThScoreFileConverter.Core.Models;
using ThScoreFileConverter.Core.Resources;
using ThScoreFileConverter.Core.Tests.UnitTesting;

namespace ThScoreFileConverter.Core.Tests.Models;

Expand Down Expand Up @@ -70,7 +71,7 @@ public void CharacterAttributeTestEmptyArgs()
[TestMethod]
public void GetLocalizedNameTest()
{
var culture = CultureInfo.CurrentCulture;
using var backup = TestHelper.BackupCultureInfo();

var name = "Marisa";
var attribute = new CharacterAttribute(name);
Expand All @@ -80,14 +81,12 @@ public void GetLocalizedNameTest()

CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
Assert.AreEqual("Marisa", attribute.GetLocalizedName());

CultureInfo.CurrentCulture = culture;
}

[TestMethod]
public void GetLocalizedNameTestUnregistered()
{
var culture = CultureInfo.CurrentCulture;
using var backup = TestHelper.BackupCultureInfo();

var name = "Rinnosuke";
var attribute = new CharacterAttribute(name);
Expand All @@ -97,14 +96,12 @@ public void GetLocalizedNameTestUnregistered()

CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
Assert.AreEqual("Rinnosuke", attribute.GetLocalizedName());

CultureInfo.CurrentCulture = culture;
}

[TestMethod]
public void GetLocalizedFullNameTest()
{
var culture = CultureInfo.CurrentCulture;
using var backup = TestHelper.BackupCultureInfo();

var name = "Marisa";
var attribute = new CharacterAttribute(name);
Expand All @@ -114,14 +111,12 @@ public void GetLocalizedFullNameTest()

CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
Assert.AreEqual("Kirisame Marisa", attribute.GetLocalizedFullName());

CultureInfo.CurrentCulture = culture;
}

[TestMethod]
public void GetLocalizedFullNameTestUnregistered()
{
var culture = CultureInfo.CurrentCulture;
using var backup = TestHelper.BackupCultureInfo();

var name = "Rinnosuke";
var attribute = new CharacterAttribute(name);
Expand All @@ -131,7 +126,5 @@ public void GetLocalizedFullNameTestUnregistered()

CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
Assert.AreEqual("RinnosukeFullName", attribute.GetLocalizedFullName());

CultureInfo.CurrentCulture = culture;
}
}
24 changes: 24 additions & 0 deletions ThScoreFileConverter.Core.Tests/UnitTesting/TestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace ThScoreFileConverter.Core.Tests.UnitTesting;
Expand All @@ -18,4 +19,27 @@ public static IEnumerable<object[]> GetInvalidEnumerators<TEnum>()
yield return new object[] { values.Min() - 1 };
yield return new object[] { values.Max() + 1 };
}

public static IDisposable BackupCultureInfo()
{
return new CultureInfoDisposable();
}

private sealed class CultureInfoDisposable : IDisposable
{
private readonly CultureInfo cultureInfo;
private readonly CultureInfo uiCultureInfo;

public CultureInfoDisposable()
{
this.cultureInfo = CultureInfo.CurrentCulture;
this.uiCultureInfo = CultureInfo.CurrentUICulture;
}

public void Dispose()
{
CultureInfo.CurrentCulture = this.cultureInfo;
CultureInfo.CurrentUICulture = this.uiCultureInfo;
}
}
}
9 changes: 3 additions & 6 deletions ThScoreFileConverter.Tests/Models/Th095/CardReplacerTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using ThScoreFileConverter.Core.Tests.UnitTesting;
using ThScoreFileConverter.Models.Th095;

namespace ThScoreFileConverter.Tests.Models.Th095;
Expand Down Expand Up @@ -28,14 +29,12 @@ public void CardReplacerTestEmpty()
[TestMethod]
public void ReplaceTestEnemy()
{
var culture = CultureInfo.CurrentCulture;
using var backup = TestHelper.BackupCultureInfo();
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP");

var replacer = new CardReplacer(Scores, false);
Assert.AreEqual("蓬莱山 輝夜", replacer.Replace("%T95CARD961"));
Assert.AreEqual("八意 永琳", replacer.Replace("%T95CARD971"));

CultureInfo.CurrentCulture = culture;
}

[TestMethod]
Expand All @@ -49,14 +48,12 @@ public void ReplaceTestName()
[TestMethod]
public void ReplaceTestHiddenEnemy()
{
var culture = CultureInfo.CurrentCulture;
using var backup = TestHelper.BackupCultureInfo();
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP");

var replacer = new CardReplacer(Scores, true);
Assert.AreEqual("蓬莱山 輝夜", replacer.Replace("%T95CARD961"));
Assert.AreEqual("??????????", replacer.Replace("%T95CARD971"));

CultureInfo.CurrentCulture = culture;
}

[TestMethod]
Expand Down
9 changes: 3 additions & 6 deletions ThScoreFileConverter.Tests/Models/Th125/CardReplacerTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using ThScoreFileConverter.Core.Tests.UnitTesting;
using ThScoreFileConverter.Models.Th125;

namespace ThScoreFileConverter.Tests.Models.Th125;
Expand Down Expand Up @@ -28,14 +29,12 @@ public void CardReplacerTestEmpty()
[TestMethod]
public void ReplaceTestEnemy()
{
var culture = CultureInfo.CurrentCulture;
using var backup = TestHelper.BackupCultureInfo();
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP");

var replacer = new CardReplacer(Scores, false);
Assert.AreEqual("古明地 こいし", replacer.Replace("%T125CARD961"));
Assert.AreEqual("古明地 さとり", replacer.Replace("%T125CARD971"));

CultureInfo.CurrentCulture = culture;
}

[TestMethod]
Expand All @@ -49,14 +48,12 @@ public void ReplaceTestName()
[TestMethod]
public void ReplaceTestHiddenEnemy()
{
var culture = CultureInfo.CurrentCulture;
using var backup = TestHelper.BackupCultureInfo();
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP");

var replacer = new CardReplacer(Scores, true);
Assert.AreEqual("??????????", replacer.Replace("%T125CARD961"));
Assert.AreEqual("古明地 さとり", replacer.Replace("%T125CARD971"));

CultureInfo.CurrentCulture = culture;
}

[TestMethod]
Expand Down
13 changes: 4 additions & 9 deletions ThScoreFileConverter.Tests/Models/Th143/CardReplacerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using NSubstitute;
using ThScoreFileConverter.Core.Models.Th143;
using ThScoreFileConverter.Core.Tests.UnitTesting;
using ThScoreFileConverter.Models.Th143;

namespace ThScoreFileConverter.Tests.Models.Th143;
Expand Down Expand Up @@ -30,14 +31,12 @@ public void CardReplacerTestEmpty()
[TestMethod]
public void ReplaceTestEnemy()
{
var culture = CultureInfo.CurrentCulture;
using var backup = TestHelper.BackupCultureInfo();
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP");

var replacer = new CardReplacer(Scores, false);
Assert.AreEqual("レミリア・スカーレット", replacer.Replace("%T143CARDL41"));
Assert.AreEqual("八雲 紫", replacer.Replace("%T143CARDL51"));

CultureInfo.CurrentCulture = culture;
}

[TestMethod]
Expand All @@ -51,14 +50,12 @@ public void ReplaceTestName()
[TestMethod]
public void ReplaceTestHiddenEnemy()
{
var culture = CultureInfo.CurrentCulture;
using var backup = TestHelper.BackupCultureInfo();
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP");

var replacer = new CardReplacer(Scores, true);
Assert.AreEqual("レミリア・スカーレット", replacer.Replace("%T143CARDL41"));
Assert.AreEqual("??????????", replacer.Replace("%T143CARDL51"));

CultureInfo.CurrentCulture = culture;
}

[TestMethod]
Expand All @@ -72,14 +69,12 @@ public void ReplaceTestHiddenName()
[TestMethod]
public void ReplaceTestScene10()
{
var culture = CultureInfo.CurrentCulture;
using var backup = TestHelper.BackupCultureInfo();
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP");

var replacer = new CardReplacer(Scores, false);
Assert.AreEqual("八雲 紫", replacer.Replace("%T143CARDL01"));
Assert.AreEqual("「運鈍根の捕物帖」", replacer.Replace("%T143CARDL02"));

CultureInfo.CurrentCulture = culture;
}

[TestMethod]
Expand Down
9 changes: 3 additions & 6 deletions ThScoreFileConverter.Tests/Models/Th165/CardReplacerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Immutable;
using System.Globalization;
using NSubstitute;
using ThScoreFileConverter.Core.Tests.UnitTesting;
using ThScoreFileConverter.Models.Th165;

namespace ThScoreFileConverter.Tests.Models.Th165;
Expand Down Expand Up @@ -36,14 +37,12 @@ public void CardReplacerTestEmpty()
[TestMethod]
public void ReplaceTestEnemy()
{
var culture = CultureInfo.CurrentCulture;
using var backup = TestHelper.BackupCultureInfo();
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP");

var replacer = new CardReplacer(Scores, false);
Assert.AreEqual("レミリア・スカーレット &amp; フランドール・スカーレット", replacer.Replace("%T165CARDN111"));
Assert.AreEqual("聖 白蓮 &amp; 豊聡耳 神子", replacer.Replace("%T165CARDN121"));

CultureInfo.CurrentCulture = culture;
}

[TestMethod]
Expand All @@ -57,14 +56,12 @@ public void ReplaceTestName()
[TestMethod]
public void ReplaceTestHiddenEnemy()
{
var culture = CultureInfo.CurrentCulture;
using var backup = TestHelper.BackupCultureInfo();
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP");

var replacer = new CardReplacer(Scores, true);
Assert.AreEqual("レミリア・スカーレット &amp; フランドール・スカーレット", replacer.Replace("%T165CARDN111"));
Assert.AreEqual("??????????", replacer.Replace("%T165CARDN121"));

CultureInfo.CurrentCulture = culture;
}

[TestMethod]
Expand Down

0 comments on commit ac3826b

Please sign in to comment.