Example #1
0
        private static IEnumerable <DynamicObject> ReMapCollection(DynamicObject dynamicObject)
        {
            var mapper = new DynamicObjectMapper();
            var items  = mapper.Map(dynamicObject);

            return(mapper.MapCollection(items));
        }
        public When_mapping_dynamic_objects_to_list_of_known_types()
        {
            dynamicObjects = new[]
            {
                new DynamicObject(typeof(CustomReferenceType))
                {
                    Properties = new PropertySet
                    {
                        { string.Empty, new CustomReferenceType {
                              Int32Property = 1, StringProperty = "One"
                          } },
                    },
                },
                new DynamicObject(typeof(CustomReferenceType))
                {
                    Properties = new PropertySet
                    {
                        { string.Empty, new CustomReferenceType {
                              Int32Property = 2, StringProperty = "Two"
                          } },
                    },
                },
            };

            var mapper = new DynamicObjectMapper(isKnownTypeProvider: new IsKnownTypeProvider());

            recreatedObjectLists = mapper.Map <CustomReferenceType>(dynamicObjects);
        }
        public void Sum()
        {
            var mapper = new DynamicObjectMapper<TestObject>(commandHandlers);
            TestObject testObject = new TestObject { Value1 = 1, Value2 = 2.1M, Value3 = 3.01D };

            dynamic object2 = mapper.Map(testObject, new[] { _testConfigDictionary["SumConfig"] });

            Assert.Equal(6.11M, object2.TotalSumValue);
        }
Example #4
0
        public void Concat()
        {
            var mapper = new DynamicObjectMapper<TestObject>(commandHandlers);
            TestObject testObject = new TestObject { FirstName = "Foo", LastName = "Bar", MiddleName = "J" };

            dynamic object2 = mapper.Map(testObject, new[] { _testConfigDictionary["ConcatConfig"] });

            Assert.Equal("Foo J Bar", object2.Name);
        }
Example #5
0
        public void Second_element_should_be_null()
        {
            dynamicObjects.ElementAt(1).ShouldBeNull();

            var mapper = new DynamicObjectMapper();
            var value  = dynamicObjects.Select(x => mapper.Map(x)).ElementAt(1);

            value.ShouldBeNull();
        }
Example #6
0
        public void Int()
        {
            var mapper = new DynamicObjectMapper<TestObject>(commandHandlers);
            TestObject testObject = new TestObject { Age = 10 };

            dynamic object2 = mapper.Map(testObject, new[] { _testConfigDictionary["IntConfig"] });

            Assert.Equal(10, object2.Age);
        }
Example #7
0
        public void FlattenToCsv()
        {
            var mapper = new DynamicObjectMapper<TestObject>(commandHandlers);
            TestObject testObject = new TestObject { Things = new[] { "Thing1", "Thing2", "Thing3" } };

            dynamic object2 = mapper.Map(testObject, new[] { _testConfigDictionary["FlattenToCsvConfig"] });

            Assert.Equal("Thing1,Thing2,Thing3", object2.FlattenedThings);
        }
Example #8
0
        public void Enum()
        {
            var mapper = new DynamicObjectMapper<TestObject>(commandHandlers);
            TestObject testObject = new TestObject { TestEnum = TestEnum.Black };

            dynamic object2 = mapper.Map(testObject, new[] { _testConfigDictionary["EnumConfig"] });

            Assert.Equal(TestEnum.Black, object2.MyColor);
        }
Example #9
0
        public void String()
        {
            var mapper = new DynamicObjectMapper<TestObject>(commandHandlers);
            TestObject testObject = new TestObject { FirstName = "Foo", LastName = "Bar" };

            dynamic object2 = mapper.Map(testObject, new[] { _testConfigDictionary["StringFirstNameConfig"], _testConfigDictionary["StringLastNameConfig"] });

            Assert.Equal("Foo", object2.FName);
            Assert.Equal("Bar", object2.LName);
        }
Example #10
0
        public void Guid()
        {
            var mapper = new DynamicObjectMapper<TestObject>(commandHandlers);
            Guid id = new Guid("9d020be8-3f97-4674-aeec-afc2735f64d5");
            TestObject testObject = new TestObject { Id = id };

            dynamic object2 = mapper.Map(testObject, new[] { _testConfigDictionary["GuidConfig"] });

            Assert.Equal(id, object2.Identification);
        }
Example #11
0
        public void Should_silently_skip_unmatching_value_when_allowing_type_validation()
        {
            var mapper = new DynamicObjectMapper(new DynamicObjectMapperSettings {
                SilentlySkipUnassignableMembers = true
            });

            var obj = mapper.Map <CustomType>(dynamicObject);

            obj.ShouldNotBeNull();
        }
