Exemplo n.º 1
0
        public void A_property_is_excluded_from_auto_patch_if_is_not_present_in_the_json()
        {
            const string json = @"
            {
                'IntProperty' : 5,
                'DateTimeProperty' : '2020-01-12'
            }";

            var patch = new Patch <SourceClass>(json);

            var targetObject = new TargetClass
            {
                StringProperty   = "original_value",
                IntProperty      = 1,
                DateTimeProperty = DateTime.UtcNow
            };

            patch.AutoPatch(targetObject,
                            x => x.StringProperty,
                            x => x.IntProperty,
                            x => x.DateTimeProperty
                            );

            Assert.AreEqual("original_value", targetObject.StringProperty);
            Assert.AreEqual(5, targetObject.IntProperty);
            Assert.AreEqual(new DateTime(2020, 1, 12), targetObject.DateTimeProperty);
        }
Exemplo n.º 2
0
        public void Patching_nested_properties_is_valid()
        {
            const string json = @"
            {
                'Nested' : {
                    'NestedProperty' : 'nested_value'
                }
            }";

            var patch = new Patch <SourceClass>(json);

            var targetObject = new TargetClass
            {
                Nested = new Nested
                {
                    NestedProperty = "original_nested"
                }
            };

            patch.AutoPatch(targetObject,
                            x => x.Nested.NestedProperty
                            );

            Assert.AreEqual("nested_value", targetObject.Nested.NestedProperty);
        }
Exemplo n.º 3
0
        public void AutoPatch_works_with_null()
        {
            const string json = @"
            {
                'stringProperty' : null
            }";

            var patch = new Patch <SourceClass>(json);

            var targetObject = new TargetClass
            {
                StringProperty = "original",
            };

            patch.AutoPatch(targetObject,
                            x => x.StringProperty
                            );

            Assert.IsNull(targetObject.StringProperty);
        }
Exemplo n.º 4
0
        public void Patch_value_is_not_applied_if_validation_fails()
        {
            const string json = @"
            {
                'IntProperty' : 15
            }";

            var patch = new Patch <SourceClass>(json);

            var targetObject = new TargetClass
            {
                IntProperty = 1
            };

            patch.AutoPatch(targetObject,
                            x => x.IntProperty
                            );

            Assert.AreEqual(1, targetObject.IntProperty);
        }
Exemplo n.º 5
0
        public void Patch_is_case_insensitive()
        {
            const string json = @"
            {
                'stringproperty' : 'json_value',
                'INTPROPERTY' : 10
            }";

            var patch = new Patch <SourceClass>(json);

            var targetObject = new TargetClass
            {
                StringProperty = "original_value",
                IntProperty    = 1
            };

            patch.AutoPatch(targetObject,
                            x => x.StringProperty,
                            x => x.IntProperty
                            );

            Assert.AreEqual("json_value", targetObject.StringProperty);
            Assert.AreEqual(10, targetObject.IntProperty);
        }