-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_test.go
More file actions
147 lines (136 loc) · 3.81 KB
/
Copy pathformat_test.go
File metadata and controls
147 lines (136 loc) · 3.81 KB
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
package jsonc
import (
"strings"
"testing"
)
func TestToJSONStripsComments(t *testing.T) {
src := `{
// line comment
"a": 1, /* block */
"b": [1, 2, 3,], // trailing
}`
out, err := ToJSON([]byte(src))
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(out), "//") || strings.Contains(string(out), "/*") {
t.Errorf("output should not contain comments: %s", out)
}
if strings.Contains(string(out), ",}") || strings.Contains(string(out), ",]") {
t.Errorf("output should not contain trailing commas: %s", out)
}
}
func TestStripCommentsAlias(t *testing.T) {
out1, err := ToJSON([]byte(`{"a":1 /* x */}`))
if err != nil {
t.Fatal(err)
}
out2, err := StripComments([]byte(`{"a":1 /* x */}`))
if err != nil {
t.Fatal(err)
}
if string(out1) != string(out2) {
t.Errorf("ToJSON and StripComments should produce identical output")
}
}
func TestFormatPrettyPrints(t *testing.T) {
src := `{"a":1,"b":[1,2,3]}`
out, err := Format([]byte(src))
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(out), "\n") {
t.Errorf("Format should produce multi-line output: %s", out)
}
if !strings.Contains(string(out), " ") {
t.Errorf("Format should use 2-space indent by default: %s", out)
}
}
func TestFormatPreservesComments(t *testing.T) {
src := `{
// keep me
"a": 1
}`
out, err := Format([]byte(src))
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(out), "keep me") {
t.Errorf("Format should preserve comments: %s", out)
}
}
func TestFormatCustomIndent(t *testing.T) {
out, err := Format([]byte(`{"a":1}`), WithIndent("\t"))
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(out), "\t\"a\"") {
t.Errorf("expected tab indent: %q", out)
}
}
func TestMinimizeRemovesWhitespace(t *testing.T) {
src := `{
"a": 1,
"b": [1, 2, 3]
}`
out, err := Minimize([]byte(src))
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(out), "\n") {
t.Errorf("Minimize should not produce newlines: %q", out)
}
}
func TestFormatRoundTrip(t *testing.T) {
src := `{
"name": "test",
"values": [1, 2, 3]
}`
out, err := Format([]byte(src))
if err != nil {
t.Fatal(err)
}
// Format result should itself be valid JSONC.
if _, err := Format(out); err != nil {
t.Errorf("Format output should be re-parseable: %v", err)
}
}
func TestFormatInvalidInput(t *testing.T) {
_, err := Format([]byte(`{not valid`))
if err == nil {
t.Fatal("expected error on invalid input")
}
}
// TestFormatCommentWithEmbeddedNewline pins a regression discovered by
// FuzzFormat: a trailing block comment whose CR was normalized to LF gets
// captured as the value's inline comment. Naively re-emitting it as
// `// <text>` would leak the text past the comment and corrupt the
// document. The encoder must fall back to /* … */ form.
func TestFormatCommentWithEmbeddedNewline(t *testing.T) {
in := []byte("{}/*\r0*/")
out, err := Format(in)
if err != nil {
t.Fatal(err)
}
// Output must round-trip through Parse without any "extra data" complaint.
if _, err := Parse(out); err != nil {
t.Errorf("Format output not re-parseable: %v\nout=%q", err, out)
}
}
// TestFormatCommentWithStarSlashSequence pins the defense against `*/`
// appearing inside text that the encoder must wrap in /* … */. We can't
// produce this through the parser (the scanner stops at the first */), but
// it is reachable via WithComment user input — exercised in the encoder
// suite. This test just confirms Format is stable for the parser-source
// case where a block comment's value already cannot contain */ but the
// trailing-content path still produces sane output.
func TestFormatTrailingBlockCommentRoundTrip(t *testing.T) {
in := []byte(`{"a": 1} /* trailing */`)
out, err := Format(in)
if err != nil {
t.Fatal(err)
}
if _, err := Parse(out); err != nil {
t.Errorf("not re-parseable: %v\nout=%q", err, out)
}
}