public void ResourceManagerStringLocalizer_GetAllStrings_MissingResourceThrows(bool includeParentCultures) { // Arrange var resourceNamesCache = new ResourceNamesCache(); var baseName = "testington"; var resourceAssembly = new TestAssemblyWrapper(); resourceAssembly.HasResources = false; var resourceManager = new TestResourceManager(baseName, resourceAssembly); var logger = Logger; var localizer = new ResourceManagerStringLocalizer( resourceManager, resourceAssembly.Assembly, baseName, resourceNamesCache, logger); // Act & Assert var exception = Assert.Throws <MissingManifestResourceException>(() => { // We have to access the result so it evaluates. #pragma warning disable CA1304 // Specify CultureInfo localizer.GetAllStrings(includeParentCultures).ToArray(); #pragma warning restore CA1304 // Specify CultureInfo }); var expectedTries = includeParentCultures ? 3 : 1; var expected = includeParentCultures ? "No manifests exist for the current culture." : $"The manifest 'testington.{CultureInfo.CurrentCulture}.resources' was not found."; Assert.Equal(expected, exception.Message); Assert.Equal(expectedTries, resourceAssembly.ManifestResourceStreamCallCount); }
public void EnumeratorCacheIsScopedByAssembly() { // Arrange var resourceNamesCache = new ResourceNamesCache(); var baseName = "test"; var resourceAssembly1 = new TestAssemblyWrapper(typeof(ResourceManagerStringLocalizerTest)); var resourceAssembly2 = new TestAssemblyWrapper(typeof(ResourceManagerStringLocalizer)); var resourceManager1 = new TestResourceManager(baseName, resourceAssembly1); var resourceManager2 = new TestResourceManager(baseName, resourceAssembly2); var resourceStreamManager1 = new TestResourceStringProvider(resourceNamesCache, resourceManager1, resourceAssembly1.Assembly, baseName); var resourceStreamManager2 = new TestResourceStringProvider(resourceNamesCache, resourceManager2, resourceAssembly2.Assembly, baseName); var logger = Logger; var localizer1 = new ResourceManagerStringLocalizer( resourceManager1, resourceStreamManager1, baseName, resourceNamesCache, logger); var localizer2 = new ResourceManagerStringLocalizer( resourceManager2, resourceStreamManager2, baseName, resourceNamesCache, logger); // Act localizer1.GetAllStrings().ToList(); localizer2.GetAllStrings().ToList(); // Assert var expectedCallCount = GetCultureInfoDepth(CultureInfo.CurrentUICulture); Assert.Equal(expectedCallCount, resourceAssembly1.ManifestResourceStreamCallCount); Assert.Equal(expectedCallCount, resourceAssembly2.ManifestResourceStreamCallCount); }
public void ResourceManagerStringLocalizer_GetAllStrings_ReturnsExpectedValue(bool includeParentCultures) { // Arrange var baseName = "test"; var resourceNamesCache = new ResourceNamesCache(); var resourceAssembly = new TestAssemblyWrapper(); var resourceManager = new TestResourceManager(baseName, resourceAssembly); var resourceStreamManager = new TestResourceStringProvider(resourceNamesCache, resourceManager, resourceAssembly.Assembly, baseName); var logger = Logger; var localizer = new ResourceManagerStringLocalizer( resourceManager, resourceStreamManager, baseName, resourceNamesCache, logger); // Act // We have to access the result so it evaluates. #pragma warning disable CA1304 // Specify CultureInfo var strings = localizer.GetAllStrings(includeParentCultures).ToList(); #pragma warning restore CA1304 // Specify CultureInfo // Assert var value = Assert.Single(strings); Assert.Equal("TestName", value.Value); }
public void GetString_LogsLocationSearched() { // Arrange var baseName = "Resources.TestResource"; var resourceNamesCache = new ResourceNamesCache(); var resourceAssembly = new TestAssemblyWrapper(); var resourceManager = new TestResourceManager(baseName, resourceAssembly); var resourceStreamManager = new TestResourceStringProvider(resourceNamesCache, resourceManager, resourceAssembly.Assembly, baseName); var logger = Logger; var localizer = new ResourceManagerStringLocalizer( resourceManager, resourceStreamManager, baseName, resourceNamesCache, logger); // Act var value = localizer["a key!"]; // Assert var write = Assert.Single(Sink.Writes); Assert.Equal("ResourceManagerStringLocalizer searched for 'a key!' in 'Resources.TestResource' with culture 'en-US'.", write.State.ToString()); }
public void EnumeratorCachesCultureWalkForSameAssembly() { // Arrange var resourceNamesCache = new ResourceNamesCache(); var baseName = "test"; var resourceAssembly = new TestAssemblyWrapper(); var resourceManager = new TestResourceManager(baseName, resourceAssembly.Assembly); var resourceStreamManager = new TestResourceStringProvider(resourceNamesCache, resourceAssembly, baseName); var logger = Logger; var localizer1 = new ResourceManagerStringLocalizer(resourceManager, resourceStreamManager, baseName, resourceNamesCache, logger); var localizer2 = new ResourceManagerStringLocalizer(resourceManager, resourceStreamManager, baseName, resourceNamesCache, logger); // Act for (int i = 0; i < 5; i++) { localizer1.GetAllStrings().ToList(); localizer2.GetAllStrings().ToList(); } // Assert var expectedCallCount = GetCultureInfoDepth(CultureInfo.CurrentUICulture); Assert.Equal(expectedCallCount, resourceAssembly.GetManifestResourceStreamCallCount); }
public void Constructor_IfTheResourceNamesCacheParameterIsNull_ShouldThrowAnArgumentNullException() { try { var _ = new ResourceManagerStringLocalizer(Mock.Of <ResourceManager>(), Mock.Of <Assembly>(), "Test", null, Mock.Of <ILogger>()); } catch (ArgumentNullException argumentNullException) { if (argumentNullException.ParamName.Equals("resourceNamesCache", StringComparison.Ordinal)) { throw; } } }
public void ResourceManagerStringLocalizer_GetAllStrings_ReturnsExpectedValue(bool includeParentCultures) { // Arrange var baseName = "test"; var resourceNamesCache = new ResourceNamesCache(); var resourceAssembly = new TestAssemblyWrapper(); var resourceManager = new TestResourceManager(baseName, resourceAssembly.Assembly); var localizer = new ResourceManagerStringLocalizer(resourceManager, resourceAssembly, baseName, resourceNamesCache); // Act // We have to access the result so it evaluates. var strings = localizer.GetAllStrings(includeParentCultures).ToList(); // Assert var value = Assert.Single(strings); Assert.Equal("TestName", value.Value); }
public void ResourceManagerStringLocalizer_GetAllStrings_ReturnsExpectedValue(bool includeParentCultures) { // Arrange var baseName = "test"; var resourceNamesCache = new ResourceNamesCache(); var resourceAssembly = new TestAssemblyWrapper(); var resourceManager = new TestResourceManager(baseName, resourceAssembly.Assembly); var localizer = new ResourceManagerStringLocalizer(resourceManager, resourceAssembly, baseName, resourceNamesCache); // Act // We have to access the result so it evaluates. var strings = localizer.GetAllStrings(includeParentCultures).ToList(); // Assert var value = Assert.Single(strings); Assert.Equal("TestName", value.Value); }
public void EnumeratorCacheIsScopedByAssembly() { // Arrange var resourceNamesCache = new ResourceNamesCache(); var baseName = "test"; var resourceAssembly1 = new TestAssemblyWrapper("Assembly1"); var resourceAssembly2 = new TestAssemblyWrapper("Assembly2"); var resourceManager1 = new TestResourceManager(baseName, resourceAssembly1.Assembly); var resourceManager2 = new TestResourceManager(baseName, resourceAssembly2.Assembly); var localizer1 = new ResourceManagerStringLocalizer(resourceManager1, resourceAssembly1, baseName, resourceNamesCache); var localizer2 = new ResourceManagerStringLocalizer(resourceManager2, resourceAssembly2, baseName, resourceNamesCache); // Act localizer1.GetAllStrings().ToList(); localizer2.GetAllStrings().ToList(); // Assert var expectedCallCount = GetCultureInfoDepth(CultureInfo.CurrentUICulture); Assert.Equal(expectedCallCount, resourceAssembly1.GetManifestResourceStreamCallCount); Assert.Equal(expectedCallCount, resourceAssembly2.GetManifestResourceStreamCallCount); }
public void EnumeratorCachesCultureWalkForSameAssembly() { // Arrange var resourceNamesCache = new ResourceNamesCache(); var baseName = "test"; var resourceAssembly = new TestAssemblyWrapper(); var resourceManager = new TestResourceManager(baseName, resourceAssembly.Assembly); var localizer1 = new ResourceManagerStringLocalizer(resourceManager, resourceAssembly, baseName, resourceNamesCache); var localizer2 = new ResourceManagerStringLocalizer(resourceManager, resourceAssembly, baseName, resourceNamesCache); // Act for (int i = 0; i < 5; i++) { localizer1.GetAllStrings().ToList(); localizer2.GetAllStrings().ToList(); } // Assert var expectedCallCount = GetCultureInfoDepth(CultureInfo.CurrentUICulture); Assert.Equal(expectedCallCount, resourceAssembly.GetManifestResourceStreamCallCount); }
public void GetString_PopulatesSearchedLocationOnLocalizedString() { // Arrange var baseName = "Resources.TestResource"; var resourceNamesCache = new ResourceNamesCache(); var resourceAssembly = new TestAssemblyWrapper(); var resourceManager = new TestResourceManager(baseName, resourceAssembly); var resourceStreamManager = new TestResourceStringProvider(resourceNamesCache, resourceManager, resourceAssembly.Assembly, baseName); var logger = Logger; var localizer = new ResourceManagerStringLocalizer( resourceManager, resourceStreamManager, baseName, resourceNamesCache, logger); // Act var value = localizer["name"]; // Assert Assert.Equal("Resources.TestResource", value.SearchedLocation); }
/// <summary> /// Initialize the current instance /// </summary> /// <param name="resourcesManager">A strongly-typed resource class, for looking up localized strings</param> /// <param name="loggerFactory">The <see cref="ILoggerFactory"/> to be used for create /// the required <see cref="ILogger"/>.</param> public CrudLocalizer( ResourceManager resourcesManager, ILoggerFactory loggerFactory ) { if (resourcesManager is null) { throw new ArgumentNullException(nameof(resourcesManager)); } if (loggerFactory is null) { throw new ArgumentNullException(nameof(loggerFactory)); } _resourcesManager = new ResourceManagerStringLocalizer( resourcesManager, resourcesManager.ResourceSetType.Assembly, resourcesManager.BaseName ?? string.Empty, new ResourceNamesCache(), loggerFactory.CreateLogger <CrudLocalizer>() ); }