Example #12
0
        public void First_element_should_be_int32_value()
        {
            var element = dynamicObjects.ElementAt(0);

            element.Type.ToType().ShouldBe(typeof(int));
            element[string.Empty].ShouldBe(1);
            var mapper = new DynamicObjectMapper();
            var value  = dynamicObjects.Select(x => mapper.Map(x)).ElementAt(0);

            value.ShouldBe(1);
        }
Example #13
0
        public void Third_element_should_be_string_value()
        {
            var element = dynamicObjects.ElementAt(2);

            element.Type.ToType().ShouldBe(typeof(string));
            element[string.Empty].ShouldBe("test");

            var mapper = new DynamicObjectMapper();
            var value  = dynamicObjects.Select(x => mapper.Map(x)).ElementAt(2);

            value.ShouldBe("test");
        }
Example #14
0
        public void Should_throw_when_preventing_type_validation()
        {
            Thread.CurrentThread.CurrentCulture   = CultureInfo.InvariantCulture;
            Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;

            var mapper = new DynamicObjectMapper(new DynamicObjectMapperSettings {
                SilentlySkipUnassignableMembers = false
            });

            var ex = Assert.Throws <DynamicObjectMapperException>(() => mapper.Map <CustomType>(dynamicObject));

            ex.Message.ShouldBe("Object of type 'System.Int64' cannot be converted to type 'System.Int32'.");
        }
        public void Should_serialize_DateTimeOffset_as_property_when_using_string_formatting2()
        {
            var mapperSettings = new DynamicObjectMapperSettings {
                FormatNativeTypesAsString = true
            };
            var mapper = new DynamicObjectMapper(mapperSettings);

            var dto1       = new DateTimeOffset(2, 1, 2, 10, 0, 0, 300, new TimeSpan(1, 30, 0));
            var dtoDynamic = mapper.MapObject(dto1);
            var dto2       = mapper.Map <DateTimeOffset>(dtoDynamic);

            dto2.ShouldBe(dto1);
        }
Example #16
0
        public void Fourth_element_should_be_dynamicobject_for_customtype()
        {
            var element = dynamicObjects.ElementAt(3);

            element.Type.ToType().ShouldBe(typeof(CustomType));
            element["Int64Property"].ShouldBe(42L);

            var mapper = new DynamicObjectMapper();
            var value  = dynamicObjects.Select(x => mapper.Map(x)).ElementAt(3);

            value.ShouldBeOfType <CustomType>();
            ((CustomType)value).Int64Property.ShouldBe(42L);
        }
        public void Should_serialize_DateTimeOffset_as_property_when_using_string_formatting2()
        {
            Skip.If(this.TestIs <JsonSerializer>(), "DateTimeOffset not supported by JsonSerializer");

            var mapperSettings = new DynamicObjectMapperSettings {
                FormatNativeTypesAsString = true
            };
            var mapper = new DynamicObjectMapper(mapperSettings);

            var dto1       = new DateTimeOffset(2, 1, 2, 10, 0, 0, 300, new TimeSpan(1, 30, 0));
            var dtoDynamic = mapper.MapObject(dto1);
            var dto2       = mapper.Map <DateTimeOffset>(dtoDynamic);

            dto2.ShouldBe(dto1);
        }
Example #18
0
        public void Map_type_should_return_a_null_element_for_null_dynamic_object_enumerable_element()
        {
            var dynamicObjects = new DynamicObject[]
            {
                new DynamicObject(new CustomClass()),
                null,
                new DynamicObject(new CustomClass()),
            };

            var mapper = new DynamicObjectMapper();
            var result = dynamicObjects.Select(x => mapper.Map(x, typeof(CustomClass)));

            result.ShouldNotBeNull();
            result.Count().ShouldBe(3);
            result.ElementAt(0).ShouldBeOfType <CustomClass>();
            result.ElementAt(1).ShouldBeNull();
            result.ElementAt(2).ShouldBeOfType <CustomClass>();
        }
Example #19
0
 public void Map(GeocacheActivity @object)
 {
     Mapper.Map(@object, (new Constants()).GetRandomProperties());
 }
Example #20
0
 public void Map(GeocacheActivity @object)
 {
     Mapper.Map(@object, "referenceCode", "activityOwner", "dateTimeCreatedUtc", "activityDate", "isArchived", "activityText", "url", "activityType", "updatedLatitude", "updatedLongitude", "isTextRot13", "isApproved", "cannotDelete", "geocache");
 }
Example #21
0
 public void Map(SimpleObject @object)
 {
     Mapper.Map(@object, "Id", "Name");
 }