Exemplo n.º 1
0
 def test_no_spaces_after_opening_and_before_closing_bracket(self):
     function_arguments = arguments() \
         .add(spaces('    ')) \
         .add(unquoted_argument('NAME')) \
         .add(spaces(' '))
     expected_formatting = 'abc(NAME)'
     self.assertFormattingArguments(expected_formatting, function_arguments)
Exemplo n.º 2
0
    def test_already_aligned_invocation(self):
        args = arguments() \
            .add(unquoted_argument('abc')).add(spaces(' ')) \
            .add(unquoted_argument('OR')).add(spaces(' ')) \
            .add(unquoted_argument('def'))

        self.assertConditionFormatting('if(abc OR def)', args)
Exemplo n.º 3
0
    def test_set_property_with_multiple_values(self):
        self.settings['keyword_and_single_value_in_one_line'] = True
        self.settings['keep_property_and_value_in_one_line'] = True
        self.settings['line_length'] = 5

        args = arguments().add(newlines(1)) \
            .add(unquoted_argument('TARGET')) \
            .add(newlines(1)) \
            .add(unquoted_argument('GTest::GTest')) \
            .add(newlines(1)) \
            .add(unquoted_argument('PROPERTY')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('INTERFACE_LINK_LIBRARIES')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('GTest::gmock')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('GTest::gtest')) \
            .add(newlines(1))

        root = file().add(command_invocation('set_property(', args))

        expected_formatting = """set_property(
  TARGET GTest::GTest
  PROPERTY
    INTERFACE_LINK_LIBRARIES
      GTest::gmock
      GTest::gtest
)"""
        self.assertFormatting(expected_formatting, root)
Exemplo n.º 4
0
    def test_trimming_spaces_between_arguments(self):
        text = 'a text with \t tab    and multiple spaces'
        function_arguments = arguments() \
            .add(spaces('    ')) \
            .add(unquoted_argument('NAME')) \
            .add(spaces('    ')) \
            .add(quoted_argument(text)) \
            .add(spaces(' '))

        expected_formatting = f'abc(NAME \"{text}\")'
        self.assertFormattingArguments(expected_formatting, function_arguments)
Exemplo n.º 5
0
    def test_splitting_before_logical_operator(self):
        self.settings['condition_splitting_move_and_or_to_newline'] = True
        self.settings['line_length'] = 10

        args = arguments() \
            .add(unquoted_argument('VERY_LONG_THING')).add(spaces(' ')) \
            .add(unquoted_argument('OR')).add(spaces(' ')) \
            .add(unquoted_argument('CMAKE_CXX_COMPILER_ID'))

        self.assertConditionFormatting(
            'if(VERY_LONG_THING\n    OR CMAKE_CXX_COMPILER_ID)', args)
Exemplo n.º 6
0
    def test_indentation_of_arguments_in_newlines(self):
        function_arguments = arguments() \
            .add(newlines(4)) \
            .add(spaces('    ')) \
            .add(unquoted_argument('NAMING')) \
            .add(newlines(4)) \
            .add(spaces('    ')) \
            .add(quoted_argument('XYZ')) \
            .add(spaces(' '))

        expected_formatting = f'abc(\n  NAMING\n  \"XYZ\")'
        self.assertFormattingArguments(expected_formatting, function_arguments)
Exemplo n.º 7
0
    def test_empty_spaces_at_end_of_line(self):
        self.settings['line_length'] = 10
        self.settings['keyword_and_single_value_in_one_line'] = True

        args = arguments().add(unquoted_argument('abc')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('TARGET')) \
            .add(spaces('    ')) \
            .add(newlines(1)) \
            .add(unquoted_argument('${PROJECT_NAME}')) \
            .add(newlines(1))

        root = file().add(command_invocation('add_custom_target(', args))
        self.assertFormatting('add_custom_target(abc\n  TARGET ${PROJECT_NAME}\n)', root)
