|
7 | 7 |
|
8 | 8 | class JSStripTest extends TestCase
|
9 | 9 | {
|
10 |
| - |
11 |
| - function test_mlcom1() |
12 |
| - { |
13 |
| - $text = '/** |
14 |
| - * A multi |
15 |
| - * line *test* |
16 |
| - * check |
17 |
| - */'; |
18 |
| - $this->assertEquals('', (new JSStrip())->compress($text)); |
19 |
| - } |
20 |
| - |
21 |
| - function test_mlcom2() |
22 |
| - { |
23 |
| - $text = 'var foo=6;/* another comment */'; |
24 |
| - $this->assertEquals('var foo=6;', (new JSStrip())->compress($text)); |
25 |
| - } |
26 |
| - |
27 |
| - function test_mlcomcond() |
28 |
| - { |
29 |
| - $text = '/*@if (@_win32)'; |
30 |
| - $this->assertEquals('/*@if(@_win32)', (new JSStrip())->compress($text)); |
31 |
| - } |
32 |
| - |
33 |
| - function test_slcom1() |
34 |
| - { |
35 |
| - $text = '// an comment'; |
36 |
| - $this->assertEquals('', (new JSStrip())->compress($text)); |
37 |
| - } |
38 |
| - |
39 |
| - function test_slcom2() |
40 |
| - { |
41 |
| - $text = 'var foo=6;// another comment '; |
42 |
| - $this->assertEquals('var foo=6;', (new JSStrip())->compress($text)); |
43 |
| - } |
44 |
| - |
45 |
| - function test_slcom3() |
46 |
| - { |
47 |
| - $text = 'var foo=6;// another comment / or something with // comments '; |
48 |
| - $this->assertEquals('var foo=6;', (new JSStrip())->compress($text)); |
49 |
| - } |
50 |
| - |
51 |
| - function test_regex1() |
52 |
| - { |
53 |
| - $text = 'foo.split( /[a-Z\/]*/ );'; |
54 |
| - $this->assertEquals('foo.split(/[a-Z\/]*/);', (new JSStrip())->compress($text)); |
55 |
| - } |
56 |
| - |
57 |
| - function test_regex_in_array() |
58 |
| - { |
59 |
| - $text = '[/"/ , /"/ , /"/]'; |
60 |
| - $this->assertEquals('[/"/,/"/,/"/]', (new JSStrip())->compress($text)); |
61 |
| - } |
62 |
| - |
63 |
| - function test_regex_in_hash() |
64 |
| - { |
65 |
| - $text = '{ a : /"/ }'; |
66 |
| - $this->assertEquals('{a:/"/}', (new JSStrip())->compress($text)); |
67 |
| - } |
68 |
| - |
69 |
| - function test_regex_preceded_by_spaces_caracters() |
70 |
| - { |
71 |
| - $text = "text.replace( \t \r\n /\"/ , " . '"//" )'; |
72 |
| - $this->assertEquals('text.replace(/"/,"//")', (new JSStrip())->compress($text)); |
73 |
| - } |
74 |
| - |
75 |
| - function test_regex_after_and_with_slashes_outside_string() |
76 |
| - { |
77 |
| - $text = 'if ( peng == bla && /pattern\//.test(url)) request = new Something();'; |
78 |
| - $this->assertEquals('if(peng==bla&&/pattern\//.test(url))request=new Something();', |
79 |
| - (new JSStrip())->compress($text)); |
80 |
| - } |
81 |
| - |
82 |
| - function test_regex_after_or_with_slashes_outside_string() |
83 |
| - { |
84 |
| - $text = 'if ( peng == bla || /pattern\//.test(url)) request = new Something();'; |
85 |
| - $this->assertEquals('if(peng==bla||/pattern\//.test(url))request=new Something();', |
86 |
| - (new JSStrip())->compress($text)); |
87 |
| - } |
88 |
| - |
89 |
| - function test_dquot1() |
90 |
| - { |
91 |
| - $text = 'var foo="Now what \\" \'do we//get /*here*/ ?";'; |
92 |
| - $this->assertEquals($text, (new JSStrip())->compress($text)); |
93 |
| - } |
94 |
| - |
95 |
| - function test_dquot2() |
96 |
| - { |
97 |
| - $text = 'var foo="Now what \\\\\\" \'do we//get /*here*/ ?";'; |
98 |
| - $this->assertEquals($text, (new JSStrip())->compress($text)); |
99 |
| - } |
100 |
| - |
101 |
| - function test_dquotrunaway() |
102 |
| - { |
103 |
| - $text = 'var foo="Now where does it end'; |
104 |
| - $this->assertEquals($text, (new JSStrip())->compress($text)); |
105 |
| - } |
106 |
| - |
107 |
| - function test_squot1() |
108 |
| - { |
109 |
| - $text = "var foo='Now what \\' \"do we//get /*here*/ ?';"; |
110 |
| - $this->assertEquals($text, (new JSStrip())->compress($text)); |
111 |
| - } |
112 |
| - |
113 |
| - function test_squotrunaway() |
114 |
| - { |
115 |
| - $text = "var foo='Now where does it end"; |
116 |
| - $this->assertEquals($text, (new JSStrip())->compress($text)); |
117 |
| - } |
118 |
| - |
119 |
| - function test_nl1() |
120 |
| - { |
121 |
| - $text = "var foo=6;\nvar baz=7;"; |
122 |
| - $this->assertEquals('var foo=6;var baz=7;', (new JSStrip())->compress($text)); |
123 |
| - } |
124 |
| - |
125 |
| - function test_lws1() |
126 |
| - { |
127 |
| - $text = " \t var foo=6;"; |
128 |
| - $this->assertEquals('var foo=6;', (new JSStrip())->compress($text)); |
129 |
| - } |
130 |
| - |
131 |
| - function test_tws1() |
132 |
| - { |
133 |
| - $text = "var foo=6; \t "; |
134 |
| - $this->assertEquals('var foo=6;', (new JSStrip())->compress($text)); |
135 |
| - } |
136 |
| - |
137 |
| - function test_shortcond() |
138 |
| - { |
139 |
| - $text = "var foo = (baz) ? 'bar' : 'bla';"; |
140 |
| - $this->assertEquals("var foo=(baz)?'bar':'bla';", (new JSStrip())->compress($text)); |
141 |
| - |
142 |
| - } |
143 |
| - |
144 |
| - function test_complexminified() |
145 |
| - { |
146 |
| - $text = 'if(!k.isXML(a))try{if(e||!l.match.PSEUDO.test(c)&&!/!=/.test(c)){var f=b.call(a,c);if(f||!d||a.document&&a.document.nodeType!==11)return f}}catch(g){}return k(c,null,null,[a]).length>0}}}(),function(){var a=c.createElement("div");a.innerHTML="<div class=\'test e\'></div><div class=\'test\'></div>";if(!!a.getElementsByClassName&&a.getElementsByClassName("e").length!==0){a.lastChild.className="e";if(a.getElementsByClassName("e").length===1)return;foo="text/*";bla="*/"'; |
147 |
| - |
148 |
| - $this->assertEquals($text, (new JSStrip())->compress($text)); |
149 |
| - } |
150 |
| - |
151 |
| - function test_multilinestring() |
152 |
| - { |
153 |
| - $text = 'var foo = "this is a \\' . "\n" . 'multiline string";'; |
154 |
| - $this->assertEquals('var foo="this is a multiline string";', (new JSStrip())->compress($text)); |
155 |
| - |
156 |
| - $text = "var foo = 'this is a \\\nmultiline string';"; |
157 |
| - $this->assertEquals("var foo='this is a multiline string';", (new JSStrip())->compress($text)); |
158 |
| - } |
159 |
| - |
160 |
| - function test_nocompress() |
161 |
| - { |
162 |
| - $text = <<<EOF |
163 |
| -var meh = 'test' ; |
164 |
| -
|
165 |
| -/* BEGIN NOCOMPRESS */ |
166 |
| -
|
167 |
| -
|
168 |
| -var foo = 'test' ; |
169 |
| -
|
170 |
| -var bar = 'test' ; |
171 |
| -
|
172 |
| -
|
173 |
| -/* END NOCOMPRESS */ |
174 |
| -
|
175 |
| -var moh = 'test' ; |
176 |
| -EOF; |
177 |
| - $out = <<<EOF |
178 |
| -var meh='test'; |
179 |
| -var foo = 'test' ; |
180 |
| -
|
181 |
| -var bar = 'test' ; |
182 |
| -var moh='test'; |
183 |
| -EOF; |
184 |
| - |
185 |
| - $this->assertEquals($out, (new JSStrip())->compress($text)); |
186 |
| - } |
187 |
| - |
188 |
| - function test_plusplus1() |
189 |
| - { |
190 |
| - $text = 'a = 5 + ++b;'; |
191 |
| - $this->assertEquals('a=5+ ++b;', (new JSStrip())->compress($text)); |
192 |
| - } |
193 |
| - |
194 |
| - function test_plusplus2() |
195 |
| - { |
196 |
| - $text = 'a = 5+ ++b;'; |
197 |
| - $this->assertEquals('a=5+ ++b;', (new JSStrip())->compress($text)); |
198 |
| - } |
199 |
| - |
200 |
| - function test_plusplus3() |
201 |
| - { |
202 |
| - $text = 'a = 5++ + b;'; |
203 |
| - $this->assertEquals('a=5++ +b;', (new JSStrip())->compress($text)); |
204 |
| - } |
205 |
| - |
206 |
| - function test_plusplus4() |
207 |
| - { |
208 |
| - $text = 'a = 5++ +b;'; |
209 |
| - $this->assertEquals('a=5++ +b;', (new JSStrip())->compress($text)); |
210 |
| - } |
211 |
| - |
212 |
| - function test_minusminus1() |
213 |
| - { |
214 |
| - $text = 'a = 5 - --b;'; |
215 |
| - $this->assertEquals('a=5- --b;', (new JSStrip())->compress($text)); |
| 10 | + /** |
| 11 | + * Basic test cases |
| 12 | + * |
| 13 | + * @return array[] [input, expected] |
| 14 | + * @see testBasics |
| 15 | + */ |
| 16 | + public function provideBasics() |
| 17 | + { |
| 18 | + return [ |
| 19 | + ['var foo=6;/* another comment */', 'var foo=6;'], |
| 20 | + ['/*@if (@_win32)', '/*@if(@_win32)'], // conditional comment |
| 21 | + ['// an comment', ''], |
| 22 | + ['var foo=6;// another comment ', 'var foo=6;'], |
| 23 | + ['var foo=6;// another comment / or something with // comments ', 'var foo=6;'], |
| 24 | + ['foo.split( /[a-Z\/]*/ );', 'foo.split(/[a-Z\/]*/);'], |
| 25 | + ['[/"/ , /"/ , /"/]', '[/"/,/"/,/"/]'], |
| 26 | + ['{ a : /"/ }', '{a:/"/}'], |
| 27 | + ['a = 5 + ++b;', 'a=5+ ++b;'], |
| 28 | + ['a = 5+ ++b;', 'a=5+ ++b;',], |
| 29 | + ['a = 5++ + b;', 'a=5++ +b;'], |
| 30 | + ['a = 5++ +b;', 'a=5++ +b;'], |
| 31 | + ['a = 5 - --b;', 'a=5- --b;'], |
| 32 | + ['a = 5- --b;', 'a=5- --b;'], |
| 33 | + ['a = 5-- - b;', 'a=5-- -b;'], |
| 34 | + ['a = 5-- -b;', 'a=5-- -b;'], |
| 35 | + ['a = 5-- +b;', 'a=5--+b;'], |
| 36 | + ['a = 5-- + b;', 'a=5--+b;'], |
| 37 | + ['a = 5++ - b;', 'a=5++-b;'], |
| 38 | + ['a = 5++ -b;', 'a=5++-b;'], |
| 39 | + ["var foo=6;\nvar baz=7;", 'var foo=6;var baz=7;'], |
| 40 | + [" \t var foo=6;", 'var foo=6;'], |
| 41 | + ["var foo=6; \t ", 'var foo=6;'], |
| 42 | + ["var foo = (baz) ? 'bar' : 'bla';", "var foo=(baz)?'bar':'bla';"], |
| 43 | + [ |
| 44 | + "text.replace( \t \r\n /\"/ , " . '"//" )', |
| 45 | + 'text.replace(/"/,"//")' |
| 46 | + ], |
| 47 | + [ |
| 48 | + 'if ( peng == bla && /pattern\//.test(url)) request = new Something();', |
| 49 | + 'if(peng==bla&&/pattern\//.test(url))request=new Something();' |
| 50 | + ], |
| 51 | + [ |
| 52 | + 'if ( peng == bla || /pattern\//.test(url)) request = new Something();', |
| 53 | + 'if(peng==bla||/pattern\//.test(url))request=new Something();' |
| 54 | + ], |
| 55 | + [ |
| 56 | + 'var foo = "this is a \\' . "\n" . 'multiline string";', |
| 57 | + 'var foo="this is a multiline string";' |
| 58 | + ], |
| 59 | + [ |
| 60 | + "var foo = 'this is a \\\nmultiline string';", |
| 61 | + "var foo='this is a multiline string';" |
| 62 | + ], |
| 63 | + [ |
| 64 | + 'var π = Math.PI, τ = 2 * π, halfπ = π / 2, ε = 1e-6, ε2 = ε * ε, radians = π / 180, degrees = 180 / π;', |
| 65 | + 'var π=Math.PI,τ=2*π,halfπ=π/2,ε=1e-6,ε2=ε*ε,radians=π/180,degrees=180/π;' |
| 66 | + ] |
| 67 | + ]; |
216 | 68 | }
|
217 | 69 |
|
218 |
| - function test_minusminus2() |
| 70 | + /** |
| 71 | + * @dataProvider provideBasics |
| 72 | + * @param string $input |
| 73 | + * @param string $expected |
| 74 | + */ |
| 75 | + public function testBasics($input, $expected) |
219 | 76 | {
|
220 |
| - $text = 'a = 5- --b;'; |
221 |
| - $this->assertEquals('a=5- --b;', (new JSStrip())->compress($text)); |
| 77 | + $this->assertEquals($expected, (new JSStrip())->compress($input)); |
222 | 78 | }
|
223 | 79 |
|
224 |
| - function test_minusminus3() |
| 80 | + /** |
| 81 | + * Test cases that should not be changed by the compressor |
| 82 | + * |
| 83 | + * @return array[] [input=output] |
| 84 | + * @see testUntouchables |
| 85 | + */ |
| 86 | + public function provideUntouchables() |
225 | 87 | {
|
226 |
| - $text = 'a = 5-- - b;'; |
227 |
| - $this->assertEquals('a=5-- -b;', (new JSStrip())->compress($text)); |
228 |
| - } |
| 88 | + return [ |
| 89 | + ['var foo="Now where does it end'], |
| 90 | + ["var foo='Now where does it end"], |
229 | 91 |
|
230 |
| - function test_minusminus4() |
231 |
| - { |
232 |
| - $text = 'a = 5-- -b;'; |
233 |
| - $this->assertEquals('a=5-- -b;', (new JSStrip())->compress($text)); |
| 92 | + ["var foo='Now what \\' \"do we//get /*here*/ ?';"], |
| 93 | + ['var foo="Now what \\" \'do we//get /*here*/ ?";'], |
| 94 | + ['var foo="Now what \\\\\\" \'do we//get /*here*/ ?";'], |
| 95 | + ]; |
234 | 96 | }
|
235 | 97 |
|
236 |
| - function test_minusplus1() |
| 98 | + /** |
| 99 | + * @dataProvider provideUntouchables |
| 100 | + * @param string $input should be equal to the expected output |
| 101 | + */ |
| 102 | + public function testUntouchables($input) |
237 | 103 | {
|
238 |
| - $text = 'a = 5-- +b;'; |
239 |
| - $this->assertEquals('a=5--+b;', (new JSStrip())->compress($text)); |
| 104 | + $this->assertEquals($input, (new JSStrip())->compress($input)); |
240 | 105 | }
|
241 | 106 |
|
242 |
| - function test_minusplus2() |
| 107 | + /** |
| 108 | + * Test cases provided as data files |
| 109 | + * |
| 110 | + * @return \Generator|string[][] [input, expected, file] |
| 111 | + * @see testFileData |
| 112 | + */ |
| 113 | + public function provideFileData() |
243 | 114 | {
|
244 |
| - $text = 'a = 5-- + b;'; |
245 |
| - $this->assertEquals('a=5--+b;', (new JSStrip())->compress($text)); |
246 |
| - } |
| 115 | + $files = glob(__DIR__ . '/data/test-*-in.js'); |
247 | 116 |
|
248 |
| - function test_plusminus1() |
249 |
| - { |
250 |
| - $text = 'a = 5++ - b;'; |
251 |
| - $this->assertEquals('a=5++-b;', (new JSStrip())->compress($text)); |
252 |
| - } |
| 117 | + foreach ($files as $file) { |
| 118 | + $input = file_get_contents($file); |
| 119 | + $output = file_get_contents(substr($file, 0, -5) . 'out.js'); |
253 | 120 |
|
254 |
| - function test_plusminus2() |
255 |
| - { |
256 |
| - $text = 'a = 5++ -b;'; |
257 |
| - $this->assertEquals('a=5++-b;', (new JSStrip())->compress($text)); |
258 |
| - } |
| 121 | + // ignore last newline |
| 122 | + if (substr($input, -1) == "\n") $input = substr($input, 0, -1); |
| 123 | + if (substr($output, -1) == "\n") $output = substr($output, 0, -1); |
259 | 124 |
|
260 |
| - function test_unusual_signs() |
261 |
| - { |
262 |
| - $text = 'var π = Math.PI, τ = 2 * π, halfπ = π / 2, ε = 1e-6, ε2 = ε * ε, radians = π / 180, degrees = 180 / π;'; |
263 |
| - $this->assertEquals('var π=Math.PI,τ=2*π,halfπ=π/2,ε=1e-6,ε2=ε*ε,radians=π/180,degrees=180/π;', (new JSStrip())->compress($text)); |
| 125 | + yield [$input, $output, $file]; |
| 126 | + } |
264 | 127 | }
|
265 | 128 |
|
266 | 129 | /**
|
267 |
| - * Test the files provided with the original JsStrip |
| 130 | + * @dataProvider provideFileData |
| 131 | + * @param string $input |
| 132 | + * @param string $output |
| 133 | + * @param string $file |
268 | 134 | */
|
269 |
| - function test_original() |
| 135 | + function testFileData($input, $output, $file) |
270 | 136 | {
|
271 |
| - $files = glob(__DIR__ . '/data/test-*-in.js'); |
272 |
| - |
273 |
| - foreach ($files as $file) { |
274 |
| - $info = "Using file $file"; |
275 |
| - $this->assertEquals(file_get_contents(substr($file, 0, -5) . 'out.js'), |
276 |
| - (new JSStrip())->compress(file_get_contents($file)), $info); |
277 |
| - }; |
| 137 | + $this->assertEquals($output, (new JSStrip())->compress($input), $file); |
278 | 138 | }
|
279 | 139 | }
|
0 commit comments