Skip to content

Commit

Permalink
fix(JSON): a panic could occur when using All+JSON+embeddedOp+$1
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
  • Loading branch information
maxatome committed Nov 8, 2021
1 parent 5d81cbf commit 55d8ae0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions td/td_json.go
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/maxatome/go-testdeep/internal/json"
"github.com/maxatome/go-testdeep/internal/location"
"github.com/maxatome/go-testdeep/internal/types"
"github.com/maxatome/go-testdeep/internal/util"
)

// forbiddenOpsInJSON contains operators forbidden inside JSON,
Expand Down Expand Up @@ -326,8 +327,7 @@ func (s *tdJSONSmuggler) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Er
}

func (s *tdJSONSmuggler) String() string {
// Only called by MarshalJSON(), and so only when p.isTestDeeper
return s.expectedValue.Interface().(TestDeep).String()
return util.ToString(s.expectedValue.Interface())
}

func (s *tdJSONSmuggler) HandleInvalid() bool {
Expand Down
19 changes: 19 additions & 0 deletions td/td_json_test.go
Expand Up @@ -231,6 +231,25 @@ func TestJSON(t *testing.T) {
Summary: mustContain("json: unsupported type"),
})

checkError(t, map[string]string{"zip": "pipo"},
td.All(td.JSON(`SuperMapOf({"zip":$1})`, "bingo")),
expectedError{
Path: mustBe(`DATA`),
Message: mustBe("compared (part 1 of 1)"),
Got: mustBe(`(map[string]string) (len=1) {
(string) (len=3) "zip": (string) (len=4) "pipo"
}`),
Expected: mustBe(`JSON(SuperMapOf(map[string]interface {}{
"zip": "bingo",
}))`),
Origin: &expectedError{
Path: mustBe(`DATA<All#1/1>["zip"]`),
Message: mustBe(`values differ`),
Got: mustBe(`"pipo"`),
Expected: mustBe(`"bingo"`),
},
})

//
// Fatal errors
checkError(t, "never tested",
Expand Down

0 comments on commit 55d8ae0

Please sign in to comment.