예제 #1
1
        public void After_Mapping()
        {
            TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
                .AfterMapping((src, dest) => dest.Name += "xxx");

            var poco = new SimplePoco
            {
                Id = Guid.NewGuid(),
                Name = "test",
            };
            var result = TypeAdapter.Adapt<SimpleDto>(poco);

            result.Id.ShouldBe(poco.Id);
            result.Name.ShouldBe(poco.Name + "xxx");
        }
예제 #2
0
        public void Clone()
        {
            TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
                .Map(dest => dest.Name, src => "a");

            var poco = new SimplePoco
            {
                Id = Guid.NewGuid(),
                Name = "test",
            };
            var result = TypeAdapter.Adapt<SimpleDto>(poco);
            result.Name.ShouldBe("a");

            var config = TypeAdapterConfig.GlobalSettings.Clone();
            var global = TypeAdapterConfig.GlobalSettings;
            config.ShouldNotBeSameAs(global);
            config.Default.ShouldNotBeSameAs(global.Default);
            config.Default.Settings.ShouldNotBeSameAs(global.Default.Settings);
            config.RuleMap.ShouldNotBeSameAs(global.RuleMap);
            foreach (var kvp in config.RuleMap)
            {
                var globalRule = global.RuleMap[kvp.Key];
                kvp.Value.ShouldNotBeSameAs(globalRule);
                kvp.Value.Settings.ShouldNotBeSameAs(globalRule.Settings);
            }
            config.Rules.ShouldNotBeSameAs(global.Rules);
            for (var i = 0; i < config.Rules.Count; i++)
            {
                config.Rules[i].ShouldNotBeSameAs(global.Rules[i]);
                config.Rules[i].Settings.ShouldNotBeSameAs(global.Rules[i].Settings);
            }
            config.Rules.Any(rule => object.ReferenceEquals(rule.Settings, config.Default.Settings)).ShouldBeTrue();
            config.Rules.ShouldContain(config.RuleMap[new TypeTuple(typeof(SimplePoco), typeof(SimpleDto))]);
        }
예제 #3
0
        public void Forked_Config_Should_Not_Apply_To_Parent()
        {
            var config = new TypeAdapterConfig();

            config.NewConfig <SimplePoco, SimpleDto>()
            .Map(dest => dest.Name2, src => src.Name2 + "Parent");

            var fork = config.Fork(child =>
                                   child.ForType <SimplePoco, SimpleDto>()
                                   .Map(dest => dest.Name1, src => src.Name1 + "Child"));

            var poco = new SimplePoco
            {
                Id    = Guid.NewGuid(),
                Name1 = "Name1",
                Name2 = "Name2",
            };

            var dtoInline = poco.Adapt <SimplePoco, SimpleDto>(fork);

            dtoInline.Id.ShouldBe(poco.Id);
            dtoInline.Name1.ShouldBe("Name1Child");
            dtoInline.Name2.ShouldBe("Name2Parent");

            var dtoParent = poco.Adapt <SimplePoco, SimpleDto>(config);

            dtoParent.Id.ShouldBe(poco.Id);
            dtoParent.Name1.ShouldBe("Name1");
            dtoParent.Name2.ShouldBe("Name2Parent");
        }
        public void Object_To_Dictionary_CamelCase()
        {
            var config = new TypeAdapterConfig();

            config.NewConfig <SimplePoco, IDictionary <string, object> >()
            .TwoWays()
            .NameMatchingStrategy(NameMatchingStrategy.ToCamelCase);

            var poco = new SimplePoco
            {
                Id   = Guid.NewGuid(),
                Name = "test",
            };

            var dict = poco.Adapt <SimplePoco, IDictionary <string, object> >(config);

            dict.Count.ShouldBe(2);
            dict["id"].ShouldBe(poco.Id);
            dict["name"].ShouldBe(poco.Name);

            var poco2 = dict.Adapt <SimplePoco>(config);

            poco2.Id.ShouldBe(dict["id"]);
            poco2.Name.ShouldBe(dict["name"]);
        }
