Exemplo n.º 1
0
        private static T MapAsValue <T>(T value)
        {
            var dynamicObject = new DynamicObjectMapper().MapObject(value);
            var mappedValue   = new DynamicObjectMapper().Map(dynamicObject);

            return((T)mappedValue);
        }
Exemplo n.º 2
0
 public void Should_map_type_info()
 {
     var type = GetType().GetTypeInfo();
     var o    = new DynamicObjectMapper().MapObject(type);
     var r1   = new DynamicObjectMapper().Map <TypeInfo>(o);
     var r2   = new DynamicObjectMapper().Map <Type>(o);
 }
        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 Should_map_jagged_int_array_to_object_array_containing_int_array()
        {
            var obj = CreateObject(new int[][]
            {
                new[] { 1, 2 },
                new[] { 3 },
                new[] { 4, 5, 6 },
            });

            var dynamicObject = new DynamicObjectMapper().MapObject(obj);

            var items = dynamicObject["Items"];

            var outerArray = items.ShouldBeOfType <int[][]>();

            outerArray.Length.ShouldBe(3);

            var innerArray1 = outerArray[0].ShouldBeOfType <int[]>();

            innerArray1.Length.ShouldBe(2);
            innerArray1[0].ShouldBe(1);
            innerArray1[1].ShouldBe(2);

            var innerArray2 = outerArray[1].ShouldBeOfType <int[]>();

            innerArray2.Length.ShouldBe(1);
            innerArray2[0].ShouldBe(3);

            var innerArray3 = outerArray[2].ShouldBeOfType <int[]>();

            innerArray3.Length.ShouldBe(3);
            innerArray3[0].ShouldBe(4);
            innerArray3[1].ShouldBe(5);
            innerArray3[2].ShouldBe(6);
        }
        public void Should_map_multidimensional_int_array_to_int_array()
        {
#pragma warning disable SA1500 // Braces for multi-line statements should not share line
            var obj = new
            {
                Items = new int[, , ]
                {
                    {
                        { 1, 2, 3 },
                        { 4, 5, 6 },
                    },
                    {
                        { 7, 8, 9 },
                        { 10, 11, 12 },
                    },
                    {
                        { 13, 14, 15 },
                        { 16, 17, 18 },
                    },
                },
            };
#pragma warning restore SA1500 // Braces for multi-line statements should not share line

            var dynamicObject = new DynamicObjectMapper().MapObject(obj);

            var items = dynamicObject["Items"];

            var array = items.ShouldBeOfType <int[]>();
            array.SequenceEqual(Enumerable.Range(1, 18)).ShouldBeTrue();
        }
Exemplo n.º 6
0
        public void Should_serialize_dynamic_object_for_entity_set()
        {
            var entity = new DynamicObjectMapper().MapCollection(new List <ProductEntity>
            {
                new ProductEntity
                {
                    Id    = 1,
                    Name  = "Product E. Name",
                    Price = 1234.56m,
                },
                new ProductEntity
                {
                    Id    = 2,
                    Name  = "Noname",
                    Price = 78.9m,
                },
            });

            var copy = entity.Serialize();

            var item0 = copy.ElementAt(0);

            item0["Id"].ShouldBe(1);
            item0["Name"].ShouldBe("Product E. Name");
            item0["Price"].ShouldBe(1234.56m);

            var item1 = copy.ElementAt(1);

            item1["Id"].ShouldBe(2);
            item1["Name"].ShouldBe("Noname");
            item1["Price"].ShouldBe(78.9m);
        }
 public void Should_map_delegate2()
 {
     F0 f = Method0;
     var o = new DynamicObject(f);
     var r = new DynamicObjectMapper().Map<F0>(o);
     var x = r();
 }
Exemplo n.º 8
0
        public void Map_null_to_nullable_valuetype_should_return_null()
        {
            int?result = new DynamicObjectMapper().Map <int?>(null);

            result.ShouldBeNull();
            result.HasValue.ShouldBeFalse();
        }
 public void Should_map_delegate3()
 {
     F1 f = Method1;
     var o = new DynamicObject(f);
     var r = new DynamicObjectMapper().Map<F1>(o);
     var x = r(5);
 }
Exemplo n.º 10
0
        private static IEnumerable <DynamicObject> ReMapCollection(DynamicObject dynamicObject)
        {
            var mapper = new DynamicObjectMapper();
            var items  = mapper.Map(dynamicObject);

            return(mapper.MapCollection(items));
        }
Exemplo n.º 11
0
        public void Object_item_4_should_be_reference_type()
        {
            var item = new DynamicObjectMapper().Map <CustomClass>(dynamicObject).Items.ElementAt(4);

            item.ShouldBeOfType <CustomReferenceType>()
            .StringProperty.ShouldBe("S 2");
        }
