public void SomeResourceFoundForNeutralVariantOfSpecifiedCulture_MultipleLayersOfResources_ShouldGetValueFromNeutralVariantOfSpecifiedCultureIfExistsOtherwiseDefaultCulture(string name, Func <IContentSource> factory)
        {
            //Arrange
            var source    = factory();
            var mock      = GetMock(source);
            var localizer = new ContentLocalizer(source, "en-US");

            mock.SetData("en-US", new Dictionary <string, string>
            {
                ["SomeKey"]                    = "SomeValue",
                ["ReferenceSomeKey"]           = "{{SomeKey}}-{{SomeKey}}",
                ["Reference_ReferenceSomeKey"] = "{{ReferenceSomeKey}}-{{ReferenceSomeKey}}",
            });

            mock.SetData("es", new Dictionary <string, string>
            {
                ["SomeKey"] = "SomeValueAf",
            });

            CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("es-US");

            //Act/Assert
            Assert.Equal("SomeValueAf", localizer["SomeKey"]);
            Assert.Equal($"{localizer["SomeKey"]}-{localizer["SomeKey"]}", localizer["ReferenceSomeKey"]);
            Assert.Equal($"{localizer["ReferenceSomeKey"]}-{localizer["ReferenceSomeKey"]}", localizer["Reference_ReferenceSomeKey"]);
        }
        public void Localizer_Returns_Localized_Content(string name, Func <IContentSource> factory)
        {
            //Arrange
            var source    = factory();
            var mock      = GetMock(source);
            var localizer = new ContentLocalizer(source, "en-US");

            mock.SetData("en-US", new Dictionary <string, string> {
                { "SomeKey", "Val-en-US" }
            });
            mock.SetData("es-MX", new Dictionary <string, string> {
                { "SomeKey", "Val-es-MX" }
            });

            //Act/Assert
            Assert.True(string.IsNullOrEmpty(localizer["Some unknown key"]));

            CultureInfo.CurrentUICulture = new CultureInfo("en-US");
            Assert.Equal("Val-en-US", localizer["SomeKey"]);

            CultureInfo.CurrentUICulture = new CultureInfo("es-MX");
            Assert.Equal("Val-es-MX", localizer["SomeKey"]);


            CultureInfo.CurrentUICulture = new CultureInfo("es-FR"); //we don't have this so it should resort to default
            Assert.Equal("Val-en-US", localizer["SomeKey"]);
        }
