Ejemplo n.º 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());
            }
        }
Ejemplo n.º 2
0
        public void IsArray()
        {
            var js     = new JavascriptEngine();
            var arrays = new object[] {
                js.ExecuteAndReturn("[\"a\"]"),
                new string[1] {
                    "a"
                },
            };
            var notArrays = new object[]
            {
                js.ExecuteAndReturn("\"a\""),
                new ExpandoObject(),
                new Dictionary <int, string>()
            };

            for (var i = 0; i < arrays.Length; i++)
            {
                try
                {
                    Assert.True(ObjectDiff.IsArray(arrays[i]), "Item {0} is an array", i);
                }
                catch (Exception e)
                {
                    throw new Exception($"Item {i} in arrays threw an exception", e);
                }
            }

            for (var i = 0; i < notArrays.Length; i++)
            {
                try
                {
                    Assert.False(ObjectDiff.IsArray(notArrays[i]), "Item {0} is not an array", i);
                }
                catch (Exception e)
                {
                    throw new Exception($"Item {i} in notArrays threw an exception", e);
                }
            }
        }