Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nieomylnieja committed Feb 1, 2024
1 parent 9eb62dc commit cc4ec16
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package yaml

import (
"testing"

"golang.org/x/xerrors"

"github.com/goccy/go-yaml/internal/errors"
"github.com/goccy/go-yaml/token"
)

func TestAsSyntaxError(t *testing.T) {
tests := []struct {
input error
expected *SyntaxError
}{
{
input: nil,
expected: nil,
},
{
input: xerrors.New("dummy test"),
expected: nil,
},
{
input: errors.ErrSyntax("some error", &token.Token{Value: "123"}),
expected: &SyntaxError{
Msg: "some error",
Token: &token.Token{Value: "123"},
},
},
{
input: xerrors.Errorf(
"something went wrong: %w",
errors.ErrSyntax("some error", &token.Token{Value: "123"})),
expected: &SyntaxError{
Msg: "some error",
Token: &token.Token{Value: "123"},
},
},
}
for _, test := range tests {
syntaxErr := AsSyntaxError(test.input)
if test.expected == nil {
if syntaxErr != nil {
t.Fatalf("wanted nil, but go %v", syntaxErr)
}
return
}
if *test.expected.Token != *syntaxErr.Token && test.expected.Msg != syntaxErr.Msg {
t.Fatalf("unexpected output.\nexpect:\n[%v]\nactual:\n[%v]", test.expected, syntaxErr)
}
}
}

0 comments on commit cc4ec16

Please sign in to comment.