test: improve code coverage for CacheManager (#422)

* refactor: remove question

* refactor: add SearchLocation parameter value

* chore: add NullName/EmptyName key in localization file

* test: rename GetJsonStringFormAssembly unit test

* refactor: remove redundant properties

* test: add unit test cover Null/Empty value in localization file
This commit is contained in:
Argo Zhang
2023-01-30 03:22:57 +08:00
committed by GitHub
parent 0d72fa8ae1
commit 6f986d3921
3 changed files with 20 additions and 14 deletions

View File

@@ -216,9 +216,9 @@ internal class CacheManager : ICacheManager
return Instance.GetOrCreate(typeKey, entry =>
{
var sections = Instance.GetOrCreate(key, entry => option.GetJsonStringFromAssembly(assembly, cultureName));
return sections?.FirstOrDefault(kv => typeName.Equals(kv.Key, StringComparison.OrdinalIgnoreCase))?
return sections.FirstOrDefault(kv => typeName.Equals(kv.Key, StringComparison.OrdinalIgnoreCase))?
.GetChildren()
.SelectMany(kv => new[] { new LocalizedString(kv.Key, kv.Value ?? kv.Key) });
.SelectMany(kv => new[] { new LocalizedString(kv.Key, kv.Value!, false, typeName) });
});
}
}

View File

@@ -310,14 +310,11 @@ public class UtilityTest : BootstrapBlazorTestBase
}
[Fact]
public void GetJsonStringFromAssembly_Ok()
public void GetJsonStringByTypeName_Ok()
{
// improve code coverage
var option = Context.Services.GetRequiredService<IOptions<JsonLocalizationOptions>>().Value;
var sections = Utility.GetJsonStringByTypeName(option, this.GetType().Assembly, "UnitTest.Utils.UtilityTest+Cat", null, true);
// 加载 UnitTest.Locals.en-US.json
// 加载 BootstrapBlazor.Locals.en.json
Assert.NotEmpty(sections);
Utility.GetJsonStringByTypeName(option, this.GetType().Assembly, "UnitTest.Utils.UtilityTest+Cat", null, true);
// dynamic
var dynamicType = EmitHelper.CreateTypeByName("test_type", new MockTableColumn[] { new("Name", typeof(string)) });
@@ -545,7 +542,18 @@ public class UtilityTest : BootstrapBlazorTestBase
}
};
var localizedStrings = Utility.GetJsonStringByTypeName(option, this.GetType().Assembly, "BootstrapBlazor.Shared.Foo", "zh-CN", true);
Assert.Equal("Test-Name", localizedStrings.First(i => i.Name == "Name").Value);
var localizer = localizedStrings.First(i => i.Name == "Name");
Assert.Equal("Test-Name", localizer.Value);
Assert.False(localizer.ResourceNotFound);
// Value is null
localizer = localizedStrings.First(i => i.Name == "NullName");
Assert.Equal("", localizer.Value);
Assert.False(localizer.ResourceNotFound);
localizer = localizedStrings.First(i => i.Name == "EmptyName");
Assert.Equal("", localizer.Value);
Assert.False(localizer.ResourceNotFound);
}
[Fact]
@@ -616,14 +624,10 @@ public class UtilityTest : BootstrapBlazorTestBase
private class Cat
{
public Foo Foo { get; set; } = new Foo();
[PlaceHolder("Test-PlaceHolder")]
[Description("Cat-Desc")]
public string? Name { get; set; }
public string? PlaceHolder { get; set; }
[CatKey]
public int Id { get; set; }

View File

@@ -1,5 +1,7 @@
{
"BootstrapBlazor.Shared.Foo": {
"Name": "Test-Name"
"Name": "Test-Name",
"NullName": null,
"EmptyName": ""
}
}