Exemplo n.º 12
0
        public void Object_item_3_should_be_value_type()
        {
            var item = new DynamicObjectMapper().Map <CustomClass>(dynamicObject).Items.ElementAt(3);

            item.ShouldBeOfType <CustomValueType>()
            .Int64Property.ShouldBe(42L);
        }
        public void Should_map_private_property_info()
        {
            var property = GetType().GetProperty(nameof(PrivateProperty), PrivateInstance);
            var o        = new DynamicObject(property);
            var r        = new DynamicObjectMapper().Map <PropertyInfo>(o);

            r.ShouldBeSameAs(property);
        }
        public void Should_map_public_static_method_info()
        {
            var m = GetType().GetMethod(nameof(PublicStaticMethod));
            var o = new DynamicObject(m);
            var r = new DynamicObjectMapper().Map <MethodInfo>(o);

            r.ShouldBeSameAs(m);
        }
        public void Should_map_private_method_info()
        {
            var m = GetType().GetMethod(nameof(PrivateMethod), PrivateInstance);
            var o = new DynamicObject(m);
            var r = new DynamicObjectMapper().Map <MethodInfo>(o);

            r.ShouldBeSameAs(m);
        }
        public void Should_map_constructor_info()
        {
            var ctor = GetType().GetConstructor(Array.Empty <Type>());
            var o    = new DynamicObject(ctor);
            var r    = new DynamicObjectMapper().Map <ConstructorInfo>(o);

            r.ShouldBeSameAs(ctor);
        }
        public void Should_map_public_static_field_info()
        {
            var field = GetType().GetField(nameof(PublicStaticField), PublicStatic);
            var o     = new DynamicObject(field);
            var r     = new DynamicObjectMapper().Map <FieldInfo>(o);

            r.ShouldBeSameAs(field);
        }
Exemplo n.º 18
0
        public void Second_element_should_be_null()
        {
            dynamicObjects.ElementAt(1).ShouldBeNull();

            var value = new DynamicObjectMapper().Map(dynamicObjects).Cast <object>().ElementAt(1);

            value.ShouldBeNull();
        }
        private IEnumerable <T> Roundtrip <T>(IEnumerable <T> obj)
        {
            var dynamicObject           = new DynamicObjectMapper().MapCollection(obj);
            var serializedDynamicObject = _serialize(dynamicObject);
            var resurrected             = new DynamicObjectMapper().Map <T>(serializedDynamicObject);

            return(resurrected);
        }
Exemplo n.º 20
0
        public void Should_map_private_static_field_info()
        {
            var field = GetType().GetField(nameof(_privateStaticField), BindingFlags.NonPublic | BindingFlags.Static);
            var o     = new DynamicObject(field);
            var r     = new DynamicObjectMapper().Map <FieldInfo>(o);

            r.ShouldBeSameAs(field);
        }
        public void Should_map_private_field_info()
        {
            var field = GetType().GetField(nameof(_privateField), PrivateInstance);
            var o     = new DynamicObject(field);
            var r     = new DynamicObjectMapper().Map <FieldInfo>(o);

            r.ShouldBeSameAs(field);
        }
Exemplo n.º 22
0
        public void Should_map_private_static_method_info()
        {
            var m = GetType().GetMethod(nameof(PrivateStaticMethod), BindingFlags.NonPublic | BindingFlags.Static);
            var o = new DynamicObject(m);
            var r = new DynamicObjectMapper().Map <MethodInfo>(o);

            r.ShouldBeSameAs(m);
        }
        private T Roundtrip <T>(T obj)
        {
            var dynamicObject           = new DynamicObjectMapper().MapObject(obj);
            var serializedDynamicObject = _serialize(dynamicObject);
            var resurrected             = new DynamicObjectMapper().Map <T>(serializedDynamicObject);

            return(resurrected);
        }
Exemplo n.º 24
0
        public void Should_map_private_static_property_info()
        {
            var property = GetType().GetProperty(nameof(PrivateStaticProperty), BindingFlags.NonPublic | BindingFlags.Static);
            var o        = new DynamicObject(property);
            var r        = new DynamicObjectMapper().Map <PropertyInfo>(o);

            r.ShouldBeSameAs(property);
        }
        public void Should_map_public_property_info()
        {
            var property = GetType().GetProperty(nameof(PublicProperty));
            var o        = new DynamicObject(property);
            var r        = new DynamicObjectMapper().Map <PropertyInfo>(o);

            r.ShouldBeSameAs(property);
        }
        public void Second_element_should_be_null()
        {
            dynamicObject.Get <object[]>()[1].ShouldBeNull();

            var array = new DynamicObjectMapper().Map <object[]>(dynamicObject);

            array[1].ShouldBeNull();
        }
        public void Third_element_should_be_string_value()
        {
            dynamicObject.Get <object[]>()[2].ShouldBe("test");

            var array = new DynamicObjectMapper().Map <object[]>(dynamicObject);

            array[2].ShouldBe("test");
        }
        public void First_element_should_be_int32_value()
        {
            dynamicObject.Get <object[]>()[0].ShouldBe(1);

            var array = new DynamicObjectMapper().Map <object[]>(dynamicObject);

            array[0].ShouldBe(1);
        }
Exemplo n.º 29
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();
        }
Exemplo n.º 30
0
        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);
        }
Exemplo n.º 31
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);
        }
Exemplo n.º 32
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);
        }
Exemplo n.º 33
0
        private static T MapAsProperty <T>(T value)
        {
            var dynamicObject = new DynamicObjectMapper().MapObject(new A <T> {
                Value = value
            });
            var mappedValue = new DynamicObjectMapper().Map(dynamicObject);

            return(((A <T>)mappedValue).Value);
        }
Exemplo n.º 34
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);
        }
Exemplo n.º 35
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);
        }
Exemplo n.º 36
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);
        }
Exemplo n.º 37
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);
        }