git @ Cat's Eye Technologies Falderal / 2332d73
Simplify. Chris Pressey 6 years ago
1 changed file(s) with 37 addition(s) and 45 deletion(s). Raw diff Collapse all Expand all
179179 >>> b.append(u'| Test body here.')
180180 >>> b.append(u'= Expected result here.')
181181 >>> 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,
183183 expectation=OutputOutcome(u'Expected result here.'),
184184 functionality=Functionality('foo'), desc_block=None,
185185 body=u'Test body here.', input=None)
188188 >>> b.append(u'| Test body here.')
189189 >>> b.append(u'? Expected error here.')
190190 >>> 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,
192192 expectation=ErrorOutcome(u'Expected error here.'),
193193 functionality=Functionality('foo'), desc_block=None,
194194 body=u'Test body here.', input=None)
195195
196196 """
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 ]
197215
198216 def __init__(self, line_num=1, filename=None, lines=None):
199217 if lines is None:
201219 self.lines = lines
202220 self.line_num = line_num
203221 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 ]
221222
222223 def __repr__(self):
223224 filename_repr = '' if self.filename is None else ', filename=%r' % self.filename
277278 def classify(self, state):
278279 """Return the Test or Pragma that this Block represents."""
279280
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):
281285 lines = None
282286 for (candidate_prefix, candidate_lines) in pattern:
283287 if candidate_prefix == prefix:
285289 break
286290 if lines is None:
287291 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 )
292295
293296 if '' in pattern_prefixes:
294297 # There is plain, non-prefixed text embedded somewhere in this Block.
319322 ("line %d: " % self.line_num) +
320323 "functionality under test not specified")
321324
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
324327
325328 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())
328330 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())
331332 else:
332333 raise NotImplementedError
333334
338339 desc_block=state.last_desc_block)
339340
340341 state.last_test_body_block = body_block
341 #state.last_test_input_block = input_block
342342
343343 return test
344344 else:
369369 functionality.add_implementation(implementation)
370370
371371
372 class TestBody(Block):
373 pass
374
375
376 class TestInput(Block):
377 pass
378
379
380372 class InterveningText(Block):
381373 pass
382374
527519 >>> d.append(u" | This is some test body.")
528520 >>> d.append(u' = Expected result')
529521 >>> 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,
531523 expectation=OutputOutcome(u'Expected result'),
532524 functionality=Functionality(u'Parse Thing'),
533525 desc_block=InterveningText(line_num=1),
557549 [u'This is some test body.\nIt extends over two lines.',
558550 u'Test with input', u'Test with input', u'Thing']
559551 >>> [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]
561553 >>> tests[1].input_block.text()
562554 u'input-for-test'
563555 >>> tests[2].input_block.text()
838830 class Test(object):
839831 """An object representing a Falderal test.
840832
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,
843835 and the body and input attributes are derived from it. However
844836 in the absence of these blocks (as in many of the internal tests)
845837 a body and/or input may be passed alone.
846838
847839 TODO: maybe write a helper function for that instead.
848840
849 >>> b = TestBody()
841 >>> b = Block()
850842 >>> b.append(u'foo')
851843 >>> b.append(u'bar')
852 >>> i = TestInput()
844 >>> i = Block()
853845 >>> i.append(u'green')
854846 >>> t = Test(body_block=b, input_block=i)
855847 >>> print t.body