summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorZecong Hu <huzecong@gmail.com>2020-07-18 08:48:31 -0400
committerGitHub <noreply@github.com>2020-07-18 05:48:31 -0700
commit6da5009936d0c220b14f8c70deb0a2c7f82ac922 (patch)
treeaf9739246eab5a361de9d0dc3bdb49635690e82b /tests
parent61eac63fda12f9ba4504def70d0d76cd3ac1bbff (diff)
downloadpycparser-6da5009936d0c220b14f8c70deb0a2c7f82ac922.tar.gz
Fix issues #378, #379, #385 (#387)
* Fix #385: generate code with nested sizeofs * Fix #378: replace assertion with check Only the assertion inside `_build_function_definition` is replaced. The assertion is not appropriate because there are possible inputs that would trigger the assertion, they're just grammatically incorrect. Thus, it is replaced with a check that raises `ParseError` on failure. * Fix #379: parse struct with nested enum
Diffstat (limited to 'tests')
-rw-r--r--tests/test_c_generator.py7
-rwxr-xr-xtests/test_c_parser.py39
2 files changed, 46 insertions, 0 deletions
diff --git a/tests/test_c_generator.py b/tests/test_c_generator.py
index dd19a11..159c763 100644
--- a/tests/test_c_generator.py
+++ b/tests/test_c_generator.py
@@ -354,6 +354,13 @@ class TestCtoC(unittest.TestCase):
self.assertEqual(generator.visit(ast.ext[0].type.type.type),
'const int')
+ def test_nested_sizeof(self):
+ src = '1'
+ for _ in range(30):
+ src = 'sizeof(' + src + ')'
+ src = 'int x = ' + src + ';'
+ self._assert_ctoc_correct(src)
+
class TestCasttoC(unittest.TestCase):
def _find_file(self, name):
diff --git a/tests/test_c_parser.py b/tests/test_c_parser.py
index b6ecdd5..d33941c 100755
--- a/tests/test_c_parser.py
+++ b/tests/test_c_parser.py
@@ -33,6 +33,13 @@ def expand_decl(decl):
elif typ in [Struct, Union]:
decls = [expand_decl(d) for d in decl.decls or []]
return [typ.__name__, decl.name, decls]
+ elif typ == Enum:
+ if decl.values is None:
+ values = None
+ else:
+ assert isinstance(decl.values, EnumeratorList)
+ values = [enum.name for enum in decl.values.enumerators]
+ return ['Enum', decl.name, values]
else:
nested = expand_decl(decl.type)
@@ -855,6 +862,31 @@ class TestCParser_fundamentals(TestCParser_base):
['Decl', 'heads',
['PtrDecl', ['PtrDecl', ['TypeDecl', ['IdentifierType', ['Node']]]]]]]]]])
+ def test_struct_enum(self):
+ s1 = """
+ struct Foo {
+ enum Bar { A = 1 };
+ };
+ """
+ self.assertEqual(expand_decl(self.parse(s1).ext[0]),
+ ['Decl', None,
+ ['Struct', 'Foo',
+ [['Decl', None,
+ ['Enum', 'Bar', ['A']]]]]])
+ s2 = """
+ struct Foo {
+ enum Bar { A = 1, B, C } bar;
+ enum Baz { D = A } baz;
+ } foo;
+ """
+ self.assertEqual(expand_decl(self.parse(s2).ext[0]),
+ ['Decl', 'foo',
+ ['TypeDecl', ['Struct', 'Foo',
+ [['Decl', 'bar',
+ ['TypeDecl', ['Enum', 'Bar', ['A', 'B', 'C']]]],
+ ['Decl', 'baz',
+ ['TypeDecl', ['Enum', 'Baz', ['D']]]]]]]])
+
def test_struct_with_extra_semis_inside(self):
s1 = """
struct {
@@ -1190,6 +1222,13 @@ class TestCParser_fundamentals(TestCParser_base):
for b in bad:
self.assertRaises(ParseError, self.parse, b)
+ def test_invalid_typedef_storage_qual_error(self):
+ """ Tests that using typedef as a storage qualifier is correctly flagged
+ as an error.
+ """
+ bad = 'typedef const int foo(int a) { return 0; }'
+ self.assertRaises(ParseError, self.parse, bad)
+
def test_duplicate_typedef(self):
""" Tests that redeclarations of existing types are parsed correctly.
This is non-standard, but allowed by many compilers.