From 2a0845b89e9c2e3dea49682f9acb5d21e7258b75 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Tue, 18 Nov 2025 16:43:18 +0100 Subject: jsonmatch: add Diff.String() --- jsonmatch/interface.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'jsonmatch/interface.go') diff --git a/jsonmatch/interface.go b/jsonmatch/interface.go index 3c10cc3..99bbb9f 100644 --- a/jsonmatch/interface.go +++ b/jsonmatch/interface.go @@ -36,11 +36,7 @@ // }, // } // for _, diff := range expected.DiffAgainst(resp.Body.Bytes()) { -// if diff.Pointer == "" { -// t.Errorf("%s: expected %s, but got %s", diff.Kind, diff.ExpectedJSON, diff.ActualJSON) -// } else { -// t.Errorf("%s at %s: expected %s, but got %s", diff.Kind, diff.Pointer, diff.ExpectedJSON, diff.ActualJSON) -// } +// t.Error(diff.String()) // } // } // @@ -137,6 +133,7 @@ package jsonmatch import ( "encoding/json" "errors" + "fmt" ) // Diffable is the common interface of types Object, Array, Scalar and Null from this package. @@ -239,6 +236,15 @@ type Diff struct { ActualJSON string } +// String returns a simple and complete string representation of the contents of this Diff. +func (d Diff) String() string { + if d.Pointer == "" { + return fmt.Sprintf("%s: expected %s, but got %s", d.Kind, d.ExpectedJSON, d.ActualJSON) + } else { + return fmt.Sprintf("%s at %s: expected %s, but got %s", d.Kind, d.Pointer, d.ExpectedJSON, d.ActualJSON) + } +} + // Pointer is a JSON pointer (RFC 6901) that references a particular JSON value relative to the root of the encoded JSON payload that was given to DiffAgainst(). // It appears in type Diff. // -- cgit v1.2.3