Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(JSON): errors with wrong origin operator #239

Merged
merged 3 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions td/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type expectedError struct {
Expected expectedErrorMatch
Summary expectedErrorMatch
Located bool
Under expectedErrorMatch
Origin *expectedError
}

Expand All @@ -86,6 +87,10 @@ func indent(str string, numSpc int) string {
return strings.ReplaceAll(str, "\n", "\n\t"+strings.Repeat(" ", numSpc))
}

func fullError(err *ctxerr.Error) string {
return strings.ReplaceAll(err.Error(), "\n", "\n\t> ")
}

func cmpErrorStr(t *testing.T, err *ctxerr.Error,
got string, expected expectedErrorMatch, fieldName string,
args ...any,
Expand All @@ -100,7 +105,7 @@ func cmpErrorStr(t *testing.T, err *ctxerr.Error,
> %s`,
tdutil.BuildTestName(args...),
fieldName, indent(got, 10), indent(expected.Exact, 10),
strings.ReplaceAll(err.Error(), "\n\t", "\n\t> "))
fullError(err))
return false
}

Expand All @@ -113,7 +118,7 @@ func cmpErrorStr(t *testing.T, err *ctxerr.Error,
tdutil.BuildTestName(args...),
fieldName,
indent(got, 16), indent(expected.Contain, 16),
strings.ReplaceAll(err.Error(), "\n\t", "\n\t> "))
fullError(err))
return false
}

Expand All @@ -126,7 +131,7 @@ func cmpErrorStr(t *testing.T, err *ctxerr.Error,
tdutil.BuildTestName(args...),
fieldName,
indent(got, 14), indent(expected.Match.String(), 14),
strings.ReplaceAll(err.Error(), "\n\t", "\n\t> "))
fullError(err))
return false
}

Expand Down Expand Up @@ -162,6 +167,16 @@ func matchError(t *testing.T, err *ctxerr.Error, expectedError expectedError,
return false
}

// under
serr, under := err.Error(), ""
if pos := strings.Index(serr, "\n[under operator "); pos > 0 {
under = serr[pos+2:]
under = under[:strings.IndexByte(under, ']')]
}
if !cmpErrorStr(t, err, under, expectedError.Under, "[under operator …]", args...) {
return false
}

// If expected is a TestDeep, the Location should be set
if expectedIsTestDeep {
expectedError.Located = true
Expand Down
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.GetLocation().IsInitialized() {
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.GetLocation().IsInitialized() {
ctx.CurOperator = curOperator
}
return curOperator.Match(ctx, got)
}

Expand Down
9 changes: 5 additions & 4 deletions td/td_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,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 +344,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 +397,12 @@ 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) MarshalJSON() ([]byte, error) {
Expand Down
Loading