예제 #5
0
        public void Fork_Setting()
        {
            var config = new TypeAdapterConfig();

            config.NewConfig <SimplePoco, SimpleDto>()
            .Fork(cfg =>
                  cfg.ForType <string, string>()
                  .MapToTargetWith((src, dest) => string.IsNullOrEmpty(src) ? dest : src));

            var poco = new SimplePoco
            {
                Id    = Guid.NewGuid(),
                Name1 = "Name1",
                Name2 = "",
            };

            var dto = new SimpleDto
            {
                Id    = poco.Id,
                Name1 = "Foo",
                Name2 = "Bar",
            };

            poco.Adapt(dto, config);

            dto.Name1.ShouldBe(poco.Name1);
            dto.Name2.ShouldBe("Bar");

            var str = poco.Name2.Adapt(dto.Name2, config);

            str.ShouldBe(poco.Name2);
        }
예제 #6
0
        public void Non_Public_Destination_Setter_Is_Populated()
        {
            var poco = new SimplePoco {Id = Guid.NewGuid(), Name = "TestName"};

            SimpleDto dto = TypeAdapter.Adapt<SimplePoco, SimpleDto>(poco);

            dto.Id.ShouldEqual(poco.Id);
            dto.Name.ShouldEqual(poco.Name);
        }
        public void Global_Destination_Transform_Is_Applied_To_Class()
        {
            TypeAdapterConfig.GlobalSettings.Default.AddDestinationTransform((string x) => x.Trim());
            TypeAdapterConfig<string, string>.Clear();

            var source = new SimplePoco {Id = new Guid(), Name = "Test    "};
            var destination = TypeAdapter.Adapt<SimpleDto>(source);

            destination.Name.ShouldEqual("Test");
        }
예제 #8
0
        public void Map_RecordType()
        {
            var source = new SimplePoco {Id = Guid.NewGuid(), Name = "bar"};
            var dest = source.Adapt<RecordType>();

            dest.Id.ShouldBe(source.Id);
            dest.Name.ShouldBe(source.Name);
            dest.Day.ShouldBe(default(DayOfWeek));
            dest.Age.ShouldBe(10);
        }
예제 #9
0
        public void Global_Destination_Transform_Is_Applied_To_Class()
        {
            TypeAdapterConfig.GlobalSettings.DestinationTransforms.Upsert <string>(x => x.Trim());

            var source = new SimplePoco {
                Id = new Guid(), Name = "Test    "
            };
            var destination = TypeAdapter.Adapt <SimpleDto>(source);

            destination.Name.ShouldEqual("Test");
        }
예제 #10
0
        public void Uri_Property_Success()
        {
            var sourceDto = new SimplePoco
            {
                Id      = 1,
                Website = new Uri("http://example.com"),
            };
            var targetDto = TypeAdapter.Adapt <SimplePoco, SimplePoco>(sourceDto);

            targetDto.Website.ShouldEqual(sourceDto.Website);
        }
        public void Adapter_Destination_Transform_Is_Applied_To_Class()
        {
            var config = TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig();
            config.AddDestinationTransform((string x) => x.Trim());
            config.Compile();

            var source = new SimplePoco { Id = new Guid(), Name = "Test    " };
            var destination = TypeAdapter.Adapt<SimpleDto>(source);

            destination.Name.ShouldEqual("Test");
        }
        public void Transform_Doesnt_Occur_If_None_Present()
        {
            TypeAdapterConfig<string, string>.Clear();
            TypeAdapterConfig<SimplePoco, SimpleDto>.Clear();

            var source = new SimplePoco { Id = new Guid(), Name = "Test    " };

            var destination = TypeAdapter.Adapt<SimpleDto>(source);

            destination.Name.ShouldEqual(source.Name);
        }
