public void Format_EmptyDiffIsEmpty_Success() { var left = JObject.Parse(@"{}"); var right = JObject.Parse(@"{}"); var patch = Differ.Diff(left, right); var operations = Formatter.Format(patch); Assert.AreEqual(0, operations.Count); }
public void Format_SupportsArrayRemove_Success() { var jdp = new JsonDiffPatch(); var formatter = new JsonDeltaFormatter(); var left = JObject.Parse(@"{ ""items"" : [""bike"", ""car"", ""bus""] }"); var right = JObject.Parse(@"{ ""items"" : [""car"", ""bus""] }"); var patch = jdp.Diff(left, right); var operations = formatter.Format(patch); Assert.AreEqual(1, operations.Count); AssertOperation(operations[0], OperationTypes.Remove, "/items/0"); }
public void Format_SupportsReplace_Success() { var jdp = new JsonDiffPatch(); var formatter = new JsonDeltaFormatter(); var left = JObject.Parse(@"{ ""p"" : false }"); var right = JObject.Parse(@"{ ""p"" : true }"); var patch = jdp.Diff(left, right); var operations = formatter.Format(patch); Assert.AreEqual(1, operations.Count); AssertOperation(operations[0], OperationTypes.Replace, "/p", new JValue(true)); }
public static void AreDeepEqual(JToken left, JToken right) { if (!JToken.DeepEquals(left, right)) { var options = new Options { DiffBehaviors = DiffBehavior.IgnoreMissingProperties, }; var jdp = new JsonDiffPatch(options); var patch = jdp.Diff(left, right); if (patch != null) { //TestContext.Out.WriteLine(patch.ToString()); var fail = false; var formatter = new JsonDeltaFormatter(); var operations = formatter.Format(patch); foreach (var operation in operations.ToList()) { if (operation.Op == "replace") { var path = operation.Path.Replace("/", "."); var valueLeft = left.SelectToken(path)?.Value <string>(); var valueRight = right.SelectToken(path)?.Value <string>(); if (string.Equals(valueLeft, valueRight, StringComparison.InvariantCultureIgnoreCase)) { continue; } } TestContext.Out.WriteLine($"{operation.Op} {operation.Path} {operation.Value} {operation.From}"); fail = true; } if (fail) { Assert.Fail("Values do not match."); } } } Assert.Pass(); }