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))'))
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))
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))
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)
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)
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)
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)
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)
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)
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)
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}")'))
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)
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))
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)
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)
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)
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})'))
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)
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)
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)
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)
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)
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)
def test_ident_target_keyword_in_command(self): function_arguments = arguments() \ .add(newlines(4)) \ .add(unquoted_argument('TARGET')) \ .add(newlines(4)) \ .add(unquoted_argument('text')) \ .add(newlines(4)) \ .add(unquoted_argument('TARGET')) \ .add(newlines(4)) \ .add(bracket_argument(2, 'text')) \ .add(newlines(4)) expected_formatting = 'abc(\n TARGET\n text\n TARGET\n [==[text]==]\n)' self.assertFormattingArguments(expected_formatting, function_arguments)
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)
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)
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)
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)
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))
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)