File: util_test.go

package info (click to toggle)
golang-github-tdewolff-minify 2.20.37-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 39,388 kB
  • sloc: javascript: 394,644; xml: 25,649; ansic: 253; makefile: 108; python: 108; sh: 47
file content (144 lines) | stat: -rw-r--r-- 4,318 bytes parent folder | download
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
package js

import (
	"io"
	"testing"

	"github.com/tdewolff/parse/v2"
	"github.com/tdewolff/parse/v2/js"
	"github.com/tdewolff/test"
)

func TestBinaryNumber(t *testing.T) {
	test.Bytes(t, binaryNumber([]byte("0b0"), 0), []byte("0"))
	test.Bytes(t, binaryNumber([]byte("0b1"), 0), []byte("1"))
	test.Bytes(t, binaryNumber([]byte("0b1001"), 0), []byte("9"))
	test.Bytes(t, binaryNumber([]byte("0b100000000000000000000000000000000000000000000000000000000000000"), 0), []byte("4611686018427387904"))
	test.Bytes(t, binaryNumber([]byte("0b1000000000000000000000000000000000000000000000000000000000000000"), 0), []byte("0b1000000000000000000000000000000000000000000000000000000000000000"))
}

func TestOctalNumber(t *testing.T) {
	test.Bytes(t, octalNumber([]byte("0o0"), 0), []byte("0"))
	test.Bytes(t, octalNumber([]byte("0o1"), 0), []byte("1"))
	test.Bytes(t, octalNumber([]byte("0o775"), 0), []byte("509"))
	test.Bytes(t, octalNumber([]byte("0o100000000000000000000"), 0), []byte("1152921504606846976"))
	test.Bytes(t, octalNumber([]byte("0o1000000000000000000000"), 0), []byte("0o1000000000000000000000"))
}

func TestHexadecimalNumber(t *testing.T) {
	test.Bytes(t, hexadecimalNumber([]byte("0x0"), 0), []byte("0"))
	test.Bytes(t, hexadecimalNumber([]byte("0x1"), 0), []byte("1"))
	test.Bytes(t, hexadecimalNumber([]byte("0xFE"), 0), []byte("254"))
	test.Bytes(t, hexadecimalNumber([]byte("0x1000000000"), 0), []byte("68719476736"))
	test.Bytes(t, hexadecimalNumber([]byte("0xd000000000"), 0), []byte("893353197568"))
	test.Bytes(t, hexadecimalNumber([]byte("0xe000000000"), 0), []byte("0xe000000000"))
	test.Bytes(t, hexadecimalNumber([]byte("0xE000000000"), 0), []byte("0xE000000000"))
	test.Bytes(t, hexadecimalNumber([]byte("0x10000000000"), 0), []byte("0x10000000000"))
}

func TestString(t *testing.T) {
	tests := []struct {
		str, expected string
	}{
		{`""`, `""`},
		{`"abc"`, `"abc"`},
		{`'abc'`, `"abc"`},
		{`"\8\9\t"`, "\"89\t\""},
		{`"\n\r"`, "`\n\\r`"},
		{`"\12\15"`, "`\n\\r`"},
		{`"\x0A\x0D"`, "`\n\\r`"},
		{`"\u000A\u000D"`, "`\n\\r`"},
		{`"\u{A}\u{D}"`, "`\n\\r`"},
		{`"\n$"`, "`\n$`"},
		{`"\n${"`, `"\n${"`},
		{`"\n\r${${"`, `"\n\r${${"`},
		{`"\12\15${${"`, `"\n\r${${"`},
		{`"\x0A\x0D${${"`, `"\n\r${${"`},
		{`"\u000A\u000D${${"`, `"\n\r${${"`},
		{`"\u{A}\u{D}${${"`, `"\n\r${${"`},
		{`"\42"`, `'"'`},
		{`"\x22"`, `'"'`},
		{`"\u0022"`, `'"'`},
		{`"\u{22}"`, `'"'`},
		{`"\42''"`, "`\"''`"},
		{`"\x22''"`, "`\"''`"},
		{`"\u0022''"`, "`\"''`"},
		{`"\u{022}''"`, "`\"''`"},
		{"\"\\42''``\"", "\"\\\"''``\""},
		{"\"\\x22''``\"", "\"\\\"''``\""},
		{"\"\\u0022''``\"", "\"\\\"''``\""},
		{"\"\\u{0022}''``\"", "\"\\\"''``\""},
		{`'\47'`, `"'"`},
		{`'\x27'`, `"'"`},
		{`'\u0027'`, `"'"`},
		{`'\u{27}'`, `"'"`},
		{`'\47""'`, "`'\"\"`"},
		{`'\x27""'`, "`'\"\"`"},
		{`'\u0027""'`, "`'\"\"`"},
		{`'\u{027}""'`, "`'\"\"`"},
		{"'\\47\"\"``'", "'\\'\"\"``'"},
		{"'\\x27\"\"``'", "'\\'\"\"``'"},
		{"'\\u0027\"\"``'", "'\\'\"\"``'"},
		{"'\\u{0027}\"\"``'", "'\\'\"\"``'"},
		{`'\140'`, "\"`\""},
		{`'\x60'`, "\"`\""},
		{`'\u0060'`, "\"`\""},
		{`'\u{60}'`, "\"`\""},
		{`'\140""'`, "'`\"\"'"},
		{`'\x60""'`, "'`\"\"'"},
		{`'\u0060""'`, "'`\"\"'"},
		{`'\u{060}""'`, "'`\"\"'"},
		{`'\140""\'\''`, "`\\`\"\"''`"},
		{`'\x60""\'\''`, "`\\`\"\"''`"},
		{`'\u0060""\'\''`, "`\\`\"\"''`"},
		{`'\u{0060}""\'\''`, "`\\`\"\"''`"},
	}

	for _, tt := range tests {
		t.Run(tt.str, func(t *testing.T) {
			test.Bytes(t, minifyString([]byte(tt.str), true), []byte(tt.expected))
		})
	}
}

func TestHasSideEffects(t *testing.T) {
	jsTests := []struct {
		js  string
		has bool
	}{
		{"1", false},
		{"a", true},
		{"a++", true},
		{"a--", true},
		{"++a", true},
		{"--a", true},
		{"delete a", true},
		{"!0", false},
		{"a=5", true},
		{"a+=5", true},
		{"a+5", false},
		{"a()", true},
		{"a.b", true},
		{"a.b()", true},
		{"a().b", true},
		{"a[b]", true},
		{"a[b()]", true},
		{"a()[b]", true},
		{"a?.b", true},
		{"a()?.b", true},
		{"a?.b()", true},
		{"new a", true},
		{"new a()", true},
	}

	for _, tt := range jsTests {
		t.Run(tt.js, func(t *testing.T) {
			ast, err := js.Parse(parse.NewInputString(tt.js), js.Options{})
			if err != io.EOF {
				test.Error(t, err)
			}
			expr := ast.List[0].(*js.ExprStmt).Value
			test.T(t, hasSideEffects(expr), tt.has)
		})
	}
}