예제 #13
0
        public void Non_Public_Destination_Setter_Is_Populated()
        {
            var poco = new SimplePoco {
                Id = Guid.NewGuid(), Name = "TestName"
            };

            SimpleDto dto = TypeAdapter.Adapt <SimplePoco, SimpleDto>(poco);

            dto.Id.ShouldBe(poco.Id);
            dto.Name.ShouldBe(poco.Name);
        }
예제 #14
0
        public void Uri_Property_Success()
        {
            var sourceDto = new SimplePoco
            {
                Id = 1,
                Website = new Uri("http://example.com"),
            };
            var targetDto = TypeAdapter.Adapt<SimplePoco, SimplePoco>(sourceDto);

            targetDto.Website.ShouldEqual(sourceDto.Website);
        }
예제 #15
0
        public void Rule_Base_Testing()
        {
            TypeAdapterConfig.GlobalSettings.When((srcType, destType, mapType) => srcType == destType)
                .Ignore("Id");

            var simplePoco = new SimplePoco {Id = Guid.NewGuid(), Name = "TestName"};

            var dto = TypeAdapter.Adapt<SimplePoco>(simplePoco);

            dto.Id.ShouldEqual(Guid.Empty);
            dto.Name.ShouldEqual(simplePoco.Name);
        }
예제 #16
0
        public void Mapped_Classes_Succeed()
        {
            TypeAdapterConfig.GlobalSettings.RequireExplicitMapping = true;

            TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig();

            var simplePoco = new SimplePoco { Id = Guid.NewGuid(), Name = "TestName" };

            var simpleDto = TypeAdapter.Adapt<SimplePoco, SimpleDto>(simplePoco);

            simpleDto.Name.ShouldEqual(simplePoco.Name);
        }
예제 #17
0
        public void Map_RecordType()
        {
            var source = new SimplePoco {
                Id = Guid.NewGuid(), Name = "bar"
            };
            var dest = source.Adapt <RecordType>();

            dest.Id.ShouldBe(source.Id);
            dest.Name.ShouldBe(source.Name);
            dest.Day.ShouldBe(default(DayOfWeek));
            dest.Age.ShouldBe(10);
        }
예제 #18
0
        public void When_Setter_Throws_Exception_Bubbles_Up()
        {
            TypeAdapterConfig<SimplePoco, SimpleDtoThatThrowsOnSet>.NewConfig().Compile();

            var poco = new SimplePoco
            {
                Id = new Guid(),
                Name = "TestName"
            };

            Assert.Throws<InvalidOperationException>(() => TypeAdapter.Adapt<SimplePoco, SimpleDtoThatThrowsOnSet>(poco));
        }
예제 #19
0
        public void Preserve_Reference_For_List()
        {
            TypeAdapterConfig.GlobalSettings.Default.Settings.PreserveReference = true;

            var poco = new SimplePoco {Id = Guid.NewGuid(), Name = "TestName"};

            var array = new[] {poco, poco};

            var array2 = TypeAdapter.Adapt<SimplePoco[], SimpleDto[]>(array);

            array2[0].ShouldBeSameAs(array2[1]);
        }
예제 #20
0
        public void Error_Thrown_With_Explicit_Configuration_On_Unmapped_Primitive()
        {
            TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = true;

            var source = new SimplePoco {
                Id = Guid.NewGuid(), Name = "TestName"
            };

            var exception = Assert.Throws <ArgumentOutOfRangeException>(() => TypeAdapter.Adapt <SimplePoco, SimpleDto>(source));

            exception.Message.ShouldContain("UnmappedMember");
        }
예제 #21
0
        public void Using_Attributes()
        {
            var id   = Guid.NewGuid();
            var poco = new SimplePoco(id)
            {
                Name = "test"
            };
            var dto = poco.Adapt <SimpleDto>();

            dto.Id.ShouldBe(id);
            dto.Name.ShouldBeNull();
        }
