forked from muhammadmuzzammil1998/jsonc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonc_test.go
196 lines (181 loc) · 5.13 KB
/
jsonc_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package jsonc
import (
"encoding/json"
"io/ioutil"
"os"
"reflect"
"testing"
)
func b(s string) []byte { return []byte(s) }
func s(b []byte) string { return string(b) }
type testsStruct struct {
validBlock []byte
validSingle []byte
invalidBlock []byte
invalidSingle []byte
}
var jsonTest, jsoncTest testsStruct
func init() {
jsonTest = testsStruct{
validBlock: b(`{"foo":"bar foo","true":false,"number":42,"object":{"test":"done"},"array":[1,2,3],"url":"https://github.com","escape":"\"wo//rking"}`),
invalidBlock: b(`{"foo":`),
}
jsoncTest = testsStruct{
validBlock: b(`{"foo": /** this is a bloc/k comm\"ent */ "bar foo", "true": /* true */ false, "number": 42, "object": { "test": "done" }, "array" : [1, 2, 3], "url" : "https://github.com", "escape":"\"wo//rking" }`),
invalidBlock: b(`{"foo": /* this is a block comment "bar foo", "true": false, "number": 42, "object": { "test": "done" }, "array" : [1, 2, 3], "url" : "https://github.com", "escape":"\"wo//rking }`),
validSingle: b("{\"foo\": // this is a single line comm\\\"ent\n\"bar foo\", \"true\": false, \"number\": 42, \"object\": { \"test\": \"done\" }, \"array\" : [1, 2, 3], \"url\" : \"https://github.com\", \"escape\":\"\\\"wo//rking\" }"),
invalidSingle: b(`{"foo": // this is a single line comment "bar foo", "true": false, "number": 42, "object": { "test": "done" }, "array" : [1, 2, 3], "url" : "https://github.com", "escape":"\"wo//rking" }`),
}
}
func TestToJSON(t *testing.T) {
tests := []struct {
name string
arg []byte
want []byte
wantErr bool
}{
{
name: "Test for valid block comment.",
arg: jsoncTest.validBlock,
want: jsonTest.validBlock,
}, {
name: "Test for invalid block comment.",
arg: jsoncTest.invalidBlock,
want: jsonTest.invalidBlock,
wantErr: true,
}, {
name: "Test for valid single line comment.",
arg: jsoncTest.validSingle,
want: jsonTest.validBlock,
}, {
name: "Test for invalid single line comment.",
arg: jsoncTest.invalidSingle,
want: jsonTest.invalidBlock,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ToJSON(tt.arg)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToJSON() = %v, want %v", s(got), s(tt.want))
}
if !json.Valid(got) && !tt.wantErr {
t.Errorf("ToJSON() = %v isn't valid.", s(got))
}
})
}
}
func TestUnmarshal(t *testing.T) {
type UnmarshalTest struct {
Foo string `json:"foo"`
True bool `json:"true"`
Num int `json:"number"`
Object struct {
Test string `json:"test"`
} `json:"object"`
Array []int `json:"array"`
URL string `json:"url"`
Escape string `json:"escape"`
}
t.Run("Testing Unmarshal()", func(t *testing.T) {
un := UnmarshalTest{}
if err := Unmarshal(jsoncTest.validBlock, &un); err != nil {
t.Errorf("Unmarshal() error = %v", err)
}
mr, err := json.Marshal(un)
if err != nil {
t.Errorf("Unmarshal() unable to marshal Unmarshal(). Error = %v, got = %v, want = %v", err, s(mr), s(jsonTest.validBlock))
}
if !reflect.DeepEqual(mr, jsonTest.validBlock) {
t.Errorf("Unmarshal() didn't work correctly. Got = %v, want = %v", s(mr), s(jsonTest.validBlock))
}
})
}
func TestReadFromFile(t *testing.T) {
tmp, err := ioutil.TempFile("", "ReadFromFileTest")
if err != nil {
t.Skip("Unable to create temp file.", err)
}
defer os.Remove(tmp.Name())
if _, err := tmp.Write(jsoncTest.validBlock); err != nil {
t.Skip("Unable to write to file.", err)
}
defer func() {
if err := tmp.Close(); err != nil {
t.Log("Unable to close the file.", err)
}
}()
tests := []struct {
name string
filename string
want []byte
want1 []byte
wantErr bool
}{
{
name: "Valid use of ReadFromFile()",
filename: tmp.Name(),
want: jsoncTest.validBlock,
want1: jsonTest.validBlock,
wantErr: false,
},
{
name: "Invalid use of ReadFromFile()",
filename: "NonExistentFile",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, err := ReadFromFile(tt.filename)
if (err != nil) != tt.wantErr {
t.Errorf("ReadFromFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ReadFromFile() got = %v, want %v", got, tt.want)
}
if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("ReadFromFile() got1 = %v, want %v", got1, tt.want1)
}
})
}
}
func TestValid(t *testing.T) {
tests := []struct {
name string
data []byte
want bool
}{
{
name: "Valid test",
data: b(`{"foo":/*comment*/"bar"}`),
want: true,
},
{
name: "Invalid test",
data: b(`{"foo"://comment without ending`),
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Valid(tt.data); got != tt.want {
t.Errorf("Valid() = %v, want %v", got, tt.want)
}
})
}
}
func BenchmarkTranslate(b *testing.B) {
for n := 0; n < b.N; n++ {
translate(jsoncTest.validSingle)
translate(jsoncTest.validBlock)
}
}
func BenchmarkValid(b *testing.B) {
for n := 0; n < b.N; n++ {
Valid(jsoncTest.validSingle)
Valid(jsoncTest.validBlock)
}
}