Debian Codegraph @

tests/syntax/catch_syntax_error.py

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 import sys
2
3 import pytest
4
5 import friendly_traceback
6 from syntax_errors_descriptions import descriptions
7
8 friendly_traceback.set_lang("en")
9
10
11 @pytest.mark.parametrize("filename", descriptions.keys())
12 def test_syntax_errors(filename):
13 cause = descriptions[filename]["in cause"]
14
15 try:
16 # __import__(filename)
17 exec("from . import %s" % filename)
18 except Exception:
19 friendly_traceback.explain_traceback(redirect="capture")
20 result = friendly_traceback.get_output()
21
22 if "tab_error" in filename:
23 assert "TabError" in result, "TabError identified incorrectly; %s" % filename
24 elif "indentation" in filename or "indented" in cause:
25 assert "IndentationError" in result, (
26 "IndentationError identified incorrectly; %s" % filename
27 )
28 else:
29 assert "SyntaxError" in result or "IndentationError" in result, (
30 "SyntaxError identified incorrectly; %s" % filename
31 )
32
33 unwrapped_result = " ".join(result.split())
34
35 if "version dependent" in descriptions[filename]:
36 causes = descriptions[filename]["version dependent"]
37 for cause in causes:
38 if cause in unwrapped_result:
39 break
40 else:
41 assert False, "\nExpected to see a version dependent cause in\n%s" % result
42 else:
43 assert cause in unwrapped_result, "\nExpected to see: %s\nIn: %s" % (cause, result)
44 if "also in cause" in descriptions[filename]:
45 other_causes = descriptions[filename]["also in cause"]
46 for cause in other_causes:
47 assert cause in unwrapped_result, "\nExpected to see: %s\nIn: %s" % (
48 cause,
49 result,
50 )
51 if "not in cause" in descriptions[filename]:
52 not_in_cause = descriptions[filename]["not in cause"]
53 for cause in not_in_cause:
54 assert cause not in unwrapped_result, "\nDid not expect to see: %s\nIn: %s" % (
55 cause,
56 result,
57 )