diff --git a/test/OrchardCore.Tests/Localization/CultureDictionaryTests.cs b/test/OrchardCore.Tests/Localization/CultureDictionaryTests.cs index bc73b6adf3f..1103cd082e7 100644 --- a/test/OrchardCore.Tests/Localization/CultureDictionaryTests.cs +++ b/test/OrchardCore.Tests/Localization/CultureDictionaryTests.cs @@ -7,13 +7,10 @@ namespace OrchardCore.Tests.Localization { public class CultureDictionaryTests { - private static PluralizationRuleDelegate _arPluralRule = n => (n == 0 ? 0 : n == 1 ? 1 : n == 2 ? 2 : n % 100 >= 3 && n % 100 <= 10 ? 3 : n % 100 >= 11 ? 4 : 5); - private static PluralizationRuleDelegate _csPluralRule = n => ((n == 1) ? 0 : (n >= 2 && n <= 4) ? 1 : 2); - [Fact] public void MergeAddsRecordToEmptyDictionary() { - var dictionary = new CultureDictionary("cs", _csPluralRule); + var dictionary = new CultureDictionary("cs", PluralizationRule.Czech); var record = new CultureDictionaryRecord("ball", "míč", "míče", "míčů"); dictionary.MergeTranslations(new[] { record }); @@ -24,7 +21,7 @@ public void MergeAddsRecordToEmptyDictionary() [Fact] public void MergeOverwritesTranslationsForSameKeys() { - var dictionary = new CultureDictionary("cs", _csPluralRule); + var dictionary = new CultureDictionary("cs", PluralizationRule.Czech); var record = new CultureDictionaryRecord("ball", "míč", "míče", "míčů"); var record2 = new CultureDictionaryRecord("ball", "balón", "balóny", "balónů"); @@ -37,7 +34,7 @@ public void MergeOverwritesTranslationsForSameKeys() [Fact] public void IndexerReturnNullIfKeyDoesntExist() { - var dictionary = new CultureDictionary("cs", _csPluralRule); + var dictionary = new CultureDictionary("cs", PluralizationRule.Czech); var key = new CultureDictionaryRecordKey("ball"); var translation = dictionary[key]; @@ -48,7 +45,7 @@ public void IndexerReturnNullIfKeyDoesntExist() public void IndexerThrowsPluralFormNotFoundExceptionIfSpecifiedPluralFormDoesntExist() { // Arrange - var dictionary = new CultureDictionary("cs", _csPluralRule); + var dictionary = new CultureDictionary("cs", PluralizationRule.Czech); var record = new CultureDictionaryRecord("ball", "míč", "míče"); dictionary.MergeTranslations(new[] { record }); @@ -64,7 +61,7 @@ public void IndexerThrowsPluralFormNotFoundExceptionIfSpecifiedPluralFormDoesntE public void EnumerateCultureDictionary() { // Arrange - var dictionary = new CultureDictionary("ar", _arPluralRule); + var dictionary = new CultureDictionary("ar", PluralizationRule.Arabic); dictionary.MergeTranslations(new List { new CultureDictionaryRecord("Hello", "مرحبا"), diff --git a/test/OrchardCore.Tests/Localization/CultureScopeTests.cs b/test/OrchardCore.Tests/Localization/CultureScopeTests.cs index 36844ed8432..49febe4b2ae 100644 --- a/test/OrchardCore.Tests/Localization/CultureScopeTests.cs +++ b/test/OrchardCore.Tests/Localization/CultureScopeTests.cs @@ -72,6 +72,7 @@ await Assert.ThrowsAsync(() => throw new Exception("Something goes wrong!!"); } }); + Assert.Equal(culture, CultureInfo.CurrentCulture); Assert.Equal(uiCulture, CultureInfo.CurrentUICulture); } diff --git a/test/OrchardCore.Tests/Localization/LocalizationManagerTests.cs b/test/OrchardCore.Tests/Localization/LocalizationManagerTests.cs index 0e259693921..519dfe34b09 100644 --- a/test/OrchardCore.Tests/Localization/LocalizationManagerTests.cs +++ b/test/OrchardCore.Tests/Localization/LocalizationManagerTests.cs @@ -8,16 +8,16 @@ namespace OrchardCore.Tests.Localization { public class LocalizationManagerTests { - private PluralizationRuleDelegate _csPluralRule = n => ((n == 1) ? 0 : (n >= 2 && n <= 4) ? 1 : 2); - private Mock _pluralRuleProvider; - private Mock _translationProvider; - private IMemoryCache _memoryCache; + private readonly Mock _pluralRuleProvider; + private readonly Mock _translationProvider; + private readonly IMemoryCache _memoryCache; public LocalizationManagerTests() { + var csPluralRule = PluralizationRule.Czech; _pluralRuleProvider = new Mock(); _pluralRuleProvider.SetupGet(o => o.Order).Returns(0); - _pluralRuleProvider.Setup(o => o.TryGetRule(It.Is(culture => culture.Name == "cs"), out _csPluralRule)).Returns(true); + _pluralRuleProvider.Setup(o => o.TryGetRule(It.Is(culture => culture.Name == "cs"), out csPluralRule)).Returns(true); _translationProvider = new Mock(); _memoryCache = new MemoryCache(new MemoryCacheOptions()); @@ -30,12 +30,13 @@ public void GetDictionaryReturnsDictionaryWithPluralRuleAndCultureIfNoTranslatio It.Is(culture => culture == "cs"), It.IsAny()) ); + var manager = new LocalizationManager(new[] { _pluralRuleProvider.Object }, new[] { _translationProvider.Object }, _memoryCache); var dictionary = manager.GetDictionary(new CultureInfo("cs")); Assert.Equal("cs", dictionary.CultureName); - Assert.Equal(_csPluralRule, dictionary.PluralRule); + Assert.Equal(PluralizationRule.Czech, dictionary.PluralRule); } [Fact] @@ -45,6 +46,7 @@ public void GetDictionaryReturnsDictionaryWithTranslationsFromProvider() _translationProvider .Setup(o => o.LoadTranslations(It.Is(culture => culture == "cs"), It.IsAny())) .Callback((culture, dictioanry) => dictioanry.MergeTranslations(new[] { dictionaryRecord })); + var manager = new LocalizationManager(new[] { _pluralRuleProvider.Object }, new[] { _translationProvider.Object }, _memoryCache); var dictionary = manager.GetDictionary(new CultureInfo("cs")); @@ -68,6 +70,7 @@ public void GetDictionarySelectsPluralRuleFromProviderWithHigherPriority() It.Is(culture => culture == "cs"), It.IsAny()) ); + var manager = new LocalizationManager(new[] { _pluralRuleProvider.Object, highPriorityRuleProvider.Object }, new[] { _translationProvider.Object }, _memoryCache); var dictionary = manager.GetDictionary(new CultureInfo("cs")); diff --git a/test/OrchardCore.Tests/Localization/NullStringLocalizerTests.cs b/test/OrchardCore.Tests/Localization/NullStringLocalizerTests.cs index 3e85cf9f6b4..5e898fea9a0 100644 --- a/test/OrchardCore.Tests/Localization/NullStringLocalizerTests.cs +++ b/test/OrchardCore.Tests/Localization/NullStringLocalizerTests.cs @@ -24,11 +24,9 @@ public void HtmlNullLocalizerSupportsPlural(string expected, int count, string s { var localizer = new NullHtmlLocalizerFactory().Create(typeof(object)); - using (var writer = new StringWriter()) - { - localizer.Plural(count, singular, plural, arguments).WriteTo(writer, HtmlEncoder.Default); - Assert.Equal(expected, writer.ToString()); - } + using var writer = new StringWriter(); + localizer.Plural(count, singular, plural, arguments).WriteTo(writer, HtmlEncoder.Default); + Assert.Equal(expected, writer.ToString()); } [Theory] diff --git a/test/OrchardCore.Tests/Localization/PluralizationRule.cs b/test/OrchardCore.Tests/Localization/PluralizationRule.cs new file mode 100644 index 00000000000..e120ccd4e5b --- /dev/null +++ b/test/OrchardCore.Tests/Localization/PluralizationRule.cs @@ -0,0 +1,12 @@ +using OrchardCore.Localization; + +namespace OrchardCore.Tests.Localization; + +public class PluralizationRule +{ + public static readonly PluralizationRuleDelegate Czech = n => ((n == 1) ? 0 : (n >= 2 && n <= 4) ? 1 : 2); + + public static readonly PluralizationRuleDelegate English = n => (n == 1) ? 0 : 1; + + public static readonly PluralizationRuleDelegate Arabic = n => (n == 0 ? 0 : n == 1 ? 1 : n == 2 ? 2 : n % 100 >= 3 && n % 100 <= 10 ? 3 : n % 100 >= 11 ? 4 : 5); +} diff --git a/test/OrchardCore.Tests/Localization/PoParserTests.cs b/test/OrchardCore.Tests/Localization/PoParserTests.cs index 1467a17c5ea..51af7684b21 100644 --- a/test/OrchardCore.Tests/Localization/PoParserTests.cs +++ b/test/OrchardCore.Tests/Localization/PoParserTests.cs @@ -53,8 +53,8 @@ public void ParseIgnoresPoeditHeader() // msgstr "Error desconegut del sistema" var entries = ParseText("PoeditHeader"); - Assert.True(entries.Count() == 1); - Assert.True(entries[0].Translations.Count() == 1); + Assert.True(entries.Length == 1); + Assert.True(entries[0].Translations.Length == 1); } [Fact] diff --git a/test/OrchardCore.Tests/Localization/PortableObjectStringLocalizerTests.cs b/test/OrchardCore.Tests/Localization/PortableObjectStringLocalizerTests.cs index fbbd6583634..6b3b30928b7 100644 --- a/test/OrchardCore.Tests/Localization/PortableObjectStringLocalizerTests.cs +++ b/test/OrchardCore.Tests/Localization/PortableObjectStringLocalizerTests.cs @@ -21,11 +21,8 @@ namespace OrchardCore.Tests.Localization { public class PortableObjectStringLocalizerTests { - private static PluralizationRuleDelegate _csPluralRule = n => ((n == 1) ? 0 : (n >= 2 && n <= 4) ? 1 : 2); - private static PluralizationRuleDelegate _enPluralRule = n => (n == 1) ? 0 : 1; - private static PluralizationRuleDelegate _arPluralRule = n => (n == 0 ? 0 : n == 1 ? 1 : n == 2 ? 2 : n % 100 >= 3 && n % 100 <= 10 ? 3 : n % 100 >= 11 ? 4 : 5); - private Mock _localizationManager; - private Mock _logger; + private readonly Mock _localizationManager; + private readonly Mock _logger; public PortableObjectStringLocalizerTests() { @@ -69,7 +66,7 @@ public void LocalizerReturnsOriginalTextIfTranslationsDoesntExistInProvidedDicti [Fact] public void LocalizerReturnsOriginalTextIfDictionaryIsEmpty() { - SetupDictionary("cs", new CultureDictionaryRecord[] { }); + SetupDictionary("cs", Array.Empty()); var localizer = new PortableObjectStringLocalizer(null, _localizationManager.Object, true, _logger.Object); using (CultureScope.Create("cs")) @@ -86,6 +83,7 @@ public void LocalizerFallbacksToParentCultureIfTranslationDoesntExistInSpecificC SetupDictionary("cs", new[] { new CultureDictionaryRecord("ball", "míč", "míče", "míčů") }); + SetupDictionary("cs-CZ", new[] { new CultureDictionaryRecord("car", "auto", "auta", "aut") }); @@ -105,9 +103,11 @@ public void LocalizerReturnsTranslationFromSpecificCultureIfItExists() SetupDictionary("cs", new[] { new CultureDictionaryRecord("ball", "míč", "míče", "míčů") }); + SetupDictionary("cs-CZ", new[] { new CultureDictionaryRecord("ball", "balón", "balóny", "balónů") }); + var localizer = new PortableObjectStringLocalizer(null, _localizationManager.Object, true, _logger.Object); using (CultureScope.Create("cs-CZ")) { @@ -141,6 +141,7 @@ public void LocalizerReturnsTranslationWithoutContextIfTranslationWithContextDoe new CultureDictionaryRecord("ball", "míč", "míče", "míčů"), new CultureDictionaryRecord("ball", "big", new [] { "míček", "míčky", "míčků" }) }); + var localizer = new PortableObjectStringLocalizer("small", _localizationManager.Object, true, _logger.Object); using (CultureScope.Create("cs")) { @@ -156,6 +157,7 @@ public void LocalizerReturnsFormattedTranslation() SetupDictionary("cs", new[] { new CultureDictionaryRecord("The page (ID:{0}) was deleted.", "Stránka (ID:{0}) byla smazána.") }); + var localizer = new PortableObjectStringLocalizer("small", _localizationManager.Object, true, _logger.Object); using (CultureScope.Create("cs")) { @@ -171,6 +173,7 @@ public void HtmlLocalizerDoesNotFormatTwiceIfFormattedTranslationContainsCurlyBr SetupDictionary("cs", new[] { new CultureDictionaryRecord("The page (ID:{0}) was deleted.", "Stránka (ID:{0}) byla smazána.") }); + var localizer = new PortableObjectStringLocalizer("small", _localizationManager.Object, true, _logger.Object); using (CultureScope.Create("cs")) { @@ -220,9 +223,11 @@ public void LocalizerReturnsCorrectTranslationForPluralIfNoPluralFormsSpecified( { // using DefaultPluralRuleProvider to test it returns correct rule TryGetRuleFromDefaultPluralRuleProvider(cultureScope.UICulture, out var rule); + Assert.NotNull(rule); SetupDictionary(culture, new[] { new CultureDictionaryRecord("ball", translations), }, rule); + var localizer = new PortableObjectStringLocalizer(null, _localizationManager.Object, true, _logger.Object); var translation = localizer.Plural(count, "ball", "{0} balls", count); @@ -255,7 +260,8 @@ public void LocalizerReturnsTranslationInCorrectPluralForm(string expected, int [InlineData("5 míčů", 5)] public void LocalizerReturnsOriginalValuesIfTranslationDoesntExistAndMultiplePluraflFormsAreSpecified(string expected, int count) { - SetupDictionary("en", new CultureDictionaryRecord[] { }); + SetupDictionary("en", Array.Empty()); + var localizer = new PortableObjectStringLocalizer(null, _localizationManager.Object, true, _logger.Object); using (CultureScope.Create("en")) { @@ -270,9 +276,11 @@ public void LocalizerReturnsOriginalValuesIfTranslationDoesntExistAndMultiplePlu [InlineData("2 balls", 2)] public void LocalizerReturnsCorrectPluralFormIfMultiplePluraflFormsAreSpecified(string expected, int count) { - SetupDictionary("en", new CultureDictionaryRecord[] { + SetupDictionary("en", new CultureDictionaryRecord[] + { new CultureDictionaryRecord("míč", "ball", "{0} balls") - }, _enPluralRule); + }, PluralizationRule.English); + var localizer = new PortableObjectStringLocalizer(null, _localizationManager.Object, true, _logger.Object); using (CultureScope.Create("en")) { @@ -287,10 +295,13 @@ public void LocalizerReturnsCorrectPluralFormIfMultiplePluraflFormsAreSpecified( [InlineData(true, "hello", "مرحبا")] public void LocalizerFallBackToParentCultureIfFallBackToParentUICulturesIsTrue(bool fallBackToParentCulture, string resourceKey, string expected) { - SetupDictionary("ar", new CultureDictionaryRecord[] { + SetupDictionary("ar", new CultureDictionaryRecord[] + { new CultureDictionaryRecord("hello", "مرحبا") - }, _arPluralRule); - SetupDictionary("ar-YE", new CultureDictionaryRecord[] { }, _arPluralRule); + }, PluralizationRule.Arabic); + + SetupDictionary("ar-YE", Array.Empty(), PluralizationRule.Arabic); + var localizer = new PortableObjectStringLocalizer(null, _localizationManager.Object, fallBackToParentCulture, _logger.Object); using (CultureScope.Create("ar-YE")) { @@ -305,23 +316,26 @@ public void LocalizerFallBackToParentCultureIfFallBackToParentUICulturesIsTrue(b [InlineData(true, new[] { "مدونة", "منتج", "قائمة", "صفحة", "مقالة" })] public void LocalizerReturnsGetAllStrings(bool includeParentCultures, string[] expected) { - SetupDictionary("ar", new CultureDictionaryRecord[] { + SetupDictionary("ar", new CultureDictionaryRecord[] + { new CultureDictionaryRecord("Blog", "مدونة"), new CultureDictionaryRecord("Menu", "قائمة"), new CultureDictionaryRecord("Page", "صفحة"), new CultureDictionaryRecord("Article", "مقالة") - }, _arPluralRule); - SetupDictionary("ar-YE", new CultureDictionaryRecord[] { + }, PluralizationRule.Arabic); + + SetupDictionary("ar-YE", new CultureDictionaryRecord[] + { new CultureDictionaryRecord("Blog", "مدونة"), new CultureDictionaryRecord("Product", "منتج") - }, _arPluralRule); + }, PluralizationRule.Arabic); var localizer = new PortableObjectStringLocalizer(null, _localizationManager.Object, false, _logger.Object); using (CultureScope.Create("ar-YE")) { var translations = localizer.GetAllStrings(includeParentCultures).Select(l => l.Value).ToArray(); - Assert.Equal(expected.Count(), translations.Count()); + Assert.Equal(expected.Length, translations.Length); } } @@ -347,13 +361,15 @@ public void LocalizerWithContextShouldCallGetDictionaryOncePerCulture(string cul // Assert _localizationManager.Verify(lm => lm.GetDictionary(It.IsAny()), Times.Exactly(expectedCalls)); + Assert.Equal("Hello", translation); } + private static bool TryGetRuleFromDefaultPluralRuleProvider(CultureInfo culture, out PluralizationRuleDelegate rule) + => ((IPluralRuleProvider)new DefaultPluralRuleProvider()).TryGetRule(culture, out rule); + private void SetupDictionary(string cultureName, IEnumerable records) - { - SetupDictionary(cultureName, records, _csPluralRule); - } + => SetupDictionary(cultureName, records, PluralizationRule.Czech); private void SetupDictionary(string cultureName, IEnumerable records, PluralizationRuleDelegate pluralRule) { @@ -363,11 +379,6 @@ private void SetupDictionary(string cultureName, IEnumerable o.GetDictionary(It.Is(c => c.Name == cultureName))).Returns(dictionary); } - private bool TryGetRuleFromDefaultPluralRuleProvider(CultureInfo culture, out PluralizationRuleDelegate rule) - { - return ((IPluralRuleProvider)new DefaultPluralRuleProvider()).TryGetRule(culture, out rule); - } - public class PortableObjectLocalizationStartup { public void ConfigureServices(IServiceCollection services) diff --git a/test/OrchardCore.Tests/OrchardCore.Tests.csproj b/test/OrchardCore.Tests/OrchardCore.Tests.csproj index 0ed8c1e2e32..1fb053a61fd 100644 --- a/test/OrchardCore.Tests/OrchardCore.Tests.csproj +++ b/test/OrchardCore.Tests/OrchardCore.Tests.csproj @@ -19,17 +19,6 @@ - - - - - - - - - - - @@ -43,17 +32,7 @@ - - - - - - - - - - - +