summaryrefslogtreecommitdiff
path: root/tests/test_c_parser.py
diff options
context:
space:
mode:
authorserpilliere <serpilliere@users.noreply.github.com>2017-03-10 15:02:00 +0100
committerEli Bendersky <eliben@users.noreply.github.com>2017-03-10 06:02:00 -0800
commit471442fe06b095c085d0219a95fbf1f177b6449b (patch)
treeca5f4fa2fecce3b869f5c04d5f52f4cd0382ee6b /tests/test_c_parser.py
parent2129f5fb1ec2d06000f10c96510874c5303d9f8c (diff)
downloadpycparser-471442fe06b095c085d0219a95fbf1f177b6449b.tar.gz
Add column support in c_parser (#178)
Diffstat (limited to 'tests/test_c_parser.py')
-rwxr-xr-xtests/test_c_parser.py63
1 files changed, 33 insertions, 30 deletions
diff --git a/tests/test_c_parser.py b/tests/test_c_parser.py
index 0835013..8b09a3f 100755
--- a/tests/test_c_parser.py
+++ b/tests/test_c_parser.py
@@ -88,12 +88,15 @@ class TestCParser_base(unittest.TestCase):
def setUp(self):
self.cparser = _c_parser
- def assert_coord(self, node, line, file=None):
+ def assert_coord(self, node, line, column=None, file=None):
self.assertEqual(node.coord.line, line)
+ if column is not None:
+ self.assertEqual(node.coord.column, column)
if file:
self.assertEqual(node.coord.file, file)
+
class TestCParser_fundamentals(TestCParser_base):
def get_decl(self, txt, index=0):
""" Given a source and an index returns the expanded
@@ -134,10 +137,10 @@ class TestCParser_fundamentals(TestCParser_base):
def test_coords(self):
""" Tests the "coordinates" of parsed elements - file
- name and line numbers, with modification insterted by
- #line directives.
+ name, line and column numbers, with modification
+ insterted by #line directives.
"""
- self.assert_coord(self.parse('int a;').ext[0], 1)
+ self.assert_coord(self.parse('int a;').ext[0], 1, 5)
t1 = """
int a;
@@ -145,9 +148,9 @@ class TestCParser_fundamentals(TestCParser_base):
int c;
"""
f1 = self.parse(t1, filename='test.c')
- self.assert_coord(f1.ext[0], 2, 'test.c')
- self.assert_coord(f1.ext[1], 3, 'test.c')
- self.assert_coord(f1.ext[2], 6, 'test.c')
+ self.assert_coord(f1.ext[0], 2, 13, 'test.c')
+ self.assert_coord(f1.ext[1], 3, 13, 'test.c')
+ self.assert_coord(f1.ext[2], 6, 13, 'test.c')
t1_1 = '''
int main() {
@@ -156,8 +159,8 @@ class TestCParser_fundamentals(TestCParser_base):
return 0;
}'''
f1_1 = self.parse(t1_1, filename='test.c')
- self.assert_coord(f1_1.ext[0].body.block_items[0], 3, 'test.c')
- self.assert_coord(f1_1.ext[0].body.block_items[1], 4, 'test.c')
+ self.assert_coord(f1_1.ext[0].body.block_items[0], 3, 13, 'test.c')
+ self.assert_coord(f1_1.ext[0].body.block_items[1], 4, 13, 'test.c')
t1_2 = '''
int main () {
@@ -165,13 +168,13 @@ class TestCParser_fundamentals(TestCParser_base):
}'''
f1_2 = self.parse(t1_2, filename='test.c')
# make sure that the Cast has a coord (issue 23)
- self.assert_coord(f1_2.ext[0].body.block_items[0].init, 3, 'test.c')
+ self.assert_coord(f1_2.ext[0].body.block_items[0].init, 3, 21, file='test.c')
t2 = """
#line 99
int c;
"""
- self.assert_coord(self.parse(t2).ext[0], 99)
+ self.assert_coord(self.parse(t2).ext[0], 99, 13)
t3 = """
int dsf;
@@ -180,9 +183,9 @@ class TestCParser_fundamentals(TestCParser_base):
char d;
"""
f3 = self.parse(t3, filename='test.c')
- self.assert_coord(f3.ext[0], 2, 'test.c')
- self.assert_coord(f3.ext[1], 3, 'test.c')
- self.assert_coord(f3.ext[2], 3000, 'in.h')
+ self.assert_coord(f3.ext[0], 2, 13, 'test.c')
+ self.assert_coord(f3.ext[1], 3, 14, 'test.c')
+ self.assert_coord(f3.ext[2], 3000, 14, 'in.h')
t4 = """
#line 20 "restore.h"
@@ -195,17 +198,17 @@ class TestCParser_fundamentals(TestCParser_base):
char* ro;
"""
f4 = self.parse(t4, filename='myb.c')
- self.assert_coord(f4.ext[0], 20, 'restore.h')
- self.assert_coord(f4.ext[1], 30, 'includes/daween.ph')
- self.assert_coord(f4.ext[2], 30, 'includes/daween.ph')
- self.assert_coord(f4.ext[3], 50000, 'includes/daween.ph')
+ self.assert_coord(f4.ext[0], 20, 13, 'restore.h')
+ self.assert_coord(f4.ext[1], 30, 14, 'includes/daween.ph')
+ self.assert_coord(f4.ext[2], 30, 17, 'includes/daween.ph')
+ self.assert_coord(f4.ext[3], 50000, 13, 'includes/daween.ph')
t5 = """
int
#line 99
c;
"""
- self.assert_coord(self.parse(t5).ext[0], 99)
+ self.assert_coord(self.parse(t5).ext[0], 99, 9)
# coord for ellipsis
t6 = """
@@ -213,7 +216,7 @@ class TestCParser_fundamentals(TestCParser_base):
...) {
}"""
f6 = self.parse(t6, filename='z.c')
- self.assert_coord(self.parse(t6).ext[0].decl.type.args.params[1], 3)
+ self.assert_coord(self.parse(t6).ext[0].decl.type.args.params[1], 3, 17)
def test_forloop_coord(self):
t = '''\
@@ -224,9 +227,9 @@ class TestCParser_fundamentals(TestCParser_base):
'''
s = self.parse(t, filename='f.c')
forloop = s.ext[0].body.block_items[0]
- self.assert_coord(forloop.init, 2, 'f.c')
- self.assert_coord(forloop.cond, 2, 'f.c')
- self.assert_coord(forloop.next, 3, 'f.c')
+ self.assert_coord(forloop.init, 2, 13, 'f.c')
+ self.assert_coord(forloop.cond, 2, 26, 'f.c')
+ self.assert_coord(forloop.next, 3, 17, 'f.c')
def test_simple_decls(self):
self.assertEqual(self.get_decl('int a;'),
@@ -510,7 +513,7 @@ class TestCParser_fundamentals(TestCParser_base):
"""
compound = self.parse(e).ext[0].body
self.assertTrue(isinstance(compound, Compound))
- self.assert_coord(compound, 2, '')
+ self.assert_coord(compound, 2)
# The C99 compound literal feature
#
@@ -747,8 +750,8 @@ class TestCParser_fundamentals(TestCParser_base):
"""
s7_ast = self.parse(s7, filename='test.c')
- self.assert_coord(s7_ast.ext[0].type.decls[2], 6, 'test.c')
- self.assert_coord(s7_ast.ext[0].type.decls[3], 78,
+ self.assert_coord(s7_ast.ext[0].type.decls[2], 6, 22, 'test.c')
+ self.assert_coord(s7_ast.ext[0].type.decls[3], 78, 22,
r'D:\eli\cpp_stuff\libc_include/sys/reent.h')
s8 = """
@@ -1566,10 +1569,10 @@ class TestCParser_whole_code(TestCParser_base):
ps1 = self.parse(s1)
self.assert_num_klass_nodes(ps1, EmptyStatement, 3)
self.assert_num_klass_nodes(ps1, Return, 1)
- self.assert_coord(ps1.ext[0].body.block_items[0], 3, '')
- self.assert_coord(ps1.ext[0].body.block_items[1], 4, '')
- self.assert_coord(ps1.ext[0].body.block_items[2], 4, '')
- self.assert_coord(ps1.ext[0].body.block_items[3], 6, '')
+ self.assert_coord(ps1.ext[0].body.block_items[0], 3)
+ self.assert_coord(ps1.ext[0].body.block_items[1], 4)
+ self.assert_coord(ps1.ext[0].body.block_items[2], 4)
+ self.assert_coord(ps1.ext[0].body.block_items[3], 6)
def test_switch_statement(self):
def assert_case_node(node, const_value):