!2130 test(#I4JTGK): localization unit test into localization test project

* test: 拆分多语言单元测试到独立工程中
pull/2131/head^2
Argo 3 years ago
parent 2706bdd119
commit 7f0005c413

@ -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}

@ -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"
]
}

@ -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<BootstrapBlazorEnTestHost>
{
}
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");
}
}
}

@ -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<BootstrapBlazorZhTestHost>
{
}
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");
}
}
}

@ -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"
}
}

@ -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": "中学"
}
}

@ -12,47 +12,6 @@ using Xunit;
namespace UnitTest.Localization
{
public class LocalizerZhTest : BootstrapBlazorTestBase
{
private IStringLocalizer<Foo> Localizer { get; }
public LocalizerZhTest()
{
Localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
}
[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<Foo> Localizer { get; }
@ -92,5 +51,4 @@ namespace UnitTest.Localization
public string? Address { get; set; }
}
}
#endregion
}

@ -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<Foo> Localizer { get; }
public LocalizerZhTest()
{
Localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
}
[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; }
}
}
}

@ -0,0 +1,52 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\UnitTest\Core\BootstrapBlazorTestBase.cs" Link="Mics\BootstrapBlazorTestBase.cs" />
<Compile Include="..\UnitTest\Core\TestBase.cs" Link="Mics\TestBase.cs" />
<Compile Include="..\UnitTest\Extensions\IServiceCollectionExtensions.cs" Link="Mics\IServiceCollectionExtensions.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="bunit" Version="1.3.42" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Content Remove="Locales\*.json" />
<EmbeddedResource Include="Locales\*.json" />
</ItemGroup>
<ItemGroup>
<None Remove="Locales\en.json" />
<None Remove="Locales\zh.json" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\src\BootstrapBlazor.Shared\Data\Foo.cs" Link="Mics\Foo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\BootstrapBlazor\BootstrapBlazor.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Mics\" />
</ItemGroup>
</Project>

@ -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<BootstrapBlazorEnTestHost>
{
}
public class BootstrapBlazorEnTestHost : BootstrapBlazorTestHost
{
protected override void ConfigureConfigration(IServiceCollection services)
{
// 增加单元测试 appsettings.json 配置文件
services.AddConfiguration("en-US");
}
}
#endregion
}

@ -32,18 +32,4 @@
<ProjectReference Include="..\..\src\BootstrapBlazor\BootstrapBlazor.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="Locales\BootstrapBlazorTestHost.zh.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>BootstrapBlazorTestHost.zh.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Resources\Core.BootstrapBlazorTestHost.zh-CN.resx">
<CustomToolNamespace></CustomToolNamespace>
</EmbeddedResource>
</ItemGroup>
</Project>

Loading…
Cancel
Save