Exemplo n.º 8
0
    def test_already_split_condition_should_have_correct_indent(self):
        args = arguments() \
            .add(unquoted_argument('CMAKE_C_COMPILER_ID')).add(spaces(' ')) \
            .add(unquoted_argument('STREQUAL')).add(spaces(' ')) \
            .add(quoted_argument('GNU')).add(spaces(' ')) \
            .add(unquoted_argument('AND')) \
            .add(newlines(1)) \
            .add(unquoted_argument('CMAKE_CXX_COMPILER_ID')).add(spaces(' ')) \
            .add(unquoted_argument('STREQUAL')).add(spaces(' ')) \
            .add(quoted_argument('GNU'))

        expected_formatting = """if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND
    CMAKE_CXX_COMPILER_ID STREQUAL "GNU")"""
        self.assertConditionFormatting(expected_formatting, args)
Exemplo n.º 9
0
    def test_invocation_wrapping_for_short_function(self):
        self.settings['wrap_short_invocations_to_single_line'] = True
        args = arguments() \
            .add(newlines(4)) \
            .add(spaces('    ')) \
            .add(unquoted_argument('argument1')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('argument2')) \
            .add(newlines(4))
        root = file().add(command_invocation('function_call(', args))

        expected_formatting = """function_call(argument1 argument2)"""

        self.assertFormatting(expected_formatting, root)
Exemplo n.º 10
0
    def test_split_with_parentheses(self):
        condition = parentheses(
            arguments().add(unquoted_argument('${CMAKE_CXX_COMPILER_ID}')).add(
                spaces(' ')).add(unquoted_argument('STREQUAL')).add(
                    spaces(' ')).add(quoted_argument('GNU')).add(newlines(5)).
            add(unquoted_argument('AND')).add(newlines(1)).add(
                unquoted_argument('${CMAKE_CXX_COMPILER_VERSION}')).add(
                    spaces(' ')).add(unquoted_argument('VERSION_LESS')).add(
                        spaces(' ')).add(quoted_argument('9')))

        expected_formatting = """if((${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU"
      AND ${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS "9"))"""

        self.assertConditionFormatting(expected_formatting,
                                       arguments().add(condition))
Exemplo n.º 11
0
    def test_invocation_with_whitespaces_before_line_end(self):
        invocation_lead_by_spaces = file() \
            .add(command_invocation('set(')) \
            .add(spaces('   ')) \
            .add(newlines(1))

        self.assertFormatting('set()\n', invocation_lead_by_spaces)
Exemplo n.º 12
0
    def test_invocation_when_keyword_and_single_values_keep_in_single_line_comments_case_at_the_end(
            self):
        self.settings['keyword_and_single_value_in_one_line'] = True
        args = arguments().add(newlines(1)) \
            .add(unquoted_argument('FILES')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('file.cpp')) \
            .add(spaces('    ')) \
            .add(line_ending('# comment', 1))

        root = file().add(command_invocation('install(', args))

        expected_formatting = """install(
  FILES file.cpp # comment
)"""
        self.assertFormatting(expected_formatting, root)
Exemplo n.º 13
0
    def test_invocation_splitting_with_line_comments(self):
        self.settings['line_length'] = 5
        args = arguments().add(unquoted_argument('abc')) \
            .add(spaces('    ')) \
            .add(line_ending('# comment', 1)) \
            .add(unquoted_argument('TARGET')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('def'))

        root = file().add(command_invocation('function(', args))

        expected_formatting = """function(abc # comment
  TARGET
    def)"""

        self.assertFormatting(expected_formatting, root)
Exemplo n.º 14
0
    def test_invocation_splitting_with_closing_parentheses_in_newline_and_keyword(
            self):
        self.settings['line_length'] = 15
        self.settings['closing_parentheses_in_newline_when_split'] = True
        args = arguments().add(unquoted_argument('abc')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('TARGET')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('def'))
        root = file().add(command_invocation('a_very_long_name(', args))

        expected_formatting = """a_very_long_name(abc
  TARGET
    def
)"""
        self.assertFormatting(expected_formatting, root)
