aboutsummaryrefslogtreecommitdiff
path: root/clang/bindings/python/tests/cindex/test_file.py
blob: 2be9b9e332611a22abc765d7b691b757f5cb9f2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os

from clang.cindex import Config, File, Index, TranslationUnit

if "CLANG_LIBRARY_PATH" in os.environ:
    Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])

import unittest

inputs_dir = os.path.join(os.path.dirname(__file__), "INPUTS")


class TestFile(unittest.TestCase):
    def test_file(self):
        index = Index.create()
        tu = index.parse("t.c", unsaved_files=[("t.c", "")])
        file = File.from_name(tu, "t.c")
        self.assertEqual(str(file), "t.c")
        self.assertEqual(file.name, "t.c")
        self.assertEqual(repr(file), "<File: t.c>")

    def test_file_eq(self):
        path = os.path.join(inputs_dir, "testfile.c")
        path_a = os.path.join(inputs_dir, "a.inc")
        path_b = os.path.join(inputs_dir, "b.inc")
        tu = TranslationUnit.from_source(path)
        main_file = File.from_name(tu, path)
        a_file = File.from_name(tu, path_a)
        a_file2 = File.from_name(tu, path_a)
        b_file = File.from_name(tu, path_b)

        self.assertEqual(a_file, a_file2)
        self.assertNotEqual(a_file, b_file)
        self.assertNotEqual(main_file, a_file)
        self.assertNotEqual(main_file, b_file)
        self.assertNotEqual(main_file, "t.c")

    def test_file_eq_in_memory(self):
        tu = TranslationUnit.from_source(
            "testfile.c",
            unsaved_files=[
                (
                    "testfile.c",
                    """
int a[] = { 
    #include "a.inc"
};
int b[] = { 
    #include "b.inc"
};
""",
                ),
                ("a.inc", "1,2,3"),
                ("b.inc", "1,2,3"),
            ],
        )

        path = os.path.join(inputs_dir, "testfile.c")
        path_a = os.path.join(inputs_dir, "a.inc")
        path_b = os.path.join(inputs_dir, "b.inc")
        tu = TranslationUnit.from_source(path)
        main_file = File.from_name(tu, path)
        a_file = File.from_name(tu, path_a)
        a_file2 = File.from_name(tu, path_a)
        b_file = File.from_name(tu, path_b)

        self.assertEqual(a_file, a_file2)
        self.assertNotEqual(a_file, b_file)
        self.assertNotEqual(main_file, a_file)
        self.assertNotEqual(main_file, b_file)
        self.assertNotEqual(main_file, "a.inc")