Repository navigation
Expand file tree
/
Copy pathjs_test.go
More file actions
89 lines (79 loc) · 2.27 KB
/
Copy pathjs_test.go
File metadata and controls
89 lines (79 loc) · 2.27 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
package js // import "github.com/tdewolff/minify/js"
import (
"bytes"
"os"
"testing"
"github.com/tdewolff/minify"
"github.com/tdewolff/test"
)
func TestJS(t *testing.T) {
jsTests := []struct {
js string
expected string
}{
{"/*comment*/", ""},
{"// comment\na", "a"},
{"/*! bang comment */", "/*!bang comment*/"},
{"function x(){}", "function x(){}"},
{"function x(a, b){}", "function x(a,b){}"},
{"a b", "a b"},
{"a\n\nb", "a\nb"},
{"a// comment\nb", "a\nb"},
{"''\na", "''\na"},
{"''\n''", "''''"},
{"]\n0", "]\n0"},
{"a\n{", "a\n{"},
{";\na", ";a"},
{",\na", ",a"},
{"}\na", "}\na"},
{"+\na", "+\na"},
{"+\n(", "+\n("},
{"+\n\"\"", "+\"\""},
{"a + ++b", "a+ ++b"}, // JSMin caution
{"var a=/\\s?auto?\\s?/i\nvar", "var a=/\\s?auto?\\s?/i\nvar"}, // #14
{"var a=0\n!function(){}", "var a=0\n!function(){}"}, // #107
{"function(){}\n\"string\"", "function(){}\n\"string\""}, // #109
{"false\n\"string\"", "false\n\"string\""}, // #109
{"`\n", "`"}, // go fuzz
{"a\n~b", "a\n~b"}, // #132
}
m := minify.New()
for _, tt := range jsTests {
r := bytes.NewBufferString(tt.js)
w := &bytes.Buffer{}
test.Minify(t, tt.js, Minify(m, w, r, nil), w.String(), tt.expected)
}
}
func TestReaderErrors(t *testing.T) {
m := minify.New()
r := test.NewErrorReader(0)
w := &bytes.Buffer{}
test.Error(t, Minify(m, w, r, nil), test.ErrPlain, "return error at first read")
}
func TestWriterErrors(t *testing.T) {
errorTests := []struct {
js string
n []int
}{
//01 2345
{"a\n{5 5", []int{0, 1, 4}},
{`/*!comment*/`, []int{0, 1, 2}},
{"false\n\"string\"", []int{1}}, // #109
}
m := minify.New()
for _, tt := range errorTests {
for _, n := range tt.n {
r := bytes.NewBufferString(tt.js)
w := test.NewErrorWriter(n)
test.Error(t, Minify(m, w, r, nil), test.ErrPlain, "return error at write", n, "in", tt.js)
}
}
}
////////////////////////////////////////////////////////////////
func ExampleMinify() {
m := minify.New()
m.AddFunc("text/javascript", Minify)
if err := m.Minify("text/javascript", os.Stdout, os.Stdin); err != nil {
panic(err)
}
}