Esempio n. 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)));
        }
Esempio n. 2
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);
            }));