Skip to content

Commit

Permalink
fix(JSON): errors with wrong origin operator
Browse files Browse the repository at this point in the history
In error reports, the origin operator was sometimes missing and
sometimes wrong when using JSON, SubJSONOf and SuperJSONOf operators,
as in:

    [under operator tdJSONUnmarshaler.resolveOp.func1 at lex.go:156]

Now the location of internal operators, as the ones used by JSON
operators, cannot appear in error reports anymore and so do not mask
the location of user visible operators.

Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
  • Loading branch information
maxatome committed Mar 26, 2023
1 parent 6a8c8ff commit 811807f
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 7 deletions.
8 changes: 6 additions & 2 deletions td/equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ func nilHandler(ctx ctxerr.Context, got, expected reflect.Value) *ctxerr.Error {
if expected.IsValid() { // here: !got.IsValid()
if expected.Type().Implements(testDeeper) {
curOperator := dark.MustGetInterface(expected).(TestDeep)
ctx.CurOperator = curOperator
if !curOperator.isInternal() {
ctx.CurOperator = curOperator
}
if curOperator.HandleInvalid() {
return curOperator.Match(ctx, got)
}
Expand Down Expand Up @@ -201,7 +203,9 @@ func deepValueEqual(ctx ctxerr.Context, got, expected reflect.Value) (err *ctxer
}
}

ctx.CurOperator = curOperator
if !curOperator.isInternal() {
ctx.CurOperator = curOperator
}
return curOperator.Match(ctx, got)
}

Expand Down
17 changes: 13 additions & 4 deletions td/td_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ func (s *tdJSONSmuggler) HandleInvalid() bool {
return true
}

func (s *tdJSONSmuggler) isInternal() bool {
return true
}

func (s *tdJSONSmuggler) TypeBehind() reflect.Type {
return s.internalTypeBehind()
}
Expand All @@ -330,7 +334,7 @@ type tdJSONPlaceholder struct {
func newJSONNamedPlaceholder(name string, expectedValue any) TestDeep {
p := tdJSONPlaceholder{
tdJSONSmuggler: tdJSONSmuggler{
tdSmugglerBase: newSmugglerBase(expectedValue, 1),
tdSmugglerBase: newSmugglerBase(expectedValue, -100), // without location
},
name: name,
}
Expand All @@ -344,7 +348,7 @@ func newJSONNamedPlaceholder(name string, expectedValue any) TestDeep {
func newJSONNumPlaceholder(num uint64, expectedValue any) TestDeep {
p := tdJSONPlaceholder{
tdJSONSmuggler: tdJSONSmuggler{
tdSmugglerBase: newSmugglerBase(expectedValue, 1),
tdSmugglerBase: newSmugglerBase(expectedValue, -100), // without location
},
num: num,
}
Expand Down Expand Up @@ -397,11 +401,16 @@ type tdJSONEmbedded struct {
}

func newJSONEmbedded(tdOp TestDeep) TestDeep {
return &tdJSONEmbedded{
e := tdJSONEmbedded{
tdJSONSmuggler: tdJSONSmuggler{
tdSmugglerBase: newSmugglerBase(tdOp, 1),
tdSmugglerBase: newSmugglerBase(tdOp, -100), // without location
},
}
return &e
}

func (e *tdJSONEmbedded) isInternal() bool {
return true
}

func (e *tdJSONEmbedded) MarshalJSON() ([]byte, error) {
Expand Down

0 comments on commit 811807f

Please sign in to comment.