public void Test_WithMultiLingualNameAttributesForDifferentCulturesAndCurrentUICultureOnlyMatchesNeutralCulture_ReturnsForTheNeutralCulture()
        {
            var service = new MultiLingualNameBasedMemberInformationGlobalizationService();

            var typeInformationStub = MockRepository.GenerateStub <ITypeInformation>();

            typeInformationStub.Stub(_ => _.Assembly).Return(TestAssemblies.En.Value);

            var propertyInformationStub = MockRepository.GenerateStub <IPropertyInformation>();

            propertyInformationStub
            .Stub(_ => _.GetCustomAttributes <MultiLingualNameAttribute> (false))
            .Return(
                new[]
            {
                new MultiLingualNameAttribute("The Name fr-FR", "fr-FR"),
                new MultiLingualNameAttribute("The Name en", "en"),
                new MultiLingualNameAttribute("The Name en-GB", "en-GB")
            });
            propertyInformationStub.Stub(_ => _.GetOriginalDeclaration()).Return(propertyInformationStub);
            propertyInformationStub.Stub(_ => _.GetOriginalDeclaringType()).Return(typeInformationStub);

            var typeInformationForResourceResolutionStub = MockRepository.GenerateStub <ITypeInformation>();


            using (new CultureScope("it-IT", "en-US"))
            {
                string multiLingualName;

                var result = service.TryGetPropertyDisplayName(propertyInformationStub, typeInformationForResourceResolutionStub, out multiLingualName);

                Assert.That(result, Is.True);
                Assert.That(multiLingualName, Is.EqualTo("The Name en"));
            }
        }
        public void Test_WithoutType_UsesInvariantCultureForNeutralCulture()
        {
            var service = new MultiLingualNameBasedMemberInformationGlobalizationService();

            var propertyInformationStub = MockRepository.GenerateStub <IPropertyInformation>();

            propertyInformationStub
            .Stub(_ => _.GetCustomAttributes <MultiLingualNameAttribute> (false))
            .Return(
                new[]
            {
                new MultiLingualNameAttribute("The Name invariant", ""),
                new MultiLingualNameAttribute("The Name en-GB", "en-GB")
            });
            propertyInformationStub.Stub(_ => _.GetOriginalDeclaration()).Return(propertyInformationStub);
            propertyInformationStub.Stub(_ => _.GetOriginalDeclaringType()).Return(null);

            var typeInformationForResourceResolutionStub = MockRepository.GenerateStub <ITypeInformation>();


            using (new CultureScope("", ""))
            {
                string multiLingualName;

                var result = service.TryGetPropertyDisplayName(propertyInformationStub, typeInformationForResourceResolutionStub, out multiLingualName);

                Assert.That(result, Is.True);
                Assert.That(multiLingualName, Is.EqualTo("The Name invariant"));
            }
        }
        public void Test_WithOverridenProperty_ReturnsTheLocalizedNameForTheOriginalDeclaration()
        {
            var service = new MultiLingualNameBasedMemberInformationGlobalizationService();

            var basePropertyInformationStub = MockRepository.GenerateStub <IPropertyInformation>();

            basePropertyInformationStub
            .Stub(_ => _.GetCustomAttributes <MultiLingualNameAttribute> (false))
            .Return(
                new[]
            {
                new MultiLingualNameAttribute("The Name", "")
            });

            var propertyInformationStub = MockRepository.GenerateStub <IPropertyInformation>();

            propertyInformationStub
            .Stub(_ => _.GetCustomAttributes <MultiLingualNameAttribute> (true))
            .Return(
                new[]
            {
                new MultiLingualNameAttribute("The Name", "")
            });
            propertyInformationStub.Stub(_ => _.GetOriginalDeclaration()).Return(basePropertyInformationStub);

            var typeInformationForResourceResolutionStub = MockRepository.GenerateStub <ITypeInformation>();

            string multiLingualName;

            var result = service.TryGetPropertyDisplayName(propertyInformationStub, typeInformationForResourceResolutionStub, out multiLingualName);

            Assert.That(result, Is.True);
            Assert.That(multiLingualName, Is.EqualTo("The Name"));
        }
        public void Test_UsesNeutralResourcesLanguageAttributeFromOriginalDeclaration()
        {
            var service = new MultiLingualNameBasedMemberInformationGlobalizationService();

            var typeInformationStub = MockRepository.GenerateStub <ITypeInformation>();

            typeInformationStub.Stub(_ => _.Assembly).Return(TestAssemblies.EnUS.Value);

            var propertyInformationStub = MockRepository.GenerateStub <IPropertyInformation>();

            propertyInformationStub
            .Stub(_ => _.GetCustomAttributes <MultiLingualNameAttribute> (false))
            .Return(
                new[]
            {
                new MultiLingualNameAttribute("The Name", "en-US")
            });
            propertyInformationStub.Stub(_ => _.GetOriginalDeclaration()).Return(propertyInformationStub);
            propertyInformationStub.Stub(_ => _.GetOriginalDeclaringType()).Return(typeInformationStub);

            var typeInformationForResourceResolutionStub = MockRepository.GenerateStub <ITypeInformation>();

            using (new CultureScope("en", "en"))
            {
                string multiLingualName;

                var result = service.TryGetPropertyDisplayName(propertyInformationStub, typeInformationForResourceResolutionStub, out multiLingualName);

                Assert.That(result, Is.True);
                Assert.That(multiLingualName, Is.EqualTo("The Name"));
            }
        }
        public void Test_WithMultipleMultiLingualNameAttributesForSameCulture_ThrowsInvalidOperationException()
        {
            var service = new MultiLingualNameBasedMemberInformationGlobalizationService();

            var typeInformationStub = MockRepository.GenerateStub <ITypeInformation>();

            typeInformationStub
            .Stub(_ => _.GetCustomAttributes <MultiLingualNameAttribute> (false))
            .Return(
                new[]
            {
                new MultiLingualNameAttribute("The Name fr-FR", "fr-FR"),
                new MultiLingualNameAttribute("The Name fr-FR", "fr-FR"),
                new MultiLingualNameAttribute("The Name en-GB", "en-GB")
            });
            typeInformationStub.Stub(_ => _.FullName).Return("The.Full.Type.Name");

            var typeInformationForResourceResolutionStub = MockRepository.GenerateStub <ITypeInformation>();

            using (new CultureScope("it-IT", "en-US"))
            {
                string multiLingualName;

                Assert.That(
                    () => service.TryGetTypeDisplayName(typeInformationStub, typeInformationForResourceResolutionStub, out multiLingualName),
                    Throws.TypeOf <InvalidOperationException>().With.Message.EqualTo(
                        "The type 'The.Full.Type.Name' has more than one MultiLingualNameAttribute for the culture 'fr-FR' applied. "
                        + "The used cultures must be unique within the set of MultiLingualNameAttributes."));
            }
        }
        public void Test_WithMultiLingualNameAttributesNotMatchingTheNeutralResourcesLanguageAttribute_ThrowsInvalidOperationException()
        {
            var service = new MultiLingualNameBasedMemberInformationGlobalizationService();

            var typeInformationStub = MockRepository.GenerateStub <ITypeInformation>();

            typeInformationStub
            .Stub(_ => _.GetCustomAttributes <MultiLingualNameAttribute> (false))
            .Return(
                new[]
            {
                new MultiLingualNameAttribute("The Name fr-FR", "fr-FR"),
                new MultiLingualNameAttribute("The Name en-GB", "en-GB")
            });
            typeInformationStub.Stub(_ => _.FullName).Return("The.Full.Type.Name");
            typeInformationStub.Stub(_ => _.Assembly).Return(TestAssemblies.En.Value);

            var typeInformationForResourceResolutionStub = MockRepository.GenerateStub <ITypeInformation>();

            using (new CultureScope("it-IT", "en-GB"))
            {
                string multiLingualName;

                Assert.That(
                    () => service.TryGetTypeDisplayName(typeInformationStub, typeInformationForResourceResolutionStub, out multiLingualName),
                    Throws.TypeOf <InvalidOperationException>().With.Message.StartsWith(
                        "The type 'The.Full.Type.Name' has no MultiLingualNameAttribute for the assembly's neutral resource language ('en') applied."));
            }
        }
        public void Test_WithNeutralResourcesLanguageAttributeSpecifiesSpecificCulture_DoesNotOverrideExistingSpecificCulture()
        {
            var service = new MultiLingualNameBasedMemberInformationGlobalizationService();

            var typeInformationStub = MockRepository.GenerateStub <ITypeInformation>();

            typeInformationStub
            .Stub(_ => _.GetCustomAttributes <MultiLingualNameAttribute> (false))
            .Return(
                new[]
            {
                new MultiLingualNameAttribute("The Name en", "en"),
                new MultiLingualNameAttribute("The Name en-GB", "en-GB"),
                new MultiLingualNameAttribute("The Name en-US", "en-US")
            });
            typeInformationStub.Stub(_ => _.Assembly).Return(TestAssemblies.EnUS.Value);

            var typeInformationForResourceResolutionStub = MockRepository.GenerateStub <ITypeInformation>();


            using (new CultureScope("en-GB", "en-GB"))
            {
                string multiLingualName;

                var result = service.TryGetTypeDisplayName(typeInformationStub, typeInformationForResourceResolutionStub, out multiLingualName);

                Assert.That(result, Is.True);
                Assert.That(multiLingualName, Is.EqualTo("The Name en-GB"));
            }
        }
        public void Test_WithOverridenPropertyHavingMultiLingualNameAttributes_ThrowsInvalidOperationException()
        {
            var service = new MultiLingualNameBasedMemberInformationGlobalizationService();

            var basePropertyInformationStub = MockRepository.GenerateStub <IPropertyInformation>();

            basePropertyInformationStub
            .Stub(_ => _.GetCustomAttributes <MultiLingualNameAttribute> (false))
            .Return(
                new[]
            {
                new MultiLingualNameAttribute("The Name", "")
            });

            var typeInformationStub = MockRepository.GenerateStub <ITypeInformation>();

            typeInformationStub.Stub(_ => _.FullName).Return("The.Full.Type.Name");

            var propertyInformationStub = MockRepository.GenerateStub <IPropertyInformation>();

            propertyInformationStub
            .Stub(_ => _.GetCustomAttributes <MultiLingualNameAttribute> (true))
            .Return(
                new[]
            {
                new MultiLingualNameAttribute("The Other Name", ""),
                new MultiLingualNameAttribute("The Name", "")
            });

            propertyInformationStub.Stub(_ => _.GetOriginalDeclaration()).Return(basePropertyInformationStub);
            propertyInformationStub.Stub(_ => _.GetOriginalDeclaringType()).Return(typeInformationStub);
            propertyInformationStub.Stub(_ => _.DeclaringType).Return(typeInformationStub);
            propertyInformationStub.Stub(_ => _.Name).Return("TheProperty");

            var typeInformationForResourceResolutionStub = MockRepository.GenerateStub <ITypeInformation>();

            string multiLingualName;

            Assert.That(
                () => service.TryGetPropertyDisplayName(propertyInformationStub, typeInformationForResourceResolutionStub, out multiLingualName),
                Throws.TypeOf <InvalidOperationException>().With.Message.EqualTo(
                    "The property 'TheProperty' overridden on type 'The.Full.Type.Name' has one or more MultiLingualNameAttributes applied via a property override. "
                    + "The MultiLingualNameAttributes maybe only be applied to the original declaration of a property."));
        }
        public void Test_WithMultipleCalls_UsesCacheToRetrieveTheLocalizedName()
        {
            var service = new MultiLingualNameBasedMemberInformationGlobalizationService();

            bool wasCalled = false;
            var  propertyInformationStub = MockRepository.GenerateStub <IPropertyInformation>();

            propertyInformationStub
            .Stub(_ => _.GetCustomAttributes <MultiLingualNameAttribute> (false))
            .Return(
                new[]
            {
                new MultiLingualNameAttribute("The Name", ""),
                new MultiLingualNameAttribute("The Name en-US", "en-US")
            })
            .WhenCalled(
                mi =>
            {
                Assert.That(wasCalled, Is.False);
                wasCalled = true;
            });
            propertyInformationStub.Stub(_ => _.GetOriginalDeclaration()).Return(propertyInformationStub);

            var typeInformationForResourceResolutionStub = MockRepository.GenerateStub <ITypeInformation>();

            using (new CultureScope("", "en-US"))
            {
                string multiLingualName;
                var    result = service.TryGetPropertyDisplayName(propertyInformationStub, typeInformationForResourceResolutionStub, out multiLingualName);

                Assert.That(result, Is.True);
                Assert.That(multiLingualName, Is.EqualTo("The Name en-US"));
            }

            using (new CultureScope("", "fr-FR"))
            {
                string multiLingualName;
                var    result = service.TryGetPropertyDisplayName(propertyInformationStub, typeInformationForResourceResolutionStub, out multiLingualName);

                Assert.That(result, Is.True);
                Assert.That(multiLingualName, Is.EqualTo("The Name"));
            }
        }
        public void Test_WithMultiLingualNameAttributeOnlyOnBaseClass_DoesNotCheckBaseClass()
        {
            var service = new MultiLingualNameBasedMemberInformationGlobalizationService();

            var typeInformationStub = MockRepository.GenerateStub <ITypeInformation>();

            typeInformationStub
            .Stub(_ => _.GetCustomAttributes <MultiLingualNameAttribute> (false))
            .Return(new MultiLingualNameAttribute[0]);

            var typeInformationForResourceResolutionStub = MockRepository.GenerateStub <ITypeInformation>();

            string multiLingualName;

            var result = service.TryGetTypeDisplayName(typeInformationStub, typeInformationForResourceResolutionStub, out multiLingualName);

            Assert.That(result, Is.False);
            Assert.That(multiLingualName, Is.Null);
            typeInformationStub.AssertWasNotCalled(_ => _.BaseType);
        }
        public void Test_WithoutMultiLingualNameAttribute_ReturnsNull()
        {
            var service = new MultiLingualNameBasedMemberInformationGlobalizationService();

            var propertyInformationStub = MockRepository.GenerateStub <IPropertyInformation>();

            propertyInformationStub
            .Stub(_ => _.GetCustomAttributes <MultiLingualNameAttribute> (false))
            .Return(new MultiLingualNameAttribute[0]);
            Assert.That(typeof(object).BaseType, Is.Null, "Defined behavior for BaseType of Object is to return null");
            propertyInformationStub.Stub(_ => _.GetOriginalDeclaration()).Return(propertyInformationStub);

            var typeInformationForResourceResolutionStub = MockRepository.GenerateStub <ITypeInformation>();

            string multiLingualName;

            var result = service.TryGetPropertyDisplayName(propertyInformationStub, typeInformationForResourceResolutionStub, out multiLingualName);

            Assert.That(result, Is.False);
            Assert.That(multiLingualName, Is.Null);
        }
        public void Test_WithMultiLingualNameAttribute_ReturnsTheName()
        {
            var service = new MultiLingualNameBasedMemberInformationGlobalizationService();

            var typeInformationStub = MockRepository.GenerateStub <ITypeInformation>();

            typeInformationStub
            .Stub(_ => _.GetCustomAttributes <MultiLingualNameAttribute> (false))
            .Return(
                new[]
            {
                new MultiLingualNameAttribute("The Name", "")
            });

            var typeInformationForResourceResolutionStub = MockRepository.GenerateStub <ITypeInformation>();

            string multiLingualName;

            var result = service.TryGetTypeDisplayName(typeInformationStub, typeInformationForResourceResolutionStub, out multiLingualName);

            Assert.That(result, Is.True);
            Assert.That(multiLingualName, Is.EqualTo("The Name"));
        }