예제 #22
0
        public void Mapping_Basic_Poco_Succeeds()
        {
            TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
                .Compile();

            IAdapter instance = TypeAdapter.GetInstance();
            var source = new SimplePoco { Id = new Guid(), Name = "Test" };

            var destination = instance.Adapt<SimpleDto>(source);

            destination.Name.ShouldEqual(source.Name);
        }
        public void Global_Destination_Transform_Is_Applied_To_Class()
        {
            TypeAdapterConfig.GlobalSettings.Default.AddDestinationTransform((string x) => x.Trim());
            TypeAdapterConfig <string, string> .Clear();

            var source = new SimplePoco {
                Id = new Guid(), Name = "TestMethod"
            };
            var destination = TypeAdapter.Adapt <SimpleDto>(source);

            destination.Name.ShouldBe("TestMethod");
        }
예제 #24
0
        public void Transform_Doesnt_Occur_If_None_Present()
        {
            TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig();

            var source = new SimplePoco {
                Id = new Guid(), Name = "Test    "
            };

            var destination = TypeAdapter.Adapt <SimpleDto>(source);

            destination.Name.ShouldEqual(source.Name);
        }
예제 #25
0
        public void When_Setter_Throws_Exception_Bubbles_Up()
        {
            TypeAdapterConfig <SimplePoco, SimpleDtoThatThrowsOnSet> .NewConfig().Compile();

            var poco = new SimplePoco
            {
                Id   = new Guid(),
                Name = "TestName"
            };

            Should.Throw <InvalidOperationException>(() => TypeAdapter.Adapt <SimplePoco, SimpleDtoThatThrowsOnSet>(poco));
        }
예제 #26
0
        public void No_Errors_Thrown_With_Default_Configuration_On_Unmapped_Primitive()
        {
            TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = false;
            TypeAdapterConfig<ParentPoco, ParentDto>.NewConfig().Compile();

            var source = new SimplePoco {Id = Guid.NewGuid(), Name = "TestName"};

            var simpleDto = TypeAdapter.Adapt<SimplePoco, SimpleDto>(source);

            simpleDto.Name.ShouldBe("TestName");
            simpleDto.UnmappedMember.ShouldBeNull();
            simpleDto.UnmappedMember2.ShouldBe(0);
        }
예제 #27
0
        public void Failed_Condition_Primitive_Does_Not_Map()
        {
            TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
                .Map(dest => dest.Name, src => src.Name, cond => cond.Name != "TestName")
                .Compile();

            var poco = new SimplePoco { Id = Guid.NewGuid(), Name = "TestName" };

            SimpleDto dto = TypeAdapter.Adapt<SimplePoco, SimpleDto>(poco);

            dto.Id.ShouldEqual(poco.Id);
            dto.Name.ShouldBeNull();
        }
예제 #28
0
        public void Mapping_Basic_Poco_Succeeds()
        {
            TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig();

            IAdapter instance = TypeAdapter.GetInstance();
            var      source   = new SimplePoco {
                Id = new Guid(), Name = "Test"
            };

            var destination = instance.Adapt <SimpleDto>(source);

            destination.Name.ShouldEqual(source.Name);
        }
예제 #29
0
        public void True_Condition_On_Target_Ignores_Map()
        {
            TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig()
            .IgnoreIf((src, dest) => !string.IsNullOrEmpty(dest.Name), dest => dest.Name)
            .Compile();

            var poco = new SimplePoco {
                Id = 1, Name = "TestName"
            };
            var dto = TypeAdapter.Adapt <SimplePoco, SimpleDto>(poco);

            dto.Id.ShouldBe(1);
            dto.Name.ShouldBe("TestName");
        }
예제 #30
0
        public void Using_Attributes_With_NameMatchingStrategy()
        {
            TypeAdapterConfig.GlobalSettings.Default.NameMatchingStrategy(NameMatchingStrategy.IgnoreCase);

            var id   = Guid.NewGuid();
            var poco = new SimplePoco(id)
            {
                Name = "test"
            };
            var dto = poco.Adapt <SimpleDto>();

            dto.IdCode.ShouldBe(id);
            dto.Name.ShouldBeNull();
        }
