Mercurial > jouvence
comparison tests/conftest.py @ 13:ee741bbe96a8
Rename to 'Jouvence'.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 04 Jan 2017 09:02:29 -0800 |
parents | 59fe8cb6190d |
children | 2ef526c301cc |
comparison
equal
deleted
inserted
replaced
12:eea60b93da2c | 13:ee741bbe96a8 |
---|---|
1 import re | 1 import re |
2 import sys | 2 import sys |
3 import logging | 3 import logging |
4 import yaml | 4 import yaml |
5 import pytest | 5 import pytest |
6 from fontaine.document import ( | 6 from jouvence.document import ( |
7 FontaineSceneElement, | 7 JouvenceSceneElement, |
8 TYPE_ACTION, TYPE_CENTEREDACTION, TYPE_CHARACTER, TYPE_DIALOG, | 8 TYPE_ACTION, TYPE_CENTEREDACTION, TYPE_CHARACTER, TYPE_DIALOG, |
9 TYPE_PARENTHETICAL, TYPE_TRANSITION, TYPE_LYRICS, TYPE_PAGEBREAK, | 9 TYPE_PARENTHETICAL, TYPE_TRANSITION, TYPE_LYRICS, TYPE_PAGEBREAK, |
10 TYPE_EMPTYLINES, | 10 TYPE_EMPTYLINES, |
11 _scene_element_type_str) | 11 _scene_element_type_str) |
12 from fontaine.parser import FontaineParser, FontaineParserError | 12 from jouvence.parser import JouvenceParser, JouvenceParserError |
13 | 13 |
14 | 14 |
15 def pytest_addoption(parser): | 15 def pytest_addoption(parser): |
16 parser.addoption( | 16 parser.addoption( |
17 '--log-debug', | 17 '--log-debug', |
18 action='store_true', | 18 action='store_true', |
19 help="Sets the Fontaine logger to output debug info to stdout.") | 19 help="Sets the Jouvence logger to output debug info to stdout.") |
20 | 20 |
21 | 21 |
22 def pytest_configure(config): | 22 def pytest_configure(config): |
23 if config.getoption('--log-debug'): | 23 if config.getoption('--log-debug'): |
24 hdl = logging.StreamHandler(stream=sys.stdout) | 24 hdl = logging.StreamHandler(stream=sys.stdout) |
25 logging.getLogger('fontaine').addHandler(hdl) | 25 logging.getLogger('jouvence').addHandler(hdl) |
26 logging.getLogger('fontaine').setLevel(logging.DEBUG) | 26 logging.getLogger('jouvence').setLevel(logging.DEBUG) |
27 | 27 |
28 | 28 |
29 def pytest_collect_file(parent, path): | 29 def pytest_collect_file(parent, path): |
30 if path.ext == '.yaml' and path.basename.startswith("test"): | 30 if path.ext == '.yaml' and path.basename.startswith("test"): |
31 return FontaineScriptTestFile(path, parent) | 31 return JouvenceScriptTestFile(path, parent) |
32 return None | 32 return None |
33 | 33 |
34 | 34 |
35 def assert_scenes(actual, scenes): | 35 def assert_scenes(actual, scenes): |
36 assert len(actual) == len(scenes) | 36 assert len(actual) == len(scenes) |
46 assert_paragraph(a, e) | 46 assert_paragraph(a, e) |
47 | 47 |
48 | 48 |
49 def assert_paragraph(actual, expected): | 49 def assert_paragraph(actual, expected): |
50 if isinstance(expected, str): | 50 if isinstance(expected, str): |
51 assert isinstance(actual, FontaineSceneElement) | 51 assert isinstance(actual, JouvenceSceneElement) |
52 assert actual.type == TYPE_ACTION | 52 assert actual.type == TYPE_ACTION |
53 assert actual.text == expected | 53 assert actual.text == expected |
54 elif isinstance(expected, FontaineSceneElement): | 54 elif isinstance(expected, JouvenceSceneElement): |
55 assert isinstance(actual, FontaineSceneElement) | 55 assert isinstance(actual, JouvenceSceneElement) |
56 assert actual.type == expected.type | 56 assert actual.type == expected.type |
57 assert actual.text == expected.text | 57 assert actual.text == expected.text |
58 else: | 58 else: |
59 raise NotImplementedError("Don't know what this is: %s" % expected) | 59 raise NotImplementedError("Don't know what this is: %s" % expected) |
60 | 60 |
61 | 61 |
62 def _c(name): | 62 def _c(name): |
63 return FontaineSceneElement(TYPE_CHARACTER, name) | 63 return JouvenceSceneElement(TYPE_CHARACTER, name) |
64 | 64 |
65 | 65 |
66 def _p(text): | 66 def _p(text): |
67 return FontaineSceneElement(TYPE_PARENTHETICAL, text) | 67 return JouvenceSceneElement(TYPE_PARENTHETICAL, text) |
68 | 68 |
69 | 69 |
70 def _d(text): | 70 def _d(text): |
71 return FontaineSceneElement(TYPE_DIALOG, text) | 71 return JouvenceSceneElement(TYPE_DIALOG, text) |
72 | 72 |
73 | 73 |
74 def _t(text): | 74 def _t(text): |
75 return FontaineSceneElement(TYPE_TRANSITION, text) | 75 return JouvenceSceneElement(TYPE_TRANSITION, text) |
76 | 76 |
77 | 77 |
78 def _l(text): | 78 def _l(text): |
79 return FontaineSceneElement(TYPE_LYRICS, text) | 79 return JouvenceSceneElement(TYPE_LYRICS, text) |
80 | 80 |
81 | 81 |
82 class UnexpectedScriptOutput(Exception): | 82 class UnexpectedScriptOutput(Exception): |
83 def __init__(self, actual, expected): | 83 def __init__(self, actual, expected): |
84 self.actual = actual | 84 self.actual = actual |
85 self.expected = expected | 85 self.expected = expected |
86 | 86 |
87 | 87 |
88 class FontaineScriptTestFile(pytest.File): | 88 class JouvenceScriptTestFile(pytest.File): |
89 def collect(self): | 89 def collect(self): |
90 spec = yaml.load_all(self.fspath.open(encoding='utf8')) | 90 spec = yaml.load_all(self.fspath.open(encoding='utf8')) |
91 for i, item in enumerate(spec): | 91 for i, item in enumerate(spec): |
92 name = '%s_%d' % (self.fspath.basename, i) | 92 name = '%s_%d' % (self.fspath.basename, i) |
93 if 'test_name' in item: | 93 if 'test_name' in item: |
94 name += '_%s' % item['test_name'] | 94 name += '_%s' % item['test_name'] |
95 yield FontaineScriptTestItem(name, self, item) | 95 yield JouvenceScriptTestItem(name, self, item) |
96 | 96 |
97 | 97 |
98 class FontaineScriptTestItem(pytest.Item): | 98 class JouvenceScriptTestItem(pytest.Item): |
99 def __init__(self, name, parent, spec): | 99 def __init__(self, name, parent, spec): |
100 super().__init__(name, parent) | 100 super().__init__(name, parent) |
101 self.spec = spec | 101 self.spec = spec |
102 | 102 |
103 def reportinfo(self): | 103 def reportinfo(self): |
104 return self.fspath, 0, "fontaine script test: %s" % self.name | 104 return self.fspath, 0, "jouvence script test: %s" % self.name |
105 | 105 |
106 def runtest(self): | 106 def runtest(self): |
107 intext = self.spec.get('in') | 107 intext = self.spec.get('in') |
108 expected = self.spec.get('out') | 108 expected = self.spec.get('out') |
109 title = self.spec.get('title') | 109 title = self.spec.get('title') |
110 if intext is None or expected is None: | 110 if intext is None or expected is None: |
111 raise Exception("No 'in' or 'out' specified.") | 111 raise Exception("No 'in' or 'out' specified.") |
112 | 112 |
113 parser = FontaineParser() | 113 parser = JouvenceParser() |
114 doc = parser.parseString(intext) | 114 doc = parser.parseString(intext) |
115 if title is not None: | 115 if title is not None: |
116 assert title == doc.title_values | 116 assert title == doc.title_values |
117 | 117 |
118 exp_scenes = make_scenes(expected) | 118 exp_scenes = make_scenes(expected) |
120 assert_scenes(doc.scenes, exp_scenes) | 120 assert_scenes(doc.scenes, exp_scenes) |
121 except AssertionError: | 121 except AssertionError: |
122 raise UnexpectedScriptOutput(doc.scenes, exp_scenes) | 122 raise UnexpectedScriptOutput(doc.scenes, exp_scenes) |
123 | 123 |
124 def repr_failure(self, excinfo): | 124 def repr_failure(self, excinfo): |
125 if isinstance(excinfo.value, FontaineParserError): | 125 if isinstance(excinfo.value, JouvenceParserError): |
126 return ('\n'.join( | 126 return ('\n'.join( |
127 ['Parser error:', str(excinfo.value)])) | 127 ['Parser error:', str(excinfo.value)])) |
128 if isinstance(excinfo.value, UnexpectedScriptOutput): | 128 if isinstance(excinfo.value, UnexpectedScriptOutput): |
129 return ('\n'.join( | 129 return ('\n'.join( |
130 ['Unexpected output:'] + | 130 ['Unexpected output:'] + |
165 cur_header = None | 165 cur_header = None |
166 cur_paras = [] | 166 cur_paras = [] |
167 | 167 |
168 for item in spec: | 168 for item in spec: |
169 if item == '<pagebreak>': | 169 if item == '<pagebreak>': |
170 cur_paras.append(FontaineSceneElement(TYPE_PAGEBREAK, None)) | 170 cur_paras.append(JouvenceSceneElement(TYPE_PAGEBREAK, None)) |
171 continue | 171 continue |
172 | 172 |
173 if RE_BLANK_LINE.match(item): | 173 if RE_BLANK_LINE.match(item): |
174 text = len(item) * '\n' | 174 text = len(item) * '\n' |
175 cur_paras.append(FontaineSceneElement(TYPE_EMPTYLINES, text)) | 175 cur_paras.append(JouvenceSceneElement(TYPE_EMPTYLINES, text)) |
176 continue | 176 continue |
177 | 177 |
178 token = item[:1] | 178 token = item[:1] |
179 if token == '.': | 179 if token == '.': |
180 if cur_header or cur_paras: | 180 if cur_header or cur_paras: |
182 cur_header = item[1:] | 182 cur_header = item[1:] |
183 cur_paras = [] | 183 cur_paras = [] |
184 elif token == '!': | 184 elif token == '!': |
185 if item[1:3] == '><': | 185 if item[1:3] == '><': |
186 cur_paras.append( | 186 cur_paras.append( |
187 FontaineSceneElement(TYPE_CENTEREDACTION, item[3:])) | 187 JouvenceSceneElement(TYPE_CENTEREDACTION, item[3:])) |
188 else: | 188 else: |
189 cur_paras.append(item[1:]) | 189 cur_paras.append(item[1:]) |
190 elif token == '@': | 190 elif token == '@': |
191 cur_paras.append(_c(item[1:])) | 191 cur_paras.append(_c(item[1:])) |
192 elif token == '=': | 192 elif token == '=': |