-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_main.py
134 lines (119 loc) · 3.85 KB
/
test_main.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
131
132
133
134
import pytest
from typer.testing import CliRunner
from main import app
runner = CliRunner()
@pytest.mark.parametrize("n", [1, 2])
def test_parser(n: int):
result = runner.invoke(app, ["parse", f"tests/parser/input_{n}.txt"])
assert result.exit_code == 0
assert result.stdout == ""
@pytest.mark.parametrize(
"n,line_numbers",
[
(1, [1, 3, 7]),
(2, [2]),
(3, [1, 4]),
],
)
def test_parser_errors(n: int, line_numbers: list[int]):
result = runner.invoke(app, ["parse", f"tests/parser/invalid_input_{n}.txt"])
assert result.exit_code == 0
for ln in line_numbers:
assert f"line {ln}" in result.stdout
assert result.stdout.lower().count("line") == len(line_numbers)
@pytest.mark.parametrize("n", [1, 2, 3])
def test_ast(n: int):
result = runner.invoke(app, ["ast", f"tests/ast/input_{n}.txt"])
assert result.exit_code == 0
with open(f"tests/ast/output_{n}.txt", encoding="utf-8") as f:
output = f.read()
assert result.stdout == output
@pytest.mark.parametrize(
"name,line_numbers,additional",
[
("break", [1], "break"),
("continue", [1], "continue"),
("vector", [1, 3, 7], "vector"),
("variables", [5, 7], "variable"),
("transpose", [7], "transpose"),
("special_matrix", [1, 11], "matrix"),
("indexing", [5, 6, 7], ""),
("indexing_bounds", [4, 11, 12], "out of bounds"),
("binary_operations", [7, 8, 14, 16, 17], "binary operation"),
("comparisons", [7, 9], "comparison"),
("compound_assignments", [10, 11, 12], "assignment"),
("ranges", [9], "range"),
("redeclaration", [12], ""),
],
)
def test_sem_errors(name: str, line_numbers: list[int], additional: str):
result = runner.invoke(app, ["sem", f"tests/semantic/{name}.txt"])
assert result.exit_code == 0
for ln in line_numbers:
assert f"line {ln}" in result.stdout
assert result.stdout.lower().count("line") == len(line_numbers)
if additional:
assert result.stdout.lower().count(additional) == len(line_numbers)
@pytest.mark.parametrize(
"name,output",
[
("simple_math", [23, 35, 1.0, 1.0, 1, -2, 9.0, 6.0, 1.0]),
("strings", ["aaa", "abcd"]),
("conditions", [0, 1, 0, 1, 0, 1, 0, 1]),
(
"vectors",
[
[1, 2, 3],
[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
[0, 0],
[[1, 1]],
[[[0], [0]], [[0], [0]], [[0], [0]]],
[[1, 1, 1]],
],
),
("variables", [2, 1, 3, "OK", 6]),
("while", [4, 3, 2, 1, 0]),
("for", [1, 10, 2, 10, 3, 10, 4, 10]),
("break_continue", [1, 2, 1, 2, 4] * 2),
(
"element_reference",
[
[1, 0],
0,
[[1, 2], [0, 1]],
[[0, 2], [0, 1]],
[[0, 0], [0, 1]],
],
),
(
"mat_operators",
[
[[2, 2], [2, 2]],
[[4, 4], [4, 4]],
[[3, 3], [3, 3]],
],
),
],
)
def test_interpreter(name: str, output: str):
result = runner.invoke(app, ["run", f"tests/interpreter/{name}.txt"])
assert result.exit_code == 0
assert result.stdout == "\n".join(map(str, output)) + "\n"
def test_interpreter_return():
result = runner.invoke(app, ["run", "tests/interpreter/return.txt"])
assert result.exit_code == 1
@pytest.mark.parametrize(
"name",
[
"parser/input_1.txt",
"parser/input_2.txt",
"ast/input_1.txt",
"ast/input_2.txt",
"ast/input_3.txt",
],
)
def test_all_on_previous_tests(
name: str,
): # run interpreter on parser tests and AST tests
result = runner.invoke(app, ["run", f"tests/{name}"])
assert result.exit_code == 0