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");
        }
Exemple #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))]);
        }
Exemple #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"]);
        }
Exemple #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);
        }
        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");
        }
        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);
        }
Exemple #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");
        }
        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);
        }
Exemple #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);
        }
        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 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);
        }
        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);
        }
Exemple #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);
        }
        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));
        }
        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]);
        }
Exemple #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");
        }
Exemple #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();
        }
        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");
        }
Exemple #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);
        }
Exemple #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));
        }
        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);
        }
        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();
        }
Exemple #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);
        }
        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");
        }
Exemple #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();
        }
Exemple #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");
        }
        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();
        }
Exemple #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>();
        }
        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);
        }
        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();
        }
        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();
        }
Exemple #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);
        }
        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();
        }
Exemple #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);
        }
        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();
        }
        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);
        }
        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");
        }
        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);
        }
Exemple #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);
        }
        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();
        }
Exemple #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);
        }
Exemple #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]);
        }
        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));
        }
        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");
        }
        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");
            }
        }
        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);
        }
        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);
        }
Exemple #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");
        }
        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);
        }
        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");
            }
        }