aboutsummaryrefslogtreecommitdiff
path: root/jsonmatch/diff_test.go
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2026-06-04 16:13:13 +0200
committerStefan Majewsky <majewsky@gmx.net>2026-06-04 16:13:13 +0200
commit2d71e70097ba48d5c1e59f455bd2499ab14844e3 (patch)
tree5a6cebf4dfc87620efbcd7dd164252c37c688898 /jsonmatch/diff_test.go
parentde1908cc7a11076f808f19f2d3ce115cda4bccd4 (diff)
downloadgo-gg-2d71e70097ba48d5c1e59f455bd2499ab14844e3.tar.gz
jsonmatch: allow embedding custom Diffable instances within Object and Array
Diffstat (limited to 'jsonmatch/diff_test.go')
-rw-r--r--jsonmatch/diff_test.go34
1 files changed, 34 insertions, 0 deletions
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))
+}