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_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_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_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_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_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_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_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_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)
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)
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_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_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)
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_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_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_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_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)
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_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_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)
def test_invocation_splitting_with_closing_parentheses_in_newline_and_newline_already_there( self): self.settings['line_length'] = 5 self.settings['closing_parentheses_in_newline_when_split'] = True args = arguments().add(unquoted_argument('abc')) \ .add(spaces(' ')) \ .add(unquoted_argument('TARGET')) \ .add(newlines(1)) root = file().add(command_invocation('a_very_long_name(', args)) expected_formatting = """a_very_long_name(abc TARGET )""" self.assertFormatting(expected_formatting, root)
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_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_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_ident_set_target_properties_example(self): function_arguments = arguments() \ .add(unquoted_argument('target_name')) \ .add(newlines(4)) \ .add(unquoted_argument('PROPERTIES')) \ .add(newlines(4)) \ .add(unquoted_argument('INTERFACE_LINK_DEPENDS')) \ .add(newlines(4)) \ .add(unquoted_argument('${VALUE}')) \ .add(newlines(4)) \ .add(unquoted_argument('JOB_POOL_COMPILE')) \ .add(newlines(4)) \ .add(unquoted_argument('${VALUE2}')) \ .add(newlines(4)) expected_formatting = """abc(target_name PROPERTIES INTERFACE_LINK_DEPENDS ${VALUE} JOB_POOL_COMPILE ${VALUE2} )""" self.assertFormattingArguments(expected_formatting, function_arguments)
def test_return_single_newline(self): self.assertFormatting('\n', file().add(newlines(3)))
def test_return_3_newlines_although_settings_allow_more(self): self.settings['succeeding_newlines'] = 5 self.assertFormatting('\n\n\n', file().add(newlines(3)))
def test_should_parse_correctly_newlines(self): file_with_new_lines = file().add(newlines(2)) self.assertReprEqual(file_with_new_lines, self.parser.parse('\n\n'))