Example #1
0
    def test_if_cont_line_level_option(self):
        src = textwrap.dedent('''\
            if foo and
                bar or
                (1 + 2) then
              print(bar)
            elseif a and
                b then
              print(bar)
            end
            ''')
        expected = textwrap.dedent('''\
            if foo and
                    bar or
                    (1 + 2) then
              print(bar)
            elseif a and
                    b then
              print(bar)
            end
            ''')

        options = indenter.IndentOptions()
        options.if_cont_line_level = 2
        formatted = indenter.IndentRule(options).apply(src)
        self.assertEqual(formatted, src)

        options.if_cont_line_level = 4
        formatted = indenter.IndentRule(options).apply(src)
        self.assertEqual(formatted, expected)
Example #2
0
    def test_comment_options(self):
        src = '''\
            local foo --a comment
               --- a second comment
            -----a third comment
            -- a valid comment
            '''
        expected_1 = textwrap.dedent('''\
            local foo -- a comment
            --- a second comment
            ----- a third comment
            -- a valid comment
            ''')
        expected_3 = textwrap.dedent('''\
            local foo --   a comment
            ---   a second comment
            -----   a third comment
            --   a valid comment
            ''')

        options = indenter.IndentOptions()
        options.check_space_before_line_comment_text = True
        options.space_before_line_comment_text = 1
        formatted = indenter.IndentRule(options).apply(src)
        self.assertEqual(formatted, expected_1)

        options.space_before_line_comment_text = 3
        formatted = indenter.IndentRule(options).apply(src)
        self.assertEqual(formatted, expected_3)
Example #3
0
    def test_break_if_statement_option(self):
        src = textwrap.dedent('''\
            if foo == nil then print(bar) elseif toto then else print(bar) end
            if log then log:notice("done") else return failure() end
            ''')
        expected = textwrap.dedent('''\
            if foo == nil then
              print(bar)
            elseif toto then
            else
              print(bar)
            end
            if log then
              log:notice("done")
            else
              return failure()
            end
            ''')

        options = indenter.IndentOptions()
        options.break_if_statement = False
        formatted = indenter.IndentRule(options).apply(src)
        self.assertEqual(formatted, src)

        options.break_if_statement = True
        formatted = indenter.IndentRule(options).apply(src)
        print(formatted)
        self.assertEqual(formatted, expected)
Example #4
0
    def test_check_field_list_option(self):
        src = textwrap.dedent('''\
            a = {1,2,    3    ,    4 , "foo"}
            foo {
              foo   = 42,
              bar   = 53,
              1,2,3   ,  4
            }
            ''')
        expected = textwrap.dedent('''\
            a = {1, 2, 3, 4, "foo"}
            foo {
              foo   = 42,
              bar   = 53,
              1, 2, 3, 4
            }
            ''')

        options = indenter.IndentOptions()
        options.check_field_list = False
        formatted = indenter.IndentRule(options).apply(src)
        print(formatted)
        self.assertEqual(formatted, src)

        options.check_field_list = True
        formatted = indenter.IndentRule(options).apply(src)
        print(formatted)
        self.assertEqual(formatted, expected)
Example #5
0
    def setupTest(self, filePrefix, options=indenter.IndentOptions()):
        options.check_param_list = True
        options.if_cont_line_level = 2

        self.maxDiff = None
        raw, exp = '', ''
        with open(currdir + '/test_sources/' + filePrefix + '_raw.lua', 'r') as content_file:
            raw = content_file.read()
        with open(currdir + '/test_sources/' + filePrefix + '_exp.lua', 'r') as content_file:
            exp = content_file.read()
        formatted = indenter.IndentRule(options).apply(raw)
        print(formatted)
        self.assertEqual(formatted, exp)
        self.assertTrue(check_lua_bytecode(raw, formatted))
Example #6
0
    def test_space_around_assign_option(self):
        src = '''\
            local foo   =bar
            foo=    bar
            foo    =    {...}
            '''
        expected = textwrap.dedent('''\
            local foo = bar
            foo = bar
            foo = {...}
            ''')

        options = indenter.IndentOptions()
        options.space_around_assign = True
        formatted = indenter.IndentRule(options).apply(src)
        self.assertEqual(formatted, expected)
Example #7
0
    def test_func_cont_line_level_option(self):
        src = '''\
            function foo(a, b, 
            c, d)
            print(bar)
            end
            '''
        expected = textwrap.dedent('''\
            function foo(a, b, 
                  c, d)
              print(bar)
            end
            ''')

        options = indenter.IndentOptions()
        options.func_cont_line_level = 3
        formatted = indenter.IndentRule(options).apply(src)
        self.assertEqual(formatted, expected)
