def test_get_content_between_second_to_last_and_last_item_with_only_one_item( self): given_ast = ast.CMakeStringList([ast.ListItemString("file1.cpp")]) given_ast_empty = ast.CMakeStringList([]) self.assertRaises( whitespace_inserter.LessThanTwoItemsException, whitespace_inserter. get_content_between_second_to_last_and_last_item, "", given_ast) self.assertRaises( whitespace_inserter.LessThanTwoItemsException, whitespace_inserter. get_content_between_second_to_last_and_last_item, "", given_ast_empty)
def test_parse_quoted_list_item_containing_multiple_items(self): givenAst = ast.Ast() result = givenAst.parse("set(TabsPls_Source " \ + "\"anything can be put here, and it will be only one item, even with a ${variable}\"\n" + "main.cpp\n" \ + "${other_var}\n" \ + "\"${another_var_in_quotes}\"\n" \ + "#a comment that will be ignored\n" \ + '"a complete line with only text"\n' \ + ")" ) expected_result = ast.SetNormalVariable( "TabsPls_Source", ast.CMakeStringList([ ast.ListItemString( "\"anything can be put here, and it will be only one item, even with a ${variable}\"" ), ast.ListItemString("main.cpp"), ast.VariableUse("other_var"), ast.VariableUse("another_var_in_quotes"), ast.ListItemString('"a complete line with only text"') ])) self.assertTrue(list(result)[0].is_same(expected_result))
def test_parse_set_normal_variable(self): givenAst = ast.Ast() result = givenAst.parse("set(TabsPls_Source main.cpp)") expected_result = ast.SetNormalVariable( "TabsPls_Source", ast.CMakeStringList([ast.ListItemString("main.cpp")])) self.assertTrue(list(result)[0].is_same(expected_result))
def test_parse_set_normal_variable_using_other_variable_in_quotes(self): givenAst = ast.Ast() result = givenAst.parse("set(TabsPls_Source \"${other_var}\")") expected_result = ast.SetNormalVariable( "TabsPls_Source", ast.CMakeStringList([ast.VariableUse("other_var")])) self.assertTrue(list(result)[0].is_same(expected_result))
def test_scan_all_complete_project_source(self): given_source = ( "project(TabsPls)\n" + "\tset(TabsPls_Headers File.hpp\n" + "Directory.hpp\n" + ")\n\n" + "set(TabsPls_Sources File.cpp\n" + "\tDirectory.cpp\n" + "\tMain.cpp\n" + ")\n\n" + "add_executable(TabsPls ${TabsPls_Headers} ${TabsPls_Sources})\n\n" + "target_sources(TabsPls PRIVATE windows_util.h windows_util.c)") given_ast = ast.Ast() matches = given_ast.scan_all(given_source) self.assertEqual(len(matches), 4) expected_first_match = ast.SetNormalVariable( "TabsPls_Headers", ast.CMakeStringList([ ast.ListItemStringWithLocation("File.hpp", 38), ast.ListItemStringWithLocation("Directory.hpp", 47) ])) self.assertTrue(list(matches[0])[0].is_same(expected_first_match)) expected_second_match = ast.SetNormalVariable( "TabsPls_Sources", ast.CMakeStringList([ ast.ListItemStringWithLocation("File.cpp", 84), ast.ListItemStringWithLocation("Directory.cpp", 94), ast.ListItemStringWithLocation("Main.cpp", 109) ])) self.assertTrue(list(matches[1])[0].is_same(expected_second_match)) expected_third_match = ast.AddExecutable( "TabsPls", ast.CMakeStringList([ ast.VariableUseWithLocation("TabsPls_Headers", 144), ast.VariableUseWithLocation("TabsPls_Sources", 163) ])) self.assertTrue(list(matches[2])[0].is_same(expected_third_match)) expected_fourth_match = ast.TargetSources( "TabsPls", ast.CMakeStringList([ ast.ListItemStringWithLocation("windows_util.h", 215), ast.ListItemStringWithLocation("windows_util.c", 230) ])) self.assertTrue(list(matches[3])[0].is_same(expected_fourth_match))
def test_get_position_after_last_list_item_string(self): #This is based entirely on the last item's location given_cmake_string_list = ast.CMakeStringList([ ast.ListItemStringWithLocation("item1.cpp", -1), ast.VariableUseWithLocation("Sources", -1), ast.ListItemStringWithLocation("item2", 14) ]) self.assertEqual( source_inserter._get_position_after_last_list_item( given_cmake_string_list), 19)
def test_get_position_after_last_variable_use_list_item(self): given_cmake_string_list = ast.CMakeStringList([ ast.ListItemString("item1.cpp"), ast.VariableUse("Sources"), ast.VariableUseWithStartAndEndLocation( "Headers", 14, ast.VariableUseTerminator(22)) ]) self.assertEqual( source_inserter._get_position_after_last_list_item( given_cmake_string_list), 22)
def test_get_content_between_second_to_last_and_last_item(self): given_ast = ast.CMakeStringList([ ast.ListItemStringWithLocation("file1.cpp", 10), ast.ListItemStringWithLocation("file2.cpp", 20) ]) given_source = "abcdefghijklmnopqrstuvwxyz" result_content = whitespace_inserter.get_content_between_second_to_last_and_last_item( given_source, given_ast) self.assertEqual(result_content, "t")
def test_parse_add_library(self): givenAst = ast.Ast() result = givenAst.parse("add_library(TabsPlsLib file.h file.cpp)") expected_result = ast.AddLibrary( "TabsPlsLib", ast.CMakeStringList( [ast.ListItemString("file.h"), ast.ListItemString("file.cpp")])) self.assertTrue(list(result)[0].is_same(expected_result))
def test_parse_add_library_with_type_and_flag(self): givenAst = ast.Ast() result = givenAst.parse( "add_library(TabsPlsLib STATIC EXCLUDE_FROM_ALL file.h file.cpp)") expected_result = ast.AddLibrary( "TabsPlsLib", ast.CMakeStringList( [ast.ListItemString("file.h"), ast.ListItemString("file.cpp")])) self.assertTrue(list(result)[0].is_same(expected_result))
def test_parse_quoted_list_item_containing_variable_use(self): givenAst = ast.Ast() result = givenAst.parse( "set(TabsPls_Source \"anything can be put here, and it will be only one item, even with a ${variable}\")" ) expected_result = ast.SetNormalVariable( "TabsPls_Source", ast.CMakeStringList([ ast.ListItemString( "\"anything can be put here, and it will be only one item, even with a ${variable}\"" ) ])) self.assertTrue(list(result)[0].is_same(expected_result))
def test_parse_add_executable_exclude_from_all_flag(self): givenAst = ast.Ast() result = givenAst.parse( "add_executable(TabsPls MACOSX_BUNDLE EXCLUDE_FROM_ALL ${TabsPls_Headers} ${TabsPls_Sources} main.cpp)" ) expected_result = ast.AddExecutable( "TabsPls", ast.CMakeStringList([ ast.VariableUse("TabsPls_Headers"), ast.VariableUse("TabsPls_Sources"), ast.ListItemString("main.cpp") ])) self.assertTrue(list(result)[0].is_same(expected_result))
def test_parse_add_executable_win32_flag(self): givenAst = ast.Ast() result = givenAst.parse( "add_executable(TabsPls WIN32 ${TabsPls_Headers} ${TabsPls_Sources} main.cpp)" ) expected_result = ast.AddExecutable( "TabsPls", ast.CMakeStringList([ ast.VariableUse("TabsPls_Headers"), ast.VariableUse("TabsPls_Sources"), ast.ListItemString("main.cpp") ])) self.assertTrue(list(result)[0].is_same(expected_result))
def test_insert_source_considering_existing_whitespace(self): given_inserter = namedtuple("FakeInserter", ["get_cmake_list_ast", "insert_source"]) given_inserter.get_cmake_list_ast = lambda: ast.CMakeStringList([ ast.ListItemStringWithLocation("file1.cpp", 0), ast.ListItemStringWithLocation("file2.cpp", 11) ]) given_inserter.insert_source = lambda source_item: source_inserter.InsertAction( 21, " {}".format(source_item)) given_full_source = "file1.cpp\n\tfile2.cpp" insert_action = source_inserter.insert_source_considering_existing_whitespace( given_inserter, "file3.cpp", given_full_source) self.assertEqual(insert_action.do(given_full_source), "file1.cpp\n\tfile2.cpp\n\tfile3.cpp")
def test_parse_target_sources(self): givenAst = ast.Ast() result = givenAst.parse( "target_sources(TabsPls PRIVATE file_linux.h file_linux.cpp ${TabsPls_Sources_Linux} PUBLIC linux_extras.h)" ) expected_result = ast.TargetSources( "TabsPls", ast.CMakeStringList([ ast.ListItemStringWithLocation("file_linux.h", 31), ast.ListItemStringWithLocation("file_linux.cpp", 44), ast.VariableUseWithLocation("TabsPls_Sources_Linux", 59), ast.ListItemStringWithLocation("linux_extras.h", 91) ])) self.assertTrue(list(result)[0].is_same(expected_result))