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_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_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_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_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_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_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_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_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_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_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_multiple_line_comments_at_the_end_of_invocation(self): args = arguments().add(unquoted_argument('abc')) \ .add(newlines(1)) \ .add(unquoted_argument('TARGET')) \ .add(newlines(1)) \ .add(line_ending('# first line', 1)) \ .add(line_ending('# second line', 1)) root = file().add(command_invocation('add_custom_target(', args)) expected_formatting = """add_custom_target(abc TARGET # first line # second line )""" 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_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_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_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_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_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_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_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)
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)
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_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)
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)
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)
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_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_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)
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)