예제 #1
0
        public void Replace_a_property_value_with_a_new_value()
        {
            var sample = PatchTests.GetSample2();

            var patchDocument = new PatchDocument();
            var pointer       = "/books/0/author";

            patchDocument.AddOperation(new ReplaceOperation {
                Path = pointer, Value = "Bob Brown"
            });

            new JsonPatcher().Patch(ref sample, patchDocument);

            Assert.Equal("Bob Brown", sample.SelectPatchToken(pointer).Value <string>());
        }
예제 #2
0
        public void Remove_a_property()
        {
            var sample = PatchTests.GetSample2();

            var patchDocument = new PatchDocument();
            var pointer       = "/books/0/author";

            patchDocument.AddOperation(new RemoveOperation {
                Path = pointer
            });

            new JsonPatcher().Patch(ref sample, patchDocument);

            Assert.Null(sample.SelectPatchToken(pointer));
        }
예제 #3
0
        public void Test_a_value()
        {
            var sample = PatchTests.GetSample2();

            var patchDocument = new PatchDocument();
            var pointer       = "/books/0/author";

            patchDocument.AddOperation(new TestOperation {
                Path = pointer, Value = new JValue("Billy Burton")
            });

            Assert.Throws(typeof(InvalidOperationException), () => {
                var patcher = new JsonPatcher();
                patcher.Patch(ref sample, patchDocument);
            });
        }
예제 #4
0
        public void Replace_a_property_value_with_an_object()
        {
            var sample = PatchTests.GetSample2();

            var patchDocument = new PatchDocument();
            var pointer       = "/books/0/author";

            patchDocument.AddOperation(new ReplaceOperation {
                Path = pointer, Value = new JObject(new[] { new JProperty("hello", "world") })
            });

            new JsonPatcher().Patch(ref sample, patchDocument);

            var newPointer = "/books/0/author/hello";

            Assert.Equal("world", sample.SelectPatchToken(newPointer).Value <string>());
        }
예제 #5
0
        public void Remove_an_array_element()
        {
            var sample = PatchTests.GetSample2();

            var patchDocument = new PatchDocument();
            var pointer       = "/books/0";

            patchDocument.AddOperation(new RemoveOperation {
                Path = pointer
            });

            var patcher = new JsonPatcher();

            patcher.Patch(ref sample, patchDocument);

            Assert.Null(sample.SelectPatchToken("/books/1"));
        }
예제 #6
0
        public void Add_an_array_element()
        {
            var sample = PatchTests.GetSample2();

            var patchDocument = new PatchDocument();
            var pointer       = "/books/-";

            patchDocument.AddOperation(new AddOperation {
                Path = pointer, Value = new JObject(new[] { new JProperty("author", "James Brown") })
            });

            var patcher = new JsonPatcher();

            patcher.Patch(ref sample, patchDocument);

            var list = sample["books"] as JArray;

            Assert.Equal(3, list.Count);
        }
예제 #7
0
        public void Add_an_non_existing_member_property()  // Why isn't this replace?
        {
            var sample = PatchTests.GetSample2();

            var patchDocument = new PatchDocument();
            var pointer       = "/books/0/SBN";

            patchDocument.AddOperation(new AddOperation {
                Path = pointer, Value = "213324234343"
            });

            var patcher = new JsonPatcher();

            patcher.Patch(ref sample, patchDocument);

            var result = sample.SelectPatchToken(pointer).Value <string>();

            Assert.Equal("213324234343", result);
        }
예제 #8
0
        public void Copy_array_element()
        {
            var sample = PatchTests.GetSample2();

            var patchDocument = new PatchDocument();
            var frompointer   = "/books/0";
            var topointer     = "/books/-";

            patchDocument.AddOperation(new CopyOperation {
                FromPath = frompointer, Path = topointer
            });

            var patcher = new JsonPatcher();

            patcher.Patch(ref sample, patchDocument);

            var result = sample.SelectPatchToken("/books/2");

            Assert.IsType(typeof(JObject), result);
        }
예제 #9
0
        public void Move_property()
        {
            var sample = PatchTests.GetSample2();

            var patchDocument = new PatchDocument();
            var frompointer   = "/books/0/author";
            var topointer     = "/books/1/author";

            patchDocument.AddOperation(new MoveOperation {
                FromPath = frompointer, Path = topointer
            });

            var patcher = new JsonPatcher();

            patcher.Patch(ref sample, patchDocument);


            var result = sample.SelectPatchToken(topointer).Value <string>();

            Assert.Equal("F. Scott Fitzgerald", result);
        }
예제 #10
0
        public void Copy_property()
        {
            var sample = PatchTests.GetSample2();

            var patchDocument = new PatchDocument();
            var frompointer   = "/books/0/ISBN";
            var topointer     = "/books/1/ISBN";

            patchDocument.AddOperation(new AddOperation {
                Path = frompointer, Value = new JValue("21123123")
            });
            patchDocument.AddOperation(new CopyOperation {
                FromPath = frompointer, Path = topointer
            });

            var patcher = new JsonPatcher();

            patcher.Patch(ref sample, patchDocument);

            var result = sample.SelectPatchToken("/books/1/ISBN");

            Assert.Equal("21123123", result);
        }