Example #1
0
        /// <summary>
        /// Ignore the actual result of a property if the expected value is null
        /// </summary>
        /// <example>
        /// Given the expectation { "a": null }, { "a": "anything" } pass.
        /// </example>
        public static JsonDiffBuilder UseIgnoreNull(this JsonDiffBuilder builder, JsonDiffPredicate predicate = null)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.Use(predicate, (expected, actual, name, diff) =>
                               expected.Type == JTokenType.Null && actual.Type != JTokenType.Undefined ? (expected, expected) : (expected, actual)));
        }
Example #2
0
        private static void Run(string expected, string actual, string diff, JsonDiffBuilder builder)
        {
            var actualDiff = builder.Build().Diff(
                JToken.Parse(expected.Replace('\'', '\"')),
                JToken.Parse(actual.Replace('\'', '\"')));

            var actualChanges = actualDiff
                                .Replace("\r", "")
                                .Split('\n', StringSplitOptions.RemoveEmptyEntries)
                                .Where(line => line.StartsWith("+") || line.StartsWith("-"));

            new JsonDiff().Verify(
                diff.Trim().Replace("\r", "").Replace('\'', '\"'),
                string.Join('\n', actualChanges));
        }
Example #3
0
        /// <summary>
        /// Assert the actual result must not be the expected result if the expected value starts with !
        /// </summary>
        /// <example>
        /// Given the expectation "!value", "a value" pass but "value" fail
        /// </example>
        public static JsonDiffBuilder UseNegate(this JsonDiffBuilder builder, JsonDiffPredicate predicate = null)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.Use(predicate, (expected, actual, name, diff) =>
            {
                if (expected.Type == JTokenType.String && actual.Type == JTokenType.String &&
                    expected.Value <string>() is string str && str.StartsWith("!"))
                {
                    if (str.Substring(1) != actual.Value <string>())
                    {
                        return (actual, actual);
                    }
                }
                return (expected, actual);
            }));