예제 #31
0
        public void Map_To_Existing_Destination_Instance_Should_Pass()
        {
            var simplePoco = new SimplePoco {
                Id = Guid.NewGuid(), Name = "TestName"
            };

            var dto = new SimpleDtoWithoutDefaultConstructor("unmapped");

            simplePoco.Adapt(dto);

            dto.Id.ShouldBe(simplePoco.Id);
            dto.Name.ShouldBe(simplePoco.Name);
            dto.Unmapped.ShouldBe("unmapped");
        }
예제 #32
0
        public void True_Constant_Ignores_Map()
        {
            TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig()
            .IgnoreIf((src, dest) => true, dest => dest.Name)
            .Compile();

            var poco = new SimplePoco {
                Id = 1, Name = "TestName"
            };
            SimpleDto dto = TypeAdapter.Adapt <SimplePoco, SimpleDto>(poco);

            dto.Id.ShouldBe(1);
            dto.Name.ShouldBeNull();
        }
예제 #33
0
        public void Map_To_Destination_Type_Without_Default_Constructor_Shoud_Throw_Exception()
        {
            var simplePoco = new SimplePoco {
                Id = Guid.NewGuid(), Name = "TestName"
            };

            Action action = () =>
            {
                var dto = TypeAdapter.Adapt <SimpleDtoWithoutDefaultConstructor>(simplePoco);
            };

            action.ShouldThrow <CompileException>()
            .InnerException.ShouldBeOfType <InvalidOperationException>();
        }
예제 #34
0
        public void Object_To_Dictionary()
        {
            var poco = new SimplePoco
            {
                Id = Guid.NewGuid(),
                Name = "test",
            };

            var dict = TypeAdapter.Adapt<Dictionary<string, object>>(poco);

            dict.Count.ShouldBe(2);
            dict["Id"].ShouldBe(poco.Id);
            dict["Name"].ShouldBe(poco.Name);
        }
        public void Mapped_Classes_Succeed()
        {
            TypeAdapterConfig.GlobalSettings.RequireExplicitMapping = true;

            TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig();

            var simplePoco = new SimplePoco {
                Id = Guid.NewGuid(), Name = "TestName"
            };

            var simpleDto = TypeAdapter.Adapt <SimplePoco, SimpleDto>(simplePoco);

            simpleDto.Name.ShouldEqual(simplePoco.Name);
        }
예제 #36
0
        public void After_Mapping_With_DestinationType_Setting()
        {
            TypeAdapterConfig.GlobalSettings.ForDestinationType<IValidatable>()
                .AfterMapping(dest => dest.Validate());

            var poco = new SimplePoco
            {
                Id = Guid.NewGuid(),
                Name = "test",
            };
            var result = TypeAdapter.Adapt<SimpleDto>(poco);

            result.IsValidated.ShouldBeTrue();
        }
예제 #37
0
        public void Null_Condition_Ignores_Map()
        {
            TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig()
            .IgnoreIf(null, dest => dest.Name)
            .Compile();

            var poco = new SimplePoco {
                Id = 1, Name = "TestName"
            };
            var dto = TypeAdapter.Adapt <SimplePoco, SimpleDto>(poco);

            dto.Id.ShouldBe(1);
            dto.Name.ShouldBeNull();
        }
예제 #38
0
        public void Rule_Base_Testing()
        {
            TypeAdapterConfig.GlobalSettings.When((srcType, destType, mapType) => srcType == destType)
            .Ignore("Id");

            var simplePoco = new SimplePoco {
                Id = Guid.NewGuid(), Name = "TestName"
            };

            var dto = TypeAdapter.Adapt <SimplePoco>(simplePoco);

            dto.Id.ShouldBe(Guid.Empty);
            dto.Name.ShouldBe(simplePoco.Name);
        }
