corrade-nucleus-nucleons – Blame information for rev 2

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