From 2d71e70097ba48d5c1e59f455bd2499ab14844e3 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Thu, 4 Jun 2026 16:13:13 +0200 Subject: jsonmatch: allow embedding custom Diffable instances within Object and Array --- jsonmatch/diff_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'jsonmatch/diff_test.go') diff --git a/jsonmatch/diff_test.go b/jsonmatch/diff_test.go index 1a49010..aba2d00 100644 --- a/jsonmatch/diff_test.go +++ b/jsonmatch/diff_test.go @@ -409,3 +409,37 @@ func TestArrayOnArrayAction(t *testing.T) { match = jsonmatch.Array{jsonmatch.Array{1, 2}} AssertEqual(t, match.DiffAgainst(message), expected) } + +func TestDispatchIntoCustomDiffable(t *testing.T) { + message := []byte(`{"name":"data.json","type":"application/json","content":"{\"foo\":1,\"bar\":3}"}`) + match := jsonmatch.Object{ + "name": "data.json", + "type": "application/json", + "content": jsonWithinJSONString{jsonmatch.Object{ + "foo": 1, + "bar": 2, + }}, + } + expected := []jsonmatch.Diff{{ + Kind: "value mismatch", + Pointer: "/content/bar", + ExpectedJSON: "2", + ActualJSON: "3", + }} + AssertEqual(t, match.DiffAgainst(message), expected) +} + +// jsonWithinJSONString appears in TestDispatchIntoCustomDiffable. +type jsonWithinJSONString struct { + inner jsonmatch.Diffable +} + +// DiffAgainst implements the DiffAgainst interface. +func (j jsonWithinJSONString) DiffAgainst(buf []byte) []jsonmatch.Diff { + var s string + err := json.Unmarshal(buf, &s) + if err != nil { + panic(err.Error()) + } + return j.inner.DiffAgainst([]byte(s)) +} -- cgit v1.2.3