git @ Cat's Eye Technologies Falderal / 6949b11
Yet more sketching. Chris Pressey 5 years ago
1 changed file(s) with 40 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
296296 ("line %d: " % self.line_num) +
297297 "incorrectly formatted test block")
298298
299 def classify(self, current_functionality=None, last_desc_block=None):
299 def classify(self, state):
300300 """Return the Test or Pragma that this Block represents."""
301
302 def make_block_from_pattern(pattern, prefix):
303 lines = None
304 for (candidate_prefix, candidate_lines) in pattern:
305 if candidate_prefix == prefix:
306 lines = candidate_lines
307 break
308 if lines is None:
309 return None
310 return self.PREFIX_MAP[prefix](line_num=self.line_num, filename=self.filename, lines=lines)
301311
302312 pattern = self.deconstruct()
303313 pattern_prefixes = [p[0] for p in pattern]
327337 elif pattern_prefixes[-1] in [u'= ', u'? ']:
328338 # TODO: several things
329339
330 if current_functionality is None:
340 if state.current_functionality is None:
331341 raise FalderalSyntaxError(
332342 ("line %d: " % self.line_num) +
333343 "functionality under test not specified")
334344
335 body_block = make_block_from_pattern(pattern, u'| ')
336 input_block = make_block_from_pattern(pattern, u'+ ')
345 body_block = make_block_from_pattern(pattern, u'| ') or state.last_test_body_block
346 input_block = make_block_from_pattern(pattern, u'+ ') or state.last_test_input_block
337347 expectation_block = make_block_from_pattern(pattern, pattern_prefixes[-1])
348 expectation_class = None
349 if isinstance(expectation_block, ExpectedError):
350 expectation_class = ErrorOutcome
351 if isinstance(expectation_block, ExpectedResult):
352 expectation_class = OutputOutcome
353 assert expectation_class
354 expectation = expectation_class(expectation_block.text())
338355
339356 test = Test(body_block=body_block,
340357 input_block=input_block,
341 expectation=expectation_block,
342 functionality=current_functionality,
343 desc_block=last_desc_block)
358 expectation=expectation,
359 functionality=state.current_functionality,
360 desc_block=state.last_desc_block)
344361
345362 return test
346363 else:
375392
376393 class InterveningText(Block):
377394 pass
395
396
397 ##### Parsing State #####
398
399
400 class ParseState(object):
401 def __init__(self):
402 self.last_desc_block = None
403 self.last_test_body_block = None
404 self.last_test_input_block = None
405 self.current_functionality = None
406 self.functionalities = None
378407
379408
380409 ##### Documents #####
525554
526555 # now process Blocks into Tests
527556
528 last_desc_block = None
529 last_test_body_block = None
530 last_used_test_body_block = None
531 last_test_input_block = None
532 current_functionality = None
557 state = ParseState()
533558
534559 tests = []
535560 for block in blocks:
536561 if isinstance(block, InterveningText):
537562 if block.is_empty():
538563 continue
539 last_desc_block = block
564 state.last_desc_block = block
540565 continue
541566
542 test_or_pragma = block.classify(current_functionality=current_functionality)
567 test_or_pragma = block.classify(state)
543568
544569 if isinstance(test_or_pragma, Test):
545570 tests.append(test_or_pragma)
549574 match = re.match(r'^\s*Tests\s+for\s+functionality\s*\"(.*?)\"\s*$', pragma_text)
550575 if match:
551576 functionality_name = match.group(1)
552 current_functionality = functionalities.setdefault(
577 state.current_functionality = functionalities.setdefault(
553578 functionality_name,
554579 Functionality(functionality_name)
555580 )