Beispiel #3
0
        public void GenerateCarousel_Merges_Two_Banners()
        {
            //Arrange
            var source = new Mock <IContentSource>();

            source.Setup(o => o.GetContentItem("Carousel", "en-US"))
            .Returns(new ContentItem {
                Name = "Carousel", Enabled = true, Value =
                    @"<exigocarousel><exigocarouselattributes type=""bootstrap3"" /><exigobanner name=""Banner_One"" /><exigobanner name=""Banner_Two"" /></exigocarousel>"
            });

            source.Setup(o => o.GetContentItem("Banner_One", "en-US"))
            .Returns(new ContentItem {
                Name = "Carousel", Enabled = true, Value =
                    @"Banner_One_Content"
            });

            source.Setup(o => o.GetContentItem("Banner_Two", "en-US"))
            .Returns(new ContentItem {
                Name = "Carousel", Enabled = true, Value =
                    @"Banner_Two_Content"
            });

            source.Setup(contentSource => contentSource.GetAllContentItemsAsync("en-US", It.IsAny <ContentVersion>()))
            .Returns(() => Task.FromResult <IEnumerable <ContentItem> >(new[]
            {
                new ContentItem
                {
                    Name = "Carousel", Enabled = true, Value = @"<exigocarousel><exigocarouselattributes type=""bootstrap3"" /><exigobanner name=""Banner_One"" /><exigobanner name=""Banner_Two"" /></exigocarousel>"
                },
                new ContentItem
                {
                    Name = "Banner_One", Enabled = true, Value = @"Banner_One_Content"
                },
                new ContentItem
                {
                    Name = "Banner_Two", Enabled = true, Value = @"Banner_Two_Content"
                }
            }));

            var localizer = new ContentLocalizer(source.Object, "en-US");

            //Act
            var value = localizer.GenerateCarousel("Carousel", null);

            //Assert
            _output.WriteLine(value);

            Assert.False(string.IsNullOrEmpty(value), "We should have a value");

            Assert.Contains("Banner_One_Content", value);
            Assert.Contains("Banner_Two_Content", value);
        }
        public void ResourceSelfReference_ShouldReturnSelfReferenceErrorMessage(string name, Func <IContentSource> factory)
        {
            //Arrange
            var source    = factory();
            var mock      = GetMock(source);
            var localizer = new ContentLocalizer(source, "en-US");


            mock.SetData("en-US", new Dictionary <string, string>
            {
                ["SomeKey"] = "{{SomeKey}}",
            });

            //Act/Assert
            Assert.Equal($"{SelfReferenceMessage} [SomeKey]", localizer["SomeKey"]);
        }
        public void ResourceNotFound_MultipleResources_ShouldReturnEmptyValue(string name, Func <IContentSource> factory)
        {
            //Arrange
            var source    = factory();
            var mock      = GetMock(source);
            var localizer = new ContentLocalizer(source, "en-US");

            mock.SetData("en-US", new Dictionary <string, string>
            {
                ["ReferenceSomeKeyThatDoesntExist"] = "{{SomeKeyThatDoesntExist}}-{{SomeKeyThatDoesntExist}}"
            });

            //Act/Assert
            Assert.Equal(string.Empty, localizer["SomeKeyThatDoesntExist"]);
            Assert.Equal("-", localizer["ReferenceSomeKeyThatDoesntExist"]);
        }
        public void Disabled_ShouldWriteBlankForResource(string name, Func <IContentSource> factory)
        {
            //Arrange
            var source    = factory();
            var mock      = GetMock(source);
            var localizer = new ContentLocalizer(source, "en-US");

            mock.SetData("en-US", new Dictionary <string, string>
            {
                ["SomeKey"]          = "SomeValue",
                ["ReferenceSomeKey"] = "{{SomeKey}}"
            });
            UpdateItem(GetItem(mock, "SomeKey"), false);
            //Act/Assert
            Assert.Equal("", localizer["SomeKey"]);
            Assert.Equal(localizer["SomeKey"], localizer["ReferenceSomeKey"]);
        }
        public void MultipleNestedResources_ShouldEvaluateNestedResources(string name, Func <IContentSource> factory)
        {
            //Arrange
            var source    = factory();
            var mock      = GetMock(source);
            var localizer = new ContentLocalizer(source, "en-US");

            mock.SetData("en-US", new Dictionary <string, string>
            {
                ["SomeKey"]          = "SomeValue",
                ["ReferenceSomeKey"] = "{{SomeKey}}-{{SomeKey}}"
            });

            //Act/Assert
            Assert.Equal("SomeValue", localizer["SomeKey"]);
            Assert.Equal($"{localizer["SomeKey"]}-{localizer["SomeKey"]}", localizer["ReferenceSomeKey"]);
        }
        public void DisabledForTimeFrame_ShouldWriteResource(string name, Func <IContentSource> factory)
        {
            //Arrange
            var source    = factory();
            var mock      = GetMock(source);
            var localizer = new ContentLocalizer(source, "en-US");

            mock.SetData("en-US", new Dictionary <string, string>
            {
                ["EndDatePassed"]       = "SomeValue2",
                ["StartDateNotReached"] = "{{EndDatePassed}}"
            });
            UpdateItem(GetItem(mock, "EndDatePassed"), true, DateTime.Now.AddDays(-2), DateTime.Now.AddDays(-1));
            UpdateItem(GetItem(mock, "StartDateNotReached"), true, DateTime.Now.AddDays(1), DateTime.Now.AddDays(2));
            //Act/Assert
            Assert.Equal("", localizer["EndDatePassed"]);
            Assert.Equal(localizer["EndDatePassed"], localizer["StartDateNotReached"]);
        }
        public void ResourceNotFoundForSpecifiedCulture_ExistsInDefaultCulture_MultipleNestedResources_ShouldGetValuesFromDefaultCulture(string name, Func <IContentSource> factory)
        {
            //Arrange
            var source    = factory();
            var mock      = GetMock(source);
            var localizer = new ContentLocalizer(source, "en-US");

            mock.SetData("en-US", new Dictionary <string, string>
            {
                ["SomeKey"]          = "SomeValue",
                ["ReferenceSomeKey"] = "{{SomeKey}}-{{SomeKey}}"
            });

            CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("af");

            //Act/Assert
            Assert.Equal("SomeValue", localizer["SomeKey"]);
            Assert.Equal($"{localizer["SomeKey"]}-{localizer["SomeKey"]}", localizer["ReferenceSomeKey"]);
        }
        public void Locizer_Falls_Back_To_Two_Letter_Culture(string name, Func <IContentSource> factory)
        {
            //Arrange
            var source    = factory();
            var mock      = GetMock(source);
            var localizer = new ContentLocalizer(source, "en-US");

            mock.SetData("en-US", new Dictionary <string, string> {
                { "SomeKey", "Val-en" }
            });

            mock.SetData("es", new Dictionary <string, string> {
                { "SomeKey", "Val-es" }
            });

            //Act/Assert
            CultureInfo.CurrentUICulture = new CultureInfo("es-MX");
            Assert.Equal("Val-es", localizer["SomeKey"]);
        }
        public void ResourceCircularReference_Transitive2_ShouldReturnCircularReferenceErrorMessage(string name, Func <IContentSource> factory)
        {
            //Arrange
            var source    = factory();
            var mock      = GetMock(source);
            var localizer = new ContentLocalizer(source, "en-US");


            mock.SetData("en-US", new Dictionary <string, string>
            {
                ["SomeKey"]  = "{{SomeKey2}}",
                ["SomeKey2"] = "{{SomeKey3}}",
                ["SomeKey3"] = "{{SomeKey2}}"
            });

            //Act/Assert
            Assert.Equal($"{CircularReferenceDetectionMessage} [SomeKey2]", localizer["SomeKey"]);
            Assert.Equal($"{CircularReferenceDetectionMessage} [SomeKey2]", localizer["SomeKey2"]);
            Assert.Equal($"{CircularReferenceDetectionMessage} [SomeKey3]", localizer["SomeKey3"]);
        }
        public void EnabledForTimeFrame_ShouldWriteResource(string name, Func <IContentSource> factory)
        {
            //Arrange
            var source    = factory();
            var mock      = GetMock(source);
            var localizer = new ContentLocalizer(source, "en-US");

            mock.SetData("en-US", new Dictionary <string, string>
            {
                ["InBetween"]         = "SomeValue",
                ["EndDateNotReached"] = "SomeValue2",
                ["StartDatePassed"]   = "{{InBetween}}"
            });
            UpdateItem(GetItem(mock, "InBetween"), true, DateTime.Now.AddDays(-1), DateTime.Now.AddDays(1));
            // We don't consider the following cases possible yet
            // UpdateItem(GetItem(mock, "EndDateNotReached"), true, DateTime.Now.AddDays(1));
            // UpdateItem(GetItem(mock, "StartDatePassed"), true, DateTime.Now.AddDays(-1));
            //Act/Assert
            Assert.Equal("SomeValue", localizer["InBetween"]);
            Assert.Equal("SomeValue2", localizer["EndDateNotReached"]);
            Assert.Equal(localizer["InBetween"], localizer["StartDatePassed"]);
        }