-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cli.py
130 lines (117 loc) · 3.01 KB
/
test_cli.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""Test cli.py for Stilted."""
import textwrap
from typing import Callable, Iterable
import pytest
from cli import main
def fake_input(lines: Iterable[str]) -> Callable[[str], str]:
"""Create a fake `input` function that return lines from `lines`."""
ilines = iter(lines)
def _fake(prompt):
print(prompt, end="")
try:
line = next(ilines)
print(line)
return line + "\n"
except StopIteration:
raise EOFError()
return _fake
@pytest.mark.parametrize(
"argv, lines, output",
[
(
[],
["123 456", "add", "=="],
"""\
|-0> 123 456
|-2> add
|-1> ==
579
|-0>
""",
),
(
[],
["]"],
"""\
|-0> ]
Error: unmatchedmark in --]--
Operand stack (0):
|-0>
""",
),
(
[],
["({{}) cvx exec"],
"""\
|-0> ({{}) cvx exec
Error: syntaxerror in exec
Operand stack (0):
|-0>
""",
),
(
["-c", "123 456 add dup =="],
[],
"""\
579
""",
),
(
["-i", "-c", "123 456 add dup =="],
["dup 2 mul pstack"],
"""\
579
|-1> dup 2 mul pstack
1158
579
|-2>
""",
),
(
["-i", "-c", ""],
["0 1 1 10 { add } for =="],
"""\
|-0> 0 1 1 10 { add } for ==
55
|-0>
""",
),
(
["-i", "-c", "", "abc", "def"],
["argv pstack"],
"""\
|-0> argv pstack
[(-c) (abc) (def)]
|-1>
""",
),
(
["-c", "argv pstack", "hello", "123"],
[],
"""\
[(-c) (hello) (123)]
""",
),
],
)
def test_prompting(capsys, argv, lines, output):
main(argv, input_fn=fake_input(lines))
assert capsys.readouterr().out.rstrip() == textwrap.dedent(output).rstrip()
@pytest.mark.parametrize(
"file_text, output",
[
("123 456\n add\n ==\n argv pstack", "579\n[({fname}) (abc)]\n"),
("123 (a) add\n", "Error: typecheck in --add--\nOperand stack (2):\n(a)\n123\n"),
],
)
def test_file_input(file_text, output, capsys, tmp_path):
foo_ps = tmp_path / "foo.ps"
foo_ps.write_text(file_text)
fname = str(foo_ps)
main([fname, "abc"])
assert capsys.readouterr().out == output.format(fname=fname)
def test_help(capsys):
with pytest.raises(SystemExit):
main(["--help"])
help_text = capsys.readouterr().out
assert "Output file name. %d will be the page number" in help_text