Example #8
0
    def test_indent_size_option(self):
        src = textwrap.dedent('''\
            do
              local a
            end
            ''')
        expected = textwrap.dedent('''\
            do
                local a
            end
            ''')

        options = indenter.IndentOptions()
        options.indent_size = 2
        formatted = indenter.IndentRule(options).apply(src)
        self.assertEqual(formatted, src)

        options.indent_size = 4
        formatted = indenter.IndentRule(options).apply(src)
        self.assertEqual(formatted, expected)
Example #9
0
    def test_check_param_list_option(self):
        src = textwrap.dedent('''\
            local a  ,b  ,  c = 1,(2 + 3)   ,   foo(3)
            foo(a  ,  b,c,  d, e)
            function bar(a,b,    c,     d) end
            ''')
        expected = textwrap.dedent('''\
            local a, b, c = 1, (2 + 3), foo(3)
            foo(a, b, c, d, e)
            function bar(a, b, c, d) end
            ''')

        options = indenter.IndentOptions()
        options.check_param_list = False
        formatted = indenter.IndentRule(options).apply(src)
        self.assertEqual(formatted, src)

        options.check_param_list = True
        formatted = indenter.IndentRule(options).apply(src)
        self.assertEqual(formatted, expected)
Example #10
0
    def test_skip_semi_colon_option(self):
        src = textwrap.dedent('''\
            print(a); print(b);
            ;
            local a = 42;;
            return 0;
            ''')
        expected = textwrap.dedent('''\
            print(a) print(b)
            local a = 42
            return 0
            ''')

        options = indenter.IndentOptions()
        options.skip_semi_colon = False
        formatted = indenter.IndentRule(options).apply(src)
        self.assertEqual(formatted, src)

        options.skip_semi_colon = True
        formatted = indenter.IndentRule(options).apply(src)
        self.assertEqual(formatted, expected)
Example #11
0
    def test_disable_table_with_comment(self):
        src = textwrap.dedent('''\
            local t = {
              -- @luastyle.disable
              1,    2,  4,
              8,    16, 32
            }
            ''')
        expected = textwrap.dedent('''\
            local t = {
              -- @luastyle.disable
              1,    2,  4,
              8,    16, 32
            }
            ''')

        options = indenter.IndentOptions()
        options.check_field_list = True
        formatted = indenter.IndentRule(options).apply(src)
        print(formatted)
        self.assertEqual(formatted, expected)
Example #12
0
 def test_if_else(self):
     options = indenter.IndentOptions()
     options.space_around_op = True
     self.setupTest('if_else', options)
Example #13
0
 def test_table(self):
     options = indenter.IndentOptions()
     options.check_field_list = True
     self.setupTest('table', options)
Example #14
0
 def test_ambiguous_with_skip_semi_colon_option(self):
     options = indenter.IndentOptions()
     options.skip_semi_colon = True
     self.setupTest('ambiguous', options)
Example #15
0
 def test_break_while(self):
     options = indenter.IndentOptions()
     options.break_while_statement = True
     self.setupTest('break_while', options)
Example #16
0
 def test_break_for(self):
     options = indenter.IndentOptions()
     options.break_for_statement = True
     self.setupTest('break_for', options)
Example #17
0
 def test_func_par(self):
     options = indenter.IndentOptions()
     options.force_func_call_space_checking = True
     options.func_call_space_n = 0
     self.setupTest('func_par', options)
Example #18
0
 def test_return_1(self):
     options = indenter.IndentOptions()
     options.space_around_op = True
     self.setupTest('return_1', options)
Example #19
0
 def test_chained_call(self):
     options = indenter.IndentOptions()
     options.space_around_op = True
     options.check_field_list = True
     self.setupTest('chained_call', options)
Example #20
0
 def test_return(self):
     options = indenter.IndentOptions()
     self.setupTest('return', options)
Example #21
0
 def test_full_2(self):
     options = indenter.IndentOptions()
     options.space_around_op = True
     options.check_field_list = True
     self.setupTest('full_2', options)
Example #22
0
 def test_operators_check(self):
     options = indenter.IndentOptions()
     options.space_around_op = True
     self.setupTest('operators_check', options)
Example #23
0
 def test_comments(self):
     options = indenter.IndentOptions()
     options.space_around_op = True
     options.check_field_list = True
     self.setupTest('comments', options)
Example #24
0
 def test_space_comma(self):
     options = indenter.IndentOptions()
     options.check_field_list = True
     self.setupTest('space_comma', options)