diff options
author | Christoph Reiter <reiter.christoph@gmail.com> | 2018-12-08 18:26:47 +0100 |
---|---|---|
committer | Christoph Reiter <reiter.christoph@gmail.com> | 2018-12-08 22:45:17 +0100 |
commit | f9a873a0881e189a2d20e4904d40bb0c5cbf0d94 (patch) | |
tree | 71b78f9ddc8cc45198ce560acded84da90b094cb /tests | |
parent | 0f4dd5e39a86e9ae749b3b4627488e19d18dd8a5 (diff) | |
download | gobject-introspection-f9a873a0881e189a2d20e4904d40bb0c5cbf0d94.tar.gz |
sourcescanner: collect error messages and expose them
It just printed errors to stderr and always returns success even if parsing
fails. This prevents us to write any tests for it.
As a first step collect all lexing/parsing error messages and print them to stderr after
the scanner is done. This allows us to add some regression tests for !78.
In the future we probably want to raise an exception with those errors if parsing
fails.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/scanner/test_sourcescanner.py | 61 |
1 files changed, 42 insertions, 19 deletions
diff --git a/tests/scanner/test_sourcescanner.py b/tests/scanner/test_sourcescanner.py index 831af486..624d9df9 100644 --- a/tests/scanner/test_sourcescanner.py +++ b/tests/scanner/test_sourcescanner.py @@ -10,7 +10,20 @@ import os from giscanner.sourcescanner import SourceScanner -two_typedefs_source = """ +class Test(unittest.TestCase): + + def _parse_files(self, code, header=True): + scanner = SourceScanner() + tmp_fd, tmp_name = tempfile.mkstemp(suffix=".h" if header else ".c") + fileobj = os.fdopen(tmp_fd, 'wb') + with fileobj: + fileobj.write(code.encode("utf-8")) + scanner.parse_files([tmp_name]) + os.unlink(tmp_name) + return scanner + + def test_length_consistency(self): + scanner = self._parse_files(""" /** * Spam: */ @@ -20,26 +33,36 @@ typedef struct _spam Spam; * Eggs: */ typedef struct _eggs Eggs; -""" +""") + self.assertEqual(len(list(scanner.get_symbols())), 2) + self.assertEqual(len(list(scanner.get_symbols())), 2) + self.assertEqual(len(list(scanner.get_comments())), 2) + self.assertEqual(len(list(scanner.get_comments())), 2) + self.assertFalse(scanner.get_errors()) -class Test(unittest.TestCase): - def setUp(self): - self.ss = SourceScanner() - tmp_fd, tmp_name = tempfile.mkstemp() - file = os.fdopen(tmp_fd, 'wt') - file.write(two_typedefs_source) - file.close() - - self.ss.parse_files([tmp_name]) - - def test_get_symbols_length_consistency(self): - self.assertEqual(len(list(self.ss.get_symbols())), 2) - self.assertEqual(len(list(self.ss.get_symbols())), 2) - - def test_get_comments_length_consistency(self): - self.assertEqual(len(list(self.ss.get_comments())), 2) - self.assertEqual(len(list(self.ss.get_comments())), 2) + def test_parser_error(self): + scanner = self._parse_files(""" +void foo() { + a = +}""") + + errors = scanner.get_errors() + self.assertEqual(len(errors), 1) + self.assertTrue("syntax error" in errors[0]) + + def test_ignore_typeof(self): + # https://gitlab.gnome.org/GNOME/gobject-introspection/merge_requests/71 + scanner = self._parse_files(""" +/** +* foo: +*/ +void foo(int bar) { + bar = ((__typeof__(bar)) (foo) (bar)); +} +""") + self.assertEqual(len(list(scanner.get_comments())), 1) + self.assertFalse(scanner.get_errors()) if __name__ == '__main__': |