-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
78 lines (70 loc) · 1.61 KB
/
parser_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 json2
import (
"reflect"
"testing"
)
func TestUnmarshal(t *testing.T) {
type TestCase struct {
input string
exp interface{}
}
tests := []TestCase{
{`null`, nil},
{`false`, false},
{`true`, true},
{`-1.0`, -1.0},
{`"foo \" bar"`, `foo " bar`},
{`["foo", "bar"]`, []interface{}{"foo", "bar"}},
{`{"foo": "bar", "bar": "baz"}`, map[string]interface{}{"foo": "bar", "bar": "baz"}},
}
for _, test := range tests {
act, err := Unmarshal(test.input)
if err != (*ParseError)(nil) {
t.Errorf("Error: %v\nInput: %s", err, test.input)
continue
}
if !reflect.DeepEqual(act, test.exp) {
t.Errorf("Expected: %#v\nActual: %#v", test.exp, act)
}
}
}
func TestUnmarshalError(t *testing.T) {
type TestCase struct {
input string
err error
}
tests := []TestCase{
{``, &ParseError{Found: nil}},
{` `, &ParseError{Found: nil}},
{`nul`, &TokenError{Found: "nul"}},
{`true false`, &ParseError{Found: &Token{
Type: Boolean,
Value: false,
}}},
{`[,]`, &ParseError{Found: &Token{
Type: Comma,
}}},
{`[12,]`, &ParseError{Found: &Token{
Type: RSquare,
}}},
{`[12,13`, &ParseError{Found: nil}},
{`{`, &ParseError{Found: nil}},
{`{true:3}`, &ParseError{Found: &Token{
Type: Boolean,
Value: true,
}}},
{`{"key":"value", "foo" "value"}`, &ParseError{Found: &Token{
Type: String,
Value: "value",
}}},
{`{"key":"value", "foo":}`, &ParseError{Found: &Token{
Type: RCurly,
}}},
}
for _, test := range tests {
_, err := Unmarshal(test.input)
if !reflect.DeepEqual(err, test.err) {
t.Errorf("Expected: %#v\nActual:%#v", err, test.err)
}
}
}