#!/usr/bin/env python # -*- coding: utf-8 -*- ''' {{&header_text}} The MIT License (MIT) Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ''' import re import unittest import jsbeautifier import copy class TestJSBeautifier(unittest.TestCase): options = None @classmethod def setUpClass(cls): pass cls.wrapregex = re.compile('^(.+)$', re.MULTILINE) def reset_options(self): true = True false = False default_options = jsbeautifier.default_options() default_options.indent_size = 4 default_options.indent_char = ' ' default_options.preserve_newlines = true default_options.jslint_happy = false default_options.indent_level = 0 default_options.break_chained_methods = false default_options.eol = '\n' {{#default_options}} default_options.{{name}} = {{&value}} {{/default_options}} self.options = copy.copy(default_options) def test_unescape(self): # Test cases contributed by test_fragment = self.decodesto self.reset_options() bt = self.bt def unicode_char(value): return chr(value) bt('"\\\\s"') # == "\\s" in the js source bt("'\\\\s'") # == '\\s' in the js source bt("'\\\\\\s'") # == '\\\s' in the js source bt("'\\s'") # == '\s' in the js source bt('"•"') bt('"—"') bt('"\\x41\\x42\\x43\\x01"', '"\\x41\\x42\\x43\\x01"') bt('"\\u2022"', '"\\u2022"') bt('"\\u{2022}"', '"\\u{2022}"') bt('a = /\\s+/') #bt('a = /\\x41/','a = /A/') bt('"\\u2022";a = /\\s+/;"\\x41\\x42\\x43\\x01".match(/\\x41/);','"\\u2022";\na = /\\s+/;\n"\\x41\\x42\\x43\\x01".match(/\\x41/);') test_fragment('"\\x41\\x42\\x01\\x43"') test_fragment('"\\x41\\x42\\u0001\\x43"') test_fragment('"\\x41\\x42\\u{0001}\\x43"') test_fragment('"\\x20\\x40\\x4a"') test_fragment('"\\xff\\x40\\x4a"') test_fragment('"\\u0072\\u016B\\u0137\\u012B\\u0074\\u0069\\u0073"') test_fragment('"\\u{0072}\\u{016B}\\u{110000}\\u{137}\\u012B\\x74\\u{0000069}\\u{073}"') test_fragment('"Google Chrome est\\u00E1 actualizado."') 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"') self.options.unescape_strings = True bt('"\\x41\\x42\\x01\\x43"', '"AB\\x01C"') bt('"\\x41\\x42\\u0001\\x43"', '"AB\\u0001C"') bt('"\\x41\\x42\\u{0001}\\x43"', '"AB\\u{0001}C"') test_fragment('"\\x20\\x40\\x4a"', '" @J"') test_fragment('"\\xff\\x40\\x4a"') test_fragment('"\\u0072\\u016B\\u0137\\u012B\\u0074\\u0069\\u0073"', '"\u0072\u016B\u0137\u012B\u0074\u0069\u0073"') test_fragment('"\\u{0072}\\u{016B}\\u{110000}\\u{137}\\u012B\\x74\\u{0000069}\\u{073}"', '"\u0072\u016B\\u{110000}\u0137\u012B\u0074\u0069\u0073"') bt('a = /\\s+/') test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff"', '"\\"\\\'", \'\\"\\\'\', "\\\\", \'\\\\\', "\\xff", "unicode \\u0000 \\" \\\' \\\\ ' + unicode_char(0xffff) + '"') # For error case, return the string unchanged test_fragment('"\\x22\\x27",\'\\x22\\x27\',"\\x5c",\'\\x5c\',"\\xff and \\xzz","unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"', '"\\"\\\'", \'\\"\\\'\', "\\\\", \'\\\\\', "\\xff and \\xzz", "unicode \\u0000 \\u0022 \\u0027 \\u005c \\uffff \\uzzzz"') self.options.unescape_strings = False def test_beautifier(self): test_fragment = self.decodesto bt = self.bt true = True false = False def unicode_char(value): return chr(value) ##============================================================ # Line wrap test inputs #....---------1---------2---------3---------4---------5---------6---------7 #....1234567890123456789012345678901234567890123456789012345678901234567890 wrap_input_1=( 'foo.bar().baz().cucumber((f && "sass") || (leans && mean));\n' + 'Test_very_long_variable_name_this_should_never_wrap\n.but_this_can\n' + 'return between_return_and_expression_should_never_wrap.but_this_can\n' + 'throw between_throw_and_expression_should_never_wrap.but_this_can\n' + 'if (wraps_can_occur && inside_an_if_block) that_is_\n.okay();\n' + 'object_literal = {\n' + ' propertx: first_token + 12345678.99999E-6,\n' + ' property: first_token_should_never_wrap + but_this_can,\n' + ' propertz: first_token_should_never_wrap + !but_this_can,\n' + ' proper: "first_token_should_never_wrap" + "but_this_can"\n' + '}'); #....---------1---------2---------3---------4---------5---------6---------7 #....1234567890123456789012345678901234567890123456789012345678901234567890 wrap_input_2=( '{\n' + ' foo.bar().baz().cucumber((f && "sass") || (leans && mean));\n' + ' Test_very_long_variable_name_this_should_never_wrap\n.but_this_can\n' + ' return between_return_and_expression_should_never_wrap.but_this_can\n' + ' throw between_throw_and_expression_should_never_wrap.but_this_can\n' + ' if (wraps_can_occur && inside_an_if_block) that_is_\n.okay();\n' + ' object_literal = {\n' + ' propertx: first_token + 12345678.99999E-6,\n' + ' property: first_token_should_never_wrap + but_this_can,\n' + ' propertz: first_token_should_never_wrap + !but_this_can,\n' + ' proper: "first_token_should_never_wrap" + "but_this_can"\n' + ' }' + '}'); {{#groups}}{{#set_mustache_tags}}.{{/set_mustache_tags}} #============================================================ {{^matrix}} # {{&name}} self.reset_options() {{#options}} self.options.{{name}} = {{&value}} {{/options}} {{#tests}} {{#test_line}}.{{/test_line}} {{/tests}} {{/matrix}} {{#matrix}} # {{&name}} - ({{#matrix_context_string}}.{{/matrix_context_string}}) self.reset_options() {{#options}} self.options.{{name}} = {{&value}} {{/options}} {{#tests}} {{#test_line}}.{{/test_line}} {{/tests}} {{/matrix}} {{#unset_mustache_tags}}.{{/unset_mustache_tags}}{{/groups}} def test_beautifier_unconverted(self): test_fragment = self.decodesto bt = self.bt self.reset_options() #============================================================ bt(None, "") self.reset_options() #============================================================ # Test user pebkac protection, converts dash names to underscored names setattr(self.options, 'end-with-newline', True) test_fragment(None, '\n') self.reset_options() #============================================================ # Test passing dictionary or tuple self.options = {'end_with_newline': True, 'eol': '\r\n' } test_fragment(None, '\r\n') self.options = {'end-with-newline': True} test_fragment(None, '\n') self.options = {'end-with-newline': False} test_fragment(None, '') self.options = ( ('end-with-newline', True), ('eol', '\r') ) test_fragment(None, '\r') self.reset_options() #============================================================ self.options.indent_size = 1 self.options.indent_char = ' ' bt('{ one_char() }', "{\n one_char()\n}") bt('var a,b=1,c=2', 'var a, b = 1,\n c = 2') self.options.indent_size = 4 self.options.indent_char = ' ' bt('{ one_char() }', "{\n one_char()\n}") self.options.indent_size = 1 self.options.indent_char = "\t" bt('{ one_char() }', "{\n\tone_char()\n}") bt('x = a ? b : c; x;', 'x = a ? b : c;\nx;') #set to something else than it should change to, but with tabs on, should override self.options.indent_size = 5 self.options.indent_char = ' ' self.options.indent_with_tabs = True bt('{ one_char() }', "{\n\tone_char()\n}") bt('x = a ? b : c; x;', 'x = a ? b : c;\nx;') self.reset_options() #============================================================ self.options.preserve_newlines = False bt('var\na=dont_preserve_newlines;', 'var a = dont_preserve_newlines;') # make sure the blank line between function definitions stays # even when preserve_newlines = False bt('function foo() {\n return 1;\n}\n\nfunction foo() {\n return 1;\n}') bt('function foo() {\n return 1;\n}\nfunction foo() {\n return 1;\n}', 'function foo() {\n return 1;\n}\n\nfunction foo() {\n return 1;\n}' ) bt('function foo() {\n return 1;\n}\n\n\nfunction foo() {\n return 1;\n}', 'function foo() {\n return 1;\n}\n\nfunction foo() {\n return 1;\n}' ) self.options.preserve_newlines = True bt('var\na=do_preserve_newlines;', 'var\n a = do_preserve_newlines;') bt('if (foo) // comment\n{\n bar();\n}') self.reset_options() #============================================================ # START tests for brace positioning # If this is ever supported, update tests for each brace style. # test_fragment('return\n{', 'return\n{') # can't support this?, but that's an improbable and extreme case anyway. self.options.brace_style = 'expand' bt('//case 1\nif (a == 1)\n{}\n//case 2\nelse if (a == 2)\n{}') bt('if(1){2}else{3}', "if (1)\n{\n 2\n}\nelse\n{\n 3\n}") bt('try{a();}catch(b){c();}catch(d){}finally{e();}', "try\n{\n a();\n}\ncatch (b)\n{\n c();\n}\ncatch (d)\n{}\nfinally\n{\n e();\n}") bt('if(a){b();}else if(c) foo();', "if (a)\n{\n b();\n}\nelse if (c) foo();") bt("if (a) {\n// comment\n}else{\n// comment\n}", "if (a)\n{\n // comment\n}\nelse\n{\n // comment\n}") # if/else statement with empty body bt('if (x) {y} else { if (x) {y}}', 'if (x)\n{\n y\n}\nelse\n{\n if (x)\n {\n y\n }\n}') bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a)\n{\n b;\n}\nelse\n{\n c;\n}') test_fragment(' /*\n* xx\n*/\n// xx\nif (foo) {\n bar();\n}', ' /*\n * xx\n */\n // xx\n if (foo)\n {\n bar();\n }') bt('if (foo)\n{}\nelse /regex/.test();') test_fragment('if (foo) {', 'if (foo)\n{') test_fragment('foo {', 'foo\n{') test_fragment('return {', 'return {') # return needs the brace. test_fragment('return /* inline */ {', 'return /* inline */ {') test_fragment('return;\n{', 'return;\n{') bt("throw {}") bt("throw {\n foo;\n}") bt('var foo = {}') bt('function x() {\n foo();\n}zzz', 'function x()\n{\n foo();\n}\nzzz') test_fragment('a: do {} while (); xxx', 'a: do {} while ();\nxxx') bt('{a: do {} while (); xxx}', '{\n a: do {} while ();xxx\n}') bt('var a = new function() {};') bt('var a = new function a() {};', 'var a = new function a()\n{};') bt('var a = new function()\n{};', 'var a = new function() {};') bt('var a = new function a()\n{};') bt('var a = new function a()\n {},\n b = new function b()\n {};') bt("foo({\n 'a': 1\n},\n10);", "foo(\n {\n 'a': 1\n },\n 10);") bt('(["foo","bar"]).each(function(i) {return i;});', '(["foo", "bar"]).each(function(i)\n{\n return i;\n});') bt('(function(i) {return i;})();', '(function(i)\n{\n return i;\n})();') bt( "test( /*Argument 1*/ {\n" + " 'Value1': '1'\n" + "}, /*Argument 2\n" + " */ {\n" + " 'Value2': '2'\n" + "});", # expected "test( /*Argument 1*/\n" + " {\n" + " 'Value1': '1'\n" + " },\n" + " /*Argument 2\n" + " */\n" + " {\n" + " 'Value2': '2'\n" + " });") bt( "test(\n" + "/*Argument 1*/ {\n" + " 'Value1': '1'\n" + "},\n" + "/*Argument 2\n" + " */ {\n" + " 'Value2': '2'\n" + "});", # expected "test(\n" + " /*Argument 1*/\n" + " {\n" + " 'Value1': '1'\n" + " },\n" + " /*Argument 2\n" + " */\n" + " {\n" + " 'Value2': '2'\n" + " });") bt( "test( /*Argument 1*/\n" + "{\n" + " 'Value1': '1'\n" + "}, /*Argument 2\n" + " */\n" + "{\n" + " 'Value2': '2'\n" + "});", # expected "test( /*Argument 1*/\n" + " {\n" + " 'Value1': '1'\n" + " },\n" + " /*Argument 2\n" + " */\n" + " {\n" + " 'Value2': '2'\n" + " });") self.options.brace_style = 'collapse' bt('//case 1\nif (a == 1) {}\n//case 2\nelse if (a == 2) {}') bt('if(1){2}else{3}', "if (1) {\n 2\n} else {\n 3\n}") bt('try{a();}catch(b){c();}catch(d){}finally{e();}', "try {\n a();\n} catch (b) {\n c();\n} catch (d) {} finally {\n e();\n}") bt('if(a){b();}else if(c) foo();', "if (a) {\n b();\n} else if (c) foo();") bt("if (a) {\n// comment\n}else{\n// comment\n}", "if (a) {\n // comment\n} else {\n // comment\n}") # if/else statement with empty body bt('if (x) {y} else { if (x) {y}}', 'if (x) {\n y\n} else {\n if (x) {\n y\n }\n}') bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a) {\n b;\n} else {\n c;\n}') test_fragment(' /*\n* xx\n*/\n// xx\nif (foo) {\n bar();\n}', ' /*\n * xx\n */\n // xx\n if (foo) {\n bar();\n }') bt('if (foo) {} else /regex/.test();') test_fragment('if (foo) {', 'if (foo) {') test_fragment('foo {', 'foo {') test_fragment('return {', 'return {') # return needs the brace. test_fragment('return /* inline */ {', 'return /* inline */ {') test_fragment('return;\n{', 'return;\n{') bt("throw {}") bt("throw {\n foo;\n}") bt('var foo = {}') bt('function x() {\n foo();\n}zzz', 'function x() {\n foo();\n}\nzzz') test_fragment('a: do {} while (); xxx', 'a: do {} while ();\nxxx') bt('{a: do {} while (); xxx}', '{\n a: do {} while ();xxx\n}') bt('var a = new function() {};') bt('var a = new function a() {};') bt('var a = new function()\n{};', 'var a = new function() {};') bt('var a = new function a()\n{};', 'var a = new function a() {};') bt('var a = new function a()\n {},\n b = new function b()\n {};', 'var a = new function a() {},\n b = new function b() {};') bt("foo({\n 'a': 1\n},\n10);", "foo({\n 'a': 1\n },\n 10);") bt('(["foo","bar"]).each(function(i) {return i;});', '(["foo", "bar"]).each(function(i) {\n return i;\n});') bt('(function(i) {return i;})();', '(function(i) {\n return i;\n})();') bt( "test( /*Argument 1*/ {\n" + " 'Value1': '1'\n" + "}, /*Argument 2\n" + " */ {\n" + " 'Value2': '2'\n" + "});", # expected "test( /*Argument 1*/ {\n" + " 'Value1': '1'\n" + " },\n" + " /*Argument 2\n" + " */\n" + " {\n" + " 'Value2': '2'\n" + " });") bt( "test(\n" + "/*Argument 1*/ {\n" + " 'Value1': '1'\n" + "},\n" + "/*Argument 2\n" + " */ {\n" + " 'Value2': '2'\n" + "});", # expected "test(\n" + " /*Argument 1*/\n" + " {\n" + " 'Value1': '1'\n" + " },\n" + " /*Argument 2\n" + " */\n" + " {\n" + " 'Value2': '2'\n" + " });") bt( "test( /*Argument 1*/\n" + "{\n" + " 'Value1': '1'\n" + "}, /*Argument 2\n" + " */\n" + "{\n" + " 'Value2': '2'\n" + "});", # expected "test( /*Argument 1*/ {\n" + " 'Value1': '1'\n" + " },\n" + " /*Argument 2\n" + " */\n" + " {\n" + " 'Value2': '2'\n" + " });") self.options.brace_style = "end-expand" bt('//case 1\nif (a == 1) {}\n//case 2\nelse if (a == 2) {}') bt('if(1){2}else{3}', "if (1) {\n 2\n}\nelse {\n 3\n}") bt('try{a();}catch(b){c();}catch(d){}finally{e();}', "try {\n a();\n}\ncatch (b) {\n c();\n}\ncatch (d) {}\nfinally {\n e();\n}") bt('if(a){b();}else if(c) foo();', "if (a) {\n b();\n}\nelse if (c) foo();") bt("if (a) {\n// comment\n}else{\n// comment\n}", "if (a) {\n // comment\n}\nelse {\n // comment\n}") # if/else statement with empty body bt('if (x) {y} else { if (x) {y}}', 'if (x) {\n y\n}\nelse {\n if (x) {\n y\n }\n}') bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a) {\n b;\n}\nelse {\n c;\n}') test_fragment(' /*\n* xx\n*/\n// xx\nif (foo) {\n bar();\n}', ' /*\n * xx\n */\n // xx\n if (foo) {\n bar();\n }') bt('if (foo) {}\nelse /regex/.test();') test_fragment('if (foo) {', 'if (foo) {') test_fragment('foo {', 'foo {') test_fragment('return {', 'return {') # return needs the brace. test_fragment('return /* inline */ {', 'return /* inline */ {') test_fragment('return;\n{', 'return;\n{') bt("throw {}") bt("throw {\n foo;\n}") bt('var foo = {}') bt('function x() {\n foo();\n}zzz', 'function x() {\n foo();\n}\nzzz') test_fragment('a: do {} while (); xxx', 'a: do {} while ();\nxxx') bt('{a: do {} while (); xxx}', '{\n a: do {} while ();xxx\n}') bt('var a = new function() {};') bt('var a = new function a() {};') bt('var a = new function()\n{};', 'var a = new function() {};') bt('var a = new function a()\n{};', 'var a = new function a() {};') bt('var a = new function a()\n {},\n b = new function b()\n {};', 'var a = new function a() {},\n b = new function b() {};') bt("foo({\n 'a': 1\n},\n10);", "foo({\n 'a': 1\n },\n 10);") bt('(["foo","bar"]).each(function(i) {return i;});', '(["foo", "bar"]).each(function(i) {\n return i;\n});') bt('(function(i) {return i;})();', '(function(i) {\n return i;\n})();') bt( "test( /*Argument 1*/ {\n" + " 'Value1': '1'\n" + "}, /*Argument 2\n" + " */ {\n" + " 'Value2': '2'\n" + "});", # expected "test( /*Argument 1*/ {\n" + " 'Value1': '1'\n" + " },\n" + " /*Argument 2\n" + " */\n" + " {\n" + " 'Value2': '2'\n" + " });") bt( "test(\n" + "/*Argument 1*/ {\n" + " 'Value1': '1'\n" + "},\n" + "/*Argument 2\n" + " */ {\n" + " 'Value2': '2'\n" + "});", # expected "test(\n" + " /*Argument 1*/\n" + " {\n" + " 'Value1': '1'\n" + " },\n" + " /*Argument 2\n" + " */\n" + " {\n" + " 'Value2': '2'\n" + " });") bt( "test( /*Argument 1*/\n" + "{\n" + " 'Value1': '1'\n" + "}, /*Argument 2\n" + " */\n" + "{\n" + " 'Value2': '2'\n" + "});", # expected "test( /*Argument 1*/ {\n" + " 'Value1': '1'\n" + " },\n" + " /*Argument 2\n" + " */\n" + " {\n" + " 'Value2': '2'\n" + " });") self.options.brace_style = 'none' bt('//case 1\nif (a == 1)\n{}\n//case 2\nelse if (a == 2)\n{}') bt('if(1){2}else{3}', "if (1) {\n 2\n} else {\n 3\n}") bt('try{a();}catch(b){c();}catch(d){}finally{e();}', "try {\n a();\n} catch (b) {\n c();\n} catch (d) {} finally {\n e();\n}") bt('if(a){b();}else if(c) foo();', "if (a) {\n b();\n} else if (c) foo();") bt("if (a) {\n// comment\n}else{\n// comment\n}", "if (a) {\n // comment\n} else {\n // comment\n}") # if/else statement with empty body bt('if (x) {y} else { if (x) {y}}', 'if (x) {\n y\n} else {\n if (x) {\n y\n }\n}') bt('if (a)\n{\nb;\n}\nelse\n{\nc;\n}', 'if (a)\n{\n b;\n}\nelse\n{\n c;\n}') test_fragment(' /*\n* xx\n*/\n// xx\nif (foo) {\n bar();\n}', ' /*\n * xx\n */\n // xx\n if (foo) {\n bar();\n }') bt('if (foo)\n{}\nelse /regex/.test();') test_fragment('if (foo) {') test_fragment('foo {') test_fragment('return {') # return needs the brace. test_fragment('return /* inline */ {') test_fragment('return;\n{') bt("throw {}") bt("throw {\n foo;\n}") bt('var foo = {}') bt('function x() {\n foo();\n}zzz', 'function x() {\n foo();\n}\nzzz') test_fragment('a: do {} while (); xxx', 'a: do {} while ();\nxxx') bt('{a: do {} while (); xxx}', '{\n a: do {} while ();xxx\n}') bt('var a = new function() {};') bt('var a = new function a() {};') bt('var a = new function()\n{};', 'var a = new function() {};') bt('var a = new function a()\n{};') bt('var a = new function a()\n {},\n b = new function b()\n {};') bt("foo({\n 'a': 1\n},\n10);", "foo({\n 'a': 1\n },\n 10);") bt('(["foo","bar"]).each(function(i) {return i;});', '(["foo", "bar"]).each(function(i) {\n return i;\n});') bt('(function(i) {return i;})();', '(function(i) {\n return i;\n})();') bt( "test( /*Argument 1*/ {\n" + " 'Value1': '1'\n" + "}, /*Argument 2\n" + " */ {\n" + " 'Value2': '2'\n" + "});", # expected "test( /*Argument 1*/ {\n" + " 'Value1': '1'\n" + " },\n" + " /*Argument 2\n" + " */\n" + " {\n" + " 'Value2': '2'\n" + " });") bt( "test(\n" + "/*Argument 1*/ {\n" + " 'Value1': '1'\n" + "},\n" + "/*Argument 2\n" + " */ {\n" + " 'Value2': '2'\n" + "});", # expected "test(\n" + " /*Argument 1*/\n" + " {\n" + " 'Value1': '1'\n" + " },\n" + " /*Argument 2\n" + " */\n" + " {\n" + " 'Value2': '2'\n" + " });") bt( "test( /*Argument 1*/\n" + "{\n" + " 'Value1': '1'\n" + "}, /*Argument 2\n" + " */\n" + "{\n" + " 'Value2': '2'\n" + "});", # expected "test( /*Argument 1*/\n" + " {\n" + " 'Value1': '1'\n" + " },\n" + " /*Argument 2\n" + " */\n" + " {\n" + " 'Value2': '2'\n" + " });") # END tests for brace position self.reset_options() #============================================================ self.options.preserve_newlines = True bt("var a = 'foo' +\n 'bar';") bt('"foo""bar""baz"', '"foo"\n"bar"\n"baz"') bt("'foo''bar''baz'", "'foo'\n'bar'\n'baz'") bt("{\n get foo() {}\n}") bt("{\n var a = get\n foo();\n}") bt("{\n set foo() {}\n}") bt("{\n var a = set\n foo();\n}") bt("var x = {\n get function()\n}") bt("var x = {\n set function()\n}") # According to my current research get/set have no special meaning outside of an object literal bt("var x = set\n\na() {}", "var x = set\n\na() {}") bt("var x = set\n\nfunction() {}", "var x = set\n\nfunction() {}") bt('for () /abc/.test()') bt('if (k) /aaa/m.test(v) && l();') bt('switch (true) {\n case /swf/i.test(foo):\n bar();\n}') bt('createdAt = {\n type: Date,\n default: Date.now\n}') bt('switch (createdAt) {\n case a:\n Date,\n default:\n Date.now\n}') self.reset_options() #============================================================ def decodesto(self, input, expectation=None): if expectation is None: expectation = input self.assertMultiLineEqual( jsbeautifier.beautify(input, self.options), expectation) # if the expected is different from input, run it again # expected output should be unchanged when run twice. if not expectation is None: self.assertMultiLineEqual( jsbeautifier.beautify(expectation, self.options), expectation) if self.options is None or not isinstance(self.options, (dict, tuple)): # Everywhere we do newlines, they should be replaced with opts.eol self.options.eol = '\r\\n' expectation = expectation.replace('\n', '\r\n') self.options.disabled = True self.assertMultiLineEqual( jsbeautifier.beautify(input, self.options), input or '') self.assertMultiLineEqual( jsbeautifier.beautify('\n\n' + expectation, self.options), '\n\n' + expectation) self.options.disabled = False; self.assertMultiLineEqual( jsbeautifier.beautify(input, self.options), expectation) if input and input.find('\n') != -1: input = input.replace('\n', '\r\n') self.assertMultiLineEqual( jsbeautifier.beautify(input, self.options), expectation) # Ensure support for auto eol detection self.options.eol = 'auto' self.assertMultiLineEqual( jsbeautifier.beautify(input, self.options), expectation) self.options.eol = '\n' def wrap(self, text): return self.wrapregex.sub(' \\1', text) def bt(self, input, expectation=None): if expectation is None: expectation = input self.decodesto(input, expectation) # If we set raw, input should be unchanged self.options.test_output_raw = True if self.options.end_with_newline: self.decodesto(input, input) self.options.test_output_raw = False current_indent_size = None if self.options.js and self.options.js['indent_size']: current_indent_size = self.options.js['indent_size'] if not current_indent_size: current_indent_size = self.options.indent_size if current_indent_size == 4 and input: wrapped_input = '{\n%s\n foo = bar;\n}' % self.wrap(input) wrapped_expect = '{\n%s\n foo = bar;\n}' % self.wrap(expectation) self.decodesto(wrapped_input, wrapped_expect) # If we set raw, input should be unchanged self.options.test_output_raw = True if self.options.end_with_newline: self.decodesto(wrapped_input, wrapped_input) self.options.test_output_raw = False if __name__ == '__main__': unittest.main()