forked from goccy/go-yaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
78 lines (75 loc) · 1.81 KB
/
error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package yaml
import (
"reflect"
"testing"
"github.com/nobl9/go-yaml/internal/errors"
"github.com/nobl9/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)
}
}
}