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