-
Notifications
You must be signed in to change notification settings - Fork 16
/
json5_test.go
154 lines (143 loc) · 3.45 KB
/
json5_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package json5
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/kylelemons/godebug/pretty"
"github.com/robertkrimen/otto"
)
type ErrorSpec struct {
At int
LineNumber int
ColumnNumber int
Message string
}
func TestJSON5Decode(t *testing.T) {
filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
data, err := ioutil.ReadFile(path)
if err != nil {
t.Errorf("error reading file: %s", err)
return nil
}
parseJSON5 := func() (interface{}, error) {
var res interface{}
return res, Unmarshal(data, &res)
}
parseJSON := func() (interface{}, error) {
var res interface{}
return res, json.Unmarshal(data, &res)
}
parseES5 := func() (interface{}, error) {
vm := otto.New()
_, err := vm.Run("x=" + string(data))
if err != nil {
return nil, err
}
v, err := vm.Get("x")
if err != nil {
return nil, err
}
return v.Export()
}
t.Logf("file: %s", path)
switch filepath.Ext(path) {
case ".json":
jd, err := parseJSON()
if err != nil {
t.Errorf("unexpected error from json decoder: %s", err)
return nil
}
j5d, err := parseJSON5()
if err != nil {
t.Errorf("unexpected error from json5 decoder: %s", err)
return nil
}
if diff := pretty.Compare(jd, j5d); diff != "" {
t.Errorf("data is not equal\n%s", diff)
return nil
}
case ".json5":
if _, err := parseJSON(); err == nil {
t.Errorf("expected JSON parsing to fail")
return nil
}
es5d, err := parseES5()
if err != nil {
t.Errorf("unexpected error from ES5 decoder: %s", err)
return nil
}
j5d, err := parseJSON5()
if err != nil {
t.Errorf("unexpected error from json5 decoder: %s", err)
return nil
}
if diff := pretty.Compare(j5d, es5d); diff != "" {
t.Errorf("data is not equal\n%s", diff)
return nil
}
case ".js":
if _, err := parseJSON(); err == nil {
t.Errorf("expected JSON parsing to fail")
return nil
}
if _, err := parseES5(); err != nil {
t.Errorf("unexected error from ES5 decoder: %s", err)
return nil
}
if _, err := parseJSON5(); err == nil {
t.Errorf("expected JSON5 parsing to fail")
return nil
}
case ".txt":
var expectedErr *ErrorSpec
specName := strings.TrimRight(path, filepath.Ext(path)) + ".errorSpec"
specFile, err := os.Open(specName)
if err != nil && !os.IsNotExist(err) {
t.Errorf("error trying to open errorSpec file %s: %s", specName, err)
return nil
}
if specFile != nil {
defer specFile.Close()
expectedErr = &ErrorSpec{}
if err := NewDecoder(specFile).Decode(expectedErr); err != nil {
t.Errorf("error decoding %s: %s", specName, err)
return nil
}
}
_, err = parseJSON5()
if err == nil {
t.Errorf("expected JSON5 parsing to fail")
return nil
}
}
return nil
})
}
// The tests below this comment were found with go-fuzz
func TestQuotedQuote(t *testing.T) {
var v struct {
E string
}
if err := Unmarshal([]byte(`{e:"'"}`), &v); err != nil {
t.Error(err)
}
if v.E != "'" {
t.Errorf(`expected "'", got %q`, v.E)
}
}
func TestInvalidNewline(t *testing.T) {
expected := "invalid character '\\n' in string literal"
var v interface{}
if err := Unmarshal([]byte("{a:'\\\r0\n'}"), &v); err == nil || err.Error() != expected {
t.Errorf("expected error %q, got %s", expected, err)
}
}