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_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_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")
Пример #4
0
    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))
Пример #5
0
    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))