forked from tdewolff/minify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_test.go
More file actions
77 lines (67 loc) · 1.94 KB
/
Copy pathjs_test.go
File metadata and controls
77 lines (67 loc) · 1.94 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
package js // import "github.com/tdewolff/minify/js"
import (
"bytes"
"os"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tdewolff/minify"
"github.com/tdewolff/test"
)
func TestJS(t *testing.T) {
var jsTests = []struct {
js string
expected string
}{
{"/*comment*/", ""},
{"// comment\na", "a"},
{"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\"\"", "+\"\""},
{"for(var a=0;;){}", "for(var a=0;;){}"},
{"a + ++b", "a+ ++b"}, // JSMin caution
{"var a=/\\s?auto?\\s?/i\nvar", "var a=/\\s?auto?\\s?/i\nvar"}, // #14
}
m := minify.New()
for _, tt := range jsTests {
b := &bytes.Buffer{}
assert.Nil(t, Minify(m, b, bytes.NewBufferString(tt.js), nil), "Minify must not return error in "+tt.js)
assert.Equal(t, tt.expected, b.String(), "Minify must give expected result in "+tt.js)
}
}
func TestReaderErrors(t *testing.T) {
m := minify.New()
r := test.NewErrorReader(0)
w := &bytes.Buffer{}
assert.Equal(t, test.ErrPlain, Minify(m, w, r, nil), "Minify must return error at first read")
}
func TestWriterErrors(t *testing.T) {
var errorTests = []int{0, 1, 4}
m := minify.New()
for _, n := range errorTests {
// writes: 01 2345
r := bytes.NewBufferString("a\n{5 5")
w := test.NewErrorWriter(n)
assert.Equal(t, test.ErrPlain, Minify(m, w, r, nil), "Minify must return error at write "+strconv.FormatInt(int64(n), 10))
}
}
////////////////////////////////////////////////////////////////
func ExampleMinify() {
m := minify.New()
m.AddFunc("text/javascript", Minify)
if err := m.Minify("text/javascript", os.Stdout, os.Stdin); err != nil {
panic(err)
}
}