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_WithMultiLingualNameAttributesForDifferentCulturesAndCurrentUICultureOnlyMatchesNeutralCulture_ReturnsForTheNeutralCulture()
        {
            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", "en"),
                new MultiLingualNameAttribute("The Name en-GB", "en-GB")
            });
            typeInformationStub.Stub(_ => _.Assembly).Return(TestAssemblies.En.Value);

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


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

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

                Assert.That(result, Is.True);
                Assert.That(multiLingualName, Is.EqualTo("The Name en"));
            }
        }
        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_WithoutAssembly_UsesInvariantCultureForNeutralCulture()
        {
            var service = new MultiLingualNameBasedMemberInformationGlobalizationService();

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

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

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


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

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

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

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

            typeInformationStub
            .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;
            });

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

            using (new CultureScope("", "en-US"))
            {
                string multiLingualName;
                var    result = service.TryGetTypeDisplayName(typeInformationStub, 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.TryGetTypeDisplayName(typeInformationStub, 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 typeInformationStub = MockRepository.GenerateStub <ITypeInformation>();

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

            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);
        }
        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"));
        }