-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_golden.py
36 lines (28 loc) · 1.17 KB
/
test_golden.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
import contextlib
import io
import logging
import os
import tempfile
import pytest
from computer_simulator.machine import machine
from computer_simulator.translator import translator
@pytest.mark.golden_test("../golden/*.yml")
def test_whole_by_golden(golden, caplog):
caplog.set_level(logging.DEBUG)
with tempfile.TemporaryDirectory() as tmpdirname:
source = os.path.join(tmpdirname, "source")
input_stream = os.path.join(tmpdirname, "input")
target = os.path.join(tmpdirname, "target")
with open(source, "w", encoding="utf-8") as file:
file.write(golden["source"])
with open(input_stream, "w", encoding="utf-8") as file:
file.write(golden["input"])
with contextlib.redirect_stdout(io.StringIO()) as stdout:
translator.main(source, target)
print("============================================================")
machine.main(target, input_stream)
with open(target, encoding="utf-8") as file:
code = file.read()
assert code == golden.out["code"]
assert stdout.getvalue() == golden.out["output"]
assert caplog.text == golden.out["log"]