diff --git a/BootstrapBlazor.sln b/BootstrapBlazor.sln index 69d6319e5..51c3878a1 100644 --- a/BootstrapBlazor.sln +++ b/BootstrapBlazor.sln @@ -113,6 +113,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "middleware", "middleware", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BootstrapBlazor.Middleware", "src\Middleware\BootstrapBlazor.Middleware\BootstrapBlazor.Middleware.csproj", "{59B49D4C-05FB-4E51-AB40-9498ADC71DD0}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTest.Localization", "test\UnitTest.Localization\UnitTest.Localization.csproj", "{99B55645-0E89-43F8-938F-2304B486AD2A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -171,6 +173,10 @@ Global {59B49D4C-05FB-4E51-AB40-9498ADC71DD0}.Debug|Any CPU.Build.0 = Debug|Any CPU {59B49D4C-05FB-4E51-AB40-9498ADC71DD0}.Release|Any CPU.ActiveCfg = Release|Any CPU {59B49D4C-05FB-4E51-AB40-9498ADC71DD0}.Release|Any CPU.Build.0 = Release|Any CPU + {99B55645-0E89-43F8-938F-2304B486AD2A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {99B55645-0E89-43F8-938F-2304B486AD2A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {99B55645-0E89-43F8-938F-2304B486AD2A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {99B55645-0E89-43F8-938F-2304B486AD2A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -199,6 +205,7 @@ Global {FFFD2EB7-AE88-4DAD-A825-528B2CEFB4B5} = {C8E79F4C-8C55-4E13-96B5-3D2BD6A07B74} {4A7F4513-C515-40DD-A217-169E1CBB2827} = {A2182155-43ED-44C1-BF6F-1B70EBD2DFFE} {59B49D4C-05FB-4E51-AB40-9498ADC71DD0} = {4A7F4513-C515-40DD-A217-169E1CBB2827} + {99B55645-0E89-43F8-938F-2304B486AD2A} = {7C1D79F1-87BC-42C1-BD5A-CDE4044AC1BD} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {0DCB0756-34FA-4FD0-AE1D-D3F08B5B3A6B} diff --git a/BootstrapBlazor.slnf b/BootstrapBlazor.slnf index 7b27a3bda..f0cf3e4cb 100644 --- a/BootstrapBlazor.slnf +++ b/BootstrapBlazor.slnf @@ -5,6 +5,7 @@ "src\\BootstrapBlazor.Server\\BootstrapBlazor.Server.csproj", "src\\BootstrapBlazor.Shared\\BootstrapBlazor.Shared.csproj", "src\\BootstrapBlazor\\BootstrapBlazor.csproj", + "test\\UnitTest.Localization\\UnitTest.Localization.csproj", "test\\UnitTest\\UnitTest.csproj" ] } diff --git a/test/UnitTest.Localization/BootstrapBlazorEnTestBase.cs b/test/UnitTest.Localization/BootstrapBlazorEnTestBase.cs new file mode 100644 index 000000000..ab876bcb8 --- /dev/null +++ b/test/UnitTest.Localization/BootstrapBlazorEnTestBase.cs @@ -0,0 +1,47 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using Bunit; +using Microsoft.Extensions.DependencyInjection; +using UnitTest.Core; +using Xunit; + +namespace UnitTest.Localization +{ + [Collection("BlazorEnTestContext")] + public class BootstrapBlazorEnTestBase + { + protected TestContext Context { get; } + + public BootstrapBlazorEnTestBase() + { + Context = BootstrapBlazorEnTestHost.Instance; + } + } + + [CollectionDefinition("BlazorEnTestContext")] + public class BootstrapBlazorEnTestCollection : ICollectionFixture + { + + } + + public class BootstrapBlazorEnTestHost : BootstrapBlazorTestHost + { + protected override void ConfigureServices(IServiceCollection services) + { + // 支持 微软 resx 格式资源文件 + services.AddLocalization(option => option.ResourcesPath = "Resources"); + services.AddBootstrapBlazor(localizationAction: options => + { + options.ResourceManagerStringLocalizerType = typeof(BootstrapBlazorEnTestHost); + }); + } + + protected override void ConfigureConfigration(IServiceCollection services) + { + // 增加单元测试 appsettings.json 配置文件 + services.AddConfiguration("en-US"); + } + } +} diff --git a/test/UnitTest.Localization/BootstrapBlazorZhTestBase .cs b/test/UnitTest.Localization/BootstrapBlazorZhTestBase .cs new file mode 100644 index 000000000..948a7a291 --- /dev/null +++ b/test/UnitTest.Localization/BootstrapBlazorZhTestBase .cs @@ -0,0 +1,47 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using Bunit; +using Microsoft.Extensions.DependencyInjection; +using UnitTest.Core; +using Xunit; + +namespace UnitTest.Localization +{ + [Collection("BlazorZhTestContext")] + public class BootstrapBlazorZhTestBase + { + protected TestContext Context { get; } + + public BootstrapBlazorZhTestBase() + { + Context = BootstrapBlazorZhTestHost.Instance; + } + } + + [CollectionDefinition("BlazorZhTestContext")] + public class BootstrapBlazorZhTestCollection : ICollectionFixture + { + + } + + public class BootstrapBlazorZhTestHost : BootstrapBlazorTestHost + { + protected override void ConfigureServices(IServiceCollection services) + { + // 支持 微软 resx 格式资源文件 + services.AddLocalization(option => option.ResourcesPath = "Resources"); + services.AddBootstrapBlazor(localizationAction: options => + { + options.ResourceManagerStringLocalizerType = typeof(BootstrapBlazorEnTestHost); + }); + } + + protected override void ConfigureConfigration(IServiceCollection services) + { + // 增加单元测试 appsettings.json 配置文件 + services.AddConfiguration("zh-CN"); + } + } +} diff --git a/test/UnitTest/CollectionOrder/DisplayNameOrder.cs b/test/UnitTest.Localization/CollectionOrder/DisplayNameOrder.cs similarity index 100% rename from test/UnitTest/CollectionOrder/DisplayNameOrder.cs rename to test/UnitTest.Localization/CollectionOrder/DisplayNameOrder.cs diff --git a/test/UnitTest.Localization/Locales/en.json b/test/UnitTest.Localization/Locales/en.json new file mode 100644 index 000000000..2d8bde23d --- /dev/null +++ b/test/UnitTest.Localization/Locales/en.json @@ -0,0 +1,29 @@ +{ + "BootstrapBlazor.Shared.Foo": { + "Name": "Name", + "DateTime": "DateTime", + "Address": "Address", + "Count": "Count", + "Complete": "Complete", + "Education": "Education", + "Hobby": "Hobby", + "Name.Required": "{0} is required.", + "Address.Required": "{0} is required.", + "Education.Required": "{0} is required.", + "Hobby.Required": "{0} is required.", + "Name.PlaceHolder": "required", + "Hobbys": "Swimming,Climb,Shoot,Chess", + "Foo.Name": "Zhangsan {0}", + "Foo.Address": "Lane {0} of Jinshajiang Road, Putuo District, Shanghai", + "Foo.Address2": "Earth, China, Lane {0} of Jinshajiang Road, Putuo District, Shanghai. Here is an example of super long cell", + "Foo.BindValue": "BindValue", + "True": "Pass", + "False": "Failed", + "NullItemText": "Not Set" + }, + "BootstrapBlazor.Shared.EnumEducation": { + "PlaceHolder": "Click to select ...", + "Primary": "Primary", + "Middel": "Middel" + } +} diff --git a/test/UnitTest.Localization/Locales/zh.json b/test/UnitTest.Localization/Locales/zh.json new file mode 100644 index 000000000..2e54bec96 --- /dev/null +++ b/test/UnitTest.Localization/Locales/zh.json @@ -0,0 +1,29 @@ +{ + "BootstrapBlazor.Shared.Foo": { + "Name": "姓名", + "DateTime": "日期", + "Address": "地址", + "Count": "数量", + "Complete": "是/否", + "Education": "学历", + "Hobby": "爱好", + "Name.Required": "{0}是必填项", + "Address.Required": "{0}是必填项", + "Education.Required": "{0}是必选项", + "Hobby.Required": "请选择一种{0}", + "Name.PlaceHolder": "不可为空", + "Hobbys": "游泳,登山,打球,下棋", + "Foo.Name": "张三 {0}", + "Foo.Address": "上海市普陀区金沙江路 {0} 弄", + "Foo.Address2": "地球、中国、上海市普陀区金沙江路 {0} 弄 这里是超长单元格示例", + "Foo.BindValue": "绑定值", + "True": "通过", + "False": "未通过", + "NullItemText": "未设置" + }, + "BootstrapBlazor.Shared.EnumEducation": { + "PlaceHolder": "请选择 ...", + "Primary": "小学", + "Middel": "中学" + } +} diff --git a/test/UnitTest/Localization/LocalizerTest.cs b/test/UnitTest.Localization/LocalizerEnTest.cs similarity index 58% rename from test/UnitTest/Localization/LocalizerTest.cs rename to test/UnitTest.Localization/LocalizerEnTest.cs index b635927b3..9db0e5c5e 100644 --- a/test/UnitTest/Localization/LocalizerTest.cs +++ b/test/UnitTest.Localization/LocalizerEnTest.cs @@ -12,47 +12,6 @@ using Xunit; namespace UnitTest.Localization { - public class LocalizerZhTest : BootstrapBlazorTestBase - { - private IStringLocalizer Localizer { get; } - - public LocalizerZhTest() - { - Localizer = Context.Services.GetRequiredService>(); - } - - [Fact] - public void Foo_Json_Ok() - { - var foo = Foo.Generate(Localizer); - - Assert.Equal("张三 1000", foo.Name); - } - - [Fact] - public void Dummy_Resource_Ok() - { - var val = Utility.GetDisplayName(typeof(Dummy), nameof(Dummy.Name)); - Assert.Equal("姓名", val); - - var model = new Dummy() { Name = "Name", Address = "Address" }; - val = Utility.GetDisplayName(model, nameof(Dummy.Address)); - Assert.Equal("Address1", val); - Assert.Equal("Name", model.Name); - Assert.Equal("Address", model.Address); - } - - class Dummy - { - [Display(Name = "Name1")] - public string? Name { get; set; } - - [Display(Name = "Address1")] - public string? Address { get; set; } - } - } - - #region 英文资源文件 public class LocalizerEnTest : BootstrapBlazorEnTestBase { private IStringLocalizer Localizer { get; } @@ -92,5 +51,4 @@ namespace UnitTest.Localization public string? Address { get; set; } } } - #endregion } diff --git a/test/UnitTest.Localization/LocalizerZhTest.cs b/test/UnitTest.Localization/LocalizerZhTest.cs new file mode 100644 index 000000000..835350b84 --- /dev/null +++ b/test/UnitTest.Localization/LocalizerZhTest.cs @@ -0,0 +1,54 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using BootstrapBlazor.Components; +using BootstrapBlazor.Shared; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Localization; +using System.ComponentModel.DataAnnotations; +using UnitTest.Core; +using Xunit; + +namespace UnitTest.Localization +{ + public class LocalizerZhTest : BootstrapBlazorZhTestBase + { + private IStringLocalizer Localizer { get; } + + public LocalizerZhTest() + { + Localizer = Context.Services.GetRequiredService>(); + } + + [Fact] + public void Foo_Json_Ok() + { + var foo = Foo.Generate(Localizer); + + Assert.Equal("张三 1000", foo.Name); + } + + [Fact] + public void Dummy_Resource_Ok() + { + var val = Utility.GetDisplayName(typeof(Dummy), nameof(Dummy.Name)); + Assert.Equal("姓名", val); + + var model = new Dummy() { Name = "Name", Address = "Address" }; + val = Utility.GetDisplayName(model, nameof(Dummy.Address)); + Assert.Equal("Address1", val); + Assert.Equal("Name", model.Name); + Assert.Equal("Address", model.Address); + } + + class Dummy + { + [Display(Name = "Name1")] + public string? Name { get; set; } + + [Display(Name = "Address1")] + public string? Address { get; set; } + } + } +} diff --git a/test/UnitTest/Resources/Core.BootstrapBlazorTestHost.en-US.resx b/test/UnitTest.Localization/Resources/BootstrapBlazorEnTestHost.en-US.resx similarity index 100% rename from test/UnitTest/Resources/Core.BootstrapBlazorTestHost.en-US.resx rename to test/UnitTest.Localization/Resources/BootstrapBlazorEnTestHost.en-US.resx diff --git a/test/UnitTest/Resources/Core.BootstrapBlazorTestHost.zh-CN.resx b/test/UnitTest.Localization/Resources/BootstrapBlazorEnTestHost.zh-CN.resx similarity index 100% rename from test/UnitTest/Resources/Core.BootstrapBlazorTestHost.zh-CN.resx rename to test/UnitTest.Localization/Resources/BootstrapBlazorEnTestHost.zh-CN.resx diff --git a/test/UnitTest.Localization/UnitTest.Localization.csproj b/test/UnitTest.Localization/UnitTest.Localization.csproj new file mode 100644 index 000000000..0144c6b87 --- /dev/null +++ b/test/UnitTest.Localization/UnitTest.Localization.csproj @@ -0,0 +1,52 @@ + + + + net6.0 + enable + + false + + + + + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/UnitTest/Core/BootstrapBlazorTestBase.cs b/test/UnitTest/Core/BootstrapBlazorTestBase.cs index f3bd6bb95..c47bec8e3 100644 --- a/test/UnitTest/Core/BootstrapBlazorTestBase.cs +++ b/test/UnitTest/Core/BootstrapBlazorTestBase.cs @@ -50,12 +50,7 @@ namespace UnitTest.Core protected virtual void ConfigureServices(IServiceCollection services) { - // 支持 微软 resx 格式资源文件 - services.AddLocalization(option => option.ResourcesPath = "Resources"); - services.AddBootstrapBlazor(localizationAction: options => - { - options.ResourceManagerStringLocalizerType = typeof(BootstrapBlazorTestHost); - }); + services.AddBootstrapBlazor(); } protected virtual void ConfigureConfigration(IServiceCollection services) @@ -70,32 +65,4 @@ namespace UnitTest.Core GC.SuppressFinalize(this); } } - - #region 英文环境 - [Collection("BlazorEnTestContext")] - public class BootstrapBlazorEnTestBase - { - protected TestContext Context { get; } - - public BootstrapBlazorEnTestBase() - { - Context = BootstrapBlazorEnTestHost.Instance; - } - } - - [CollectionDefinition("BlazorEnTestContext")] - public class BootstrapBlazorEnTestCollection : ICollectionFixture - { - - } - - public class BootstrapBlazorEnTestHost : BootstrapBlazorTestHost - { - protected override void ConfigureConfigration(IServiceCollection services) - { - // 增加单元测试 appsettings.json 配置文件 - services.AddConfiguration("en-US"); - } - } - #endregion } diff --git a/test/UnitTest/UnitTest.csproj b/test/UnitTest/UnitTest.csproj index 37297e8a2..93cd6c0b1 100644 --- a/test/UnitTest/UnitTest.csproj +++ b/test/UnitTest/UnitTest.csproj @@ -32,18 +32,4 @@ - - - True - True - BootstrapBlazorTestHost.zh.resx - - - - - - - - -