예제 #39
0
        public void IgnoreIf_Apply_To_RecordType()
        {
            TypeAdapterConfig <SimplePoco, SimpleRecord> .NewConfig()
            .IgnoreIf((src, dest) => src.Name == "TestName", dest => dest.Name)
            .Compile();

            var poco = new SimplePoco {
                Id = 1, Name = "TestName"
            };
            var dto = TypeAdapter.Adapt <SimplePoco, SimpleRecord>(poco);

            dto.Id.ShouldBe(1);
            dto.Name.ShouldBeNull();
        }
예제 #40
0
        public void Map_With_Adapter()
        {
            TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig()
            .Compile();

            IMapper instance = new Mapper();
            var     source   = new SimplePoco {
                Id = Guid.NewGuid(), Name = "TestMethod"
            };

            var destination = instance.From(source).AdaptToType <SimpleDto>();

            destination.Name.ShouldBe(source.Name);
        }
예제 #41
0
        public void Property_Is_Mapped_From_Null_Value_Successfully()
        {
            TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
                .Map(dest => dest.AnotherName, src => (string)null)
                .Compile();

            var poco = new SimplePoco { Id = Guid.NewGuid(), Name = "TestName" };

            var dto = TypeAdapter.Adapt<SimplePoco, SimpleDto>(poco);

            dto.Id.ShouldEqual(poco.Id);
            dto.Name.ShouldEqual(poco.Name);
            dto.AnotherName.ShouldBeNull();
        }
예제 #42
0
        public void False_Condition_Primitive_Does_Not_Map()
        {
            TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
                .Map(dest => dest.Name, src => src.Name, cond => false)
                .Compile();

            var poco = new SimplePoco {Id = Guid.NewGuid(), Name = "TestName"};

            IAdapter instance = TypeAdapter.GetInstance();
            SimpleDto dto = instance.Adapt<SimplePoco, SimpleDto>(poco);

            dto.Id.ShouldBe(poco.Id);
            dto.Name.ShouldBeNull();
        }
        public void Adapter_Destination_Transform_Is_Applied_To_Class()
        {
            var config = TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig();

            config.AddDestinationTransform((string x) => x.Trim());
            config.Compile();

            var source = new SimplePoco {
                Id = new Guid(), Name = "TestMethod    "
            };
            var destination = TypeAdapter.Adapt <SimplePoco, SimpleDto>(source);

            destination.Name.ShouldBe("TestMethod");
        }
        public void Transform_Doesnt_Occur_If_None_Present()
        {
            TypeAdapterConfig <string, string> .Clear();

            TypeAdapterConfig <SimplePoco, SimpleDto> .Clear();

            var source = new SimplePoco {
                Id = new Guid(), Name = "TestMethod    "
            };

            var destination = TypeAdapter.Adapt <SimpleDto>(source);

            destination.Name.ShouldBe(source.Name);
        }
예제 #45
0
        public void Passed_Condition_Primitive_Does_Map()
        {
            TypeAdapterConfig <SimplePoco, SimpleDto> .NewConfig()
            .Map(dest => dest.Name, src => src.Name, cond => cond.Name == "TestName");

            var poco = new SimplePoco {
                Id = Guid.NewGuid(), Name = "TestName"
            };

            SimpleDto dto = TypeAdapter.Adapt <SimplePoco, SimpleDto>(poco);

            dto.Id.ShouldEqual(poco.Id);
            dto.Name.ShouldEqual("TestName");
        }
예제 #46
0
        public void Property_Is_Mapped_To_Different_Property_Successfully()
        {
            TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
                .Map(dest => dest.AnotherName, src => src.Name)
                .Compile();

            var poco = new SimplePoco {Id = Guid.NewGuid(), Name = "TestName"};

            var dto = TypeAdapter.Adapt<SimplePoco, SimpleDto>(poco);

            dto.Id.ShouldEqual(poco.Id);
            dto.Name.ShouldEqual(poco.Name);
            dto.AnotherName.ShouldEqual(poco.Name);
        }
