Simplify.
Chris Pressey
6 years ago
179 | 179 | >>> b.append(u'| Test body here.') |
180 | 180 | >>> b.append(u'= Expected result here.') |
181 | 181 | >>> print b.classify(ParseState(current_functionality=f)) |
182 | Test(body_block=TestBody(line_num=1), input_block=None, | |
182 | Test(body_block=Block(line_num=1), input_block=None, | |
183 | 183 | expectation=OutputOutcome(u'Expected result here.'), |
184 | 184 | functionality=Functionality('foo'), desc_block=None, |
185 | 185 | body=u'Test body here.', input=None) |
188 | 188 | >>> b.append(u'| Test body here.') |
189 | 189 | >>> b.append(u'? Expected error here.') |
190 | 190 | >>> print b.classify(ParseState(current_functionality=f)) |
191 | Test(body_block=TestBody(line_num=1), input_block=None, | |
191 | Test(body_block=Block(line_num=1), input_block=None, | |
192 | 192 | expectation=ErrorOutcome(u'Expected error here.'), |
193 | 193 | functionality=Functionality('foo'), desc_block=None, |
194 | 194 | body=u'Test body here.', input=None) |
195 | 195 | |
196 | 196 | """ |
197 | ||
198 | PREFIXES = [ | |
199 | u'| ', | |
200 | u'+ ', | |
201 | u'? ', | |
202 | u'= ', | |
203 | u'->', | |
204 | ] | |
205 | VALID_PATTERNS = [ | |
206 | [u'->'], | |
207 | [u'> '], | |
208 | [u'| ', u'= '], | |
209 | [u'| ', u'? '], | |
210 | [u'| ', u'+ ', u'= '], | |
211 | [u'| ', u'+ ', u'? '], | |
212 | [u'+ ', u'= '], | |
213 | [u'+ ', u'? '], | |
214 | ] | |
197 | 215 | |
198 | 216 | def __init__(self, line_num=1, filename=None, lines=None): |
199 | 217 | if lines is None: |
201 | 219 | self.lines = lines |
202 | 220 | self.line_num = line_num |
203 | 221 | self.filename = filename |
204 | self.PREFIXES = [ | |
205 | u'| ', | |
206 | u'+ ', | |
207 | u'? ', | |
208 | u'= ', | |
209 | u'->', | |
210 | ] | |
211 | self.VALID_PATTERNS = [ | |
212 | [u'->'], | |
213 | [u'> '], | |
214 | [u'| ', u'= '], | |
215 | [u'| ', u'? '], | |
216 | [u'| ', u'+ ', u'= '], | |
217 | [u'| ', u'+ ', u'? '], | |
218 | [u'+ ', u'= '], | |
219 | [u'+ ', u'? '], | |
220 | ] | |
221 | 222 | |
222 | 223 | def __repr__(self): |
223 | 224 | filename_repr = '' if self.filename is None else ', filename=%r' % self.filename |
277 | 278 | def classify(self, state): |
278 | 279 | """Return the Test or Pragma that this Block represents.""" |
279 | 280 | |
280 | def make_block_from_pattern(cls, pattern, prefix): | |
281 | pattern = self.deconstruct() | |
282 | pattern_prefixes = [p[0] for p in pattern] | |
283 | ||
284 | def make_block_from_pattern(prefix): | |
281 | 285 | lines = None |
282 | 286 | for (candidate_prefix, candidate_lines) in pattern: |
283 | 287 | if candidate_prefix == prefix: |
285 | 289 | break |
286 | 290 | if lines is None: |
287 | 291 | return None |
288 | return cls(line_num=self.line_num, filename=self.filename, lines=lines) | |
289 | ||
290 | pattern = self.deconstruct() | |
291 | pattern_prefixes = [p[0] for p in pattern] | |
292 | return Block( | |
293 | line_num=self.line_num, filename=self.filename, lines=lines | |
294 | ) | |
292 | 295 | |
293 | 296 | if '' in pattern_prefixes: |
294 | 297 | # There is plain, non-prefixed text embedded somewhere in this Block. |
319 | 322 | ("line %d: " % self.line_num) + |
320 | 323 | "functionality under test not specified") |
321 | 324 | |
322 | body_block = make_block_from_pattern(TestBody, pattern, u'| ') or state.last_test_body_block | |
323 | input_block = make_block_from_pattern(TestInput, pattern, u'+ ') or state.last_test_input_block | |
325 | body_block = make_block_from_pattern(u'| ') or state.last_test_body_block | |
326 | input_block = make_block_from_pattern(u'+ ') or state.last_test_input_block | |
324 | 327 | |
325 | 328 | if pattern_prefixes[-1] == u'= ': |
326 | expectation_block = make_block_from_pattern(Block, pattern, u'= ') | |
327 | expectation = OutputOutcome(expectation_block.text()) | |
329 | expectation = OutputOutcome(make_block_from_pattern(u'= ').text()) | |
328 | 330 | elif pattern_prefixes[-1] == u'? ': |
329 | expectation_block = make_block_from_pattern(Block, pattern, u'? ') | |
330 | expectation = ErrorOutcome(expectation_block.text()) | |
331 | expectation = ErrorOutcome(make_block_from_pattern(u'? ').text()) | |
331 | 332 | else: |
332 | 333 | raise NotImplementedError |
333 | 334 | |
338 | 339 | desc_block=state.last_desc_block) |
339 | 340 | |
340 | 341 | state.last_test_body_block = body_block |
341 | #state.last_test_input_block = input_block | |
342 | 342 | |
343 | 343 | return test |
344 | 344 | else: |
369 | 369 | functionality.add_implementation(implementation) |
370 | 370 | |
371 | 371 | |
372 | class TestBody(Block): | |
373 | pass | |
374 | ||
375 | ||
376 | class TestInput(Block): | |
377 | pass | |
378 | ||
379 | ||
380 | 372 | class InterveningText(Block): |
381 | 373 | pass |
382 | 374 | |
527 | 519 | >>> d.append(u" | This is some test body.") |
528 | 520 | >>> d.append(u' = Expected result') |
529 | 521 | >>> d.extract_tests(functionalities) |
530 | [Test(body_block=TestBody(line_num=4), input_block=None, | |
522 | [Test(body_block=Block(line_num=4), input_block=None, | |
531 | 523 | expectation=OutputOutcome(u'Expected result'), |
532 | 524 | functionality=Functionality(u'Parse Thing'), |
533 | 525 | desc_block=InterveningText(line_num=1), |
557 | 549 | [u'This is some test body.\nIt extends over two lines.', |
558 | 550 | u'Test with input', u'Test with input', u'Thing'] |
559 | 551 | >>> [t.input_block for t in tests] |
560 | [None, TestInput(line_num=8), TestInput(line_num=12), None] | |
552 | [None, Block(line_num=8), Block(line_num=12), None] | |
561 | 553 | >>> tests[1].input_block.text() |
562 | 554 | u'input-for-test' |
563 | 555 | >>> tests[2].input_block.text() |
838 | 830 | class Test(object): |
839 | 831 | """An object representing a Falderal test. |
840 | 832 | |
841 | Normally a TestBody block is given as the body_block argument, | |
842 | and possibly a TestInput block is given as input_block, | |
833 | Normally a test body Block is given as the body_block argument, | |
834 | and possibly a test input Block is given as input_block, | |
843 | 835 | and the body and input attributes are derived from it. However |
844 | 836 | in the absence of these blocks (as in many of the internal tests) |
845 | 837 | a body and/or input may be passed alone. |
846 | 838 | |
847 | 839 | TODO: maybe write a helper function for that instead. |
848 | 840 | |
849 | >>> b = TestBody() | |
841 | >>> b = Block() | |
850 | 842 | >>> b.append(u'foo') |
851 | 843 | >>> b.append(u'bar') |
852 | >>> i = TestInput() | |
844 | >>> i = Block() | |
853 | 845 | >>> i.append(u'green') |
854 | 846 | >>> t = Test(body_block=b, input_block=i) |
855 | 847 | >>> print t.body |