示例#1
0
        public static Dictionary <Type, Type> InitializeSupportedPairsFromMapper(this IMapper mapper, bool addCollectionVariants = true, bool addReverseVariants = false)
        {
            var maps = mapper.ConfigurationProvider.GetAllTypeMaps();
            var supportedTypePairs = AdapterHelper.InitializeSupportedTypePairs();

            foreach (var item in maps)
            {
                supportedTypePairs.AddTypePair(item.SourceType, item.DestinationType, addCollectionVariants: addCollectionVariants, addReverseVariants: addReverseVariants);
            }

            return(supportedTypePairs);
        }
示例#2
0
        public void MapSupportedTypes_ForUnknownType_FindsSourceType([Values(typeof(TestType))] Type sourceType)
        {
            try
            {
                SupportedTypePairs = AdapterHelper.InitializeSupportedTypePairs();

                var mappedDestinationTypes = MapSupportedTypes(new Type[] { sourceType });

                Assert.IsNotNull(mappedDestinationTypes);
                Assert.IsTrue(mappedDestinationTypes.Length == 1);
                Assert.IsTrue(sourceType == mappedDestinationTypes[0]);
            }
            finally
            {
                SupportedTypePairs = null;
            }
        }
示例#3
0
        public void MapSupportedTypes_ForKnownType_FindsType(Type configSourceType, Type configDestinationType, Type instanceSourceType, Type instanceDestinationType)
        {
            try
            {
                var supportedTypePairs = AdapterHelper.InitializeSupportedTypePairs();
                AdapterHelper.AddTypePair(supportedTypePairs, configSourceType, configDestinationType);
                SupportedTypePairs = supportedTypePairs;

                var mappedInstanceDestinationTypes = MapSupportedTypes(new Type[] { instanceSourceType });

                Assert.IsNotNull(mappedInstanceDestinationTypes);
                Assert.IsTrue(mappedInstanceDestinationTypes.Length == 1);
                Assert.IsTrue(instanceSourceType != mappedInstanceDestinationTypes[0]);
                Assert.IsTrue(mappedInstanceDestinationTypes[0] == instanceDestinationType);
            }
            finally
            {
                SupportedTypePairs = null;
            }
        }
示例#4
0
        public void AdapterInterceptor_InvalidDeclaration_ThrowsException()
        {
            var typePair = AdapterHelper.InitializeSupportedTypePairs();

            typePair.AddTypePair(typeof(TestType), typeof(CustomTestType));
            // Should not throw exception, we are just repeating what is already defined
            typePair.AddTypePair(typeof(CustomTestType), typeof(TestType));

            var ex1 = Assert.Throws <AdapterInterceptorException>(() =>
            {
                typePair.AddTypePair(typeof(TestType), typeof(UnknownType));
            });

            TestContext.WriteLine(ex1);
            Assert.AreEqual("Source type mapping for type {Type: com.github.akovac35.AdapterInterceptor.Tests.TestTypes.TestType} is already defined for type {Type: com.github.akovac35.AdapterInterceptor.Tests.TestTypes.CustomTestType} and can't be added for type {Type: com.github.akovac35.AdapterInterceptor.Tests.TestTypes.UnknownType}. Review documentation and usage or use a less generic variant AdapterInterceptor<TTarget>.", ex1.Message);

            var ex2 = Assert.Throws <AdapterInterceptorException>(() =>
            {
                typePair.AddTypePair(typeof(CustomTestType), typeof(UnknownType));
            });

            TestContext.WriteLine(ex2.Message);
            Assert.AreEqual("Source type mapping for type {Type: com.github.akovac35.AdapterInterceptor.Tests.TestTypes.CustomTestType} is already defined for type {Type: com.github.akovac35.AdapterInterceptor.Tests.TestTypes.TestType} and can't be added for type {Type: com.github.akovac35.AdapterInterceptor.Tests.TestTypes.UnknownType}. Review documentation and usage or use a less generic variant AdapterInterceptor<TTarget>.", ex2.Message);
        }