Exemplo n.º 15
0
    def test_command_with_line_comment(self):
        command = """add_test(
    NAME # a name
    CONFIGURATIONS)"""

        expected_args = arguments() \
            .add(newlines(1)) \
            .add(spaces('    ')) \
            .add(unquoted_argument('NAME')) \
            .add(spaces(' ')) \
            .add(line_ending('# a name', 1)) \
            .add(spaces('    ')) \
            .add(unquoted_argument('CONFIGURATIONS'))

        expected_parsed_structure = file().add(command_invocation('add_test(', expected_args))

        self.assertReprEqual(expected_parsed_structure, self.parser.parse(command))
Exemplo n.º 16
0
    def test_indentation_should_not_apply_to_content_of_bracket_argument_endif_should_be_indented(self):
        function_arguments = arguments() \
            .add(newlines(4)) \
            .add(spaces('    ')) \
            .add(bracket_argument(2, 'text\n  endif(\nother'))

        expected_formatting = f'abc(\n[==[text\n  endif(\nother]==])'
        self.assertFormattingArguments(expected_formatting, function_arguments)
Exemplo n.º 17
0
    def test_invocation_splitting_when_line_length_exceeded(self):
        self.settings['line_length'] = 15
        args = arguments().add(unquoted_argument('abc')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('def'))
        root = file().add(command_invocation('a_very_long_name(', args))

        expected_formatting = """a_very_long_name(abc
  def)"""
        self.assertFormatting(expected_formatting, root)
Exemplo n.º 18
0
    def test_real_add_test_command_example(self):
        command = """add_test(
    NAME dbg-${TARGET}-fast
    CONFIGURATIONS Debug
    COMMAND ${Runner_BINARY_DEBUG} $<TARGET_FILE:${TARGET}>
        ("${DATA_PATH_OPTION}"
        [===[--text]===])
    )"""

        expected_arguments_in_parentheses = parentheses(arguments()
                                                        .add(quoted_argument('${DATA_PATH_OPTION}'))
                                                        .add(newlines(1))
                                                        .add(spaces('        '))
                                                        .add(bracket_argument(3, '--text')))

        expected_args = arguments() \
            .add(newlines(1)) \
            .add(spaces('    ')) \
            .add(unquoted_argument('NAME')) \
            .add(spaces(' ')) \
            .add(unquoted_argument('dbg-${TARGET}-fast')) \
            .add(newlines(1)) \
            .add(spaces('    ')) \
            .add(unquoted_argument('CONFIGURATIONS')) \
            .add(spaces(' ')) \
            .add(unquoted_argument('Debug')) \
            .add(newlines(1)) \
            .add(spaces('    ')) \
            .add(unquoted_argument('COMMAND')) \
            .add(spaces(' ')) \
            .add(unquoted_argument('${Runner_BINARY_DEBUG}')) \
            .add(spaces(' ')) \
            .add(unquoted_argument('$<TARGET_FILE:${TARGET}>')) \
            .add(newlines(1)) \
            .add(spaces('        ')) \
            .add(expected_arguments_in_parentheses) \
            .add(newlines(1)) \
            .add(spaces('    '))
        expected_invocation = command_invocation('add_test(', expected_args)
        expected_parsed_structure = file().add(expected_invocation)

        self.assertReprEqual(expected_parsed_structure, self.parser.parse(command))
Exemplo n.º 19
0
    def test_invocation_when_keyword_is_first_argument_move_to_newline(self):
        self.settings['keyword_and_single_value_in_one_line'] = True
        self.settings['line_length'] = 5
        args = arguments() \
            .add(unquoted_argument('FILES')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('file.cpp')) \
            .add(newlines(1))

        root = file().add(command_invocation('install(', args))
        self.assertFormatting('install(\n  FILES file.cpp\n)', root)
