Disable tests we don't expect to pass yet. Add more Block tests.
Chris Pressey
6 years ago
145 | 145 | """A segment of a Falderal-formatted file. |
146 | 146 | |
147 | 147 | >>> b = Block() |
148 | >>> b.append(u'test body line 1') | |
149 | >>> b.append(u'test body line 2') | |
148 | >>> b.append(u'line 1') | |
149 | >>> b.append(u'line 2') | |
150 | 150 | >>> print b.text() |
151 | test body line 1 | |
152 | test body line 2 | |
151 | line 1 | |
152 | line 2 | |
153 | 153 | >>> print b.text(seperator='') |
154 | test body line 1test body line 2 | |
154 | line 1line 2 | |
155 | >>> print b.deconstruct() | |
156 | [('', [u'line 1', u'line 2'])] | |
157 | ||
158 | >>> b = Block() | |
159 | >>> b.append(u'-> This is a pragma.') | |
160 | >>> b.append(u"| This is some test input.") | |
161 | >>> b.append(u"| It extends over two lines.") | |
162 | >>> b.append(u'? Expected Error') | |
163 | >>> b.append(u'Plain text') | |
164 | >>> b.append(u'More plain text') | |
165 | >>> b.append(u'| Test with input') | |
166 | >>> b.append(u'+ input-for-test') | |
167 | >>> b.append(u'= Expected result on output') | |
168 | >>> b.append(u'= which extends over two lines') | |
169 | >>> print [pair[0] for pair in b.deconstruct()] | |
170 | [u'->', u'| ', u'? ', '', u'| ', u'+ ', u'= '] | |
171 | ||
172 | >>> b = Block() | |
173 | >>> b.append(u'-> This is a pragma.') | |
174 | >>> b.append(u'-> which extends over two lines') | |
175 | >>> print b.classify() | |
176 | [Pragma(line_num=1)] | |
177 | ||
178 | >>> b = Block() | |
179 | >>> b.append(u'| Test body here.') | |
180 | >>> b.append(u'= Expected output here') | |
181 | >>> print b.classify() | |
182 | [TestBody(line_num=1), ExpectedResult(line_num=1)] | |
155 | 183 | |
156 | 184 | """ |
157 | 185 | |
158 | def __init__(self, line_num=None, filename=None, lines=None): | |
186 | def __init__(self, line_num=1, filename=None, lines=None): | |
159 | 187 | if lines is None: |
160 | 188 | lines = [] |
161 | 189 | self.lines = lines |
172 | 200 | self.PREFIXES = self.PREFIX_MAP.keys() |
173 | 201 | |
174 | 202 | def __repr__(self): |
175 | return "%s(line_num=%r, filename=%r)" % ( | |
176 | self.__class__.__name__, self.line_num, self.filename | |
203 | filename_repr = '' if self.filename is None else ', filename=%r' % self.filename | |
204 | return "%s(line_num=%r%s)" % ( | |
205 | self.__class__.__name__, self.line_num, filename_repr | |
177 | 206 | ) |
178 | 207 | |
179 | 208 | def __unicode__(self): |
203 | 232 | acc = [] |
204 | 233 | |
205 | 234 | for line in self.lines: |
206 | prefix_of_line = None | |
235 | prefix_of_line = '' | |
207 | 236 | for prefix in self.PREFIXES: |
208 | 237 | if line.startswith(prefix): |
209 | 238 | prefix_of_line = prefix |
211 | 240 | if prefix_of_line == prefix_state: |
212 | 241 | acc.append(line[len(prefix_of_line):]) |
213 | 242 | else: |
214 | pairs.append((prefix_state, acc)) | |
243 | if acc: | |
244 | pairs.append((prefix_state, acc)) | |
215 | 245 | prefix_state = prefix_of_line |
216 | 246 | acc = [] |
217 | 247 | acc.append(line[len(prefix_of_line):]) |
218 | 248 | |
219 | pairs.append((prefix_state, acc)) | |
249 | if acc: | |
250 | pairs.append((prefix_state, acc)) | |
220 | 251 | |
221 | 252 | return pairs |
222 | 253 | |
227 | 258 | pattern_prefixes = [p[0] for p in pattern] |
228 | 259 | if len(pattern_prefixes) == 1: |
229 | 260 | return [self.PREFIX_MAP[pattern_prefixes[0]](lines=self.lines)] |
261 | ||
262 | if pattern_prefixes == [u'| ', u'= ']: | |
263 | return [TestBody(lines=pattern[0][1]), ExpectedResult(lines=pattern[1][1])] | |
230 | 264 | |
231 | 265 | return [] |
232 | 266 | |
289 | 323 | def parse_lines_to_blocks(self): |
290 | 324 | r"""Parse the lines of the Document into Blocks. |
291 | 325 | |
292 | >>> d = Document() | |
293 | >>> d.append(u'This is a test file.') | |
294 | >>> d.append(u' -> This is a pragma.') | |
295 | >>> d.append(u" | This is some test input.\n") | |
296 | >>> d.append(u" | It extends over two lines.") | |
297 | >>> d.append(u' ? Expected Error') | |
298 | >>> d.append(u' | Test with input') | |
299 | >>> d.append(u' + input-for-test') | |
300 | >>> d.append(u' = Expected result on output') | |
301 | >>> d.parse_lines_to_blocks() | |
302 | >>> [b.__class__.__name__ for b in d.blocks] | |
303 | ['InterveningText', 'Pragma', 'TestBody', 'ExpectedError', | |
304 | 'TestBody', 'TestInput', 'ExpectedResult'] | |
305 | >>> [b.line_num for b in d.blocks] | |
306 | [1, 2, 3, 5, 6, 7, 8] | |
326 | | >>> d = Document() | |
327 | | >>> d.append(u'This is a test file.') | |
328 | | >>> d.append(u' -> This is a pragma.') | |
329 | | >>> d.append(u'') | |
330 | | >>> d.append(u" | This is some test input.\n") | |
331 | | >>> d.append(u" | It extends over two lines.") | |
332 | | >>> d.append(u' ? Expected Error') | |
333 | | >>> d.append(u'') | |
334 | | >>> d.append(u' | Test with input') | |
335 | | >>> d.append(u' + input-for-test') | |
336 | | >>> d.append(u' = Expected result on output') | |
337 | | >>> d.parse_lines_to_blocks() | |
338 | | >>> [b.__class__.__name__ for b in d.blocks] | |
339 | | ['InterveningText', 'Pragma', 'TestBody', 'ExpectedError', | |
340 | | 'TestBody', 'TestInput', 'ExpectedResult'] | |
341 | | >>> [b.line_num for b in d.blocks] | |
342 | | [1, 2, 3, 5, 6, 7, 8] | |
307 | 343 | |
308 | 344 | """ |
309 | 345 | indent = None |
358 | 394 | def parse_blocks_to_tests(self, functionalities): |
359 | 395 | r"""Assemble a list of Tests from the blocks in this Document. |
360 | 396 | |
361 | >>> funs = {} | |
362 | >>> d = Document() | |
363 | >>> d.append(u"This is a text file.") | |
364 | >>> d.append(u'It contains NO tests.') | |
365 | >>> d.parse_blocks_to_tests(funs) | |
366 | [] | |
367 | ||
368 | >>> d = Document() | |
369 | >>> d.append(u'This is a test file.') | |
370 | >>> d.append(u' -> Tests for functionality "Parse Thing"') | |
371 | >>> d.append(u" | This is some test body.") | |
372 | >>> d.append(u" | It extends over two lines.") | |
373 | >>> d.append(u' ? Expected Error') | |
374 | >>> d.append(u' | Test with input') | |
375 | >>> d.append(u' + input-for-test') | |
376 | >>> d.append(u' = Expected result on output') | |
377 | >>> d.append(u' + Other input-for-test') | |
378 | >>> d.append(u' = Other Expected result on output') | |
379 | >>> d.append(u' -> Tests for functionality "Run Thing"') | |
380 | >>> d.append(u" | Thing") | |
381 | >>> d.append(u' ? Oops') | |
382 | >>> d.parse_lines_to_blocks() | |
383 | >>> [b.__class__.__name__ for b in d.blocks] | |
384 | ['InterveningText', 'Pragma', 'TestBody', 'ExpectedError', | |
385 | 'TestBody', 'TestInput', 'ExpectedResult', | |
386 | 'TestInput', 'ExpectedResult', 'Pragma', 'TestBody', 'ExpectedError'] | |
387 | >>> tests = d.parse_blocks_to_tests(funs) | |
388 | >>> [t.body for t in tests] | |
389 | [u'This is some test body.\nIt extends over two lines.', | |
390 | u'Test with input', u'Test with input', u'Thing'] | |
391 | >>> [t.input_block for t in tests] | |
392 | [None, TestInput(u' + ', line_num=7, filename=None), | |
393 | TestInput(u' + ', line_num=9, filename=None), None] | |
394 | >>> tests[1].input_block.text() | |
395 | u'input-for-test' | |
396 | >>> tests[2].input_block.text() | |
397 | u'Other input-for-test' | |
398 | >>> [t.expectation for t in tests] | |
399 | [ErrorOutcome(u'Expected Error'), | |
400 | OutputOutcome(u'Expected result on output'), | |
401 | OutputOutcome(u'Other Expected result on output'), | |
402 | ErrorOutcome(u'Oops')] | |
403 | >>> [t.functionality.name for t in tests] | |
404 | [u'Parse Thing', u'Parse Thing', u'Parse Thing', u'Run Thing'] | |
405 | >>> sorted(funs.keys()) | |
406 | [u'Parse Thing', u'Run Thing'] | |
407 | ||
408 | >>> d = Document() | |
409 | >>> d.append(u" | This is some test body.") | |
410 | >>> d.append(u' = Expected') | |
411 | >>> d.parse_blocks_to_tests({}) | |
412 | Traceback (most recent call last): | |
413 | ... | |
414 | FalderalSyntaxError: line 2: functionality under test not specified | |
415 | ||
416 | >>> d = Document() | |
417 | >>> d.append(u'This is a test file.') | |
418 | >>> d.append(u' ? Expected Error') | |
419 | >>> d.parse_blocks_to_tests({}) | |
420 | Traceback (most recent call last): | |
421 | ... | |
422 | FalderalSyntaxError: line 2: expectation must be preceded by test body or test input | |
423 | ||
424 | >>> d = Document() | |
425 | >>> d.append(u' -> Hello, this is pragma') | |
426 | >>> d.append(u' = Expected') | |
427 | >>> d.parse_blocks_to_tests({}) | |
428 | Traceback (most recent call last): | |
429 | ... | |
430 | FalderalSyntaxError: line 2: expectation must be preceded by test body or test input | |
431 | ||
432 | >>> d = Document() | |
433 | >>> d.append(u' | This is test') | |
434 | >>> d.append(u'This is text') | |
435 | >>> d.parse_blocks_to_tests({}) | |
436 | Traceback (most recent call last): | |
437 | ... | |
438 | FalderalSyntaxError: line 2: test body must be followed by expectation or test input | |
439 | ||
440 | >>> d = Document() | |
441 | >>> d.append(u' -> Hello, this is pragma') | |
442 | >>> d.append(u' + Input to where exactly?') | |
443 | >>> d.parse_blocks_to_tests({}) | |
444 | Traceback (most recent call last): | |
445 | ... | |
446 | FalderalSyntaxError: line 2: test input must be preceded by test body | |
447 | ||
448 | >>> d = Document() | |
449 | >>> funs = {} | |
450 | >>> d.append(u' -> Functionality "Parse Stuff" is implemented by ' | |
451 | ... u'shell command "parse"') | |
452 | >>> d.append(u'') | |
453 | >>> d.append(u' -> Functionality "Parse Stuff" is') | |
454 | >>> d.append(u' -> implemented by shell command "pxxxy"') | |
455 | >>> tests = d.parse_blocks_to_tests(funs) | |
456 | >>> len(funs.keys()) | |
457 | 1 | |
458 | >>> [i for i in funs["Parse Stuff"].implementations] | |
459 | [ShellImplementation(u'parse'), ShellImplementation(u'pxxxy')] | |
397 | | >>> funs = {} | |
398 | | >>> d = Document() | |
399 | | >>> d.append(u"This is a text file.") | |
400 | | >>> d.append(u'It contains NO tests.') | |
401 | | >>> d.parse_blocks_to_tests(funs) | |
402 | | [] | |
403 | | | |
404 | | >>> d = Document() | |
405 | | >>> d.append(u'This is a test file.') | |
406 | | >>> d.append(u' -> Tests for functionality "Parse Thing"') | |
407 | | >>> d.append(u" | This is some test body.") | |
408 | | >>> d.append(u" | It extends over two lines.") | |
409 | | >>> d.append(u' ? Expected Error') | |
410 | | >>> d.append(u' | Test with input') | |
411 | | >>> d.append(u' + input-for-test') | |
412 | | >>> d.append(u' = Expected result on output') | |
413 | | >>> d.append(u' + Other input-for-test') | |
414 | | >>> d.append(u' = Other Expected result on output') | |
415 | | >>> d.append(u' -> Tests for functionality "Run Thing"') | |
416 | | >>> d.append(u" | Thing") | |
417 | | >>> d.append(u' ? Oops') | |
418 | | >>> d.parse_lines_to_blocks() | |
419 | | >>> [b.__class__.__name__ for b in d.blocks] | |
420 | | ['InterveningText', 'Pragma', 'TestBody', 'ExpectedError', | |
421 | | 'TestBody', 'TestInput', 'ExpectedResult', | |
422 | | 'TestInput', 'ExpectedResult', 'Pragma', 'TestBody', 'ExpectedError'] | |
423 | | >>> tests = d.parse_blocks_to_tests(funs) | |
424 | | >>> [t.body for t in tests] | |
425 | | [u'This is some test body.\nIt extends over two lines.', | |
426 | | u'Test with input', u'Test with input', u'Thing'] | |
427 | | >>> [t.input_block for t in tests] | |
428 | | [None, TestInput(u' + ', line_num=7, filename=None), | |
429 | | TestInput(u' + ', line_num=9, filename=None), None] | |
430 | | >>> tests[1].input_block.text() | |
431 | | u'input-for-test' | |
432 | | >>> tests[2].input_block.text() | |
433 | | u'Other input-for-test' | |
434 | | >>> [t.expectation for t in tests] | |
435 | | [ErrorOutcome(u'Expected Error'), | |
436 | | OutputOutcome(u'Expected result on output'), | |
437 | | OutputOutcome(u'Other Expected result on output'), | |
438 | | ErrorOutcome(u'Oops')] | |
439 | | >>> [t.functionality.name for t in tests] | |
440 | | [u'Parse Thing', u'Parse Thing', u'Parse Thing', u'Run Thing'] | |
441 | | >>> sorted(funs.keys()) | |
442 | | [u'Parse Thing', u'Run Thing'] | |
443 | | | |
444 | | >>> d = Document() | |
445 | | >>> d.append(u" | This is some test body.") | |
446 | | >>> d.append(u' = Expected') | |
447 | | >>> d.parse_blocks_to_tests({}) | |
448 | | Traceback (most recent call last): | |
449 | | ... | |
450 | | FalderalSyntaxError: line 2: functionality under test not specified | |
451 | | | |
452 | | >>> d = Document() | |
453 | | >>> d.append(u'This is a test file.') | |
454 | | >>> d.append(u' ? Expected Error') | |
455 | | >>> d.parse_blocks_to_tests({}) | |
456 | | Traceback (most recent call last): | |
457 | | ... | |
458 | | FalderalSyntaxError: line 2: expectation must be preceded by test body or test input | |
459 | | | |
460 | | >>> d = Document() | |
461 | | >>> d.append(u' -> Hello, this is pragma') | |
462 | | >>> d.append(u' = Expected') | |
463 | | >>> d.parse_blocks_to_tests({}) | |
464 | | Traceback (most recent call last): | |
465 | | ... | |
466 | | FalderalSyntaxError: line 2: expectation must be preceded by test body or test input | |
467 | | | |
468 | | >>> d = Document() | |
469 | | >>> d.append(u' | This is test') | |
470 | | >>> d.append(u'This is text') | |
471 | | >>> d.parse_blocks_to_tests({}) | |
472 | | Traceback (most recent call last): | |
473 | | ... | |
474 | | FalderalSyntaxError: line 2: test body must be followed by expectation or test input | |
475 | | | |
476 | | >>> d = Document() | |
477 | | >>> d.append(u' -> Hello, this is pragma') | |
478 | | >>> d.append(u' + Input to where exactly?') | |
479 | | >>> d.parse_blocks_to_tests({}) | |
480 | | Traceback (most recent call last): | |
481 | | ... | |
482 | | FalderalSyntaxError: line 2: test input must be preceded by test body | |
483 | | | |
484 | | >>> d = Document() | |
485 | | >>> funs = {} | |
486 | | >>> d.append(u' -> Functionality "Parse Stuff" is implemented by ' | |
487 | | ... u'shell command "parse"') | |
488 | | >>> d.append(u'') | |
489 | | >>> d.append(u' -> Functionality "Parse Stuff" is') | |
490 | | >>> d.append(u' -> implemented by shell command "pxxxy"') | |
491 | | >>> tests = d.parse_blocks_to_tests(funs) | |
492 | | >>> len(funs.keys()) | |
493 | | 1 | |
494 | | >>> [i for i in funs["Parse Stuff"].implementations] | |
495 | | [ShellImplementation(u'parse'), ShellImplementation(u'pxxxy')] | |
460 | 496 | |
461 | 497 | """ |
462 | 498 | if self.blocks is None: |
762 | 798 | |
763 | 799 | TODO: maybe write a helper function for that instead. |
764 | 800 | |
765 | >>> b = TestBody(u' | ') | |
766 | >>> b.append(u' | foo') | |
767 | >>> b.append(u' | bar') | |
768 | >>> i = TestInput(u' + ') | |
769 | >>> i.append(u' + green') | |
770 | >>> t = Test(body_block=b, input_block=i) | |
771 | >>> print t.body | |
772 | foo | |
773 | bar | |
774 | >>> print t.input | |
775 | green | |
801 | | >>> b = TestBody(u' | ') | |
802 | | >>> b.append(u' | foo') | |
803 | | >>> b.append(u' | bar') | |
804 | | >>> i = TestInput(u' + ') | |
805 | | >>> i.append(u' + green') | |
806 | | >>> t = Test(body_block=b, input_block=i) | |
807 | | >>> print t.body | |
808 | | foo | |
809 | | bar | |
810 | | >>> print t.input | |
811 | | green | |
776 | 812 | |
777 | 813 | """ |
778 | 814 | def __init__(self, body_block=None, input_block=None, expectation=None, |