diff options
| author | Michal Gorny <mgorny@gentoo.org> | 2017-11-10 16:44:12 +0000 |
|---|---|---|
| committer | Michal Gorny <mgorny@gentoo.org> | 2017-11-10 16:44:12 +0000 |
| commit | 084e43bfbfd463df8e325fe69e7f8a546baea46e (patch) | |
| tree | 2e8cde891b9c487ad264802a81a879fc3943042c /clang/bindings/python/tests/cindex/test_diagnostics.py | |
| parent | ddbb5ee1673964b398db890d13139e1fa2ebc1ae (diff) | |
| download | bcm5719-llvm-084e43bfbfd463df8e325fe69e7f8a546baea46e.tar.gz bcm5719-llvm-084e43bfbfd463df8e325fe69e7f8a546baea46e.zip | |
[python] [tests] Rewrite to use standard unittest module
Rewrite the tests from using plain 'assert' mixed with some nosetests
methods to the standard unittest module layout. Improve the code
to use the most canonical assertion methods whenever possible.
This has a few major advantages:
- the code uses standard methods now, resulting in a reduced number
of WTFs whenever someone with basic Python knowledge gets to read it,
- completely unnecessary dependency on nosetests is removed since
the standard library supplies all that is necessary for the tests
to run,
- the tests can be run via any test runner, including the one built-in
in Python,
- the failure output for most of the tests is improved from 'assertion
x == y failed' to actually telling the values.
Differential Revision: https://reviews.llvm.org/D39763
llvm-svn: 317897
Diffstat (limited to 'clang/bindings/python/tests/cindex/test_diagnostics.py')
| -rw-r--r-- | clang/bindings/python/tests/cindex/test_diagnostics.py | 197 |
1 files changed, 100 insertions, 97 deletions
diff --git a/clang/bindings/python/tests/cindex/test_diagnostics.py b/clang/bindings/python/tests/cindex/test_diagnostics.py index 23cbe89f658..78b327daa72 100644 --- a/clang/bindings/python/tests/cindex/test_diagnostics.py +++ b/clang/bindings/python/tests/cindex/test_diagnostics.py @@ -1,102 +1,105 @@ from clang.cindex import * from .util import get_tu +import unittest + + # FIXME: We need support for invalid translation units to test better. -def test_diagnostic_warning(): - tu = get_tu('int f0() {}\n') - 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 == 11 - assert (tu.diagnostics[0].spelling == - 'control reaches end of non-void function') - -def test_diagnostic_note(): - # FIXME: We aren't getting notes here for some reason. - tu = get_tu('#define A x\nvoid *A = 1;\n') - assert len(tu.diagnostics) == 1 - assert tu.diagnostics[0].severity == Diagnostic.Warning - assert tu.diagnostics[0].location.line == 2 - assert tu.diagnostics[0].location.column == 7 - assert 'incompatible' in tu.diagnostics[0].spelling -# assert tu.diagnostics[1].severity == Diagnostic.Note -# assert tu.diagnostics[1].location.line == 1 -# assert tu.diagnostics[1].location.column == 11 -# assert tu.diagnostics[1].spelling == 'instantiated from' - -def test_diagnostic_fixit(): - tu = get_tu('struct { int f0; } x = { f0 : 1 };') - 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 == 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 - assert tu.diagnostics[0].fixits[0].range.start.column == 26 - assert tu.diagnostics[0].fixits[0].range.end.line == 1 - assert tu.diagnostics[0].fixits[0].range.end.column == 30 - assert tu.diagnostics[0].fixits[0].value == '.f0 = ' - -def test_diagnostic_range(): - tu = get_tu('void f() { int i = "a" + 1; }') - 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 == 16 - assert tu.diagnostics[0].spelling.startswith('incompatible pointer to') - assert len(tu.diagnostics[0].fixits) == 0 - assert len(tu.diagnostics[0].ranges) == 1 - assert tu.diagnostics[0].ranges[0].start.line == 1 - assert tu.diagnostics[0].ranges[0].start.column == 20 - assert tu.diagnostics[0].ranges[0].end.line == 1 - assert tu.diagnostics[0].ranges[0].end.column == 27 - try: - tu.diagnostics[0].ranges[1].start.line - except IndexError: - assert True - else: - assert False - -def test_diagnostic_category(): - """Ensure that category properties work.""" - tu = get_tu('int f(int i) { return 7; }', all_warnings=True) - assert len(tu.diagnostics) == 1 - d = tu.diagnostics[0] - - assert d.severity == Diagnostic.Warning - assert d.location.line == 1 - assert d.location.column == 11 - - assert d.category_number == 2 - assert d.category_name == 'Semantic Issue' - -def test_diagnostic_option(): - """Ensure that category option properties work.""" - tu = get_tu('int f(int i) { return 7; }', all_warnings=True) - assert len(tu.diagnostics) == 1 - d = tu.diagnostics[0] - - assert d.option == '-Wunused-parameter' - assert d.disable_option == '-Wno-unused-parameter' - -def test_diagnostic_children(): - tu = get_tu('void f(int x) {} void g() { f(); }') - assert len(tu.diagnostics) == 1 - d = tu.diagnostics[0] - - children = d.children - assert len(children) == 1 - assert children[0].severity == Diagnostic.Note - assert children[0].spelling.endswith('declared here') - assert children[0].location.line == 1 - assert children[0].location.column == 1 - -def test_diagnostic_string_repr(): - tu = get_tu('struct MissingSemicolon{}') - assert len(tu.diagnostics) == 1 - d = tu.diagnostics[0] - - assert repr(d) == '<Diagnostic severity 3, location <SourceLocation file \'t.c\', line 1, column 26>, spelling "expected \';\' after struct">' - + +class TestDiagnostics(unittest.TestCase): + def test_diagnostic_warning(self): + tu = get_tu('int f0() {}\n') + self.assertEqual(len(tu.diagnostics), 1) + self.assertEqual(tu.diagnostics[0].severity, Diagnostic.Warning) + self.assertEqual(tu.diagnostics[0].location.line, 1) + self.assertEqual(tu.diagnostics[0].location.column, 11) + self.assertEqual(tu.diagnostics[0].spelling, + 'control reaches end of non-void function') + + def test_diagnostic_note(self): + # FIXME: We aren't getting notes here for some reason. + tu = get_tu('#define A x\nvoid *A = 1;\n') + self.assertEqual(len(tu.diagnostics), 1) + self.assertEqual(tu.diagnostics[0].severity, Diagnostic.Warning) + self.assertEqual(tu.diagnostics[0].location.line, 2) + self.assertEqual(tu.diagnostics[0].location.column, 7) + self.assertIn('incompatible', tu.diagnostics[0].spelling) +# self.assertEqual(tu.diagnostics[1].severity, Diagnostic.Note) +# self.assertEqual(tu.diagnostics[1].location.line, 1) +# self.assertEqual(tu.diagnostics[1].location.column, 11) +# self.assertEqual(tu.diagnostics[1].spelling, 'instantiated from') + + def test_diagnostic_fixit(self): + tu = get_tu('struct { int f0; } x = { f0 : 1 };') + self.assertEqual(len(tu.diagnostics), 1) + self.assertEqual(tu.diagnostics[0].severity, Diagnostic.Warning) + self.assertEqual(tu.diagnostics[0].location.line, 1) + self.assertEqual(tu.diagnostics[0].location.column, 26) + self.assertRegexpMatches(tu.diagnostics[0].spelling, + 'use of GNU old-style.*') + self.assertEqual(len(tu.diagnostics[0].fixits), 1) + self.assertEqual(tu.diagnostics[0].fixits[0].range.start.line, 1) + self.assertEqual(tu.diagnostics[0].fixits[0].range.start.column, 26) + self.assertEqual(tu.diagnostics[0].fixits[0].range.end.line, 1) + self.assertEqual(tu.diagnostics[0].fixits[0].range.end.column, 30) + self.assertEqual(tu.diagnostics[0].fixits[0].value, '.f0 = ') + + def test_diagnostic_range(self): + tu = get_tu('void f() { int i = "a" + 1; }') + self.assertEqual(len(tu.diagnostics), 1) + self.assertEqual(tu.diagnostics[0].severity, Diagnostic.Warning) + self.assertEqual(tu.diagnostics[0].location.line, 1) + self.assertEqual(tu.diagnostics[0].location.column, 16) + self.assertRegexpMatches(tu.diagnostics[0].spelling, + 'incompatible pointer to.*') + self.assertEqual(len(tu.diagnostics[0].fixits), 0) + self.assertEqual(len(tu.diagnostics[0].ranges), 1) + self.assertEqual(tu.diagnostics[0].ranges[0].start.line, 1) + self.assertEqual(tu.diagnostics[0].ranges[0].start.column, 20) + self.assertEqual(tu.diagnostics[0].ranges[0].end.line, 1) + self.assertEqual(tu.diagnostics[0].ranges[0].end.column, 27) + with self.assertRaises(IndexError): + tu.diagnostics[0].ranges[1].start.line + + def test_diagnostic_category(self): + """Ensure that category properties work.""" + tu = get_tu('int f(int i) { return 7; }', all_warnings=True) + self.assertEqual(len(tu.diagnostics), 1) + d = tu.diagnostics[0] + + self.assertEqual(d.severity, Diagnostic.Warning) + self.assertEqual(d.location.line, 1) + self.assertEqual(d.location.column, 11) + + self.assertEqual(d.category_number, 2) + self.assertEqual(d.category_name, 'Semantic Issue') + + def test_diagnostic_option(self): + """Ensure that category option properties work.""" + tu = get_tu('int f(int i) { return 7; }', all_warnings=True) + self.assertEqual(len(tu.diagnostics), 1) + d = tu.diagnostics[0] + + self.assertEqual(d.option, '-Wunused-parameter') + self.assertEqual(d.disable_option, '-Wno-unused-parameter') + + def test_diagnostic_children(self): + tu = get_tu('void f(int x) {} void g() { f(); }') + self.assertEqual(len(tu.diagnostics), 1) + d = tu.diagnostics[0] + + children = d.children + self.assertEqual(len(children), 1) + self.assertEqual(children[0].severity, Diagnostic.Note) + self.assertRegexpMatches(children[0].spelling, + '.*declared here') + self.assertEqual(children[0].location.line, 1) + self.assertEqual(children[0].location.column, 1) + + def test_diagnostic_string_repr(self): + tu = get_tu('struct MissingSemicolon{}') + self.assertEqual(len(tu.diagnostics), 1) + d = tu.diagnostics[0] + + self.assertEqual(repr(d), '<Diagnostic severity 3, location <SourceLocation file \'t.c\', line 1, column 26>, spelling "expected \';\' after struct">') |

