forked from matthiasmullie/minify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSTest.php
More file actions
344 lines (315 loc) · 8.22 KB
/
Copy pathJSTest.php
File metadata and controls
344 lines (315 loc) · 8.22 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
require_once __DIR__ . '/../Minify.php';
require_once __DIR__ . '/../JS.php';
require_once __DIR__ . '/../Exception.php';
use MatthiasMullie\Minify;
/**
* JS minifier test case.
*/
class JSTest extends PHPUnit_Framework_TestCase
{
private $minifier;
/**
* Prepares the environment before running a test.
*/
protected function setUp()
{
parent::setUp();
$this->minifier = new Minify\JS();
}
/**
* Cleans up the environment after running a test.
*/
protected function tearDown()
{
$this->minifier = null;
parent::tearDown();
}
/**
* Test JS minifier rules, provided by dataProvider
*
* @test
* @dataProvider dataProvider
*/
public function minify($input, $expected, $debug = false)
{
$this->minifier->debug((bool) $debug);
$this->minifier->add($input);
$result = $this->minifier->minify();
$this->assertEquals($expected, $result);
}
/**
* Test cases [input, expected result, options]
*
* @return array
*/
public function dataProvider()
{
$tests = array();
// escaped quotes should not terminate string
$tests[] = array(
'alert("Escaped quote which is same as string quotes: \"; should not match")',
'alert("Escaped quote which is same as string quotes: \"; should not match")',
);
// regex delimiters need to be treated as strings
// (two forward slashes could look like a comment)
$tests[] = array(
'/abc\/def\//.test("abc")',
'/abc\/def\//.test("abc")',
);
$tests[] = array(
'var a = /abc\/def\//.test("abc")',
'var a=/abc\/def\//.test("abc")',
);
// mixture of quotes starting in comment/regex, to make sure strings are
// matched correctly, not inside comment/regex.
$tests[] = array(
'/abc"def/.test("abc")',
'/abc"def/.test("abc")',
);
$tests[] = array(
'/* Bogus " */var test="test";',
'var test="test"',
);
// replace comments
$tests[] = array(
'/* This is a JS comment */',
'',
);
// https://github.com/matthiasmullie/minify/issues/10
$tests[] = array(
'// first mutation patch
// second mutation patch
// third mutation patch
// fourth mutation patch',
'',
);
// https://github.com/matthiasmullie/minify/issues/10
$tests[] = array(
'/////////////////////////
// first mutation patch
// second mutation patch
// third mutation patch
// fourth mutation patch
/////////////////////////',
'',
);
// make sure no ; is added in places it shouldn't
$tests[] = array(
'if(true){}else{}',
'if(true){}else{}',
);
$tests[] = array(
'do{i++}while(i<1)',
'do{i++}while(i<1)',
);
$tests[] = array(
'if(true)statement;else statement',
'if(true)statement;else statement',
);
$tests[] = array(
'for (i = 0; (i < 10); i++) statement',
'for(i=0;(i<10);i++)statement', // @todo: ideally, we could get rid of some () too, here
);
$tests[] = array(
'-1
+2',
'-1+2',
);
// test where newline should be preserved (for ASI) or semicolon added
$tests[] = array(
'alert("this is a test");',
'alert("this is a test")',
);
$tests[] = array(
'function(){console.log("this is a test");}',
'function(){console.log("this is a test")}',
);
$tests[] = array(
'alert("this is a test")
alert("this is another test")',
'alert("this is a test")
alert("this is another test")',
);
$tests[] = array(
'a=b+c
d=e+f',
'a=b+c
d=e+f',
);
$tests[] = array(
'a++
++b',
'a++
++b',
);
$tests[] = array(
'!a
!b',
'!a
!b',
);
$tests[] = array(
// don't confuse with 'if'
'digestif
(true)
statement',
'digestif(true)
statement',
);
$tests[] = array(
'if
(
(
true
)
&&
(
true
)
)
statement',
'if((true)&&(true))
statement',
);
$tests[] = array(
'var
variable
=
"value";',
'var variable="value"',
);
$tests[] = array(
'var variable = {
test:
{
}
}',
'var variable={test:{}}',
);
$tests[] = array(
'if
(
true
)
{
}
else
{
}',
'if(true){}
else{}',
);
$tests[] = array(
'if ( true ) {
} else {
}',
'if(true){}else{}',
);
$tests[] = array(
'do
{
i++
}
while
(
i<1
)',
'do{i++}
while(i<1)',
);
$tests[] = array(
'if ( true )
statement
else
statement',
'if(true)
statement
else statement',
);
// remove whitespace around operators
$tests[] = array(
'a = 1 + 2',
'a=1+2',
);
$tests[] = array(
'object . property',
'object.property',
);
$tests[] = array(
'object
.property',
'object.property',
);
$tests[] = array(
'alert ( "this is a test" );',
'alert("this is a test")',
);
// add comment in between whitespace that needs to be stripped
$tests[] = array(
'object
// haha, some comment, just to make things harder!
.property',
'object.property',
);
$tests[] = array(
'
// check if it isn\'t a text-element
if(currentElement.attr(\'type\') != \'text\')
{
// remove the current one
currentElement.remove();
}
// already a text element
else newElement = currentElement;
',
'if(currentElement.attr(\'type\')!=\'text\'){currentElement.remove()}
else newElement=currentElement',
);
$tests[] = array(
'var jsBackend =
{
debug: false,
current: {}
}',
'var jsBackend={debug:false,current:{}}',
);
$tests[] = array(
'var utils =
{
debug: false
}
utils.array =
{
}',
'var utils={debug:false}
utils.array={}',
);
// https://github.com/matthiasmullie/minify/issues/15
$tests[] = array(
'if ( !data.success )
deferred.reject(); else
deferred.resolve(data);',
'if(!data.success)
deferred.reject();else deferred.resolve(data)',
);
$tests[] = array(
"if ( typeof jQuery === 'undefined' )
throw new Error('.editManager.js: jQuery is required and must be loaded first');",
"if(typeof jQuery==='undefined')
throw new Error('.editManager.js: jQuery is required and must be loaded first')",
);
// https://github.com/matthiasmullie/minify/issues/14
$tests[] = array(
'function foo (a, b) {
return a / b;
}
function foo (a, b) {
return a / b;
}',
'function foo(a,b){return a/b}
function foo(a,b){return a/b}',
);
return $tests;
}
}