-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ipywalrus.py
50 lines (37 loc) · 1.62 KB
/
test_ipywalrus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import pytest
from IPython.testing.globalipapp import start_ipython
from IPython.utils.io import capture_output
@pytest.fixture(scope="session")
def pure_ipython():
yield start_ipython()
@pytest.fixture(scope="function")
def ipywalrus_enabled(pure_ipython):
pure_ipython.run_line_magic(magic_name="load_ext", line="ipywalrus")
yield pure_ipython
pure_ipython.run_line_magic(magic_name="unload_ext", line="ipywalrus")
tests = [
("a := 5", 5),
("b := 5 + 5", 10),
("c := [5]", [5]),
("d := [z := 5]", [5]),
("e := (x := 5)", 5),
]
@pytest.mark.parametrize("input, expected_output", tests)
def test_syntaxerror_without_extension(pure_ipython, input, expected_output):
with capture_output() as captured:
pure_ipython.run_cell(raw_cell=input)
assert "SyntaxError: invalid syntax" in captured.stdout
assert input in captured.stdout
@pytest.mark.parametrize("input, expected_output", tests)
def test_works_with_extension(ipywalrus_enabled, input, expected_output):
with capture_output() as captured:
ipywalrus_enabled.run_cell(raw_cell=input)
assert captured.stdout == "Out[1]: " + str(expected_output) + "\n"
@pytest.mark.parametrize("input, expected_output", tests)
def test_syntaxerror_unloaded_extension(pure_ipython, input, expected_output):
pure_ipython.run_line_magic(magic_name="load_ext", line="ipywalrus")
pure_ipython.run_line_magic(magic_name="unload_ext", line="ipywalrus")
with capture_output() as captured:
pure_ipython.run_cell(raw_cell=input)
assert "SyntaxError: invalid syntax" in captured.stdout
assert input in captured.stdout