Continue to sketch.
Chris Pressey
6 years ago
0 | 0 | import codecs |
1 | 1 | import os |
2 | from os.path import basename | |
3 | 2 | import re |
4 | 3 | from subprocess import Popen, PIPE |
5 | 4 | from tempfile import mkstemp |
204 | 203 | u'> ': LiterateCode, |
205 | 204 | } |
206 | 205 | self.PREFIXES = self.PREFIX_MAP.keys() |
206 | self.VALID_PATTERNS = [ | |
207 | [u'->'], | |
208 | [u'> '], | |
209 | [u'| ', u'= '], | |
210 | [u'| ', u'? '], | |
211 | [u'| ', u'+ ', u'= '], | |
212 | [u'| ', u'+ ', u'? '], | |
213 | [u'+ ', u'= '], | |
214 | [u'+ ', u'? '], | |
215 | ] | |
207 | 216 | |
208 | 217 | def __repr__(self): |
209 | 218 | filename_repr = '' if self.filename is None else ', filename=%r' % self.filename |
281 | 290 | ("line %d: " % self.line_num) + |
282 | 291 | "test body must be followed by expectation or test input") |
283 | 292 | |
284 | valid_patterns = [ | |
285 | [u'->'], | |
286 | [u'> '], | |
287 | [u'| ', u'= '], | |
288 | [u'| ', u'? '], | |
289 | [u'| ', u'+ ', u'= '], | |
290 | [u'| ', u'+ ', u'? '], | |
291 | [u'+ ', u'= '], | |
292 | [u'+ ', u'? '], | |
293 | ] | |
294 | if pattern_prefixes in valid_patterns: | |
293 | if pattern_prefixes in self.VALID_PATTERNS: | |
295 | 294 | return [self.PREFIX_MAP[prefix](line_num=self.line_num, filename=self.filename, lines=lines) for (prefix, lines) in pattern] |
296 | 295 | raise FalderalSyntaxError( |
297 | 296 | ("line %d: " % self.line_num) + |
318 | 317 | ("line %d: " % self.line_num) + |
319 | 318 | "test body must be followed by expectation or test input") |
320 | 319 | |
320 | if pattern_prefixes not in self.VALID_PATTERNS: | |
321 | raise FalderalSyntaxError( | |
322 | ("line %d: " % self.line_num) + | |
323 | "incorrectly formatted test block") | |
324 | ||
321 | 325 | if pattern_prefixes == [u'->']: |
322 | # Pragma | |
323 | pass | |
326 | return Pragma(line_num=self.line_num, filename=self.filename, lines=pattern[0][1]) | |
324 | 327 | elif pattern_prefixes[-1] in [u'= ', u'? ']: |
325 | # TODO: valid patterns, several other things | |
326 | ||
328 | # TODO: several things | |
329 | ||
327 | 330 | if current_functionality is None: |
328 | 331 | raise FalderalSyntaxError( |
329 | 332 | ("line %d: " % self.line_num) + |
476 | 479 | new_blocks.extend(block.split()) |
477 | 480 | self.blocks = new_blocks |
478 | 481 | |
479 | def parse_lines_to_tests(self): | |
482 | def parse_lines_to_tests(self, functionalities): | |
480 | 483 | r"""Parse the lines of the Document into Tests. |
481 | 484 | |
482 | 485 | """ |
526 | 529 | last_test_body_block = None |
527 | 530 | last_used_test_body_block = None |
528 | 531 | last_test_input_block = None |
532 | current_functionality = None | |
529 | 533 | |
530 | 534 | tests = [] |
531 | 535 | for block in blocks: |
535 | 539 | last_desc_block = block |
536 | 540 | continue |
537 | 541 | |
538 | test_or_pragma = block.classify() | |
542 | test_or_pragma = block.classify(current_functionality=current_functionality) | |
539 | 543 | |
540 | 544 | if isinstance(test_or_pragma, Test): |
541 | 545 | tests.append(test_or_pragma) |
542 | 546 | elif isinstance(test_or_pragma, Pragma): |
543 | pass | |
544 | # execute the pragma | |
547 | block = test_or_pragma | |
548 | pragma_text = block.text(seperator=' ') | |
549 | match = re.match(r'^\s*Tests\s+for\s+functionality\s*\"(.*?)\"\s*$', pragma_text) | |
550 | if match: | |
551 | functionality_name = match.group(1) | |
552 | current_functionality = functionalities.setdefault( | |
553 | functionality_name, | |
554 | Functionality(functionality_name) | |
555 | ) | |
556 | match = re.match(r'^\s*Functionality\s*\"(.*?)\"\s*is\s+implemented\s+by\s+shell\s+command\s*\"(.*?)\"\s*$', pragma_text) | |
557 | if match: | |
558 | functionality_name = match.group(1) | |
559 | command = match.group(2) | |
560 | functionality = functionalities.setdefault( | |
561 | functionality_name, | |
562 | Functionality(functionality_name) | |
563 | ) | |
564 | implementation = ShellImplementation(command) | |
565 | functionality.add_implementation(implementation) | |
545 | 566 | else: |
546 | 567 | raise NotImplementedError('need Pragma or Test') |
547 | 568 |