| 1 |
import os |
| 2 |
|
| 3 |
from clang.cindex import File, Index, TranslationUnit |
| 4 |
|
| 5 |
|
| 6 |
import unittest |
| 7 |
|
| 8 |
inputs_dir = os.path.join(os.path.dirname(__file__), "INPUTS") |
| 9 |
|
| 10 |
|
| 11 |
class TestFile(unittest.TestCase): |
| 12 |
def test_file(self): |
| 13 |
index = Index.create() |
| 14 |
tu = index.parse("t.c", unsaved_files=[("t.c", "")]) |
| 15 |
file = File.from_name(tu, "t.c") |
| 16 |
self.assertEqual(str(file), "t.c") |
| 17 |
self.assertEqual(file.name, "t.c") |
| 18 |
self.assertEqual(repr(file), "<File: t.c>") |
| 19 |
|
| 20 |
def test_file_eq(self): |
| 21 |
path = os.path.join(inputs_dir, "testfile.c") |
| 22 |
path_a = os.path.join(inputs_dir, "a.inc") |
| 23 |
path_b = os.path.join(inputs_dir, "b.inc") |
| 24 |
tu = TranslationUnit.from_source(path) |
| 25 |
main_file = File.from_name(tu, path) |
| 26 |
a_file = File.from_name(tu, path_a) |
| 27 |
a_file2 = File.from_name(tu, path_a) |
| 28 |
b_file = File.from_name(tu, path_b) |
| 29 |
|
| 30 |
self.assertEqual(a_file, a_file2) |
| 31 |
self.assertNotEqual(a_file, b_file) |
| 32 |
self.assertNotEqual(main_file, a_file) |
| 33 |
self.assertNotEqual(main_file, b_file) |
| 34 |
self.assertNotEqual(main_file, "t.c") |
| 35 |
|
| 36 |
def test_file_eq_in_memory(self): |
| 37 |
tu = TranslationUnit.from_source( |
| 38 |
"testfile.c", |
| 39 |
unsaved_files=[ |
| 40 |
( |
| 41 |
"testfile.c", |
| 42 |
""" |
| 43 |
int a[] = { |
| 44 |
#include "a.inc" |
| 45 |
}; |
| 46 |
int b[] = { |
| 47 |
#include "b.inc" |
| 48 |
}; |
| 49 |
""", |
| 50 |
), |
| 51 |
("a.inc", "1,2,3"), |
| 52 |
("b.inc", "1,2,3"), |
| 53 |
], |
| 54 |
) |
| 55 |
|
| 56 |
path = os.path.join(inputs_dir, "testfile.c") |
| 57 |
path_a = os.path.join(inputs_dir, "a.inc") |
| 58 |
path_b = os.path.join(inputs_dir, "b.inc") |
| 59 |
tu = TranslationUnit.from_source(path) |
| 60 |
main_file = File.from_name(tu, path) |
| 61 |
a_file = File.from_name(tu, path_a) |
| 62 |
a_file2 = File.from_name(tu, path_a) |
| 63 |
b_file = File.from_name(tu, path_b) |
| 64 |
|
| 65 |
self.assertEqual(a_file, a_file2) |
| 66 |
self.assertNotEqual(a_file, b_file) |
| 67 |
self.assertNotEqual(main_file, a_file) |
| 68 |
self.assertNotEqual(main_file, b_file) |
| 69 |
self.assertNotEqual(main_file, "a.inc") |