-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
166 lines (124 loc) · 4.19 KB
/
test.py
File metadata and controls
166 lines (124 loc) · 4.19 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""
Test driver for document tests.
"""
from typing import Callable, Any
import os, sys
from mdl import tree_parser, parse_to_doc, format_html, document, parse_tree_dump, structure, format_mdl
import mdl
from dataclasses import dataclass
from shelljob import fs #type: ignore
bad_count = 0
def passed(text : str) -> str:
return '\x1b[32m{} ✔ \x1b[m'.format(text)
def failed(text : str) -> str:
return '\x1b[31m{} ✗\x1b[m'.format(text)
def status(text, okay):
global bad_count
if not okay:
bad_count += 1
print( passed(text) if okay else failed(text), end=' ' )
def test_mdl( fname: str ) -> None:
print( fname, end= ' ' )
base = os.path.splitext( fname )[0]
tp = tree_parser.TreeParser()
parse_name = base + '.parse'
if os.path.exists( parse_name ):
parsed = tp.parse_file( fname )
parse_dump = parse_tree_dump.get_dump( parsed )
with open( parse_name, 'r', encoding = 'utf-8' ) as check:
check_dump = check.read()
status( 'Parse', parse_dump == check_dump )
doc_name = base + '.doc'
if os.path.exists( doc_name ):
doc = mdl.load_document( fname )
doc_dump = document.dump_document( doc )
with open( doc_name, 'r', encoding = 'utf-8' ) as check:
check_dump = check.read()
status( 'Doc', doc_dump == check_dump )
directions = check_directions( fname, base, parse_file=tp.parse_file )
if not directions.skip_doc:
test_rewrite( fname )
html_name = base + '.html'
if os.path.exists( html_name ):
doc = mdl.load_document( fname )
writer = mdl.HtmlWriter()
html = writer.render( doc )
with open( html_name, 'r', encoding = 'utf-8' ) as check:
check_html = check.read()
status( 'HTML', html == check_html )
print()
def test_rewrite( fname: str ) -> None:
orig_doc = mdl.load_document( fname )
orig_dump = document.dump_document( orig_doc )
mdl_format = format_mdl.MdlWriter().render( orig_doc )
with fs.NamedTempFile() as nm:
with open(nm, 'w', encoding='utf-8') as out_file:
out_file.write( mdl_format )
dup_doc = mdl.load_document( nm )
dup_dump = document.dump_document( dup_doc )
status( "MDL", orig_dump == dup_dump )
if orig_dump != dup_dump:
save_to = '/tmp/' + os.path.basename(fname)
with open( save_to, 'w', encoding='utf-8' ) as out_file:
out_file.write( mdl_format )
print( "Bad MDL saved to: ", save_to )
@dataclass
class Directions:
skip_doc: bool
def check_directions(
fname: str,
base : str,
*,
parse_file: Callable[[str],Any]
) -> Directions:
# Yaml was the historical name of these, kept to distinguish from .mcl in structures test
directions_name = base + '.yaml'
expect_fail = False
skip_doc = False
if os.path.exists( directions_name ):
test = structure.structure_load( directions_name )
fail_parse = test.get('fail-parse')
is_skip_doc = test.get('skip-doc', False)
assert isinstance(is_skip_doc, bool)
skip_doc = is_skip_doc
if fail_parse is not None:
expect_fail = True
try:
parse_file( fname )
okay = False
except mdl.ParseException as e:
okay = e.code == fail_parse
if not okay:
print( e.code, '!=', fail_parse)
status( 'Fail-Parse', okay )
return Directions(
skip_doc=expect_fail or skip_doc
)
def test_mcl( fname: str ) -> None:
print( fname, end=' ')
base = os.path.splitext( fname )[0]
check_directions(fname, base, parse_file=structure.structure_load)
json_name = base + '.json'
if os.path.exists( json_name ):
obj = structure.structure_load( fname )
json = structure.structure_format_json( obj, pretty = True )
with open( json_name, 'r', encoding = 'utf-8' ) as check:
check_json = check.read()
status( 'JSON', json == check_json )
dump_name = base + '.dump'
if os.path.exists( dump_name ):
obj = structure.structure_load( fname )
dump = structure.dump_structure( obj )
with open( dump_name, 'r', encoding = 'utf-8' ) as check:
check_dump = check.read()
status( 'Dump', dump == check_dump )
print()
def main() -> None:
for fname in fs.find( 'test/docs', name_regex = r".*\.mdl" ):
test_mdl( fname )
for fname in fs.find( 'test/structures', name_regex = r".*\.mcl" ):
test_mcl( fname )
if bad_count > 0:
print(failed(f"{bad_count} errors"))
sys.exit(1)
main()