예제 #47
0
        public void No_Errors_Thrown_With_Default_Configuration_On_Unmapped_Primitive()
        {
            TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = false;

            var source = new SimplePoco {
                Id = Guid.NewGuid(), Name = "TestName"
            };

            var simpleDto = TypeAdapter.Adapt <SimplePoco, SimpleDto>(source);

            simpleDto.Name.ShouldEqual("TestName");
            simpleDto.UnmappedMember.ShouldBeNull();
            simpleDto.UnmappedMember2.ShouldEqual(0);
        }
예제 #48
0
        public void After_Mapping_With_DestinationType_Setting()
        {
            TypeAdapterConfig.GlobalSettings.ForDestinationType <IValidatable>()
            .AfterMapping(dest => dest.Validate());

            var poco = new SimplePoco
            {
                Id   = Guid.NewGuid(),
                Name = "test",
            };
            var result = TypeAdapter.Adapt <SimpleDto>(poco);

            result.IsValidated.ShouldBeTrue();
        }
예제 #49
0
        public void Object_To_Dictionary()
        {
            var poco = new SimplePoco
            {
                Id   = Guid.NewGuid(),
                Name = "test",
            };

            var dict = TypeAdapter.Adapt <Dictionary <string, object> >(poco);

            dict.Count.ShouldBe(2);
            dict["Id"].ShouldBe(poco.Id);
            dict["Name"].ShouldBe(poco.Name);
        }
예제 #50
0
        public void Preserve_Reference_For_List()
        {
            TypeAdapterConfig.GlobalSettings.Default.Settings.PreserveReference = true;

            var poco = new SimplePoco {
                Id = Guid.NewGuid(), Name = "TestName"
            };

            var array = new[] { poco, poco };

            var array2 = TypeAdapter.Adapt <SimplePoco[], SimpleDto[]>(array);

            array2[0].ShouldBeSameAs(array2[1]);
        }
예제 #51
0
        public void When_Condition_Throws_Exception_Bubbles_Up()
        {
            TypeAdapterConfig<SimplePoco, SimplePoco>.NewConfig()
                .Map(dest => dest.Amount, src => src.Amount, cond => cond.Amount/cond.Count > 0).Compile();

            var poco = new SimplePoco
            {
                Id = new Guid(),
                Name = "TestName",
                Amount = 100,
                Count = 0
            };

            var exception = Assert.Throws<DivideByZeroException>(() => TypeAdapter.Adapt<SimplePoco, SimplePoco>(poco));
        }
예제 #52
0
        public void Object_To_Dictionary_CamelCase()
        {
            TypeAdapterConfig.GlobalSettings.Default.NameMatchingStrategy(NameMatchingStrategy.ToCamelCase);
            var poco = new SimplePoco
            {
                Id = Guid.NewGuid(),
                Name = "test",
            };

            var dict = TypeAdapter.Adapt<Dictionary<string, object>>(poco);

            dict.Count.ShouldBe(2);
            dict["id"].ShouldBe(poco.Id);
            dict["name"].ShouldBe(poco.Name);
        }
        public void Dest_Calls_Calls_Factory_Method_With_ConstructUsing()
        {
            TypeAdapterConfig<SimplePoco, SimpleDtoWithDefaultConstructor>.NewConfig()
                .IgnoreNullValues(true)
                .ConstructUsing(src => new SimpleDtoWithDefaultConstructor{Unmapped = "unmapped"})
                .Compile();

            var simplePoco = new SimplePoco { Id = Guid.NewGuid(), Name = "TestName" };

            var dto = TypeAdapter.Adapt<SimpleDtoWithDefaultConstructor>(simplePoco);

            dto.Id.ShouldEqual(simplePoco.Id);
            dto.Name.ShouldEqual(simplePoco.Name);
            dto.Unmapped.ShouldEqual("unmapped");
        }
        public void Construct_From_Interface()
        {
            TypeAdapterConfig<SimplePoco, ISimpleDtoWithDefaultConstructor>.NewConfig()
                .IgnoreNullValues(true)
                .ConstructUsing(src => new SimpleDtoWithDefaultConstructor {Unmapped = "unmapped"})
                .Compile();

            var simplePoco = new SimplePoco { Id = Guid.NewGuid(), Name = "TestName" };

            var dto = TypeAdapter.Adapt<ISimpleDtoWithDefaultConstructor>(simplePoco);

            dto.Id.ShouldBe(simplePoco.Id);
            dto.Name.ShouldBe(simplePoco.Name);
            dto.Unmapped.ShouldBe("unmapped");
        }
