Example #1
0
        public void AbleToHandleCircularReferences_All()
        {
            CircularObject original = new CircularObject();

            original.AddChild();
            original.AddChild();
            original.FirstChild.AddChild();

            var snapshot = ObjectDiffPatch.Snapshot(original).ToString();

            ObjectDiffPatch.DefaultSerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All;
            // see http://www.newtonsoft.com/json/help/html/PreserveReferencesHandlingObject.htm

            var snapshotWithRefs = ObjectDiffPatch.Snapshot(original).ToString();

            Assert.NotEqual(snapshot, snapshotWithRefs);

            var updated = ObjectDiffPatch.PatchObject(new CircularObject(), snapshotWithRefs);

            // they should be equal
            Assert.Equal(Newtonsoft.Json.JsonConvert.SerializeObject(updated, ObjectDiffPatch.DefaultSerializerSettings),
                         Newtonsoft.Json.JsonConvert.SerializeObject(original, ObjectDiffPatch.DefaultSerializerSettings));
            Assert.NotNull(updated.FirstChild);
            Assert.NotNull(updated.FirstChild.Parent);
            Assert.NotNull(updated.FirstChild.FirstChild);

            // revertBack
            var diff = ObjectDiffPatch.GenerateDiff(original, new CircularObject());

            updated = ObjectDiffPatch.PatchObject(original, diff.NewValues);
            //ensure everything is alright
            Assert.Null(updated.FirstChild);
            Assert.Null(updated.Parent);
            Assert.Null(updated.Children);
        }
Example #2
0
        public CircularObject AddChild()
        {
            var item = new CircularObject()
            {
                Parent = this
            };

            if (FirstChild == null)
            {
                FirstChild = item;
            }
            if (Children == null)
            {
                Children = new List <CircularObject> ();
            }
            Children.Add(item);
            return(item);
        }
Example #3
0
        public void AbleToHandleCircularReferences_Ignore()
        {
            CircularObject original = new CircularObject();

            original.AddChild();
            original.AddChild();
            original.FirstChild.AddChild();

            var snapshot = ObjectDiffPatch.Snapshot(original).ToString();

            ObjectDiffPatch.DefaultSerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All;

            var snapshotWithRefs = ObjectDiffPatch.Snapshot(original).ToString();

            Assert.NotEqual(snapshot, snapshotWithRefs);

            var updated = ObjectDiffPatch.PatchObject(new CircularObject(), snapshotWithRefs);

            // they should be different, since all circular references are ignored!
            Assert.NotEqual(updated, original);
        }
 public CircularObject AddChild ()
 {
     var item = new CircularObject () { Parent = this };
     if (FirstChild == null)
         FirstChild = item;
     if (Children == null)
         Children = new List<CircularObject> ();
     Children.Add (item);
     return item;
 }
        public void AbleToHandleCircularReferences_All ()
        {
            CircularObject original = new CircularObject ();
            original.AddChild ();
            original.AddChild ();
            original.FirstChild.AddChild ();

            var snapshot = ObjectDiffPatch.Snapshot (original).ToString ();

            ObjectDiffPatch.DefaultSerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All;
            // see http://www.newtonsoft.com/json/help/html/PreserveReferencesHandlingObject.htm

            var snapshotWithRefs = ObjectDiffPatch.Snapshot (original).ToString ();

            Assert.NotEqual (snapshot, snapshotWithRefs);

            var updated = ObjectDiffPatch.PatchObject (new CircularObject (), snapshotWithRefs);
           // they should be equal
            Assert.Equal (Newtonsoft.Json.JsonConvert.SerializeObject (updated, ObjectDiffPatch.DefaultSerializerSettings),
                Newtonsoft.Json.JsonConvert.SerializeObject (original, ObjectDiffPatch.DefaultSerializerSettings));
            Assert.NotNull (updated.FirstChild);
            Assert.NotNull (updated.FirstChild.Parent);
            Assert.NotNull (updated.FirstChild.FirstChild);

            // revertBack
            var diff = ObjectDiffPatch.GenerateDiff (original, new CircularObject ());

            updated = ObjectDiffPatch.PatchObject (original, diff.NewValues);
            //ensure everything is alright
            Assert.Null (updated.FirstChild);
            Assert.Null (updated.Parent);
            Assert.Null (updated.Children);
        }
        public void AbleToHandleCircularReferences_Ignore ()
        {
            CircularObject original = new CircularObject ();
            original.AddChild ();
            original.AddChild ();
            original.FirstChild.AddChild ();

            var snapshot = ObjectDiffPatch.Snapshot (original).ToString ();

            ObjectDiffPatch.DefaultSerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All;

            var snapshotWithRefs = ObjectDiffPatch.Snapshot (original).ToString ();

            Assert.NotEqual (snapshot, snapshotWithRefs);

            var updated = ObjectDiffPatch.PatchObject (new CircularObject (), snapshotWithRefs);
            // they should be different, since all circular references are ignored!
            Assert.NotEqual (updated, original);
        }