assert-json-diff icon indicating copy to clipboard operation
assert-json-diff copied to clipboard

Shallow vs deep inclusive compare

Open jyn514 opened this issue 2 years ago • 2 comments

I ran the following test just now, which passed:

        assert_json_include!(actual: &serialized, expected: serde_json::json!({
            "x": [0],
            "y": [1, 3],
        }))

Then I changed it to

        assert_json_include!(actual: &serialized, expected: serde_json::json!({
            "x": [0, 2],
            "y": [1, 3],
        }))

which also passed.

I think what's going on is that the Inclusive compare mode is being passed recursively, instead of only for the outer object. That's very non-intuitive for me, I'd expect it to only be for the keys and not the inner values. Would it be possible to add a new mode that does a shallow inclusive compare instead of a deep inclusive compare?

jyn514 avatar Mar 01 '22 16:03 jyn514

I think the current setup works well for deeply nested objects where you only care parts of it changing. But you're right, for arrays that might be a little confusing. Though I would argue things work as intended based on what you posted. The first one is a subset of the second, so seems ok to me.

Can you give some more examples of how such a swallow mode would work?

davidpdrsn avatar Mar 01 '22 17:03 davidpdrsn

@davidpdrsn I'd expect to be inclusive for the outer object but exact for everything else. For example, these would all pass:

assert_json_include_shallow!(actual: json!([1, 2, 3]), expected: json!([1, 2]));
assert_json_include_shallow!(actual: json!({"x": [1, 2, 3]}, "y": 0), expected: json!({"x": [1, 2, 3]}));

but these would all fail:

assert_json_include_shallow!(actual: json!({"x": [1, 2, 3]}), expected: json!({"x": [1, 2]}));
assert_json_include_shallow!(actual: json!({"x": {"y": 0, "z": 1}}), expected: json!({"x": {"y": 0}}));

jyn514 avatar Mar 01 '22 18:03 jyn514