public void ToObjectEnumPropertyFromString()
        {
            var dict = new Dictionary<string, object>()
            {
                { "EnumProperty", "One" },
            };

            var value = dict.ToObject<ClassType>();
            Assert.Equal(EnumType.One, value.EnumProperty);
        }
        public void ToObjectPrimitiveProperties()
        {
            var dict = new Dictionary<string, object>()
            {
                { "StringProperty", "a" }, 
                { "IntProperty", 1 },
            };

            var value = dict.ToObject<ClassType>();
            Assert.Equal("a", value.StringProperty);
            Assert.Equal(1, value.IntProperty);
        }
        public void AsObjectUnknownProperty()
        {
            var dict = new Dictionary<string, object>()
            {
                { "StringProperty", "a" }, 
                { "IntProperty", 1 },
                { "UnknownProperty", "u" }
            };

            var value = dict.ToObject<ClassType>();
            Assert.Equal("a", value.StringProperty);
            Assert.Equal(1, value.IntProperty);
        }
        public void AsObjectPrivateSetter()
        {
            var dict = new Dictionary<string, object>()
            {
                { "StringProperty", "a" }, 
                { "IntProperty", 1 },
                { "StringPropertyPrivateSetter", "p" }
            };

            var value = dict.ToObject<ClassType>();
            Assert.Equal("a", value.StringProperty);
            Assert.Equal(1, value.IntProperty);
            Assert.Equal("p", value.StringPropertyPrivateSetter);
        }
        public void AsObjectField()
        {
            var dict = new Dictionary<string, object>()
            {
                { "StringProperty", "a" }, 
                { "IntProperty", 1 },
                { "StringField", "f" }
            };

            var value = dict.ToObject<ClassType>();
            Assert.Equal("a", value.StringProperty);
            Assert.Equal(1, value.IntProperty);
            Assert.Equal(null, value.StringField);
        }
        public void DifferentTypes()
        {
            // Arrange
            var dict = new Dictionary<string, string>()
            {
                { "Id", "11" }, // Should be a Guid
                { "Date", "1999-01-01" },
                { "Enum", "One" },
                { "Array", "1,2,3" },
                { "Text", "eleven" },
            };

            // Act
            dict.ToObject<RecursiveData>();
        }
        public void DifferentTypes()
        {
            // Arrange
            var dict = new Dictionary<string, object>()
            {
                { "Id", 11 }, // Should be a Guid
                { "Date", new DateTime(1999, 1, 1) },
                { "Enum", Enumeration.One },
                { "Array", new[] { 1, 2, 3, } },
                { "Text", "eleven" },
            };

            // Act
            dict.ToObject<RecursiveData>();
        }
        public void ToObjectDynamicODataEntry()
        {
            var _ = ODataDynamic.Expression;
            var dict = new Dictionary<string, object>()
            {
                { "StringProperty", "a" }, 
                { "IntProperty", 1 },
            };

            dynamic value = dict.ToObject<ODataEntry>(true);
            Assert.Equal("a", value.StringProperty);
            Assert.Equal(1, value.IntProperty);
        }
 public void MapsFromDictionaryNonGeneric()
 {
     var dictionary = new Dictionary<string, object> { { "PublicProperty", "Public Property" }, { "IgnoreMe", "la la la" } };
     var testClass = dictionary.ToObject(typeof(TestClass)) as TestClass;
     Assert.AreEqual("Public Property", testClass.PublicProperty);
 }
        public void ToObjectCompoundCollectionProperty()
        {
            var dict = new Dictionary<string, object>()
            {
                { "StringProperty", "a" }, 
                { "IntProperty", 1 },
                { "CompoundCollectionProperty", new[]
                    {
                        new Dictionary<string, object>() { { "StringProperty", "x" }, { "IntProperty", 1 } }, 
                        new Dictionary<string, object>() { { "StringProperty", "y" }, { "IntProperty", 2 } },
                        new Dictionary<string, object>() { { "StringProperty", "z" }, { "IntProperty", 3 } },
                    } 
                }
            };

            var value = dict.ToObject<ClassType>();
            Assert.Equal("a", value.StringProperty);
            Assert.Equal(1, value.IntProperty);
            for (var index = 0; index < 3; index++)
            {
                var kv = (dict["CompoundCollectionProperty"] as IList<IDictionary<string, object>>)[index];
                Assert.Equal(kv["StringProperty"], value.CompoundCollectionProperty[index].StringProperty);
                Assert.Equal(kv["IntProperty"], value.CompoundCollectionProperty[index].IntProperty);
            }
        }
        public void ToObjectODataEntry()
        {
            var dict = new Dictionary<string, object>()
            {
                { "StringProperty", "a" }, 
                { "IntProperty", 1 },
            };

            var value = dict.ToObject<ODataEntry>(false);
            Assert.Equal("a", value["StringProperty"]);
            Assert.Equal(1, value["IntProperty"]);
        }
        public void ToObjectIntCollection()
        {
            var dict = new Dictionary<string, object>()
            {
                { "StringProperty", "a" }, 
                { "IntProperty", 1 },
                { "IntCollectionProperty", new [] {1, 2, 3}  }
            };

            var value = dict.ToObject<ClassType>();
            Assert.Equal("a", value.StringProperty);
            Assert.Equal(1, value.IntProperty);
            for (var index = 0; index < 3; index++)
            {
                Assert.Equal((dict["IntCollectionProperty"] as IList<int>)[index], value.IntCollectionProperty[index]);
            }
        }
        public void ToObjectCompoundProperty()
        {
            var dict = new Dictionary<string, object>()
            {
                { "StringProperty", "a" }, 
                { "IntProperty", 1 },
                { "CompoundProperty", new Dictionary<string, object>() { { "StringProperty", "z" }, { "IntProperty", 0 } }  }
            };

            var value = dict.ToObject<ClassType>();
            Assert.Equal("a", value.StringProperty);
            Assert.Equal(1, value.IntProperty);
            Assert.Equal("z", value.CompoundProperty.StringProperty);
            Assert.Equal(0, value.CompoundProperty.IntProperty);
        }
        public void NestedDictionary()
        {
            var expected = new RecursiveData
            {
                Id = new Guid("366f4bd3-6717-4b14-9c79-70515296df7e"),
                Date = new DateTime(1999, 1, 1),
                Enum = Enumeration.One,
                Text = "level 1",
                Array = new[] { 1, 2, 3, },
                Nested = new RecursiveData
                {
                    Id = new Guid("e591be31-289f-4a99-ba67-288ea24b7d7e"),
                    Date = new DateTime(1999, 2, 2),
                    Enum = Enumeration.Two,
                    Text = "level 2",
                    Array = new[] { 4, 5, 6, },
                    Nested = null,
                },
            };

            var dict = new Dictionary<string, object>()
            {
                // 1st level
                { "Id", expected.Id },
                { "Date", expected.Date },
                { "Enum", expected.Enum },
                { "Text", expected.Text },
                { "Array", expected.Array },

                // 2nd level
                {
                    "Nested",
                    new Dictionary<string, object>
                    {
                        { "Id", expected.Nested.Id },
                        { "Date", expected.Nested.Date },
                        { "Enum", expected.Nested.Enum },
                        { "Text", expected.Nested.Text },
                        { "Array", expected.Nested.Array },
                        { "Nested", expected.Nested.Nested },
                    }
                }
            };

            var actual = dict.ToObject<RecursiveData>();

            // 1st level
            Assert.AreEqual(expected.Id, actual.Id);
            Assert.AreEqual(expected.Date, actual.Date);
            Assert.AreEqual(expected.Enum, actual.Enum);
            Assert.AreEqual(expected.Text, actual.Text);
            CollectionAssert.AreEqual(expected.Array, actual.Array);

            // 2nd level
            Assert.AreEqual(expected.Nested.Id, actual.Nested.Id);
            Assert.AreEqual(expected.Nested.Date, actual.Nested.Date);
            Assert.AreEqual(expected.Nested.Enum, actual.Nested.Enum);
            Assert.AreEqual(expected.Nested.Text, actual.Nested.Text);
            CollectionAssert.AreEqual(expected.Nested.Array, actual.Nested.Array);
        }
        public void ToObject_WhenDictionaryContainsDataWhichCannotBeMatched2_MustThrowInvalidOperationException()
        {
            var dictionary = new Dictionary<string, object> { { "Name", "Marbach" }, { "Street", "Elsewhere" }};

            Assert.Throws<InvalidOperationException>(() => dictionary.ToObject<Input>());
        }
        public void MissingProperty()
        {
            // Arrange
            var dict = new Dictionary<string, object>()
            {
                { "Id", new Guid("366f4bd3-6717-4b14-9c79-70515296df7e") },
                { "Date", new DateTime(1999, 1, 1) },
                { "Enum", Enumeration.One },
                { "Text", "eleven" },
                { "Array", new[] { 1, 2, 3, } },
                { "Integer", 11 }, // There's no Integer
            };

            // Act
            dict.ToObject<RecursiveData>();
        }
        public void Single()
        {
            // Arrange
            var expected = new SingleData { Id = new Guid("366f4bd3-6717-4b14-9c79-70515296df7e") };
            var dict = new Dictionary<string, object>() { { "Id", new Guid("366f4bd3-6717-4b14-9c79-70515296df7e") }, };

            // Act
            var actual = dict.ToObject<SingleData>();

            // Assert
            Assert.AreEqual(expected.Id, actual.Id);
        }
        public void Single()
        {
            // Arrange
            var dict = new Dictionary<string, string>() { { "Id", "366f4bd3-6717-4b14-9c79-70515296df7e" } };

            // Act
            var actual = dict.ToObject<SingleData>();

            // Assert
            Assert.AreEqual(new Guid("366f4bd3-6717-4b14-9c79-70515296df7e"), actual.Id);
        }
 public void MapsFromDictionary()
 {
     var dictionary = new Dictionary<string, object> { { "PublicProperty", "Public Property" }, { "IgnoreMe", "la la la"} };
     var testClass = dictionary.ToObject<TestClass>();
     Assert.AreEqual("Public Property", testClass.PublicProperty);
 }
        public void ToObjectClassWithoutDefaultCtor()
        {
            var dict = new Dictionary<string, object>()
            {
                { "Data", new ClassNoDefaultConstructor("test") },
            };

            Assert.Throws<InvalidOperationException>(() => dict.ToObject<ClassNoDefaultConstructor>());
        }
        public void ToObjectSpatialV4()
        {
            CustomConverters.RegisterTypeConverter(typeof(SpatialV4.GeographyPoint), V4.Adapter.TypeConverters.CreateGeographyPoint);
            var dict = new Dictionary<string, object>()
            {
                { "PointV4", SpatialV4.GeographyPoint.Create(SpatialV4.CoordinateSystem.Geography(100), 1, 2, null, null) },
            };

            var value = dict.ToObject<ClassType>();
            Assert.Equal(100, value.PointV4.CoordinateSystem.EpsgId);
            Assert.Equal(1d, value.PointV4.Latitude);
            Assert.Equal(2d, value.PointV4.Longitude);
        }
        public void LengthyTest()
        {
            var expected = new RecursiveData
            {
                Id = new Guid("366f4bd3-6717-4b14-9c79-70515296df7e"),
                Date = new DateTime(1999, 1, 1),
                Enum = Enumeration.One,
                Text = "level 1",
                Array = new[] { 1, 2, 3, },
                Nested = new RecursiveData
                {
                    Id = new Guid("e591be31-289f-4a99-ba67-288ea24b7d7e"),
                    Date = new DateTime(1999, 2, 2),
                    Enum = Enumeration.Two,
                    Text = "level 2",
                    Array = new[] { 4, 5, 6, },
                    Nested = new RecursiveData
                    {
                        Id = null,
                        Date = null,
                        Enum = null,
                        Text = null,
                        Nested = new RecursiveData
                        {
                            Id = new Guid("3bfdd62f-8b31-4aa2-931d-46535f291b0e"),
                            Date = new DateTime(1999, 4, 4),
                            Enum = Enumeration.Four,
                            Text = "level 4",
                            Array = new[] { 10, 11, 12, },
                            Nested = null,
                        },
                    },
                },
            };

            var dict =
                new Dictionary<string, object>()
                {
                    // 1st level
                    { "Id", expected.Id },
                    { "Date", expected.Date },
                    { "Enum", expected.Enum },
                    { "Text", expected.Text },
                    { "Array", expected.Array },

                    // 2nd level
                    { "Nested.Id", expected.Nested.Id },
                    { "Nested.Date", expected.Nested.Date },
                    { "Nested.Enum", expected.Nested.Enum },
                    { "Nested.Text", expected.Nested.Text },
                    { "Nested.Array", expected.Nested.Array },

                    // 3rd level
                    { "Nested.Nested.Id", expected.Nested.Nested.Id },
                    { "Nested.Nested.Date", expected.Nested.Nested.Date },
                    { "Nested.Nested.Enum", expected.Nested.Nested.Enum },
                    { "Nested.Nested.Text", expected.Nested.Nested.Text },
                    { "Nested.Nested.Array", expected.Nested.Nested.Array },

                    // 4th level
                    { "Nested.Nested.Nested.Id", expected.Nested.Nested.Nested.Id },
                    { "Nested.Nested.Nested.Date", expected.Nested.Nested.Nested.Date },
                    { "Nested.Nested.Nested.Enum", expected.Nested.Nested.Nested.Enum },
                    { "Nested.Nested.Nested.Text", expected.Nested.Nested.Nested.Text },
                    { "Nested.Nested.Nested.Array", expected.Nested.Nested.Nested.Array },

                    // 5th level
                    { "Nested.Nested.Nested.Nested.Id", null },
                    { "Nested.Nested.Nested.Nested.Date", null },
                    { "Nested.Nested.Nested.Nested.Enum", null },
                    { "Nested.Nested.Nested.Nested.Text", null },
                    { "Nested.Nested.Nested.Nested.Array", null },
                };

            var actual = dict.ToObject<RecursiveData>();

            // 1st level
            Assert.AreEqual(expected.Id, actual.Id);
            Assert.AreEqual(expected.Date, actual.Date);
            Assert.AreEqual(expected.Enum, actual.Enum);
            Assert.AreEqual(expected.Text, actual.Text);
            CollectionAssert.AreEqual(expected.Array, actual.Array);

            // 2nd level
            Assert.AreEqual(expected.Nested.Id, actual.Nested.Id);
            Assert.AreEqual(expected.Nested.Date, actual.Nested.Date);
            Assert.AreEqual(expected.Nested.Enum, actual.Nested.Enum);
            Assert.AreEqual(expected.Nested.Text, actual.Nested.Text);
            CollectionAssert.AreEqual(expected.Nested.Array, actual.Nested.Array);

            // 3rd level
            Assert.AreEqual(expected.Nested.Nested.Id, actual.Nested.Nested.Id);
            Assert.AreEqual(expected.Nested.Nested.Date, actual.Nested.Nested.Date);
            Assert.AreEqual(expected.Nested.Nested.Enum, actual.Nested.Nested.Enum);
            Assert.AreEqual(expected.Nested.Nested.Text, actual.Nested.Nested.Text);
            CollectionAssert.AreEqual(expected.Nested.Nested.Array, actual.Nested.Nested.Array);

            // 4th level
            Assert.AreEqual(expected.Nested.Nested.Nested.Id, actual.Nested.Nested.Nested.Id);
            Assert.AreEqual(expected.Nested.Nested.Nested.Date, actual.Nested.Nested.Nested.Date);
            Assert.AreEqual(expected.Nested.Nested.Nested.Enum, actual.Nested.Nested.Nested.Enum);
            Assert.AreEqual(expected.Nested.Nested.Nested.Text, actual.Nested.Nested.Nested.Text);
            CollectionAssert.AreEqual(expected.Nested.Nested.Nested.Array, actual.Nested.Nested.Nested.Array);

            // 5th level
            Assert.AreEqual(null, actual.Nested.Nested.Nested.Nested);
        }
        public void ToObjectCombinedEnumPropertyFromInt()
        {
            var dict = new Dictionary<string, object>()
            {
                { "EnumProperty", EnumType.One | EnumType.Two },
            };

            var value = dict.ToObject<ClassType>();
            Assert.Equal(EnumType.Three, value.EnumProperty);
        }
        public void MissingProperty()
        {
            // Arrange
            var dict = new Dictionary<string, string>()
            {
                { "Id", "366f4bd3-6717-4b14-9c79-70515296df7e" },
                { "Date", "1999-01-01" },
                { "Enum", "One" },
                { "Text", "eleven" },
                { "Integer", "11" }, // There's no Integer
            };

            // Act
            dict.ToObject<RecursiveData>();
        }