Debian Codegraph @

tests/unit/test_check_syntax.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 """In this file, we test to ensure that the output of
2 check_syntax is as expected.
3
4 We also test to ensure that check_syntax does not accidently
5 change any existing error handling settings.
6
7 """
8
9 import friendly_traceback as ft
10
11
12 def test_check_syntax():
13 # set-up
14 bad_code_syntax = "True = 1"
15 bad_code_exec = "a = b" # Not a syntax error, but a NameError
16 good_code = "c = 1"
17
18 ft.set_stream("capture")
19 original_include = ft.get_include()
20 installed = ft.is_installed()
21 # ----- end of set-up
22
23 # When a SyntaxError is raised, check_syntax returns False
24
25 assert not ft.editors_helpers.check_syntax(source=bad_code_syntax)
26 result = ft.get_output() # content is flushed
27 assert "SyntaxError" in result
28
29 assert not ft.get_output() # confirm that content was flushed
30
31 # When no SyntaxError is raised, check_syntax returns a tuple
32 # containing a code object and a file name
33 assert ft.editors_helpers.check_syntax(source=bad_code_exec)
34 assert ft.editors_helpers.check_syntax(source=good_code)
35 assert not ft.get_output() # no new exceptions recorded
36
37 try:
38 exec(bad_code_syntax, {})
39 except Exception:
40 assert not ft.get_output()
41
42 # Ensure that a call to check_syntax only install() temporarily
43 # if it was not installed before.
44
45 ft.uninstall()
46 ft.editors_helpers.check_syntax(source=bad_code_syntax)
47 assert not ft.is_installed()
48 ft.editors_helpers.check_syntax(source=bad_code_syntax, include="python_tb")
49 assert not ft.is_installed()
50
51 # When friendly is "installed", a call to check_syntax
52 # leaves its include unchanged.
53 ft.install(redirect="capture")
54
55 ft.set_include("explain")
56 ft.editors_helpers.check_syntax(source=bad_code_syntax)
57 assert ft.get_include() == "explain"
58 ft.editors_helpers.check_syntax(source=bad_code_syntax, include="python_tb")
59 assert ft.get_include() == "explain"
60
61 # A call to advanced_code_syntax, with a language specified as an argument
62 # should leave the previous language unchanged.
63
64 ft.set_lang("en")
65 assert not ft.editors_helpers.check_syntax(source=bad_code_syntax, lang="fr")
66 result = ft.get_output()
•67 assert "SyntaxError" in result
68 assert ft.get_lang() == "en"
69
70 # Clean up and restore for other tests
71 ft.get_output()
72 ft.set_stream(None)
73 if installed:
74 ft.uninstall()
75 ft.set_include(original_include)
76
77
78 def test_no_tb_does_not_contain_friendly_tb_items():
79 """Verify items from ``"friendly_tb"`` are discarded in ``"no_tb"`` group."""
80 # Strictly speaking, this is not a unit test for check_syntax proper but
81 # this "include" option is only used in check_syntax.
82 no_tb_group = ft.base_formatters.items_groups["no_tb"]
83 friendly_tb_group = ft.base_formatters.items_groups["friendly_tb"]
84 assert not any(item in no_tb_group for item in friendly_tb_group)
85
86
87 if __name__ == "__main__":
88 test_check_syntax()
89 print("Success!")