corrade-nucleus-nucleons – Blame information for rev 24
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
20 | office | 1 | /* |
2 | {{&header_text}} |
||
3 | |||
4 | The MIT License (MIT) |
||
5 | |||
6 | Copyright (c) 2007-2017 Einar Lielmanis, Liam Newman, and contributors. |
||
7 | |||
8 | Permission is hereby granted, free of charge, to any person |
||
9 | obtaining a copy of this software and associated documentation files |
||
10 | (the "Software"), to deal in the Software without restriction, |
||
11 | including without limitation the rights to use, copy, modify, merge, |
||
12 | publish, distribute, sublicense, and/or sell copies of the Software, |
||
13 | and to permit persons to whom the Software is furnished to do so, |
||
14 | subject to the following conditions: |
||
15 | |||
16 | The above copyright notice and this permission notice shall be |
||
17 | included in all copies or substantial portions of the Software. |
||
18 | |||
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||
20 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||
21 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
||
22 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
||
23 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
||
24 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
||
25 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||
26 | SOFTWARE. |
||
27 | */ |
||
28 | /*jshint unused:false */ |
||
29 | |||
30 | function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_beautify) |
||
31 | { |
||
32 | |||
33 | var default_opts = { |
||
34 | indent_size: 4, |
||
35 | indent_char: ' ', |
||
36 | preserve_newlines: true, |
||
37 | jslint_happy: false, |
||
38 | keep_array_indentation: false, |
||
39 | brace_style: 'collapse', |
||
40 | space_before_conditional: true, |
||
41 | break_chained_methods: false, |
||
42 | selector_separator: '\n', |
||
43 | end_with_newline: false |
||
44 | }; |
||
45 | var opts; |
||
46 | |||
47 | {{#default_options}} default_opts.{{name}} = {{&value}}; |
||
48 | {{/default_options}} |
||
49 | |||
50 | function reset_options() |
||
51 | { |
||
52 | opts = JSON.parse(JSON.stringify(default_opts)); |
||
53 | } |
||
54 | |||
55 | function test_js_beautifier(input) |
||
56 | { |
||
57 | return js_beautify(input, opts); |
||
58 | } |
||
59 | |||
60 | var sanitytest; |
||
61 | |||
62 | // test the input on beautifier with the current flag settings |
||
63 | // does not check the indentation / surroundings as bt() does |
||
64 | function test_fragment(input, expected) |
||
65 | { |
||
66 | expected = expected || expected === '' ? expected : input; |
||
67 | sanitytest.expect(input, expected); |
||
68 | // if the expected is different from input, run it again |
||
69 | // expected output should be unchanged when run twice. |
||
70 | if (expected !== input) { |
||
71 | sanitytest.expect(expected, expected); |
||
72 | } |
||
73 | |||
74 | // Everywhere we do newlines, they should be replaced with opts.eol |
||
75 | opts.eol = '\r\\n'; |
||
76 | expected = expected.replace(/[\n]/g, '\r\n'); |
||
77 | sanitytest.expect(input, expected); |
||
78 | if (input.indexOf('\n') !== -1) { |
||
79 | input = input.replace(/[\n]/g, '\r\n'); |
||
80 | sanitytest.expect(input, expected); |
||
81 | // Ensure support for auto eol detection |
||
82 | opts.eol = 'auto'; |
||
83 | sanitytest.expect(input, expected); |
||
84 | } |
||
85 | opts.eol = '\n'; |
||
86 | } |
||
87 | |||
88 | |||
89 | |||
90 | // test the input on beautifier with the current flag settings |
||
91 | // test both the input as well as { input } wrapping |
||
92 | function bt(input, expectation) |
||
93 | { |
||
94 | var wrapped_input, wrapped_expectation; |
||
95 | |||
96 | expectation = expectation || expectation === '' ? expectation : input; |
||
97 | sanitytest.test_function(test_js_beautifier, 'js_beautify'); |
||
98 | test_fragment(input, expectation); |
||
99 | |||
100 | // If we set raw, input should be unchanged |
||
101 | opts.test_output_raw = true; |
||
102 | if (!opts.end_with_newline) { |
||
103 | test_fragment(input, input); |
||
104 | } |
||
105 | opts.test_output_raw = false; |
||
106 | |||
107 | // test also the returned indentation |
||
108 | // e.g if input = "asdf();" |
||
109 | // then test that this remains properly formatted as well: |
||
110 | // { |
||
111 | // asdf(); |
||
112 | // indent; |
||
113 | // } |
||
114 | |||
115 | var current_indent_size = opts.js ? opts.js.indent_size : null; |
||
116 | current_indent_size = current_indent_size ? current_indent_size : opts.indent_size; |
||
117 | if (current_indent_size === 4 && input) { |
||
118 | wrapped_input = '{\n' + input.replace(/^(.+)$/mg, ' $1') + '\n foo = bar;\n}'; |
||
119 | wrapped_expectation = '{\n' + expectation.replace(/^(.+)$/mg, ' $1') + '\n foo = bar;\n}'; |
||
120 | test_fragment(wrapped_input, wrapped_expectation); |
||
121 | |||
122 | // If we set raw, input should be unchanged |
||
123 | opts.test_output_raw = true; |
||
124 | if (!opts.end_with_newline) { |
||
125 | test_fragment(wrapped_input, wrapped_input); |
||
126 | } |
||
127 | opts.test_output_raw = false; |
||
128 | } |
||
129 | |||
130 | } |
||
131 | |||
132 | // run all tests for the given brace style ("collapse", "expand", "end-expand", or "none"). |
||
133 | // uses various whitespace combinations before and after opening and closing braces, |
||
134 | // respectively, for most of the tests' inputs. |
||
135 | function beautify_brace_tests(brace_style) { |
||
136 | |||
137 | var indent_on_wrap_str = ' '; // could use Array(opts.indent_size + 1).join(' '); if we wanted to replace _all_ of the hardcoded 4-space in the test and expectation strings |
||
138 | |||
139 | function permute_brace_tests(expect_open_white, expect_close_white) { |
||
140 | |||
141 | // run the tests that need permutation against a specific combination of |
||
142 | // pre-opening-brace and pre-closing-brace whitespace |
||
143 | function run_brace_permutation(test_open_white, test_close_white) { |
||
144 | var to = test_open_white, |
||
145 | tc = test_close_white, |
||
146 | eo = expect_open_white ? expect_open_white : to === '' ? ' ' : to, |
||
147 | ec = expect_close_white ? expect_close_white : tc === '' ? ' ' : tc, |
||
148 | i = eo === '\n' ? indent_on_wrap_str: ''; |
||
149 | |||
150 | bt( '//case 1\nif (a == 1)' + to + '{}\n//case 2\nelse if (a == 2)' + to + '{}', |
||
151 | '//case 1\nif (a == 1)' + eo + '{}\n//case 2\nelse if (a == 2)' + eo + '{}'); |
||
152 | bt( 'if(1)' + to + '{2}' + tc + 'else' + to + '{3}', |
||
153 | 'if (1)' + eo + '{\n 2\n}' + ec + 'else' + eo + '{\n 3\n}'); |
||
154 | bt( 'try' + to + '{a();}' + tc + |
||
155 | 'catch(b)' + to + '{c();}' + tc + |
||
156 | 'catch(d)' + to + '{}' + tc + |
||
157 | 'finally' + to + '{e();}', |
||
158 | // expected |
||
159 | 'try' + eo + '{\n a();\n}' + ec + |
||
160 | 'catch (b)' + eo + '{\n c();\n}' + ec + |
||
161 | 'catch (d)' + eo + '{}' + ec + |
||
162 | 'finally' + eo + '{\n e();\n}'); |
||
163 | bt( 'if(a)' + to + '{b();}' + tc + 'else if(c) foo();', |
||
164 | 'if (a)' + eo + '{\n b();\n}' + ec + 'else if (c) foo();'); |
||
165 | // if/else statement with empty body |
||
166 | bt( 'if (a)' + to + '{\n// comment\n}' + tc + 'else' + to + '{\n// comment\n}', |
||
167 | 'if (a)' + eo + '{\n // comment\n}' + ec + 'else' + eo + '{\n // comment\n}'); |
||
168 | bt( 'if (x)' + to + '{y}' + tc + 'else' + to + '{ if (x)' + to + '{y}}', |
||
169 | 'if (x)' + eo + '{\n y\n}' + ec + 'else' + eo + '{\n if (x)' + eo + i + '{\n y\n }\n}'); |
||
170 | bt( 'if (a)' + to + '{\nb;\n}' + tc + 'else' + to + '{\nc;\n}', |
||
171 | 'if (a)' + eo + '{\n b;\n}' + ec + 'else' + eo + '{\n c;\n}'); |
||
172 | test_fragment(' /*\n* xx\n*/\n// xx\nif (foo)' + to + '{\n bar();\n}', |
||
173 | ' /*\n * xx\n */\n // xx\n if (foo)' + eo + i + '{\n bar();\n }'); |
||
174 | bt( 'if (foo)' + to + '{}' + tc + 'else /regex/.test();', |
||
175 | 'if (foo)' + eo + '{}' + ec + 'else /regex/.test();'); |
||
176 | test_fragment('if (foo)' + to + '{', 'if (foo)' + eo + '{'); |
||
177 | test_fragment('foo' + to + '{', 'foo' + eo + '{'); |
||
178 | test_fragment('return;' + to + '{', 'return;' + eo + '{'); |
||
179 | bt( 'function x()' + to + '{\n foo();\n}zzz', 'function x()' + eo +'{\n foo();\n}\nzzz'); |
||
180 | bt( 'var a = new function a()' + to + '{};', 'var a = new function a()' + eo + '{};'); |
||
181 | bt( 'var a = new function a()' + to + ' {},\n b = new function b()' + to + ' {};', |
||
182 | 'var a = new function a()' + eo + i + '{},\n b = new function b()' + eo + i + '{};'); |
||
183 | bt("foo(" + to + "{\n 'a': 1\n},\n10);", |
||
184 | "foo(" + (eo === ' ' ? '' : eo) + i + "{\n 'a': 1\n },\n 10);"); // "foo( {..." is a weird case |
||
185 | bt('(["foo","bar"]).each(function(i)' + to + '{return i;});', |
||
186 | '(["foo", "bar"]).each(function(i)' + eo + '{\n return i;\n});'); |
||
187 | bt('(function(i)' + to + '{return i;})();', '(function(i)' + eo + '{\n return i;\n})();'); |
||
188 | |||
189 | bt( "test( /*Argument 1*/" + to + "{\n" + |
||
190 | " 'Value1': '1'\n" + |
||
191 | "}, /*Argument 2\n" + |
||
192 | " */ {\n" + |
||
193 | " 'Value2': '2'\n" + |
||
194 | "});", |
||
195 | // expected |
||
196 | "test( /*Argument 1*/" + eo + i + "{\n" + |
||
197 | " 'Value1': '1'\n" + |
||
198 | " },\n" + |
||
199 | " /*Argument 2\n" + |
||
200 | " */\n" + |
||
201 | " {\n" + |
||
202 | " 'Value2': '2'\n" + |
||
203 | " });"); |
||
204 | |||
205 | bt( "test( /*Argument 1*/" + to + "{\n" + |
||
206 | " 'Value1': '1'\n" + |
||
207 | "}, /*Argument 2\n" + |
||
208 | " */\n" + |
||
209 | "{\n" + |
||
210 | " 'Value2': '2'\n" + |
||
211 | "});", |
||
212 | // expected |
||
213 | "test( /*Argument 1*/" + eo + i + "{\n" + |
||
214 | " 'Value1': '1'\n" + |
||
215 | " },\n" + |
||
216 | " /*Argument 2\n" + |
||
217 | " */\n" + |
||
218 | " {\n" + |
||
219 | " 'Value2': '2'\n" + |
||
220 | " });"); |
||
221 | } |
||
222 | |||
223 | run_brace_permutation('\n', '\n'); |
||
224 | run_brace_permutation('\n', ' '); |
||
225 | run_brace_permutation(' ', ' '); |
||
226 | run_brace_permutation(' ', '\n'); |
||
227 | run_brace_permutation('',''); |
||
228 | |||
229 | // brace tests that don't make sense to permutate |
||
230 | test_fragment('return {'); // return needs the brace. |
||
231 | test_fragment('return /* inline */ {'); |
||
232 | bt('throw {}'); |
||
233 | bt('throw {\n foo;\n}'); |
||
234 | bt( 'var foo = {}'); |
||
235 | test_fragment('a: do {} while (); xxx', 'a: do {} while ();\nxxx'); |
||
236 | bt( '{a: do {} while (); xxx}', '{\n a: do {} while ();xxx\n}'); |
||
237 | bt( 'var a = new function() {};'); |
||
238 | bt( 'var a = new function()\n{};', 'var a = new function() {};'); |
||
239 | bt( "test(\n" + |
||
240 | "/*Argument 1*/ {\n" + |
||
241 | " 'Value1': '1'\n" + |
||
242 | "},\n" + |
||
243 | "/*Argument 2\n" + |
||
244 | " */ {\n" + |
||
245 | " 'Value2': '2'\n" + |
||
246 | "});", |
||
247 | // expected |
||
248 | "test(\n" + |
||
249 | " /*Argument 1*/\n" + |
||
250 | " {\n" + |
||
251 | " 'Value1': '1'\n" + |
||
252 | " },\n" + |
||
253 | " /*Argument 2\n" + |
||
254 | " */\n" + |
||
255 | " {\n" + |
||
256 | " 'Value2': '2'\n" + |
||
257 | " });"); |
||
258 | } |
||
259 | |||
260 | reset_options(); |
||
261 | opts.brace_style = brace_style; |
||
262 | |||
263 | switch(opts.brace_style) { |
||
264 | case 'collapse': |
||
265 | permute_brace_tests(' ', ' '); |
||
266 | break; |
||
267 | case 'expand': |
||
268 | permute_brace_tests('\n', '\n'); |
||
269 | break; |
||
270 | case 'end-expand': |
||
271 | permute_brace_tests(' ', '\n'); |
||
272 | break; |
||
273 | case 'none': |
||
274 | permute_brace_tests(); |
||
275 | break; |
||
276 | } |
||
277 | } |
||
278 | |||
279 | function unicode_char(value) { |
||
280 | return String.fromCharCode(value); |
||
281 | } |
||
282 | |||
283 | function beautifier_tests() |
||
284 | { |
||
285 | sanitytest = test_obj; |
||
286 | |||
287 | {{#groups}}{{#set_mustache_tags}}.{{/set_mustache_tags}} |
||
288 | //============================================================ |
||
289 | {{^matrix}} |
||
290 | // {{&name}} |
||
291 | reset_options(); |
||
292 | {{#options}} |
||
293 | opts.{{name}} = {{&value}}; |
||
294 | {{/options}} |
||
295 | {{#tests}} |
||
296 | {{#test_line}}.{{/test_line}}; |
||
297 | {{/tests}} |
||
298 | |||
299 | {{/matrix}} |
||
300 | {{#matrix}} |
||
301 | // {{&name}} - ({{#matrix_context_string}}.{{/matrix_context_string}}) |
||
302 | reset_options(); |
||
303 | {{#options}} |
||
304 | opts.{{name}} = {{&value}}; |
||
305 | {{/options}} |
||
306 | {{#tests}} |
||
307 | {{#test_line}}.{{/test_line}}; |
||
308 | {{/tests}} |
||
309 | |||
310 | {{/matrix}} |
||
311 | {{#unset_mustache_tags}}.{{/unset_mustache_tags}}{{/groups}} |
||
312 | } |
||
313 | |||
314 | function beautifier_unconverted_tests() |
||
315 | { |
||
316 | sanitytest = test_obj; |
||
317 | |||
318 | reset_options(); |
||
319 | //============================================================ |
||
320 | opts.indent_size = 1; |
||
321 | opts.indent_char = ' '; |
||
322 | bt('{ one_char() }', "{\n one_char()\n}"); |
||
323 | |||
324 | bt('var a,b=1,c=2', 'var a, b = 1,\n c = 2'); |
||
325 | |||
326 | opts.indent_size = 4; |
||
327 | opts.indent_char = ' '; |
||
328 | bt('{ one_char() }', "{\n one_char()\n}"); |
||
329 | |||
330 | opts.indent_size = 1; |
||
331 | opts.indent_char = "\t"; |
||
332 | bt('{ one_char() }', "{\n\tone_char()\n}"); |
||
333 | bt('x = a ? b : c; x;', 'x = a ? b : c;\nx;'); |
||
334 | |||
335 | //set to something else than it should change to, but with tabs on, should override |
||
336 | opts.indent_size = 5; |
||
337 | opts.indent_char = ' '; |
||
338 | opts.indent_with_tabs = true; |
||
339 | |||
340 | bt('{ one_char() }', "{\n\tone_char()\n}"); |
||
341 | bt('x = a ? b : c; x;', 'x = a ? b : c;\nx;'); |
||
342 | |||
343 | opts.indent_size = 4; |
||
344 | opts.indent_char = ' '; |
||
345 | opts.indent_with_tabs = false; |
||
346 | |||
347 | reset_options(); |
||
348 | //============================================================ |
||
349 | opts.preserve_newlines = false; |
||
350 | |||
351 | bt('var\na=dont_preserve_newlines;', 'var a = dont_preserve_newlines;'); |
||
352 | |||
353 | // make sure the blank line between function definitions stays |
||
354 | // even when preserve_newlines = false |
||
355 | bt('function foo() {\n return 1;\n}\n\nfunction foo() {\n return 1;\n}'); |
||
356 | bt('function foo() {\n return 1;\n}\nfunction foo() {\n return 1;\n}', |
||
357 | 'function foo() {\n return 1;\n}\n\nfunction foo() {\n return 1;\n}' |
||
358 | ); |
||
359 | bt('function foo() {\n return 1;\n}\n\n\nfunction foo() {\n return 1;\n}', |
||
360 | 'function foo() {\n return 1;\n}\n\nfunction foo() {\n return 1;\n}' |
||
361 | ); |
||
362 | |||
363 | opts.preserve_newlines = true; |
||
364 | bt('var\na=do_preserve_newlines;', 'var\n a = do_preserve_newlines;'); |
||
365 | bt('if (foo) // comment\n{\n bar();\n}'); |
||
366 | |||
367 | |||
368 | reset_options(); |
||
369 | //============================================================ |
||
370 | opts.keep_array_indentation = false; |
||
371 | bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f']", |
||
372 | "a = ['a', 'b', 'c',\n 'd', 'e', 'f'\n]"); |
||
373 | bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i']", |
||
374 | "a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i'\n]"); |
||
375 | bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i']", |
||
376 | "a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i'\n]"); |
||
377 | bt('var x = [{}\n]', 'var x = [{}]'); |
||
378 | bt('var x = [{foo:bar}\n]', 'var x = [{\n foo: bar\n}]'); |
||
379 | bt("a = ['something',\n 'completely',\n 'different'];\nif (x);", |
||
380 | "a = ['something',\n 'completely',\n 'different'\n];\nif (x);"); |
||
381 | bt("a = ['a','b','c']", "a = ['a', 'b', 'c']"); |
||
382 | |||
383 | bt("a = ['a', 'b','c']", "a = ['a', 'b', 'c']"); |
||
384 | bt("x = [{'a':0}]", |
||
385 | "x = [{\n 'a': 0\n}]"); |
||
386 | bt('{a([[a1]], {b;});}', |
||
387 | '{\n a([\n [a1]\n ], {\n b;\n });\n}'); |
||
388 | bt("a();\n [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();", |
||
389 | "a();\n[\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n].toString();"); |
||
390 | bt("a();\na = [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();", |
||
391 | "a();\na = [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n].toString();"); |
||
392 | bt("function() {\n Foo([\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ]);\n}", |
||
393 | "function() {\n Foo([\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ]);\n}"); |
||
394 | bt('function foo() {\n return [\n "one",\n "two"\n ];\n}'); |
||
395 | // 4 spaces per indent input, processed with 4-spaces per indent |
||
396 | bt( "function foo() {\n" + |
||
397 | " return [\n" + |
||
398 | " {\n" + |
||
399 | " one: 'x',\n" + |
||
400 | " two: [\n" + |
||
401 | " {\n" + |
||
402 | " id: 'a',\n" + |
||
403 | " name: 'apple'\n" + |
||
404 | " }, {\n" + |
||
405 | " id: 'b',\n" + |
||
406 | " name: 'banana'\n" + |
||
407 | " }\n" + |
||
408 | " ]\n" + |
||
409 | " }\n" + |
||
410 | " ];\n" + |
||
411 | "}", |
||
412 | "function foo() {\n" + |
||
413 | " return [{\n" + |
||
414 | " one: 'x',\n" + |
||
415 | " two: [{\n" + |
||
416 | " id: 'a',\n" + |
||
417 | " name: 'apple'\n" + |
||
418 | " }, {\n" + |
||
419 | " id: 'b',\n" + |
||
420 | " name: 'banana'\n" + |
||
421 | " }]\n" + |
||
422 | " }];\n" + |
||
423 | "}"); |
||
424 | // 3 spaces per indent input, processed with 4-spaces per indent |
||
425 | bt( "function foo() {\n" + |
||
426 | " return [\n" + |
||
427 | " {\n" + |
||
428 | " one: 'x',\n" + |
||
429 | " two: [\n" + |
||
430 | " {\n" + |
||
431 | " id: 'a',\n" + |
||
432 | " name: 'apple'\n" + |
||
433 | " }, {\n" + |
||
434 | " id: 'b',\n" + |
||
435 | " name: 'banana'\n" + |
||
436 | " }\n" + |
||
437 | " ]\n" + |
||
438 | " }\n" + |
||
439 | " ];\n" + |
||
440 | "}", |
||
441 | "function foo() {\n" + |
||
442 | " return [{\n" + |
||
443 | " one: 'x',\n" + |
||
444 | " two: [{\n" + |
||
445 | " id: 'a',\n" + |
||
446 | " name: 'apple'\n" + |
||
447 | " }, {\n" + |
||
448 | " id: 'b',\n" + |
||
449 | " name: 'banana'\n" + |
||
450 | " }]\n" + |
||
451 | " }];\n" + |
||
452 | "}"); |
||
453 | |||
454 | opts.keep_array_indentation = true; |
||
455 | bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f']"); |
||
456 | bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i']"); |
||
457 | bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i']"); |
||
458 | bt('var x = [{}\n]', 'var x = [{}\n]'); |
||
459 | bt('var x = [{foo:bar}\n]', 'var x = [{\n foo: bar\n }\n]'); |
||
460 | bt("a = ['something',\n 'completely',\n 'different'];\nif (x);"); |
||
461 | bt("a = ['a','b','c']", "a = ['a', 'b', 'c']"); |
||
462 | bt("a = ['a', 'b','c']", "a = ['a', 'b', 'c']"); |
||
463 | bt("x = [{'a':0}]", |
||
464 | "x = [{\n 'a': 0\n}]"); |
||
465 | bt('{a([[a1]], {b;});}', |
||
466 | '{\n a([[a1]], {\n b;\n });\n}'); |
||
467 | bt("a();\n [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();", |
||
468 | "a();\n [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();"); |
||
469 | bt("a();\na = [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();", |
||
470 | "a();\na = [\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ].toString();"); |
||
471 | bt("function() {\n Foo([\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ]);\n}", |
||
472 | "function() {\n Foo([\n ['sdfsdfsd'],\n ['sdfsdfsdf']\n ]);\n}"); |
||
473 | bt('function foo() {\n return [\n "one",\n "two"\n ];\n}'); |
||
474 | // 4 spaces per indent input, processed with 4-spaces per indent |
||
475 | bt( "function foo() {\n" + |
||
476 | " return [\n" + |
||
477 | " {\n" + |
||
478 | " one: 'x',\n" + |
||
479 | " two: [\n" + |
||
480 | " {\n" + |
||
481 | " id: 'a',\n" + |
||
482 | " name: 'apple'\n" + |
||
483 | " }, {\n" + |
||
484 | " id: 'b',\n" + |
||
485 | " name: 'banana'\n" + |
||
486 | " }\n" + |
||
487 | " ]\n" + |
||
488 | " }\n" + |
||
489 | " ];\n" + |
||
490 | "}"); |
||
491 | // 3 spaces per indent input, processed with 4-spaces per indent |
||
492 | // Should be unchanged, but is not - #445 |
||
493 | // bt( "function foo() {\n" + |
||
494 | // " return [\n" + |
||
495 | // " {\n" + |
||
496 | // " one: 'x',\n" + |
||
497 | // " two: [\n" + |
||
498 | // " {\n" + |
||
499 | // " id: 'a',\n" + |
||
500 | // " name: 'apple'\n" + |
||
501 | // " }, {\n" + |
||
502 | // " id: 'b',\n" + |
||
503 | // " name: 'banana'\n" + |
||
504 | // " }\n" + |
||
505 | // " ]\n" + |
||
506 | // " }\n" + |
||
507 | // " ];\n" + |
||
508 | // "}"); |
||
509 | |||
510 | |||
511 | reset_options(); |
||
512 | //============================================================ |
||
513 | bt('a = //comment\n /regex/;'); |
||
514 | |||
515 | bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a) {\n b;\n} else {\n c;\n}'); |
||
516 | |||
517 | // tests for brace positioning |
||
518 | beautify_brace_tests('expand'); |
||
519 | beautify_brace_tests('collapse'); |
||
520 | beautify_brace_tests('end-expand'); |
||
521 | beautify_brace_tests('none'); |
||
522 | |||
523 | test_fragment('roo = {\n /*\n ****\n FOO\n ****\n */\n BAR: 0\n};'); |
||
524 | |||
525 | bt('"foo""bar""baz"', '"foo"\n"bar"\n"baz"'); |
||
526 | bt("'foo''bar''baz'", "'foo'\n'bar'\n'baz'"); |
||
527 | |||
528 | |||
529 | test_fragment("if (zz) {\n // ....\n}\n(function"); |
||
530 | |||
531 | bt("{\n get foo() {}\n}"); |
||
532 | bt("{\n var a = get\n foo();\n}"); |
||
533 | bt("{\n set foo() {}\n}"); |
||
534 | bt("{\n var a = set\n foo();\n}"); |
||
535 | bt("var x = {\n get function()\n}"); |
||
536 | bt("var x = {\n set function()\n}"); |
||
537 | |||
538 | // According to my current research get/set have no special meaning outside of an object literal |
||
539 | bt("var x = set\n\na() {}", "var x = set\n\na() {}"); |
||
540 | bt("var x = set\n\nfunction() {}", "var x = set\n\nfunction() {}"); |
||
541 | |||
542 | bt('<!-- foo\nbar();\n-->'); |
||
543 | bt('<!-- dont crash'); // --> |
||
544 | bt('for () /abc/.test()'); |
||
545 | bt('if (k) /aaa/m.test(v) && l();'); |
||
546 | bt('switch (true) {\n case /swf/i.test(foo):\n bar();\n}'); |
||
547 | bt('createdAt = {\n type: Date,\n default: Date.now\n}'); |
||
548 | bt('switch (createdAt) {\n case a:\n Date,\n default:\n Date.now\n}'); |
||
549 | |||
550 | reset_options(); |
||
551 | //============================================================ |
||
552 | opts.space_before_conditional = false; |
||
553 | bt('if(a) b()'); |
||
554 | |||
555 | |||
556 | reset_options(); |
||
557 | //============================================================ |
||
558 | opts.preserve_newlines = true; |
||
559 | bt('var a = 42; // foo\n\nvar b;'); |
||
560 | bt('var a = 42; // foo\n\n\nvar b;'); |
||
561 | bt("var a = 'foo' +\n 'bar';"); |
||
562 | bt("var a = \"foo\" +\n \"bar\";"); |
||
563 | bt('this.oa = new OAuth(\n' + |
||
564 | ' _requestToken,\n' + |
||
565 | ' _accessToken,\n' + |
||
566 | ' consumer_key\n' + |
||
567 | ');'); |
||
568 | |||
569 | |||
570 | reset_options(); |
||
571 | //============================================================ |
||
572 | opts.unescape_strings = false; |
||
573 | bt('"\\\\s"'); // == "\\s" in the js source |
||
574 | bt("'\\\\s'"); // == '\\s' in the js source |
||
575 | bt("'\\\\\\s'"); // == '\\\s' in the js source |
||
576 | bt("'\\s'"); // == '\s' in the js source |
||
577 | bt('"•"'); |
||
578 | bt('"—"'); |
||
579 | bt('"\\x41\\x42\\x43\\x01"', '"\\x41\\x42\\x43\\x01"'); |
||
580 | bt('"\\u2022"', '"\\u2022"'); |
||
581 | bt('a = /\s+/'); |
||
582 | // bt('a = /\\x41/','a = /A/'); |
||
583 | bt('"\\u2022";a = /\s+/;"\\x41\\x42\\x43\\x01".match(/\\x41/);','"\\u2022";\na = /\s+/;\n"\\x41\\x42\\x43\\x01".match(/\\x41/);'); |
||
584 | test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"', '"\\x22\\x27", \'\\x22\\x27\', "\\x5c", \'\\x5c\', "\\xff and \\xzz", "unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"'); |
||
585 | |||
586 | opts.unescape_strings = true; |
||
587 | test_fragment('"\\x20\\x40\\x4a"', '" @J"'); |
||
588 | test_fragment('"\\xff\\x40\\x4a"'); |
||
589 | test_fragment('"\\u0072\\u016B\\u0137\\u012B\\u0074\\u0069\\u0073"', '"\u0072\u016B\u0137\u012B\u0074\u0069\u0073"'); |
||
590 | test_fragment('"Google Chrome est\\u00E1 actualizado."', '"Google Chrome está actualizado."'); |
||
591 | test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff"', |
||
592 | '"\\"\\\'", \'\\"\\\'\', "\\\\", \'\\\\\', "\\xff and \\xzz", "unicode \\u0000 \\" \\\' \\\\ ' + unicode_char(0xffff) + '"'); |
||
593 | |||
594 | // For error case, return the string unchanged |
||
595 | test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"', |
||
596 | '"\\"\\\'", \'\\"\\\'\', "\\\\", \'\\\\\', "\\xff and \\xzz", "unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"'); |
||
597 | |||
598 | reset_options(); |
||
599 | //============================================================ |
||
600 | bt('return function();'); |
||
601 | bt('var a = function();'); |
||
602 | bt('var a = 5 + function();'); |
||
603 | |||
604 | bt('import foo.*;', 'import foo.*;'); // actionscript's import |
||
605 | test_fragment('function f(a: a, b: b)'); // actionscript |
||
606 | |||
607 | bt('{\n foo // something\n ,\n bar // something\n baz\n}'); |
||
608 | bt('function a(a) {} function b(b) {} function c(c) {}', 'function a(a) {}\n\nfunction b(b) {}\n\nfunction c(c) {}'); |
||
609 | bt('foo(a, function() {})'); |
||
610 | |||
611 | bt('foo(a, /regex/)'); |
||
612 | |||
613 | bt('/* foo */\n"x"'); |
||
614 | |||
615 | reset_options(); |
||
616 | //============================================================ |
||
617 | opts.break_chained_methods = false; |
||
618 | opts.preserve_newlines = false; |
||
619 | bt('foo\n.bar()\n.baz().cucumber(fat)', 'foo.bar().baz().cucumber(fat)'); |
||
620 | bt('foo\n.bar()\n.baz().cucumber(fat); foo.bar().baz().cucumber(fat)', 'foo.bar().baz().cucumber(fat);\nfoo.bar().baz().cucumber(fat)'); |
||
621 | bt('foo\n.bar()\n.baz().cucumber(fat)\n foo.bar().baz().cucumber(fat)', 'foo.bar().baz().cucumber(fat)\nfoo.bar().baz().cucumber(fat)'); |
||
622 | bt('this\n.something = foo.bar()\n.baz().cucumber(fat)', 'this.something = foo.bar().baz().cucumber(fat)'); |
||
623 | bt('this.something.xxx = foo.moo.bar()'); |
||
624 | bt('this\n.something\n.xxx = foo.moo\n.bar()', 'this.something.xxx = foo.moo.bar()'); |
||
625 | |||
626 | opts.break_chained_methods = false; |
||
627 | opts.preserve_newlines = true; |
||
628 | bt('foo\n.bar()\n.baz().cucumber(fat)', 'foo\n .bar()\n .baz().cucumber(fat)'); |
||
629 | bt('foo\n.bar()\n.baz().cucumber(fat); foo.bar().baz().cucumber(fat)', 'foo\n .bar()\n .baz().cucumber(fat);\nfoo.bar().baz().cucumber(fat)'); |
||
630 | bt('foo\n.bar()\n.baz().cucumber(fat)\n foo.bar().baz().cucumber(fat)', 'foo\n .bar()\n .baz().cucumber(fat)\nfoo.bar().baz().cucumber(fat)'); |
||
631 | bt('this\n.something = foo.bar()\n.baz().cucumber(fat)', 'this\n .something = foo.bar()\n .baz().cucumber(fat)'); |
||
632 | bt('this.something.xxx = foo.moo.bar()'); |
||
633 | bt('this\n.something\n.xxx = foo.moo\n.bar()', 'this\n .something\n .xxx = foo.moo\n .bar()'); |
||
634 | |||
635 | opts.break_chained_methods = true; |
||
636 | opts.preserve_newlines = false; |
||
637 | bt('foo\n.bar()\n.baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat)'); |
||
638 | bt('foo\n.bar()\n.baz().cucumber(fat); foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat);\nfoo.bar()\n .baz()\n .cucumber(fat)'); |
||
639 | bt('foo\n.bar()\n.baz().cucumber(fat)\n foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat)\nfoo.bar()\n .baz()\n .cucumber(fat)'); |
||
640 | bt('this\n.something = foo.bar()\n.baz().cucumber(fat)', 'this.something = foo.bar()\n .baz()\n .cucumber(fat)'); |
||
641 | bt('this.something.xxx = foo.moo.bar()'); |
||
642 | bt('this\n.something\n.xxx = foo.moo\n.bar()', 'this.something.xxx = foo.moo.bar()'); |
||
643 | |||
644 | opts.break_chained_methods = true; |
||
645 | opts.preserve_newlines = true; |
||
646 | bt('foo\n.bar()\n.baz().cucumber(fat)', 'foo\n .bar()\n .baz()\n .cucumber(fat)'); |
||
647 | bt('foo\n.bar()\n.baz().cucumber(fat); foo.bar().baz().cucumber(fat)', 'foo\n .bar()\n .baz()\n .cucumber(fat);\nfoo.bar()\n .baz()\n .cucumber(fat)'); |
||
648 | bt('foo\n.bar()\n.baz().cucumber(fat)\n foo.bar().baz().cucumber(fat)', 'foo\n .bar()\n .baz()\n .cucumber(fat)\nfoo.bar()\n .baz()\n .cucumber(fat)'); |
||
649 | bt('this\n.something = foo.bar()\n.baz().cucumber(fat)', 'this\n .something = foo.bar()\n .baz()\n .cucumber(fat)'); |
||
650 | bt('this.something.xxx = foo.moo.bar()'); |
||
651 | bt('this\n.something\n.xxx = foo.moo\n.bar()', 'this\n .something\n .xxx = foo.moo\n .bar()'); |
||
652 | |||
653 | reset_options(); |
||
654 | //============================================================ |
||
655 | // Line wrap test intputs |
||
656 | //.............---------1---------2---------3---------4---------5---------6---------7 |
||
657 | //.............1234567890123456789012345678901234567890123456789012345678901234567890 |
||
658 | wrap_input_1=('foo.bar().baz().cucumber((fat && "sassy") || (leans && mean));\n' + |
||
659 | 'Test_very_long_variable_name_this_should_never_wrap\n.but_this_can\n' + |
||
660 | 'return between_return_and_expression_should_never_wrap.but_this_can\n' + |
||
661 | 'throw between_throw_and_expression_should_never_wrap.but_this_can\n' + |
||
662 | 'if (wraps_can_occur && inside_an_if_block) that_is_\n.okay();\n' + |
||
663 | 'object_literal = {\n' + |
||
664 | ' propertx: first_token + 12345678.99999E-6,\n' + |
||
665 | ' property: first_token_should_never_wrap + but_this_can,\n' + |
||
666 | ' propertz: first_token_should_never_wrap + !but_this_can,\n' + |
||
667 | ' proper: "first_token_should_never_wrap" + "but_this_can"\n' + |
||
668 | '}'); |
||
669 | |||
670 | //.............---------1---------2---------3---------4---------5---------6---------7 |
||
671 | //.............1234567890123456789012345678901234567890123456789012345678901234567890 |
||
672 | wrap_input_2=('{\n' + |
||
673 | ' foo.bar().baz().cucumber((fat && "sassy") || (leans && mean));\n' + |
||
674 | ' Test_very_long_variable_name_this_should_never_wrap\n.but_this_can\n' + |
||
675 | ' return between_return_and_expression_should_never_wrap.but_this_can\n' + |
||
676 | ' throw between_throw_and_expression_should_never_wrap.but_this_can\n' + |
||
677 | ' if (wraps_can_occur && inside_an_if_block) that_is_\n.okay();\n' + |
||
678 | ' object_literal = {\n' + |
||
679 | ' propertx: first_token + 12345678.99999E-6,\n' + |
||
680 | ' property: first_token_should_never_wrap + but_this_can,\n' + |
||
681 | ' propertz: first_token_should_never_wrap + !but_this_can,\n' + |
||
682 | ' proper: "first_token_should_never_wrap" + "but_this_can"\n' + |
||
683 | ' }' + |
||
684 | '}'); |
||
685 | |||
686 | opts.preserve_newlines = false; |
||
687 | opts.wrap_line_length = 0; |
||
688 | //.............---------1---------2---------3---------4---------5---------6---------7 |
||
689 | //.............1234567890123456789012345678901234567890123456789012345678901234567890 |
||
690 | test_fragment(wrap_input_1, |
||
691 | /* expected */ |
||
692 | 'foo.bar().baz().cucumber((fat && "sassy") || (leans && mean));\n' + |
||
693 | 'Test_very_long_variable_name_this_should_never_wrap.but_this_can\n' + |
||
694 | 'return between_return_and_expression_should_never_wrap.but_this_can\n' + |
||
695 | 'throw between_throw_and_expression_should_never_wrap.but_this_can\n' + |
||
696 | 'if (wraps_can_occur && inside_an_if_block) that_is_.okay();\n' + |
||
697 | 'object_literal = {\n' + |
||
698 | ' propertx: first_token + 12345678.99999E-6,\n' + |
||
699 | ' property: first_token_should_never_wrap + but_this_can,\n' + |
||
700 | ' propertz: first_token_should_never_wrap + !but_this_can,\n' + |
||
701 | ' proper: "first_token_should_never_wrap" + "but_this_can"\n' + |
||
702 | '}'); |
||
703 | |||
704 | opts.wrap_line_length = 70; |
||
705 | //.............---------1---------2---------3---------4---------5---------6---------7 |
||
706 | //.............1234567890123456789012345678901234567890123456789012345678901234567890 |
||
707 | test_fragment(wrap_input_1, |
||
708 | /* expected */ |
||
709 | 'foo.bar().baz().cucumber((fat && "sassy") || (leans && mean));\n' + |
||
710 | 'Test_very_long_variable_name_this_should_never_wrap.but_this_can\n' + |
||
711 | 'return between_return_and_expression_should_never_wrap.but_this_can\n' + |
||
712 | 'throw between_throw_and_expression_should_never_wrap.but_this_can\n' + |
||
713 | 'if (wraps_can_occur && inside_an_if_block) that_is_.okay();\n' + |
||
714 | 'object_literal = {\n' + |
||
715 | ' propertx: first_token + 12345678.99999E-6,\n' + |
||
716 | ' property: first_token_should_never_wrap + but_this_can,\n' + |
||
717 | ' propertz: first_token_should_never_wrap + !but_this_can,\n' + |
||
718 | ' proper: "first_token_should_never_wrap" + "but_this_can"\n' + |
||
719 | '}'); |
||
720 | |||
721 | opts.wrap_line_length = 40; |
||
722 | //.............---------1---------2---------3---------4---------5---------6---------7 |
||
723 | //.............1234567890123456789012345678901234567890123456789012345678901234567890 |
||
724 | test_fragment(wrap_input_1, |
||
725 | /* expected */ |
||
726 | 'foo.bar().baz().cucumber((fat &&\n' + |
||
727 | ' "sassy") || (leans && mean));\n' + |
||
728 | 'Test_very_long_variable_name_this_should_never_wrap\n' + |
||
729 | ' .but_this_can\n' + |
||
730 | 'return between_return_and_expression_should_never_wrap\n' + |
||
731 | ' .but_this_can\n' + |
||
732 | 'throw between_throw_and_expression_should_never_wrap\n' + |
||
733 | ' .but_this_can\n' + |
||
734 | 'if (wraps_can_occur &&\n' + |
||
735 | ' inside_an_if_block) that_is_.okay();\n' + |
||
736 | 'object_literal = {\n' + |
||
737 | ' propertx: first_token +\n' + |
||
738 | ' 12345678.99999E-6,\n' + |
||
739 | ' property: first_token_should_never_wrap +\n' + |
||
740 | ' but_this_can,\n' + |
||
741 | ' propertz: first_token_should_never_wrap +\n' + |
||
742 | ' !but_this_can,\n' + |
||
743 | ' proper: "first_token_should_never_wrap" +\n' + |
||
744 | ' "but_this_can"\n' + |
||
745 | '}'); |
||
746 | |||
747 | opts.wrap_line_length = 41; |
||
748 | // NOTE: wrap is only best effort - line continues until next wrap point is found. |
||
749 | //.............---------1---------2---------3---------4---------5---------6---------7 |
||
750 | //.............1234567890123456789012345678901234567890123456789012345678901234567890 |
||
751 | test_fragment(wrap_input_1, |
||
752 | /* expected */ |
||
753 | 'foo.bar().baz().cucumber((fat && "sassy") ||\n' + |
||
754 | ' (leans && mean));\n' + |
||
755 | 'Test_very_long_variable_name_this_should_never_wrap\n' + |
||
756 | ' .but_this_can\n' + |
||
757 | 'return between_return_and_expression_should_never_wrap\n' + |
||
758 | ' .but_this_can\n' + |
||
759 | 'throw between_throw_and_expression_should_never_wrap\n' + |
||
760 | ' .but_this_can\n' + |
||
761 | 'if (wraps_can_occur &&\n' + |
||
762 | ' inside_an_if_block) that_is_.okay();\n' + |
||
763 | 'object_literal = {\n' + |
||
764 | ' propertx: first_token +\n' + |
||
765 | ' 12345678.99999E-6,\n' + |
||
766 | ' property: first_token_should_never_wrap +\n' + |
||
767 | ' but_this_can,\n' + |
||
768 | ' propertz: first_token_should_never_wrap +\n' + |
||
769 | ' !but_this_can,\n' + |
||
770 | ' proper: "first_token_should_never_wrap" +\n' + |
||
771 | ' "but_this_can"\n' + |
||
772 | '}'); |
||
773 | |||
774 | opts.wrap_line_length = 45; |
||
775 | // NOTE: wrap is only best effort - line continues until next wrap point is found. |
||
776 | //.............---------1---------2---------3---------4---------5---------6---------7 |
||
777 | //.............1234567890123456789012345678901234567890123456789012345678901234567890 |
||
778 | test_fragment(wrap_input_2, |
||
779 | /* expected */ |
||
780 | '{\n' + |
||
781 | ' foo.bar().baz().cucumber((fat && "sassy") ||\n' + |
||
782 | ' (leans && mean));\n' + |
||
783 | ' Test_very_long_variable_name_this_should_never_wrap\n' + |
||
784 | ' .but_this_can\n' + |
||
785 | ' return between_return_and_expression_should_never_wrap\n' + |
||
786 | ' .but_this_can\n' + |
||
787 | ' throw between_throw_and_expression_should_never_wrap\n' + |
||
788 | ' .but_this_can\n' + |
||
789 | ' if (wraps_can_occur &&\n' + |
||
790 | ' inside_an_if_block) that_is_.okay();\n' + |
||
791 | ' object_literal = {\n' + |
||
792 | ' propertx: first_token +\n' + |
||
793 | ' 12345678.99999E-6,\n' + |
||
794 | ' property: first_token_should_never_wrap +\n' + |
||
795 | ' but_this_can,\n' + |
||
796 | ' propertz: first_token_should_never_wrap +\n' + |
||
797 | ' !but_this_can,\n' + |
||
798 | ' proper: "first_token_should_never_wrap" +\n' + |
||
799 | ' "but_this_can"\n' + |
||
800 | ' }\n'+ |
||
801 | '}'); |
||
802 | |||
803 | opts.preserve_newlines = true; |
||
804 | opts.wrap_line_length = 0; |
||
805 | //.............---------1---------2---------3---------4---------5---------6---------7 |
||
806 | //.............1234567890123456789012345678901234567890123456789012345678901234567890 |
||
807 | test_fragment(wrap_input_1, |
||
808 | /* expected */ |
||
809 | 'foo.bar().baz().cucumber((fat && "sassy") || (leans && mean));\n' + |
||
810 | 'Test_very_long_variable_name_this_should_never_wrap\n' + |
||
811 | ' .but_this_can\n' + |
||
812 | 'return between_return_and_expression_should_never_wrap.but_this_can\n' + |
||
813 | 'throw between_throw_and_expression_should_never_wrap.but_this_can\n' + |
||
814 | 'if (wraps_can_occur && inside_an_if_block) that_is_\n' + |
||
815 | ' .okay();\n' + |
||
816 | 'object_literal = {\n' + |
||
817 | ' propertx: first_token + 12345678.99999E-6,\n' + |
||
818 | ' property: first_token_should_never_wrap + but_this_can,\n' + |
||
819 | ' propertz: first_token_should_never_wrap + !but_this_can,\n' + |
||
820 | ' proper: "first_token_should_never_wrap" + "but_this_can"\n' + |
||
821 | '}'); |
||
822 | |||
823 | opts.wrap_line_length = 70; |
||
824 | //.............---------1---------2---------3---------4---------5---------6---------7 |
||
825 | //.............1234567890123456789012345678901234567890123456789012345678901234567890 |
||
826 | test_fragment(wrap_input_1, |
||
827 | /* expected */ |
||
828 | 'foo.bar().baz().cucumber((fat && "sassy") || (leans && mean));\n' + |
||
829 | 'Test_very_long_variable_name_this_should_never_wrap\n' + |
||
830 | ' .but_this_can\n' + |
||
831 | 'return between_return_and_expression_should_never_wrap.but_this_can\n' + |
||
832 | 'throw between_throw_and_expression_should_never_wrap.but_this_can\n' + |
||
833 | 'if (wraps_can_occur && inside_an_if_block) that_is_\n' + |
||
834 | ' .okay();\n' + |
||
835 | 'object_literal = {\n' + |
||
836 | ' propertx: first_token + 12345678.99999E-6,\n' + |
||
837 | ' property: first_token_should_never_wrap + but_this_can,\n' + |
||
838 | ' propertz: first_token_should_never_wrap + !but_this_can,\n' + |
||
839 | ' proper: "first_token_should_never_wrap" + "but_this_can"\n' + |
||
840 | '}'); |
||
841 | |||
842 | |||
843 | opts.wrap_line_length = 40; |
||
844 | //.............---------1---------2---------3---------4---------5---------6---------7 |
||
845 | //.............1234567890123456789012345678901234567890123456789012345678901234567890 |
||
846 | test_fragment(wrap_input_1, |
||
847 | /* expected */ |
||
848 | 'foo.bar().baz().cucumber((fat &&\n' + |
||
849 | ' "sassy") || (leans && mean));\n' + |
||
850 | 'Test_very_long_variable_name_this_should_never_wrap\n' + |
||
851 | ' .but_this_can\n' + |
||
852 | 'return between_return_and_expression_should_never_wrap\n' + |
||
853 | ' .but_this_can\n' + |
||
854 | 'throw between_throw_and_expression_should_never_wrap\n' + |
||
855 | ' .but_this_can\n' + |
||
856 | 'if (wraps_can_occur &&\n' + |
||
857 | ' inside_an_if_block) that_is_\n' + |
||
858 | ' .okay();\n' + |
||
859 | 'object_literal = {\n' + |
||
860 | ' propertx: first_token +\n' + |
||
861 | ' 12345678.99999E-6,\n' + |
||
862 | ' property: first_token_should_never_wrap +\n' + |
||
863 | ' but_this_can,\n' + |
||
864 | ' propertz: first_token_should_never_wrap +\n' + |
||
865 | ' !but_this_can,\n' + |
||
866 | ' proper: "first_token_should_never_wrap" +\n' + |
||
867 | ' "but_this_can"\n' + |
||
868 | '}'); |
||
869 | |||
870 | opts.wrap_line_length = 41; |
||
871 | // NOTE: wrap is only best effort - line continues until next wrap point is found. |
||
872 | //.............---------1---------2---------3---------4---------5---------6---------7 |
||
873 | //.............1234567890123456789012345678901234567890123456789012345678901234567890 |
||
874 | test_fragment(wrap_input_1, |
||
875 | /* expected */ |
||
876 | 'foo.bar().baz().cucumber((fat && "sassy") ||\n' + |
||
877 | ' (leans && mean));\n' + |
||
878 | 'Test_very_long_variable_name_this_should_never_wrap\n' + |
||
879 | ' .but_this_can\n' + |
||
880 | 'return between_return_and_expression_should_never_wrap\n' + |
||
881 | ' .but_this_can\n' + |
||
882 | 'throw between_throw_and_expression_should_never_wrap\n' + |
||
883 | ' .but_this_can\n' + |
||
884 | 'if (wraps_can_occur &&\n' + |
||
885 | ' inside_an_if_block) that_is_\n' + |
||
886 | ' .okay();\n' + |
||
887 | 'object_literal = {\n' + |
||
888 | ' propertx: first_token +\n' + |
||
889 | ' 12345678.99999E-6,\n' + |
||
890 | ' property: first_token_should_never_wrap +\n' + |
||
891 | ' but_this_can,\n' + |
||
892 | ' propertz: first_token_should_never_wrap +\n' + |
||
893 | ' !but_this_can,\n' + |
||
894 | ' proper: "first_token_should_never_wrap" +\n' + |
||
895 | ' "but_this_can"\n' + |
||
896 | '}'); |
||
897 | |||
898 | opts.wrap_line_length = 45; |
||
899 | // NOTE: wrap is only best effort - line continues until next wrap point is found. |
||
900 | //.............---------1---------2---------3---------4---------5---------6---------7 |
||
901 | //.............1234567890123456789012345678901234567890123456789012345678901234567890 |
||
902 | test_fragment(wrap_input_2, |
||
903 | /* expected */ |
||
904 | '{\n' + |
||
905 | ' foo.bar().baz().cucumber((fat && "sassy") ||\n' + |
||
906 | ' (leans && mean));\n' + |
||
907 | ' Test_very_long_variable_name_this_should_never_wrap\n' + |
||
908 | ' .but_this_can\n' + |
||
909 | ' return between_return_and_expression_should_never_wrap\n' + |
||
910 | ' .but_this_can\n' + |
||
911 | ' throw between_throw_and_expression_should_never_wrap\n' + |
||
912 | ' .but_this_can\n' + |
||
913 | ' if (wraps_can_occur &&\n' + |
||
914 | ' inside_an_if_block) that_is_\n' + |
||
915 | ' .okay();\n' + |
||
916 | ' object_literal = {\n' + |
||
917 | ' propertx: first_token +\n' + |
||
918 | ' 12345678.99999E-6,\n' + |
||
919 | ' property: first_token_should_never_wrap +\n' + |
||
920 | ' but_this_can,\n' + |
||
921 | ' propertz: first_token_should_never_wrap +\n' + |
||
922 | ' !but_this_can,\n' + |
||
923 | ' proper: "first_token_should_never_wrap" +\n' + |
||
924 | ' "but_this_can"\n' + |
||
925 | ' }\n'+ |
||
926 | '}'); |
||
927 | |||
928 | reset_options(); |
||
929 | //============================================================ |
||
930 | opts.preserve_newlines = false; |
||
931 | bt('if (foo) // comment\n bar();'); |
||
932 | bt('if (foo) // comment\n (bar());'); |
||
933 | bt('if (foo) // comment\n (bar());'); |
||
934 | bt('if (foo) // comment\n /asdf/;'); |
||
935 | bt('this.oa = new OAuth(\n' + |
||
936 | ' _requestToken,\n' + |
||
937 | ' _accessToken,\n' + |
||
938 | ' consumer_key\n' + |
||
939 | ');', |
||
940 | 'this.oa = new OAuth(_requestToken, _accessToken, consumer_key);'); |
||
941 | bt('foo = {\n x: y, // #44\n w: z // #44\n}'); |
||
942 | bt('switch (x) {\n case "a":\n // comment on newline\n break;\n case "b": // comment on same line\n break;\n}'); |
||
943 | bt('this.type =\n this.options =\n // comment\n this.enabled null;', |
||
944 | 'this.type = this.options =\n // comment\n this.enabled null;'); |
||
945 | bt('someObj\n .someFunc1()\n // This comment should not break the indent\n .someFunc2();', |
||
946 | 'someObj.someFunc1()\n // This comment should not break the indent\n .someFunc2();'); |
||
947 | |||
948 | bt('if (true ||\n!true) return;', 'if (true || !true) return;'); |
||
949 | |||
950 | // these aren't ready yet. |
||
951 | //bt('if (foo) // comment\n bar() /*i*/ + baz() /*j\n*/ + asdf();'); |
||
952 | bt('if\n(foo)\nif\n(bar)\nif\n(baz)\nwhee();\na();', |
||
953 | 'if (foo)\n if (bar)\n if (baz) whee();\na();'); |
||
954 | bt('if\n(foo)\nif\n(bar)\nif\n(baz)\nwhee();\nelse\na();', |
||
955 | 'if (foo)\n if (bar)\n if (baz) whee();\n else a();'); |
||
956 | bt('if (foo)\nbar();\nelse\ncar();', |
||
957 | 'if (foo) bar();\nelse car();'); |
||
958 | |||
959 | bt('if (foo) if (bar) if (baz);\na();', |
||
960 | 'if (foo)\n if (bar)\n if (baz);\na();'); |
||
961 | bt('if (foo) if (bar) if (baz) whee();\na();', |
||
962 | 'if (foo)\n if (bar)\n if (baz) whee();\na();'); |
||
963 | bt('if (foo) a()\nif (bar) if (baz) whee();\na();', |
||
964 | 'if (foo) a()\nif (bar)\n if (baz) whee();\na();'); |
||
965 | bt('if (foo);\nif (bar) if (baz) whee();\na();', |
||
966 | 'if (foo);\nif (bar)\n if (baz) whee();\na();'); |
||
967 | bt('if (options)\n' + |
||
968 | ' for (var p in options)\n' + |
||
969 | ' this[p] = options[p];', |
||
970 | 'if (options)\n'+ |
||
971 | ' for (var p in options) this[p] = options[p];'); |
||
972 | bt('if (options) for (var p in options) this[p] = options[p];', |
||
973 | 'if (options)\n for (var p in options) this[p] = options[p];'); |
||
974 | |||
975 | bt('if (options) do q(); while (b());', |
||
976 | 'if (options)\n do q(); while (b());'); |
||
977 | bt('if (options) while (b()) q();', |
||
978 | 'if (options)\n while (b()) q();'); |
||
979 | bt('if (options) do while (b()) q(); while (a());', |
||
980 | 'if (options)\n do\n while (b()) q(); while (a());'); |
||
981 | |||
982 | bt('function f(a, b, c,\nd, e) {}', |
||
983 | 'function f(a, b, c, d, e) {}'); |
||
984 | |||
985 | bt('function f(a,b) {if(a) b()}function g(a,b) {if(!a) b()}', |
||
986 | 'function f(a, b) {\n if (a) b()\n}\n\nfunction g(a, b) {\n if (!a) b()\n}'); |
||
987 | bt('function f(a,b) {if(a) b()}\n\n\n\nfunction g(a,b) {if(!a) b()}', |
||
988 | 'function f(a, b) {\n if (a) b()\n}\n\nfunction g(a, b) {\n if (!a) b()\n}'); |
||
989 | |||
990 | // This is not valid syntax, but still want to behave reasonably and not side-effect |
||
991 | bt('(if(a) b())(if(a) b())', |
||
992 | '(\n if (a) b())(\n if (a) b())'); |
||
993 | bt('(if(a) b())\n\n\n(if(a) b())', |
||
994 | '(\n if (a) b())\n(\n if (a) b())'); |
||
995 | |||
996 | |||
997 | |||
998 | bt("if\n(a)\nb();", "if (a) b();"); |
||
999 | bt('var a =\nfoo', 'var a = foo'); |
||
1000 | bt('var a = {\n"a":1,\n"b":2}', "var a = {\n \"a\": 1,\n \"b\": 2\n}"); |
||
1001 | bt("var a = {\n'a':1,\n'b':2}", "var a = {\n 'a': 1,\n 'b': 2\n}"); |
||
1002 | bt('var a = /*i*/ "b";'); |
||
1003 | bt('var a = /*i*/\n"b";', 'var a = /*i*/ "b";'); |
||
1004 | bt('var a = /*i*/\nb;', 'var a = /*i*/ b;'); |
||
1005 | bt('{\n\n\n"x"\n}', '{\n "x"\n}'); |
||
1006 | bt('if(a &&\nb\n||\nc\n||d\n&&\ne) e = f', 'if (a && b || c || d && e) e = f'); |
||
1007 | bt('if(a &&\n(b\n||\nc\n||d)\n&&\ne) e = f', 'if (a && (b || c || d) && e) e = f'); |
||
1008 | test_fragment('\n\n"x"', '"x"'); |
||
1009 | bt('a = 1;\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nb = 2;', |
||
1010 | 'a = 1;\nb = 2;'); |
||
1011 | |||
1012 | opts.preserve_newlines = true; |
||
1013 | bt('if (foo) // comment\n bar();'); |
||
1014 | bt('if (foo) // comment\n (bar());'); |
||
1015 | bt('if (foo) // comment\n (bar());'); |
||
1016 | bt('if (foo) // comment\n /asdf/;'); |
||
1017 | bt('foo = {\n x: y, // #44\n w: z // #44\n}'); |
||
1018 | bt('switch (x) {\n case "a":\n // comment on newline\n break;\n case "b": // comment on same line\n break;\n}'); |
||
1019 | bt('this.type =\n this.options =\n // comment\n this.enabled null;'); |
||
1020 | bt('someObj\n .someFunc1()\n // This comment should not break the indent\n .someFunc2();'); |
||
1021 | |||
1022 | bt('if (true ||\n!true) return;', 'if (true ||\n !true) return;'); |
||
1023 | |||
1024 | // these aren't ready yet. |
||
1025 | // bt('if (foo) // comment\n bar() /*i*/ + baz() /*j\n*/ + asdf();'); |
||
1026 | bt('if\n(foo)\nif\n(bar)\nif\n(baz)\nwhee();\na();', |
||
1027 | 'if (foo)\n if (bar)\n if (baz)\n whee();\na();'); |
||
1028 | bt('if\n(foo)\nif\n(bar)\nif\n(baz)\nwhee();\nelse\na();', |
||
1029 | 'if (foo)\n if (bar)\n if (baz)\n whee();\n else\n a();'); |
||
1030 | bt('if (foo)\nbar();\nelse\ncar();', |
||
1031 | 'if (foo)\n bar();\nelse\n car();'); |
||
1032 | bt('if (foo) bar();\nelse\ncar();', |
||
1033 | 'if (foo) bar();\nelse\n car();'); |
||
1034 | |||
1035 | bt('if (foo) if (bar) if (baz);\na();', |
||
1036 | 'if (foo)\n if (bar)\n if (baz);\na();'); |
||
1037 | bt('if (foo) if (bar) if (baz) whee();\na();', |
||
1038 | 'if (foo)\n if (bar)\n if (baz) whee();\na();'); |
||
1039 | bt('if (foo) a()\nif (bar) if (baz) whee();\na();', |
||
1040 | 'if (foo) a()\nif (bar)\n if (baz) whee();\na();'); |
||
1041 | bt('if (foo);\nif (bar) if (baz) whee();\na();', |
||
1042 | 'if (foo);\nif (bar)\n if (baz) whee();\na();'); |
||
1043 | bt('if (options)\n' + |
||
1044 | ' for (var p in options)\n' + |
||
1045 | ' this[p] = options[p];'); |
||
1046 | bt('if (options) for (var p in options) this[p] = options[p];', |
||
1047 | 'if (options)\n for (var p in options) this[p] = options[p];'); |
||
1048 | |||
1049 | bt('if (options) do q(); while (b());', |
||
1050 | 'if (options)\n do q(); while (b());'); |
||
1051 | bt('if (options) do; while (b());', |
||
1052 | 'if (options)\n do; while (b());'); |
||
1053 | bt('if (options) while (b()) q();', |
||
1054 | 'if (options)\n while (b()) q();'); |
||
1055 | bt('if (options) do while (b()) q(); while (a());', |
||
1056 | 'if (options)\n do\n while (b()) q(); while (a());'); |
||
1057 | |||
1058 | bt('function f(a, b, c,\nd, e) {}', |
||
1059 | 'function f(a, b, c,\n d, e) {}'); |
||
1060 | |||
1061 | bt('function f(a,b) {if(a) b()}function g(a,b) {if(!a) b()}', |
||
1062 | 'function f(a, b) {\n if (a) b()\n}\n\nfunction g(a, b) {\n if (!a) b()\n}'); |
||
1063 | bt('function f(a,b) {if(a) b()}\n\n\n\nfunction g(a,b) {if(!a) b()}', |
||
1064 | 'function f(a, b) {\n if (a) b()\n}\n\n\n\nfunction g(a, b) {\n if (!a) b()\n}'); |
||
1065 | // This is not valid syntax, but still want to behave reasonably and not side-effect |
||
1066 | bt('(if(a) b())(if(a) b())', |
||
1067 | '(\n if (a) b())(\n if (a) b())'); |
||
1068 | bt('(if(a) b())\n\n\n(if(a) b())', |
||
1069 | '(\n if (a) b())\n\n\n(\n if (a) b())'); |
||
1070 | |||
1071 | // space between functions |
||
1072 | bt('/*\n * foo\n */\nfunction foo() {}'); |
||
1073 | bt('// a nice function\nfunction foo() {}'); |
||
1074 | bt('function foo() {}\nfunction foo() {}', |
||
1075 | 'function foo() {}\n\nfunction foo() {}' |
||
1076 | ); |
||
1077 | |||
1078 | bt('[\n function() {}\n]'); |
||
1079 | |||
1080 | |||
1081 | |||
1082 | bt("if\n(a)\nb();", "if (a)\n b();"); |
||
1083 | bt('var a =\nfoo', 'var a =\n foo'); |
||
1084 | bt('var a = {\n"a":1,\n"b":2}', "var a = {\n \"a\": 1,\n \"b\": 2\n}"); |
||
1085 | bt("var a = {\n'a':1,\n'b':2}", "var a = {\n 'a': 1,\n 'b': 2\n}"); |
||
1086 | bt('var a = /*i*/ "b";'); |
||
1087 | bt('var a = /*i*/\n"b";', 'var a = /*i*/\n "b";'); |
||
1088 | bt('var a = /*i*/\nb;', 'var a = /*i*/\n b;'); |
||
1089 | bt('{\n\n\n"x"\n}', '{\n\n\n "x"\n}'); |
||
1090 | bt('if(a &&\nb\n||\nc\n||d\n&&\ne) e = f', 'if (a &&\n b ||\n c ||\n d &&\n e) e = f'); |
||
1091 | bt('if(a &&\n(b\n||\nc\n||d)\n&&\ne) e = f', 'if (a &&\n (b ||\n c ||\n d) &&\n e) e = f'); |
||
1092 | test_fragment('\n\n"x"', '"x"'); |
||
1093 | |||
1094 | // this beavior differs between js and python, defaults to unlimited in js, 10 in python |
||
1095 | bt('a = 1;\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nb = 2;', |
||
1096 | 'a = 1;\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nb = 2;'); |
||
1097 | opts.max_preserve_newlines = 8; |
||
1098 | bt('a = 1;\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nb = 2;', |
||
1099 | 'a = 1;\n\n\n\n\n\n\n\nb = 2;'); |
||
1100 | |||
1101 | reset_options(); |
||
1102 | //============================================================ |
||
1103 | |||
1104 | |||
1105 | Urlencoded.run_tests(sanitytest); |
||
1106 | } |
||
1107 | |||
1108 | beautifier_tests(); |
||
1109 | beautifier_unconverted_tests(); |
||
1110 | } |
||
1111 | |||
1112 | if (typeof exports !== "undefined") { |
||
1113 | exports.run_javascript_tests = run_javascript_tests; |
||
1114 | } |