-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
67 lines (47 loc) · 2.04 KB
/
conftest.py
File metadata and controls
67 lines (47 loc) · 2.04 KB
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
import json
from pathlib import Path
import pytest
from tokens import Token, token_decoder
@pytest.fixture
def source_to_known_tokens(request: pytest.FixtureRequest) -> tuple[str, list[Token]]:
stage, name = request.param
script_path = Path(__file__).resolve()
script_directory = script_path.parent
# Load program source
program_path = f"{script_directory}/test-programs/chapter-{stage:02}/valid/{name}.c"
with open(program_path, "r") as fp:
program_source = fp.read()
# Load expected tokens
tokens_path = f"{script_directory}/test-programs/chapter-{stage:02}/valid/{name}.tokens"
with open(tokens_path, "r") as fp:
expected_tokens_str = fp.read()
tokens = json.loads(expected_tokens_str, object_hook=token_decoder)
# tokens: list[Token] = []
# for line in lines:
# token = parse_known_token(line)
# tokens.append(token)
return program_source, tokens
@pytest.fixture
def source_code(request: pytest.FixtureRequest) -> str:
stage, folder, name = request.param
return source_code_loader(stage, folder, name)
def source_code_loader(stage: int, folder: str, name: str) -> str:
script_path = Path(__file__).resolve()
script_directory = script_path.parent
# Load program source
program_path = f"{script_directory}/test-programs/chapter-{stage:02}/{folder}/{name}.c"
with open(program_path, "r") as fp:
program_source = fp.read()
return program_source
def known_assembly_loader(stage: int, folder: str, name: str) -> list[str]:
script_path = Path(__file__).resolve()
script_directory = script_path.parent
# Load known assembly file
program_path = f"{script_directory}/test-programs/chapter-{stage:02}/{folder}/{name}.asm"
with open(program_path, "r") as fp:
program_source = fp.readlines()
return program_source
# def parse_known_token(token_str: str) -> Token:
# data = json.loads(token_str.strip())
# token_type = TokenType[data["type"]]
# return Token(token_type, data["lexeme"], data["line"], data["col"])