Mercurial > jouvence
annotate tests/conftest.py @ 2:59fe8cb6190d
Add lots of tests, fix lots of bugs.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 03 Jan 2017 09:05:28 -0800 |
parents | 74b83e3d921e |
children | ee741bbe96a8 |
rev | line source |
---|---|
2
59fe8cb6190d
Add lots of tests, fix lots of bugs.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
1 import re |
0 | 2 import sys |
3 import logging | |
4 import yaml | |
5 import pytest | |
6 from fontaine.document import ( | |
7 FontaineSceneElement, | |
1
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
8 TYPE_ACTION, TYPE_CENTEREDACTION, TYPE_CHARACTER, TYPE_DIALOG, |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
9 TYPE_PARENTHETICAL, TYPE_TRANSITION, TYPE_LYRICS, TYPE_PAGEBREAK, |
2
59fe8cb6190d
Add lots of tests, fix lots of bugs.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
10 TYPE_EMPTYLINES, |
1
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
11 _scene_element_type_str) |
0 | 12 from fontaine.parser import FontaineParser, FontaineParserError |
13 | |
14 | |
15 def pytest_addoption(parser): | |
16 parser.addoption( | |
17 '--log-debug', | |
18 action='store_true', | |
19 help="Sets the Fontaine logger to output debug info to stdout.") | |
20 | |
21 | |
22 def pytest_configure(config): | |
23 if config.getoption('--log-debug'): | |
24 hdl = logging.StreamHandler(stream=sys.stdout) | |
25 logging.getLogger('fontaine').addHandler(hdl) | |
26 logging.getLogger('fontaine').setLevel(logging.DEBUG) | |
27 | |
28 | |
29 def pytest_collect_file(parent, path): | |
30 if path.ext == '.yaml' and path.basename.startswith("test"): | |
31 return FontaineScriptTestFile(path, parent) | |
32 return None | |
33 | |
34 | |
35 def assert_scenes(actual, scenes): | |
36 assert len(actual) == len(scenes) | |
37 for a, e in zip(actual, scenes): | |
38 assert_scene(a, e[0], e[1:]) | |
39 | |
40 | |
41 def assert_scene(actual, header, paragraphs): | |
42 if header is not None: | |
43 assert actual.header == header | |
44 assert len(actual.paragraphs) == len(paragraphs) | |
45 for a, e in zip(actual.paragraphs, paragraphs): | |
46 assert_paragraph(a, e) | |
47 | |
48 | |
49 def assert_paragraph(actual, expected): | |
50 if isinstance(expected, str): | |
51 assert isinstance(actual, FontaineSceneElement) | |
52 assert actual.type == TYPE_ACTION | |
53 assert actual.text == expected | |
54 elif isinstance(expected, FontaineSceneElement): | |
55 assert isinstance(actual, FontaineSceneElement) | |
56 assert actual.type == expected.type | |
57 assert actual.text == expected.text | |
58 else: | |
59 raise NotImplementedError("Don't know what this is: %s" % expected) | |
60 | |
61 | |
62 def _c(name): | |
63 return FontaineSceneElement(TYPE_CHARACTER, name) | |
64 | |
65 | |
66 def _p(text): | |
67 return FontaineSceneElement(TYPE_PARENTHETICAL, text) | |
68 | |
69 | |
70 def _d(text): | |
71 return FontaineSceneElement(TYPE_DIALOG, text) | |
72 | |
73 | |
1
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
74 def _t(text): |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
75 return FontaineSceneElement(TYPE_TRANSITION, text) |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
76 |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
77 |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
78 def _l(text): |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
79 return FontaineSceneElement(TYPE_LYRICS, text) |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
80 |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
81 |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
82 class UnexpectedScriptOutput(Exception): |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
83 def __init__(self, actual, expected): |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
84 self.actual = actual |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
85 self.expected = expected |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
86 |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
87 |
0 | 88 class FontaineScriptTestFile(pytest.File): |
89 def collect(self): | |
90 spec = yaml.load_all(self.fspath.open(encoding='utf8')) | |
91 for i, item in enumerate(spec): | |
92 name = '%s_%d' % (self.fspath.basename, i) | |
93 if 'test_name' in item: | |
94 name += '_%s' % item['test_name'] | |
95 yield FontaineScriptTestItem(name, self, item) | |
96 | |
97 | |
98 class FontaineScriptTestItem(pytest.Item): | |
99 def __init__(self, name, parent, spec): | |
100 super().__init__(name, parent) | |
101 self.spec = spec | |
102 | |
103 def reportinfo(self): | |
104 return self.fspath, 0, "fontaine script test: %s" % self.name | |
105 | |
106 def runtest(self): | |
107 intext = self.spec.get('in') | |
108 expected = self.spec.get('out') | |
109 title = self.spec.get('title') | |
110 if intext is None or expected is None: | |
111 raise Exception("No 'in' or 'out' specified.") | |
112 | |
113 parser = FontaineParser() | |
114 doc = parser.parseString(intext) | |
115 if title is not None: | |
116 assert title == doc.title_values | |
1
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
117 |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
118 exp_scenes = make_scenes(expected) |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
119 try: |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
120 assert_scenes(doc.scenes, exp_scenes) |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
121 except AssertionError: |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
122 raise UnexpectedScriptOutput(doc.scenes, exp_scenes) |
0 | 123 |
124 def repr_failure(self, excinfo): | |
125 if isinstance(excinfo.value, FontaineParserError): | |
126 return ('\n'.join( | |
127 ['Parser error:', str(excinfo.value)])) | |
1
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
128 if isinstance(excinfo.value, UnexpectedScriptOutput): |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
129 return ('\n'.join( |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
130 ['Unexpected output:'] + |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
131 ['', 'Actual:'] + |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
132 list(_repr_doc_scenes(excinfo.value.actual)) + |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
133 ['', 'Expected:'] + |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
134 list(_repr_expected_scenes(excinfo.value.expected)))) |
0 | 135 return super().repr_failure(excinfo) |
136 | |
137 | |
1
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
138 def _repr_doc_scenes(scenes): |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
139 for s in scenes: |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
140 yield 'Scene: "%s"' % s.header |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
141 for p in s.paragraphs: |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
142 yield ' %s: "%s"' % (_scene_element_type_str(p.type), |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
143 p.text) |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
144 |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
145 |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
146 def _repr_expected_scenes(scenes): |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
147 for s in scenes: |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
148 yield 'Scene: "%s"' % s[0] |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
149 for p in s[1:]: |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
150 if isinstance(p, str): |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
151 yield ' ACTION: "%s"' % p |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
152 else: |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
153 yield ' %s: "%s"' % (_scene_element_type_str(p.type), |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
154 p.text) |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
155 |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
156 |
2
59fe8cb6190d
Add lots of tests, fix lots of bugs.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
157 RE_BLANK_LINE = re.compile(r"^\s+$") |
59fe8cb6190d
Add lots of tests, fix lots of bugs.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
158 |
59fe8cb6190d
Add lots of tests, fix lots of bugs.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
159 |
0 | 160 def make_scenes(spec): |
161 if not isinstance(spec, list): | |
162 raise Exception("Script specs must be lists.") | |
163 | |
164 out = [] | |
165 cur_header = None | |
166 cur_paras = [] | |
167 | |
168 for item in spec: | |
1
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
169 if item == '<pagebreak>': |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
170 cur_paras.append(FontaineSceneElement(TYPE_PAGEBREAK, None)) |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
171 continue |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
172 |
2
59fe8cb6190d
Add lots of tests, fix lots of bugs.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
173 if RE_BLANK_LINE.match(item): |
59fe8cb6190d
Add lots of tests, fix lots of bugs.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
174 text = len(item) * '\n' |
59fe8cb6190d
Add lots of tests, fix lots of bugs.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
175 cur_paras.append(FontaineSceneElement(TYPE_EMPTYLINES, text)) |
59fe8cb6190d
Add lots of tests, fix lots of bugs.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
176 continue |
59fe8cb6190d
Add lots of tests, fix lots of bugs.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
177 |
0 | 178 token = item[:1] |
179 if token == '.': | |
180 if cur_header or cur_paras: | |
181 out.append([cur_header] + cur_paras) | |
182 cur_header = item[1:] | |
1
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
183 cur_paras = [] |
0 | 184 elif token == '!': |
1
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
185 if item[1:3] == '><': |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
186 cur_paras.append( |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
187 FontaineSceneElement(TYPE_CENTEREDACTION, item[3:])) |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
188 else: |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
189 cur_paras.append(item[1:]) |
0 | 190 elif token == '@': |
191 cur_paras.append(_c(item[1:])) | |
192 elif token == '=': | |
193 cur_paras.append(_d(item[1:])) | |
194 elif token == '_': | |
195 cur_paras.append(_p(item[1:])) | |
1
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
196 elif token == '>': |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
197 cur_paras.append(_t(item[1:])) |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
198 elif token == '~': |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
199 cur_paras.append(_l(item[1:])) |
0 | 200 else: |
201 raise Exception("Unknown token: %s" % token) | |
202 if cur_header or cur_paras: | |
203 out.append([cur_header] + cur_paras) | |
204 return out |