-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler_test.py
More file actions
55 lines (47 loc) · 1.52 KB
/
compiler_test.py
File metadata and controls
55 lines (47 loc) · 1.52 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
from pathlib import Path
import subprocess
import compiler
import os
UPDATE = os.getenv('UPDATE') == 'true' # to force write golden files
def test_lexer_paragraph() -> None:
assert(
compiler.Compiler().tokenize('text') ==
[
compiler.Lexer.TextToken(text='text', bold=False, italic=False),
compiler.Lexer.NewLineToken()
]
)
def test_parser() -> None:
assert(
compiler.Compiler().parse([
compiler.Lexer.TextToken(text='text', bold=False, italic=False),
compiler.Lexer.NewLineToken()
]) ==
compiler.Parser.ASTRootNode(children=[
compiler.Parser.ASTParagraphNode(children=[
compiler.Parser.ASTTextNode(text='text', bold=False, italic=False)
])
])
)
def test_code_gen() -> None:
assert(
compiler.Compiler().gen(
compiler.Parser.ASTRootNode(children=[
compiler.Parser.ASTParagraphNode(children=[
compiler.Parser.ASTTextNode(text='text', bold=False, italic=False)
])
])
) == '<p>text</p>'
)
def test_golden() -> None:
for filepath in list(Path('testdata').glob('*.text')):
actual = prettify_html(compiler.Compiler().compile(filepath.read_text()))
golden = filepath.with_suffix('.html')
if UPDATE:
golden.write_text(actual)
expected = golden.read_text()
assert(expected == actual)
def prettify_html(html: str) -> str:
result = subprocess.run(['prettier', '--parser', 'html'], input=html, text=True, capture_output=True)
assert(result.returncode == 0)
return result.stdout