Exemplo n.º 20
0
    def test_splitting_already_split_invocation_after_and(self):
        self.settings['condition_splitting_move_and_or_to_newline'] = False
        self.settings['line_length'] = 10

        args = arguments() \
            .add(unquoted_argument('VERY_LONG_THING')).add(newlines(1)) \
            .add(unquoted_argument('AND')).add(spaces(' ')) \
            .add(unquoted_argument('CMAKE_CXX_COMPILER_ID'))

        self.assertConditionFormatting(
            'if(VERY_LONG_THING AND\n    CMAKE_CXX_COMPILER_ID)', args)
Exemplo n.º 21
0
    def test_special_handling_of_set_property_with_keeping_in_single_line(
            self):
        self.settings['keyword_and_single_value_in_one_line'] = True
        self.settings['line_length'] = 5
        args = arguments().add(newlines(1)) \
            .add(unquoted_argument('GLOBAL')) \
            .add(newlines(1)) \
            .add(unquoted_argument('PROPERTY')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('CONFIGURATION_MSVC_RELEASE_SECURE_PREPARE')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('FALSE')) \
            .add(newlines(1))

        root = file().add(command_invocation('set_property(', args))

        expected_formatting = """set_property(
  GLOBAL PROPERTY
    CONFIGURATION_MSVC_RELEASE_SECURE_PREPARE FALSE
)"""
        self.assertFormatting(expected_formatting, root)
Exemplo n.º 22
0
    def test_invocation_when_keyword_and_single_values_keep_in_single_line_comments_case_in_the_middle(
            self):
        self.settings['keyword_and_single_value_in_one_line'] = True
        args = arguments().add(newlines(1)) \
            .add(unquoted_argument('DESTINATION')) \
            .add(spaces('    ')) \
            .add(quoted_argument('include/folder')) \
            .add(spaces('    ')) \
            .add(line_ending('# comment', 1)) \
            .add(unquoted_argument('NAMESPACE')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('unofficial::graphicsmagick::')) \
            .add(newlines(1))

        root = file().add(command_invocation('install(', args))

        expected_formatting = """install(
  DESTINATION "include/folder" # comment
  NAMESPACE unofficial::graphicsmagick::
)"""
        self.assertFormatting(expected_formatting, root)
Exemplo n.º 23
0
    def test_special_handling_of_get_property(self):
        self.settings['keyword_and_single_value_in_one_line'] = True
        self.settings['line_length'] = 5
        args = arguments().add(unquoted_argument('dirs')) \
            .add(newlines(1)) \
            .add(unquoted_argument('DIRECTORY')) \
            .add(spaces('    ')) \
            .add(quoted_argument('${CMAKE_CURRENT_SOURCE_DIR}')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('PROPERTY')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('SUBDIRECTORIES')) \
            .add(newlines(1))

        root = file().add(command_invocation('get_property(', args))

        expected_formatting = """get_property(dirs
  DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  PROPERTY SUBDIRECTORIES
)"""
        self.assertFormatting(expected_formatting, root)
Exemplo n.º 24
0
    def test_splitting_while_properties_keep_same_line(self):
        self.settings['keep_property_and_value_in_one_line'] = True
        self.settings['line_length'] = 10
        self.settings['keywords'] = ['BAR', 'TARGET', 'FOO']

        args = arguments().add(unquoted_argument('TARGET')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('abcd')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('PROPERTIES')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('FOO')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('def')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('BAR')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('def2'))

        root = file().add(command_invocation('set_property(', args))

        expected_formatting = """set_property(
  TARGET abcd
  PROPERTIES
    FOO def
    BAR def2)"""
        self.assertFormatting(expected_formatting, root)
Exemplo n.º 25
0
    def test_splitting_long_line_with_multiple_arguments_and_properties(self):
        self.settings['line_length'] = 30
        self.settings['closing_parentheses_in_newline_when_split'] = True

        args = arguments().add(unquoted_argument('abcd')) \
            .add(spaces('    ')) \
            .add(line_ending('# comment', 1)) \
            .add(unquoted_argument('some_target')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('PROPERTIES')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('TARGET')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('def')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('TARGET')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('def'))

        root = file().add(command_invocation('some_name(', args))

        expected_formatting = """some_name(abcd # comment
  some_target
  PROPERTIES
    TARGET
      def
    TARGET
      def
)"""
        self.assertFormatting(expected_formatting, root)
