コード例 #1
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)
コード例 #2
0
    def test_invocation_when_keyword_and_single_values_keep_in_single_line(
            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(unquoted_argument('file.hpp')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('DESTINATION')) \
            .add(spaces('    ')) \
            .add(quoted_argument('include/folder')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('NAMESPACE')) \
            .add(spaces('    ')) \
            .add(unquoted_argument('unofficial::graphicsmagick::')) \
            .add(newlines(1))

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

        expected_formatting = """install(
  FILES
    file.cpp
    file.hpp
  DESTINATION "include/folder"
  NAMESPACE unofficial::graphicsmagick::
)"""
        self.assertFormatting(expected_formatting, root)
コード例 #3
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)
コード例 #4
0
    def test_escape_sequence_in_quoted_argument(self):
        command = 'string("\\\\")'

        expected_args = arguments().add(quoted_argument('\\\\'))
        expected_parsed_structure = file().add(command_invocation('string(', expected_args))

        self.assertReprEqual(expected_parsed_structure, self.parser.parse(command))
コード例 #5
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)
コード例 #6
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)
コード例 #7
0
    def test_with_unquoted_arguments_in_braces(self):
        start_invocation = 'include('
        expected_args = arguments().add(
            parentheses(arguments().add(unquoted_argument('some')))
        )
        root = file().add(command_invocation(start_invocation, expected_args))

        self.assertReprEqual(root, self.parser.parse(start_invocation + '(some))'))
コード例 #8
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)
コード例 #9
0
    def test_with_quoted_argument_with_escaped_quote_inside(self):
        start_invocation = 'name('
        argument_content = 'simple\n\\\" text'
        root = file().add(
            command_invocation(start_invocation,
                               arguments().add(quoted_argument(argument_content)))
        )

        self.assertReprEqual(root, self.parser.parse(
            f'{start_invocation}"{argument_content}")'))
コード例 #10
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)
コード例 #11
0
    def test_with_bracket_argument(self):
        start_invocation = 'function_name('
        bracket_start = '[['
        bracket_end = ']]'
        bracket_argument_data = 'this is bracket_dwad832423#$@#$ content]===] still there'

        root = file().add(command_invocation(start_invocation,
                                             arguments().add(bracket_argument(0, bracket_argument_data))))

        self.assertReprEqual(root, self.parser.parse(
            f'{start_invocation}{bracket_start}{bracket_argument_data}{bracket_end})'))
コード例 #12
0
    def test_invocation_splitting_with_closing_parentheses_in_newline(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('def'))
        root = file().add(command_invocation('a_very_long_name(', args))

        expected_formatting = """a_very_long_name(abc
  def
)"""
        self.assertFormatting(expected_formatting, root)
コード例 #13
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)
コード例 #14
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)
コード例 #15
0
    def test_if_statement_with_space_while_other_invocations_are_not_affected(
            self):
        self.settings['space_after_loop_condition'] = True
        invocation = file() \
            .add(command_invocation('if(')) \
            .add(newlines(1)) \
            .add(command_invocation('abc(')) \
            .add(newlines(1)) \
            .add(command_invocation('endif('))

        expected_formatting = """if ()
  abc()
endif()"""

        self.assertFormatting(expected_formatting, invocation)
コード例 #16
0
    def test_multiple_line_comments_at_the_start_of_invocation(self):
        args = arguments().add(newlines(1)) \
            .add(line_ending('# first line', 1)) \
            .add(line_ending('# second line', 1)) \
            .add(unquoted_argument('TARGET')) \
            .add(newlines(1))

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

        expected_formatting = """add_custom_target(
  # first line
  # second line
  TARGET
)"""
        self.assertFormatting(expected_formatting, root)
コード例 #17
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)
コード例 #18
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)
コード例 #19
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))
コード例 #20
0
    def test_function_declaration_should_indent_correctly_within_its_scope(
            self):
        function_with_invocation_in_second_line = file() \
            .add(command_invocation('function(')) \
            .add(newlines(1)) \
            .add(line_ending('# comment', 1)) \
            .add(command_invocation('test(')) \
            .add(newlines(1)) \
            .add(command_invocation('endfunction(')) \
            .add(newlines(1)) \
            .add(command_invocation('test2('))
        expected_formatting = """function()
  # comment
  test()
endfunction()
test2()"""
        self.assertFormatting(expected_formatting,
                              function_with_invocation_in_second_line)
コード例 #21
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)
コード例 #22
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)
コード例 #23
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))
コード例 #24
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)
コード例 #25
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)
コード例 #26
0
    def test_uppercase_if_statement_handled_correctly_like_lowercase(self):
        self.settings['space_after_loop_condition'] = True
        self.settings['force_command_lowercase'] = False

        invocation = file() \
            .add(command_invocation('IF(')) \
            .add(newlines(1)) \
            .add(command_invocation('abc(')) \
            .add(newlines(1)) \
            .add(command_invocation('ELSEIF (')) \
            .add(newlines(1)) \
            .add(command_invocation('def(')) \
            .add(newlines(1)) \
            .add(command_invocation('ENDIF('))

        expected_formatting = """IF ()
  abc()
ELSEIF ()
  def()
ENDIF()"""

        self.assertFormatting(expected_formatting, invocation)
コード例 #27
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)
コード例 #28
0
    def test_invocation_wrapping_only_when_line_length_is_smaller_than_set_threshold(
            self):
        self.settings['wrap_short_invocations_to_single_line'] = True
        self.settings['line_length'] = 15

        args = arguments() \
            .add(newlines(1)) \
            .add(unquoted_argument('abc')) \
            .add(newlines(1)) \
            .add(unquoted_argument('def'))
        wrappable_invocation = command_invocation('wr(', args)
        not_wrappable_invocation = command_invocation(
            'a_very_long_name_command(', args)
        root = file().add(wrappable_invocation) \
            .add(newlines(1)) \
            .add(not_wrappable_invocation)

        expected_formatting = """wr(abc def)
a_very_long_name_command(
  abc
  def)"""

        self.assertFormatting(expected_formatting, root)
コード例 #29
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)
コード例 #30
0
 def test_command_invocation_should_be_by_default_lowercase(self):
     invocation = file() \
         .add(command_invocation('abc(')) \
         .add(spaces('   \t')) \
         .add(line_ending('# a comment', 1))
     self.assertFormatting('abc() # a comment\n', invocation)