Ejemplo n.º 1
0
        public void Insert_array_elements()
        {
            var sample = PatchTests.GetSample2();

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

            patchDocument.AddOperation(new AddEachOperation()
            {
                Path  = pointer,
                Value = new JArray()
                {
                    new JObject(new[] { new JProperty("author", "James Brown") }),
                    new JObject(new[] { new JProperty("cat", "Garfield") }),
                    new JObject(new[] { new JProperty("producer", "Kingston") }),
                }
            });

            patchDocument.ApplyTo(sample);

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

            Assert.Equal(5, list.Count);
            Assert.Equal(list[0]["author"].ToString(), "James Brown");
            Assert.Equal(list[1]["cat"].ToString(), "Garfield");
            Assert.Equal(list[2]["producer"].ToString(), "Kingston");
        }
Ejemplo n.º 2
0
        public void Remove_a_property()
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(sample);

            Assert.Throws(typeof(ArgumentException), () => { pointer.Find(sample); });
        }
Ejemplo n.º 3
0
        public void Replace_a_property_value_with_a_new_value()
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(new JsonNetTargetAdapter(sample));

            Assert.Equal("Bob Brown", (string)pointer.Find(sample));
        }
Ejemplo n.º 4
0
        public void Test_a_value()
        {
            var sample = PatchTests.GetSample2();

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

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

            Assert.Throws(typeof(InvalidOperationException), () =>
            {
                patchDocument.ApplyTo(sample);
            });
        }
Ejemplo n.º 5
0
        public void Append_array_elements_wrongDataType()
        {
            var sample = PatchTests.GetSample2();

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

            patchDocument.AddOperation(new AddEachOperation()
            {
                Path  = pointer,
                Value = new JObject(new[] { new JProperty("producer", "Kingston") })
            });

            Assert.Throws(typeof(ArgumentException), () => {
                patchDocument.ApplyTo(sample);
            });
        }
Ejemplo n.º 6
0
        public void Replace_a_property_value_with_an_object()
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(new JsonNetTargetAdapter(sample));

            var newPointer = new JsonPointer("/books/0/author/hello");

            Assert.Equal("world", (string)newPointer.Find(sample));
        }
Ejemplo n.º 7
0
        public void Add_an_array_element()
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(sample);

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

            Assert.Equal(3, list.Count);
        }
Ejemplo n.º 8
0
        public void Copy_array_element()
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(sample);

            var result = new JsonPointer("/books/2").Find(sample);

            Assert.IsType(typeof(JObject), result);
        }
Ejemplo n.º 9
0
        public void Remove_an_array_element()
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(sample);

            Assert.Throws(typeof(PathNotFoundException), () =>
            {
                var x = pointer.Find("/books/1");
            });
        }
Ejemplo n.º 10
0
        public void Add_an_non_existing_member_property()  // Why isn't this replace?
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(sample);


            var result = (string)pointer.Find(sample);

            Assert.Equal("213324234343", result);
        }
Ejemplo n.º 11
0
        public void Move_property()
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(sample);


            var result = (string)topointer.Find(sample);

            Assert.Equal("F. Scott Fitzgerald", result);
        }
Ejemplo n.º 12
0
        public void Append_array_elements_noIndex()
        {
            var sample = PatchTests.GetSample2();

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

            patchDocument.AddOperation(new AddEachOperation()
            {
                Path  = pointer,
                Value = new JArray()
                {
                    new JObject(new[] { new JProperty("author", "James Brown") }),
                    new JObject(new[] { new JProperty("cat", "Garfield") }),
                    new JObject(new[] { new JProperty("producer", "Kingston") }),
                }
            });

            Assert.Throws(typeof(ArgumentException), () => {
                patchDocument.ApplyTo(sample);
            });
        }
Ejemplo n.º 13
0
        public void Copy_property()
        {
            var sample = PatchTests.GetSample2();

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

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

            patchDocument.ApplyTo(sample);

            var result = new JsonPointer("/books/1/ISBN").Find(sample);

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