Exemplo n.º 26
0
    def test_splitting_custom_target_command(self):
        self.settings['line_length'] = 10
        self.settings['keep_command_in_single_line'] = True

        args = arguments().add(unquoted_argument('${target}-resources')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('COMMAND')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('${CMAKE_COMMAND}')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('-E')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('echo')) \
            .add(spaces('    ')) \
            .add(quoted_argument('Copy resource files for ${target}'))

        root = file().add(command_invocation('add_custom_target(', args))

        expected_formatting = """add_custom_target(${target}-resources
  COMMAND
    ${CMAKE_COMMAND} -E echo "Copy resource files for ${target}")"""

        self.assertFormatting(expected_formatting, root)
Exemplo n.º 27
0
    def test_if_statement_should_indent_properly_also_removing_unneeded_spaces(
            self):
        root = file() \
            .add(command_invocation('if (')) \
            .add(newlines(1)) \
            .add(spaces('         ')) \
            .add(command_invocation('test(')) \
            .add(newlines(1)) \
            .add(spaces('         ')) \
            .add(command_invocation('elseif(')) \
            .add(newlines(1)) \
            .add(spaces('         ')) \
            .add(command_invocation('test(')) \
            .add(newlines(1)) \
            .add(spaces('         ')) \
            .add(command_invocation('endif('))

        expected_formatting = """if()
  test()
elseif()
  test()
endif()"""

        self.assertFormatting(expected_formatting, root)
Exemplo n.º 28
0
    def test_invocation_when_double_keyword_occurs_should_keep_it_in_one_line(
            self):
        self.settings['keyword_and_single_value_in_one_line'] = True
        self.settings['line_length'] = 5
        args = arguments().add(newlines(1)) \
            .add(unquoted_argument('ARCHIVE')) \
            .add(newlines(1)) \
            .add(unquoted_argument('DESTINATION')) \
            .add(spaces('    ')) \
            .add(quoted_argument('include/folder')) \
            .add(newlines(1))

        root = file().add(command_invocation('install(', args))

        expected_formatting = """install(
  ARCHIVE DESTINATION "include/folder"
)"""
        self.assertFormatting(expected_formatting, root)
Exemplo n.º 29
0
    def test_multiple_line_comments_before_value(self):
        args = arguments().add(unquoted_argument('abc')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('TARGET')) \
            .add(newlines(1)) \
            .add(line_ending('# first line', 1)) \
            .add(line_ending('# second line', 1)) \
            .add(unquoted_argument('${PROJECT_NAME}')) \
            .add(newlines(1))

        root = file().add(command_invocation('add_custom_target(', args))

        expected_formatting = """add_custom_target(abc
  TARGET
    # first line
    # second line
    ${PROJECT_NAME}
)"""
        self.assertFormatting(expected_formatting, root)
Exemplo n.º 30
0
    def test_splitting_only_after_logical_operations(self):
        self.settings['condition_splitting_move_and_or_to_newline'] = False
        self.settings['line_length'] = 10

        args = arguments() \
            .add(unquoted_argument('CMAKE_C_COMPILER_ID')).add(spaces(' ')) \
            .add(unquoted_argument('STREQUAL')).add(spaces(' ')) \
            .add(quoted_argument('GNU')).add(spaces(' ')) \
            .add(unquoted_argument('AND')).add(spaces(' ')) \
            .add(unquoted_argument('CMAKE_CXX_COMPILER_ID')).add(spaces(' ')) \
            .add(unquoted_argument('STREQUAL')).add(spaces(' ')) \
            .add(quoted_argument('GNU'))

        expected_formatting = """if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND
    CMAKE_CXX_COMPILER_ID STREQUAL "GNU")"""
        self.assertConditionFormatting(expected_formatting, args)