Example #1
0
        public void Adding_same_feature_multiple_times_should_replace_eachother()
        {
            var featureA = new MappingExpressionFeatureA(1);
            var featureB = new MappingExpressionFeatureB(1);
            var config   = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <Source, Dest>()
                .AddOrUpdateFeature(new MappingExpressionFeatureA(3))
                .AddOrUpdateFeature(new MappingExpressionFeatureA(2))
                .AddOrUpdateFeature(featureA)
                .AddOrUpdateFeature(new MappingExpressionFeatureB(3))
                .AddOrUpdateFeature(new MappingExpressionFeatureB(2))
                .AddOrUpdateFeature(featureB)
                .ReverseMap();
            });


            var typeMap = config.FindTypeMapFor <Source, Dest>();

            typeMap.Features.Count().ShouldBe(2);

            var typeMapReverse = config.FindTypeMapFor <Dest, Source>();

            typeMapReverse.Features.Count().ShouldBe(2);

            Validate(featureA);
            Validate(featureB);

            void Validate(MappingExpressionFeatureBase feature)
            {
                feature.ConfigureTypeMaps.ShouldBeOfLength(1);
                feature.ReverseMaps.ShouldBeOfLength(1);
            }
        }
Example #2
0
        public void Add_single_feature_with_reverse()
        {
            var featureA = new MappingExpressionFeatureA(1);
            var config   = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <Source, Dest>()
                .SetFeature(featureA)
                .ReverseMap();
            });

            var typeMap = config.FindTypeMapFor <Source, Dest>();

            typeMap.Features.Count().ShouldBe(1);

            var typeMapReverse = config.FindTypeMapFor <Dest, Source>();

            typeMapReverse.Features.Count().ShouldBe(0);

            Validate <TypeMapFeatureA>(featureA);

            void Validate <TFeature>(MappingExpressionFeatureBase feature)
                where TFeature : TypeMapFeatureBase
            {
                feature.ConfigureTypeMaps.ShouldBeOfLength(1);
                feature.ReverseExecutedCount.ShouldBe(1);

                var typeMapFeature = typeMap.Features.Get <TFeature>();

                typeMapFeature.ShouldNotBeNull();
                typeMapFeature.Value.ShouldBe(feature.Value);
                typeMapFeature.SealedCount.ShouldBe(1);
            }
        }
Example #3
0
        public void Adding_same_feature_should_replace_eachother()
        {
            var featureA = new MappingExpressionFeatureA(1);
            var featureB = new MappingExpressionFeatureB(2);
            var config   = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <Source, Dest>()
                .SetFeature(new MappingExpressionFeatureA(3))
                .SetFeature(new MappingExpressionFeatureA(2))
                .SetFeature(featureA)
                .SetFeature(new MappingExpressionFeatureB(3))
                .SetFeature(new MappingExpressionFeatureB(2))
                .SetFeature(featureB)
                .ReverseMap();
            });


            var typeMap = config.FindTypeMapFor <Source, Dest>();

            typeMap.Features.Count().ShouldBe(2);

            var typeMapReverse = config.ResolveTypeMap(typeof(Dest), typeof(Source));

            typeMapReverse.Features.Count().ShouldBe(0);

            Validate(featureA);
            Validate(featureB);

            void Validate(MappingExpressionFeatureBase feature)
            {
                feature.ConfigureTypeMaps.ShouldBeOfLength(1);
                feature.ReverseExecutedCount.ShouldBe(1);
            }
        }
Example #4
0
        public void Add_multiple_features_with_reverse_overriden()
        {
            var featureA          = new MappingExpressionFeatureA(1);
            var featureB          = new MappingExpressionFeatureB(2);
            var overridenFeatureB = new MappingExpressionFeatureB(10);
            var config            = new MapperConfiguration(
                cfg =>
            {
                cfg.CreateMap <Source, Dest>()
                .SetFeature(featureA)
                .SetFeature(featureB)
                .ReverseMap()
                .SetFeature(overridenFeatureB);
            }
                );

            var typeMap = config.FindTypeMapFor <Source, Dest>();

            typeMap.Features.Count().ShouldBe(2);

            var typeMapReverse = config.FindTypeMapFor <Dest, Source>();

            typeMapReverse.Features.Count().ShouldBe(2);

            Validate <TypeMapFeatureA>(featureA, typeMap);
            Validate <TypeMapFeatureB>(featureB, typeMap);
            Validate <TypeMapFeatureA>(featureA, typeMapReverse, value: featureA.Value + 1);
            Validate <TypeMapFeatureB>(overridenFeatureB, typeMapReverse, 0);

            void Validate <TFeature>(
                MappingExpressionFeatureBase feature,
                TypeMap map,
                int reverseExecutedCount = 1,
                int?value = null
                ) where TFeature : TypeMapFeatureBase
            {
                feature.ConfigureTypeMaps.ShouldBeOfLength(1);
                feature.ReverseMaps.ShouldBeOfLength(reverseExecutedCount);

                var typeMapFeature = map.Features.Get <TFeature>();

                typeMapFeature.ShouldNotBeNull();
                typeMapFeature.Value.ShouldBe(value ?? feature.Value);
                typeMapFeature.SealedCount.ShouldBe(1);
            }
        }
Example #5
0
        public void Add_multiple_features()
        {
            var featureA = new MappingExpressionFeatureA(1);
            var featureB = new MappingExpressionFeatureB(2);
            var config   = new MapperConfiguration(cfg =>
            {
                cfg.CreateMissingTypeMaps = true;
                cfg.CreateMap <Source, Dest>()
                .SetFeature(featureA)
                .SetFeature(featureB);
            });


            var typeMap = config.FindTypeMapFor <Source, Dest>();

            typeMap.Features.Count().ShouldBe(2);

            var typeMapReverse = config.ResolveTypeMap(typeof(Dest), typeof(Source));

            typeMapReverse.Features.Count().ShouldBe(0);

            Validate <TypeMapFeatureA>(featureA);
            Validate <TypeMapFeatureB>(featureB);

            void Validate <TFeature>(MappingExpressionFeatureBase feature)
                where TFeature : TypeMapFeatureBase
            {
                feature.ConfigureTypeMaps.ShouldBeOfLength(1);
                feature.ReverseExecutedCount.ShouldBe(0);

                var typeMapFeature = typeMap.Features.Get <TFeature>();

                typeMapFeature.ShouldNotBeNull();
                typeMapFeature.Value.ShouldBe(feature.Value);
                typeMapFeature.SealedCount.ShouldBe(1);
            }
        }