| 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 |
|
| 14 |
bad_code_syntax = "True = 1" |
| 15 |
bad_code_exec = "a = b" |
| 16 |
good_code = "c = 1" |
| 17 |
|
| 18 |
ft.set_stream("capture") |
| 19 |
original_include = ft.get_include() |
| 20 |
installed = ft.is_installed() |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
assert not ft.editors_helpers.check_syntax(source=bad_code_syntax) |
| 26 |
result = ft.get_output() |
| 27 |
assert "SyntaxError" in result |
| 28 |
|
| 29 |
assert not ft.get_output() |
| 30 |
|
| 31 |
|
| 32 |
|
| 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() |
| 36 |
|
| 37 |
try: |
| 38 |
exec(bad_code_syntax, {}) |
| 39 |
except Exception: |
| 40 |
assert not ft.get_output() |
| 41 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 52 |
|
| 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 |
|
| 62 |
|
| 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 |
|
| 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 |
|
| 81 |
|
| 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!") |