-
Notifications
You must be signed in to change notification settings - Fork 2
/
scanner_test.go
120 lines (108 loc) · 2.92 KB
/
scanner_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
// Copyright 2016 Richard Gibson. All rights reserved.
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package canonicaljson
import (
"encoding/json"
"testing"
)
// Tests of simple examples.
type example struct {
compact string
indent string
}
var examples = []example{
{`1`, `1`},
{`{}`, `{}`},
{`[]`, `[]`},
{`{"":2}`, "{\n\t\"\": 2\n}"},
{`[3]`, "[\n\t3\n]"},
{`[1,2,3]`, "[\n\t1,\n\t2,\n\t3\n]"},
{`{"x":1}`, "{\n\t\"x\": 1\n}"},
{ex1, ex1i},
}
var ex1 = `[true,false,null,"x",1,1.5E0,0,-500]`
var ex1i = `[
true,
false,
null,
"x",
1,
1.5E0,
0,
-500
]`
func TestMarshal(t *testing.T) {
var obj interface{}
for _, tt := range examples {
if err := json.Unmarshal([]byte(tt.compact), &obj); err != nil {
t.Errorf("json.Unmarshal(%#q): %v", tt.compact, err)
} else {
s, err := Marshal(obj)
if err != nil {
t.Errorf("Marshal(%#q): %v", tt.compact, err)
} else if string(s) != tt.compact {
t.Errorf("Marshal(%#q) = %#q, want original", tt.compact, string(s))
}
}
if err := json.Unmarshal([]byte(tt.indent), &obj); err != nil {
t.Errorf("json.Unmarshal(%#q): %v", tt.indent, err)
} else {
s, err := Marshal(obj)
if err != nil {
t.Errorf("Marshal(%#q): %v", tt.indent, err)
} else if string(s) != tt.compact {
t.Errorf("Marshal(%#q) = %#q, want %#q", tt.indent, string(s), tt.compact)
}
}
}
}
func TestMarshalSeparators(t *testing.T) {
// U+2028 and U+2029 should be unescaped inside strings.
// They should not appear outside strings.
tests := []struct {
in, compact string
}{
{"{\"\u2028\": 1}", "{\"\u2028\":1}"},
{"{\"\u2029\" :2}", "{\"\u2029\":2}"},
}
for _, tt := range tests {
var obj interface{}
if err := json.Unmarshal([]byte(tt.in), &obj); err != nil {
t.Errorf("json.Unmarshal(%q): %v", tt.in, err)
} else {
s, err := Marshal(obj)
if err != nil {
t.Errorf("Marshal(%#q): %v", tt.in, err)
} else if string(s) != tt.compact {
t.Errorf("Marshal(%q) = %q, want %q", tt.in, s, tt.compact)
}
}
}
}
func TestMarshalIndent(t *testing.T) {
var obj interface{}
for _, tt := range examples {
if err := json.Unmarshal([]byte(tt.indent), &obj); err != nil {
t.Errorf("json.Unmarshal(%#q): %v", tt.indent, err)
} else {
s, err := MarshalIndent(obj, "", "\t")
if err != nil {
t.Errorf("MarshalIndent(%#q): %v", tt.indent, err)
} else if string(s) != tt.indent {
t.Errorf("MarshalIndent(%#q) = %#q, want original", tt.indent, s)
}
}
if err := json.Unmarshal([]byte(tt.compact), &obj); err != nil {
t.Errorf("json.Unmarshal(%#q): %v", tt.compact, err)
} else {
s, err := MarshalIndent(obj, "", "\t")
if err != nil {
t.Errorf("MarshalIndent(%#q): %v", tt.compact, err)
} else if string(s) != tt.indent {
t.Errorf("MarshalIndent(%#q) = %#q, want %#q", tt.compact, s, tt.indent)
}
}
}
}