Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for the external localization #14465

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Volo.Abp.Localization.TestResources.Base.Validation;
using Volo.Abp.Localization.TestResources.External;
using Volo.Abp.Localization.TestResources.Source;
using Volo.Abp.Modularity;
using Volo.Abp.VirtualFileSystem;
Expand Down Expand Up @@ -34,6 +35,8 @@ public override void ConfigureServices(ServiceConfigurationContext context)
options.Resources
.Get<LocalizationTestResource>()
.AddVirtualJson("/Volo/Abp/Localization/TestResources/SourceExt");

options.GlobalContributors.Add<TestExternalLocalizationContributor>();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using Shouldly;
using Volo.Abp.Localization.TestResources.External;
using Volo.Abp.Localization.TestResources.Source;
using Volo.Abp.Testing;
using Xunit;
Expand Down Expand Up @@ -367,4 +368,11 @@ public async Task Should_Get_Supported_Cultures()
var cultures = await _localizer.GetSupportedCulturesAsync();
cultures.Count().ShouldBeGreaterThan(0);
}

[Fact]
public void Should_Get_Localized_Text_From_External()
{
var externalLocalizer = _localizerFactory.CreateByResourceName(TestExternalLocalizationStore.TestExternalResourceNames.ExternalResource1);
externalLocalizer["Car"].Value.ShouldBe("Car");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Localization;
using Shouldly;
using Volo.Abp.DynamicProxy;
using Volo.Abp.Localization.TestResources.External;
using Volo.Abp.Testing;
using Xunit;

Expand Down Expand Up @@ -59,4 +60,34 @@ await Assert.ThrowsAsync<AbpException>(
async () => await _factory.CreateByResourceNameAsync("UnknownResourceName")
);
}

[Fact]
public void Should_Create_External_Resource_By_Name()
{
using (CultureHelper.Use("en"))
{
var localizer = _factory.CreateByResourceNameOrNull(TestExternalLocalizationStore.TestExternalResourceNames.ExternalResource1);
localizer.ShouldNotBeNull();
localizer["CarPlural"].Value.ShouldBe("CarPlural");

var localizer2 = _factory.CreateByResourceNameOrNull(TestExternalLocalizationStore.TestExternalResourceNames.ExternalResource2);
localizer2.ShouldNotBeNull();
localizer2["CarPlural"].Value.ShouldBe("CarPlural");
}
}

[Fact]
public async Task Should_Create_External_Resource_By_Name_Async()
{
using (CultureHelper.Use("en"))
{
var localizer = await _factory.CreateByResourceNameOrNullAsync(TestExternalLocalizationStore.TestExternalResourceNames.ExternalResource1);
localizer.ShouldNotBeNull();
localizer["CarPlural"].Value.ShouldBe("CarPlural");

var localizer2 = await _factory.CreateByResourceNameOrNullAsync(TestExternalLocalizationStore.TestExternalResourceNames.ExternalResource2);
localizer2.ShouldNotBeNull();
localizer2["CarPlural"].Value.ShouldBe("CarPlural");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;

namespace Volo.Abp.Localization.TestResources.External;

public class TestExternalLocalizationContributor : ILocalizationResourceContributor
{
public bool IsDynamic => true;

private string ResourceName { get; set; }

public void Initialize(LocalizationResourceInitializationContext context)
{
ResourceName = context.Resource.ResourceName;
}

public LocalizedString GetOrNull(string cultureName, string name)
{
switch (ResourceName)
{
case TestExternalLocalizationStore.TestExternalResourceNames.ExternalResource1:
case TestExternalLocalizationStore.TestExternalResourceNames.ExternalResource2:
return new LocalizedString(name, name );
default:
return null;
}
}

public void Fill(string cultureName, Dictionary<string, LocalizedString> dictionary)
{
}

public Task FillAsync(string cultureName, Dictionary<string, LocalizedString> dictionary)
{
return Task.CompletedTask;
}

public Task<IEnumerable<string>> GetSupportedCulturesAsync()
{
return Task.FromResult(new List<string>().AsEnumerable());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Localization.External;

namespace Volo.Abp.Localization.TestResources.External;

public class TestExternalLocalizationStore : IExternalLocalizationStore, ITransientDependency
{
private readonly IDictionary<string, LocalizationResourceBase> _resources = new LocalizationResourceDictionary();

public LocalizationResourceBase GetResourceOrNull(string resourceName)
{
return GetOrCreateResource(resourceName);
}

public Task<LocalizationResourceBase> GetResourceOrNullAsync(string resourceName)
{
return Task.FromResult(GetOrCreateResource(resourceName));
}

public Task<string[]> GetResourceNamesAsync()
{
return Task.FromResult(new []{TestExternalResourceNames.ExternalResource1, TestExternalResourceNames.ExternalResource2});
}

public async Task<LocalizationResourceBase[]> GetResourcesAsync()
{
var resourceNames = await GetResourceNamesAsync();
return resourceNames.Select(GetResourceOrNull).ToArray();
}

private LocalizationResourceBase GetOrCreateResource(string resourceName)
{
if(resourceName != TestExternalResourceNames.ExternalResource1 && resourceName != TestExternalResourceNames.ExternalResource2)
{
return null;
}

return _resources.GetOrAdd(resourceName, name => new NonTypedLocalizationResource(name));
}

public class TestExternalResourceNames
{
public const string ExternalResource1 = "TestExternalResource1";

public const string ExternalResource2 = "TestExternalResource2";
}
}