forked from tdewolff/minify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_test.go
More file actions
109 lines (97 loc) · 2.38 KB
/
Copy pathjson_test.go
File metadata and controls
109 lines (97 loc) · 2.38 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
package json
import (
"bytes"
"fmt"
"os"
"regexp"
"testing"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/test"
)
func TestJSON(t *testing.T) {
jsonTests := []struct {
json string
expected string
}{
{"", ""},
{"{ \"a\": [1, 2] }", "{\"a\":[1,2]}"},
{"[{ \"a\": [{\"x\": null}, true] }]", "[{\"a\":[{\"x\":null},true]}]"},
{"{ \"a\": 1, \"b\": 2 }", "{\"a\":1,\"b\":2}"},
{"1.3e1", "13"},
{"1E+03", "1e3"},
{"0.1", "0.1"},
{"-0.1", "-0.1"},
{"1.0", "1"},
}
m := minify.New()
for _, tt := range jsonTests {
t.Run(tt.json, func(t *testing.T) {
r := bytes.NewBufferString(tt.json)
w := &bytes.Buffer{}
err := Minify(m, w, r, nil)
test.Minify(t, tt.json, err, w.String(), tt.expected)
})
}
}
func TestJSON_IgnoreNumbers(t *testing.T) {
jsonTests := []struct {
json string
expected string
}{
{"", ""},
{"{ \"a\": [1, 2] }", "{\"a\":[1,2]}"},
{"[{ \"a\": [{\"x\": null}, true] }]", "[{\"a\":[{\"x\":null},true]}]"},
{"{ \"a\": 1, \"b\": 2 }", "{\"a\":1,\"b\":2}"},
{"{ \"a\": 1 , \"b\": 2 }", "{\"a\":1,\"b\":2}"},
{"1.3e1", "1.3e1"},
{"1E+03", "1E+03"},
{"0.1", "0.1"},
{"-0.1", "-0.1"},
{"1.0", "1.0"},
{"10000", "10000"},
}
m := Minifier{KeepNumbers: true}
for _, tt := range jsonTests {
t.Run(tt.json, func(t *testing.T) {
r := bytes.NewBufferString(tt.json)
w := &bytes.Buffer{}
err := m.Minify(nil, w, r, nil)
test.Minify(t, tt.json, err, w.String(), tt.expected)
})
}
}
func TestReaderErrors(t *testing.T) {
r := test.NewErrorReader(0)
w := &bytes.Buffer{}
m := minify.New()
err := Minify(m, w, r, nil)
test.T(t, err, test.ErrPlain, "return error at first read")
}
func TestWriterErrors(t *testing.T) {
errorTests := []struct {
json string
n []int
}{
//01 234 56 78
{`{"key":[100,200]}`, []int{0, 1, 2, 3, 4, 5, 7, 8}},
}
m := minify.New()
for _, tt := range errorTests {
for _, n := range tt.n {
t.Run(fmt.Sprint(tt.json, " ", tt.n), func(t *testing.T) {
r := bytes.NewBufferString(tt.json)
w := test.NewErrorWriter(n)
err := Minify(m, w, r, nil)
test.T(t, err, test.ErrPlain)
})
}
}
}
////////////////////////////////////////////////////////////////
func ExampleMinify() {
m := minify.New()
m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), Minify)
if err := m.Minify("application/json", os.Stdout, os.Stdin); err != nil {
panic(err)
}
}