forked from goccy/go-yaml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Expose syntax and type errors (#1)
Ref: goccy#424
- Loading branch information
1 parent
5cf06f1
commit 99bcbfd
Showing
4 changed files
with
142 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package yaml | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/goccy/go-yaml/internal/errors" | ||
"github.com/goccy/go-yaml/token" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
func TestAsSyntaxError(t *testing.T) { | ||
tests := []struct { | ||
input error | ||
expected *TokenScopedError | ||
}{ | ||
{ | ||
input: nil, | ||
expected: nil, | ||
}, | ||
{ | ||
input: xerrors.New("dummy test"), | ||
expected: nil, | ||
}, | ||
{ | ||
input: errors.ErrSyntax("some error", &token.Token{Value: "123"}), | ||
expected: &TokenScopedError{ | ||
Msg: "some error", | ||
Token: &token.Token{Value: "123"}, | ||
}, | ||
}, | ||
{ | ||
input: xerrors.Errorf( | ||
"something went wrong: %w", | ||
errors.ErrSyntax("some error", &token.Token{Value: "123"})), | ||
expected: &TokenScopedError{ | ||
Msg: "some error", | ||
Token: &token.Token{Value: "123"}, | ||
}, | ||
}, | ||
{ | ||
input: errUnknownField("unknown field", &token.Token{Value: "123"}), | ||
expected: &TokenScopedError{ | ||
Msg: "unknown field", | ||
Token: &token.Token{Value: "123"}, | ||
}, | ||
}, | ||
{ | ||
input: errDuplicateKey("duplicate key", &token.Token{Value: "123"}), | ||
expected: &TokenScopedError{ | ||
Msg: "duplicate key", | ||
Token: &token.Token{Value: "123"}, | ||
}, | ||
}, | ||
{ | ||
input: errTypeMismatch(reflect.TypeOf("string"), reflect.TypeOf(0), &token.Token{Value: "123"}), | ||
expected: &TokenScopedError{ | ||
Msg: "cannot unmarshal int into Go value of type string", | ||
Token: &token.Token{Value: "123"}, | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
syntaxErr := AsTokenScopedError(test.input) | ||
if test.expected == nil { | ||
if syntaxErr != nil { | ||
t.Fatalf("wanted nil, but go %v", syntaxErr) | ||
} | ||
continue | ||
} | ||
if syntaxErr == nil { | ||
t.Fatalf("must not be nil") | ||
} | ||
if *test.expected.Token != *syntaxErr.Token || test.expected.Msg != syntaxErr.Msg { | ||
t.Fatalf("unexpected output.\nexpect:\n[%v]\nactual:\n[%v]", test.expected, syntaxErr) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters