示例#1
0
        public void DiffKeysWithArrays()
        {
            using (var engine = new JavascriptEngine()) {
                engine.Execute("var a = {};");
                var startObj = engine.ExecuteAndReturn("a");

                engine.Execute("var a = {};");
                engine.Execute("a.users = []");
                var endObj = engine.ExecuteAndReturn("a");

                // Added
                var diff = new ObjectDiff();
                diff.Diff(startObj, endObj, "a");

                Assert.AreEqual(1, diff.Added.Count);
                Assert.AreEqual("a.users", diff.Added.Keys.First(), "It actually equals " + diff.Added.Keys.First());

                var val = JsonConvert.SerializeObject(diff.Added.First().Value);
                Assert.AreEqual("[]", val);
            }
            using (var engine = new JavascriptEngine())
            {
                engine.Execute("var a = {};");
                engine.Execute("a.users = []");
                var startObj = engine.ExecuteAndReturn("a");

                engine.Execute("var a = {};");
                engine.Execute("a.users = []");
                engine.Execute("a.users[1] = 12");
                var endObj = engine.ExecuteAndReturn("a");

                // Added
                var diff = new ObjectDiff();
                diff.Diff(startObj, endObj, "a");

                Assert.AreEqual(2, diff.Added.Count);
                Assert.True(diff.Added.ContainsKey("a.users[0]"));
                Assert.True(diff.Added.ContainsKey("a.users[1]"));

                // Deleted
                diff = new ObjectDiff();
                diff.Diff(endObj, startObj, "a");

                Assert.AreEqual(2, diff.Deleted.Count);
                Assert.True(diff.Deleted.Contains("a.users[0]"));
                Assert.True(diff.Deleted.Contains("a.users[1]"));

                // Modified
                engine.Execute("var a = {};");
                engine.Execute("a.users = []");
                engine.Execute("a.users[1] = 14");
                var changedObj = engine.ExecuteAndReturn("a");

                diff = new ObjectDiff();
                diff.Diff(endObj, changedObj, "a");

                Assert.AreEqual(1, diff.Modified.Count);
                Assert.AreEqual("a.users[1]", diff.Modified.Keys.First(), "It actually equals " + diff.Modified.Keys.First());
            }
        }
示例#2
0
        public ObjectDiff ProcessChanges()
        {
            var diff = new ObjectDiff();

            if (diff.Diff(_prevObject, _object))
            {
                diff.Id = ++_prevId;
                // Store those changes(patch)
                _changes.Add(diff.Id, diff);

                // Update the _objectProps object with new object
                _prevObject = ObjectDiff.ToJObject(_object);
            }

            return(diff);
        }
示例#3
0
        public void Diff()
        {
            GameThread.IgnoreThreadRestrictions = true;
            using (var ge = new GameEngine(null)) {
                ge.EngineInitialized.WaitOne();
                var diff = new ObjectDiff();

                dynamic sobj = new ExpandoObject();
                dynamic eobj = new ExpandoObject();

                eobj.Tim      = new ExpandoObject();
                eobj.Tim.bill = 12;

                diff.Diff(sobj, eobj);
                Assert.AreEqual(diff.Added["Tim"], ObjectDiff.ToJObject(eobj.Tim));
                Assert.IsEmpty(diff.Deleted);
                Assert.IsEmpty(diff.Modified);

                // Equal
                diff = new ObjectDiff();
                sobj = eobj;
                eobj = new ExpandoObject();

                eobj.Tim      = new ExpandoObject();
                eobj.Tim.bill = 12;

                diff.Diff(sobj, eobj);
                Assert.IsEmpty(diff.Added);
                Assert.IsEmpty(diff.Deleted);
                Assert.IsEmpty(diff.Modified);

                // Modify
                diff = new ObjectDiff();
                sobj = eobj;
                eobj = new ExpandoObject();

                eobj.Tim      = new ExpandoObject();
                eobj.Tim.bill = "chicken";

                diff.Diff(sobj, eobj);
                Assert.IsEmpty(diff.Added);
                Assert.IsEmpty(diff.Deleted);
                Assert.AreEqual(((JValue)diff.Modified["Tim.bill"]).Value, "chicken");

                // Delete
                diff = new ObjectDiff();
                sobj = eobj;
                eobj = new ExpandoObject();

                eobj.Tim = new ExpandoObject();

                diff.Diff(sobj, eobj);
                Assert.IsEmpty(diff.Added);
                Assert.IsEmpty(diff.Modified);
                Assert.Contains("Tim.bill", diff.Deleted);

                eobj = new ExpandoObject();

                diff.Diff(sobj, eobj);
                Assert.IsEmpty(diff.Added);
                Assert.IsEmpty(diff.Modified);
                Assert.Contains("Tim", diff.Deleted);
            }
        }