git @ Cat's Eye Technologies Falderal / 19c0063
Disable tests we don't expect to pass yet. Add more Block tests. Chris Pressey 5 years ago
1 changed file(s) with 172 addition(s) and 136 deletion(s). Raw diff Collapse all Expand all
145145 """A segment of a Falderal-formatted file.
146146
147147 >>> 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')
150150 >>> print b.text()
151 test body line 1
152 test body line 2
151 line 1
152 line 2
153153 >>> 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)]
155183
156184 """
157185
158 def __init__(self, line_num=None, filename=None, lines=None):
186 def __init__(self, line_num=1, filename=None, lines=None):
159187 if lines is None:
160188 lines = []
161189 self.lines = lines
172200 self.PREFIXES = self.PREFIX_MAP.keys()
173201
174202 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
177206 )
178207
179208 def __unicode__(self):
203232 acc = []
204233
205234 for line in self.lines:
206 prefix_of_line = None
235 prefix_of_line = ''
207236 for prefix in self.PREFIXES:
208237 if line.startswith(prefix):
209238 prefix_of_line = prefix
211240 if prefix_of_line == prefix_state:
212241 acc.append(line[len(prefix_of_line):])
213242 else:
214 pairs.append((prefix_state, acc))
243 if acc:
244 pairs.append((prefix_state, acc))
215245 prefix_state = prefix_of_line
216246 acc = []
217247 acc.append(line[len(prefix_of_line):])
218248
219 pairs.append((prefix_state, acc))
249 if acc:
250 pairs.append((prefix_state, acc))
220251
221252 return pairs
222253
227258 pattern_prefixes = [p[0] for p in pattern]
228259 if len(pattern_prefixes) == 1:
229260 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])]
230264
231265 return []
232266
289323 def parse_lines_to_blocks(self):
290324 r"""Parse the lines of the Document into Blocks.
291325
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]
307343
308344 """
309345 indent = None
358394 def parse_blocks_to_tests(self, functionalities):
359395 r"""Assemble a list of Tests from the blocks in this Document.
360396
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')]
460496
461497 """
462498 if self.blocks is None:
762798
763799 TODO: maybe write a helper function for that instead.
764800
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
776812
777813 """
778814 def __init__(self, body_block=None, input_block=None, expectation=None,