summaryrefslogtreecommitdiff
path: root/bindings/python/tests
diff options
context:
space:
mode:
Diffstat (limited to 'bindings/python/tests')
-rw-r--r--bindings/python/tests/cindex/test_cursor.py6
-rw-r--r--bindings/python/tests/cindex/test_diagnostics.py2
-rw-r--r--bindings/python/tests/cindex/test_location.py50
-rw-r--r--bindings/python/tests/cindex/test_type.py76
4 files changed, 132 insertions, 2 deletions
diff --git a/bindings/python/tests/cindex/test_cursor.py b/bindings/python/tests/cindex/test_cursor.py
index a653ba7bf28ef..3dde891a9c264 100644
--- a/bindings/python/tests/cindex/test_cursor.py
+++ b/bindings/python/tests/cindex/test_cursor.py
@@ -1,4 +1,4 @@
-from clang.cindex import Index, CursorKind
+from clang.cindex import Index, CursorKind, TypeKind
kInput = """\
// FIXME: Find nicer way to drop builtins and other cruft.
@@ -47,13 +47,17 @@ def test_get_children():
assert len(s0_nodes) == 2
assert s0_nodes[0].kind == CursorKind.FIELD_DECL
assert s0_nodes[0].spelling == 'a'
+ assert s0_nodes[0].type.kind == TypeKind.INT
assert s0_nodes[1].kind == CursorKind.FIELD_DECL
assert s0_nodes[1].spelling == 'b'
+ assert s0_nodes[1].type.kind == TypeKind.INT
assert tu_nodes[1].kind == CursorKind.STRUCT_DECL
assert tu_nodes[1].spelling == 's1'
+ assert tu_nodes[1].displayname == 's1'
assert tu_nodes[1].is_definition() == False
assert tu_nodes[2].kind == CursorKind.FUNCTION_DECL
assert tu_nodes[2].spelling == 'f0'
+ assert tu_nodes[2].displayname == 'f0(int, int)'
assert tu_nodes[2].is_definition() == True
diff --git a/bindings/python/tests/cindex/test_diagnostics.py b/bindings/python/tests/cindex/test_diagnostics.py
index c1ff0e38baada..98f97d3bd3b13 100644
--- a/bindings/python/tests/cindex/test_diagnostics.py
+++ b/bindings/python/tests/cindex/test_diagnostics.py
@@ -36,7 +36,7 @@ def test_diagnostic_fixit():
assert len(tu.diagnostics) == 1
assert tu.diagnostics[0].severity == Diagnostic.Warning
assert tu.diagnostics[0].location.line == 1
- assert tu.diagnostics[0].location.column == 31
+ assert tu.diagnostics[0].location.column == 26
assert tu.diagnostics[0].spelling.startswith('use of GNU old-style')
assert len(tu.diagnostics[0].fixits) == 1
assert tu.diagnostics[0].fixits[0].range.start.line == 1
diff --git a/bindings/python/tests/cindex/test_location.py b/bindings/python/tests/cindex/test_location.py
new file mode 100644
index 0000000000000..47c1c6021f554
--- /dev/null
+++ b/bindings/python/tests/cindex/test_location.py
@@ -0,0 +1,50 @@
+from clang.cindex import Index
+
+baseInput="int one;\nint two;\n"
+
+def assert_location(loc, line, column, offset):
+ assert loc.line == line
+ assert loc.column == column
+ assert loc.offset == offset
+
+def test_location():
+ index = Index.create()
+ tu = index.parse('t.c', unsaved_files = [('t.c',baseInput)])
+
+ for n in tu.cursor.get_children():
+ if n.spelling == 'one':
+ assert_location(n.location,line=1,column=5,offset=4)
+ if n.spelling == 'two':
+ assert_location(n.location,line=2,column=5,offset=13)
+
+ # adding a linebreak at top should keep columns same
+ tu = index.parse('t.c', unsaved_files = [('t.c',"\n"+baseInput)])
+
+ for n in tu.cursor.get_children():
+ if n.spelling == 'one':
+ assert_location(n.location,line=2,column=5,offset=5)
+ if n.spelling == 'two':
+ assert_location(n.location,line=3,column=5,offset=14)
+
+ # adding a space should affect column on first line only
+ tu = index.parse('t.c', unsaved_files = [('t.c'," "+baseInput)])
+
+ for n in tu.cursor.get_children():
+ if n.spelling == 'one':
+ assert_location(n.location,line=1,column=6,offset=5)
+ if n.spelling == 'two':
+ assert_location(n.location,line=2,column=5,offset=14)
+
+def test_extent():
+ index = Index.create()
+ tu = index.parse('t.c', unsaved_files = [('t.c',baseInput)])
+
+ for n in tu.cursor.get_children():
+ if n.spelling == 'one':
+ assert_location(n.extent.start,line=1,column=1,offset=0)
+ assert_location(n.extent.end,line=1,column=8,offset=7)
+ assert baseInput[n.extent.start.offset:n.extent.end.offset] == "int one"
+ if n.spelling == 'two':
+ assert_location(n.extent.start,line=2,column=1,offset=9)
+ assert_location(n.extent.end,line=2,column=8,offset=16)
+ assert baseInput[n.extent.start.offset:n.extent.end.offset] == "int two"
diff --git a/bindings/python/tests/cindex/test_type.py b/bindings/python/tests/cindex/test_type.py
new file mode 100644
index 0000000000000..cd27a4cbb98da
--- /dev/null
+++ b/bindings/python/tests/cindex/test_type.py
@@ -0,0 +1,76 @@
+from clang.cindex import Index, CursorKind, TypeKind
+
+kInput = """\
+
+typedef int I;
+
+struct teststruct {
+ int a;
+ I b;
+ long c;
+ unsigned long d;
+ signed long e;
+ const int f;
+ int *g;
+ int ***h;
+};
+
+"""
+
+def test_a_struct():
+ index = Index.create()
+ tu = index.parse('t.c', unsaved_files = [('t.c',kInput)])
+
+ for n in tu.cursor.get_children():
+ if n.spelling == 'teststruct':
+ fields = list(n.get_children())
+
+ assert all(x.kind == CursorKind.FIELD_DECL for x in fields)
+
+ assert fields[0].spelling == 'a'
+ assert not fields[0].type.is_const_qualified()
+ assert fields[0].type.kind == TypeKind.INT
+ assert fields[0].type.get_canonical().kind == TypeKind.INT
+
+ assert fields[1].spelling == 'b'
+ assert not fields[1].type.is_const_qualified()
+ assert fields[1].type.kind == TypeKind.TYPEDEF
+ assert fields[1].type.get_canonical().kind == TypeKind.INT
+ assert fields[1].type.get_declaration().spelling == 'I'
+
+ assert fields[2].spelling == 'c'
+ assert not fields[2].type.is_const_qualified()
+ assert fields[2].type.kind == TypeKind.LONG
+ assert fields[2].type.get_canonical().kind == TypeKind.LONG
+
+ assert fields[3].spelling == 'd'
+ assert not fields[3].type.is_const_qualified()
+ assert fields[3].type.kind == TypeKind.ULONG
+ assert fields[3].type.get_canonical().kind == TypeKind.ULONG
+
+ assert fields[4].spelling == 'e'
+ assert not fields[4].type.is_const_qualified()
+ assert fields[4].type.kind == TypeKind.LONG
+ assert fields[4].type.get_canonical().kind == TypeKind.LONG
+
+ assert fields[5].spelling == 'f'
+ assert fields[5].type.is_const_qualified()
+ assert fields[5].type.kind == TypeKind.INT
+ assert fields[5].type.get_canonical().kind == TypeKind.INT
+
+ assert fields[6].spelling == 'g'
+ assert not fields[6].type.is_const_qualified()
+ assert fields[6].type.kind == TypeKind.POINTER
+ assert fields[6].type.get_pointee().kind == TypeKind.INT
+
+ assert fields[7].spelling == 'h'
+ assert not fields[7].type.is_const_qualified()
+ assert fields[7].type.kind == TypeKind.POINTER
+ assert fields[7].type.get_pointee().kind == TypeKind.POINTER
+ assert fields[7].type.get_pointee().get_pointee().kind == TypeKind.POINTER
+ assert fields[7].type.get_pointee().get_pointee().get_pointee().kind == TypeKind.INT
+
+ break
+
+ else:
+ assert False, "Didn't find teststruct??"