/// <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))); }
/// <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); }));
public JsonDiffBuilder Use(JsonDiffPredicate match, JsonDiffNormalize normalize) { if (normalize is null) { throw new ArgumentNullException(nameof(normalize)); } if (match is null) { _rules.Add(normalize); } else { _rules.Add((expected, actual, name, diff) => match(expected, actual, name) ? normalize(expected, actual, name, diff) : (expected, actual)); } return(this); }