예제 #55
0
        public void Error_Thrown_With_Explicit_Configuration_On_Unmapped_Primitive()
        {
            try
            {
                TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = true;
                TypeAdapterConfig<ParentPoco, ParentDto>.NewConfig().Compile();

                var source = new SimplePoco {Id = Guid.NewGuid(), Name = "TestName"};

                TypeAdapter.Adapt<SimplePoco, SimpleDto>(source);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                ex.Message.ShouldContain("UnmappedMember");
            }
        }
예제 #56
0
        public void Object_To_Dictionary_Ignore_Null_Values()
        {
            TypeAdapterConfig<SimplePoco, Dictionary<string, object>>.NewConfig()
                .IgnoreNullValues(true);

            var poco = new SimplePoco
            {
                Id = Guid.NewGuid(),
                Name = null,
            };

            var dict = TypeAdapter.Adapt<Dictionary<string, object>>(poco);

            dict.Count.ShouldBe(1);
            dict["Id"].ShouldBe(poco.Id);
        }
예제 #57
0
        public void Passing_Runtime_Value()
        {
            TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
                .Map(dest => dest.CreatedBy,
                    src => MapContext.Current.Parameters["user"]);

            var poco = new SimplePoco
            {
                Id = Guid.NewGuid(),
                Name = "test",
            };
            var dto = poco.BuildAdapter()
                .AddParameters("user", this.User)
                .AdaptToType<SimpleDto>();

            dto.CreatedBy.ShouldBe(this.User);
        }
예제 #58
0
        public void Alter_Config_After_Map_Should_Error()
        {
            TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
                .Map(dest => dest.Name, src => "a");

            var poco = new SimplePoco
            {
                Id = Guid.NewGuid(),
                Name = "test",
            };
            var result = TypeAdapter.Adapt<SimpleDto>(poco);
            result.Name.ShouldBe("a");

            var ex = Assert.Throws<InvalidOperationException>(() =>
                TypeAdapterConfig<SimplePoco, SimpleDto>.ForType()
                    .Map(dest => dest.Name, src => "b"));
            ex.Message.ShouldContain("TypeAdapter.Adapt was already called");
        }
예제 #59
0
        public void Object_To_Dictionary_Flexible()
        {
            TypeAdapterConfig.GlobalSettings.Default.NameMatchingStrategy(NameMatchingStrategy.Flexible);
            var poco = new SimplePoco
            {
                Id = Guid.NewGuid(),
                Name = "test",
            };

            var dict = new Dictionary<string, object>
            {
                ["id"] = Guid.NewGuid()
            };

            TypeAdapter.Adapt(poco, dict);

            dict.Count.ShouldBe(2);
            dict["id"].ShouldBe(poco.Id);
            dict["Name"].ShouldBe(poco.Name);
        }
예제 #60
0
        public void Unmapped_Classes_Should_Throw()
        {
            try
            {
                //compile first to prevent type initialize exception
                TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig().Compile();

                TypeAdapterConfig.GlobalSettings.RequireExplicitMapping = true;
                TypeAdapterConfig<SimplePoco, SimpleDto>.Clear();

                var simplePoco = new SimplePoco {Id = Guid.NewGuid(), Name = "TestName"};

                TypeAdapter.Adapt<SimpleDto>(simplePoco);
                Assert.Fail();
            }
            catch (InvalidOperationException ex)
            {
                ex.Message.ShouldContain("SimplePoco");
                ex.Message.ShouldContain("SimpleDto");
            }
        }