示例#1
0
        public string JsonObjectDiff()
        {
            JToken jTokenOriginal = JToken.Parse(objectOriginalJSON);
            JToken jTokenModified = JToken.Parse(objectModifiedJSON);
            JToken jTokensDiff    = new JsonDiffPatch().Diff(jTokenOriginal, jTokenModified);

            try
            {
                return(jTokensDiff.ToString());
            }
            catch (Exception)
            {
                return(null);
            }
        }
示例#2
0
        public bool DiffObject(bool waitForUser)
        {
            var left  = JToken.FromObject(_obj1);
            var right = JToken.FromObject(_obj2);

            JToken patch = new JsonDiffPatch().Diff(left, right);

            if (patch != null)
            {
                if (!waitForUser)
                {
                    return(true);
                }

                Console.WriteLine(patch.ToString());
                Console.WriteLine();
                Console.WriteLine("Type 'Y' to replace item. Type 'V' to show changes.");
                HandleUserResponse();
            }
            return(false);
        }
        private static string?GetJsonDiff(JToken?first, JToken?second)
        {
            const int truncate = 100;
            var       diff     = new JsonDiffPatch(new Options {
                TextDiff = TextDiffMode.Simple
            }).Diff(first, second);

            if (diff is null)
            {
                return(null);
            }

            // JsonDiffPatch.Diff returns null if there are no diffs
            var lineLogs = diff.ToString().Split('\n').Take(truncate);

            if (lineLogs.Count() >= truncate)
            {
                lineLogs = lineLogs.Concat(new[] { "...truncated..." });
            }

            return(string.Join('\n', lineLogs));
        }
        public static AndConstraint <JTokenAssertions> EqualWithJsonDiffOutput(this JTokenAssertions instance, TestContext testContext, JToken expected, string expectedLocation, string actualLocation, string because = "", params object[] becauseArgs)
        {
            const int truncate = 100;
            var       diff     = new JsonDiffPatch(new Options {
                TextDiff = TextDiffMode.Simple
            }).Diff(instance.Subject, expected);

            // JsonDiffPatch.Diff returns null if there are no diffs
            var lineLogs = (diff?.ToString() ?? string.Empty)
                           .Split('\n')
                           .Take(truncate);

            if (lineLogs.Count() > truncate)
            {
                lineLogs = lineLogs.Concat(new[] { "...truncated..." });
            }

            var testPassed       = diff is null;
            var isBaselineUpdate = !testPassed && BaselineHelper.ShouldSetBaseline(testContext);

            if (isBaselineUpdate)
            {
                BaselineHelper.SetBaseline(actualLocation, expectedLocation);
            }

            Execute.Assertion
            .BecauseOf(because, becauseArgs)
            .ForCondition(testPassed)
            .FailWith(
                BaselineHelper.GetAssertionFormatString(isBaselineUpdate),
                string.Join('\n', lineLogs),
                BaselineHelper.GetAbsolutePathRelativeToRepoRoot(actualLocation),
                BaselineHelper.GetAbsolutePathRelativeToRepoRoot(expectedLocation));

            return(new AndConstraint <JTokenAssertions>(instance));
        }
        public static AndConstraint <JTokenAssertions> EqualWithJsonDiffOutput(this JTokenAssertions instance, JToken expected, string expectedLocation, string actualLocation, string because = "", params object[] becauseArgs)
        {
            const int truncate = 100;
            var       diff     = new JsonDiffPatch(new Options {
                TextDiff = TextDiffMode.Simple
            }).Diff(instance.Subject, expected);

            // JsonDiffPatch.Diff returns null if there are no diffs
            var lineLogs = (diff?.ToString() ?? string.Empty)
                           .Split('\n')
                           .Take(truncate);

            if (lineLogs.Count() > truncate)
            {
                lineLogs = lineLogs.Concat(new[] { "...truncated..." });
            }

            Execute.Assertion
            .BecauseOf(because, becauseArgs)
            .ForCondition(diff is null)
            .FailWith(@"
Found diffs between actual and expected:
{0}
View this diff with:

git diff --color-words --no-index {1} {2}

Windows copy command:
copy /y {1} {2}

Unix copy command:
cp {1} {2}
", string.Join('\n', lineLogs), actualLocation, expectedLocation);

            return(new AndConstraint <JTokenAssertions>(instance));
        }