Example #1
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)
Example #2
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))
Example #3
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)
Example #4
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)
Example #5
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))
Example #6
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}")'))
Example #7
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)
Example #8
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)
Example #9
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)
Example #10
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))
Example #11
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)
Example #12
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)
Example #13
0
    def test_with_empty_quoted_argument(self):
        start_invocation = 'include('
        root = file().add(command_invocation(start_invocation, arguments().add(quoted_argument(''))))

        self.assertReprEqual(root, self.parser.parse(start_invocation + '\"\")'))