git @ Cat's Eye Technologies Falderal / 706cd55
I have a feeling that this is not the path we want to go down. Chris Pressey 5 years ago
1 changed file(s) with 57 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
157157 test body line 1test body line 2
158158
159159 """
160 def __init__(self, line_num=None, filename=None):
161 self.lines = []
160 def __init__(self, line_num=None, filename=None, lines=None):
161 if lines is None:
162 lines = []
163 self.lines = lines
162164 self.line_num = line_num
163165 self.filename = filename
164166
188190 return seperator.join(prefix + line for line in self.lines)
189191
190192 def classify(self):
193 """Returns a list of Blocks of more specific subclasses of Block."""
191194
192195 PREFIX = {
193196 u'| ': TestBody,
198201 u'> ': LiterateCode,
199202 }
200203
204 contains_subblocks = False
201205 for prefix in PREFIX.keys():
202206 if all(line.startswith(prefix) for line in self.lines):
203 return PREFIX[prefix]
204
205 # TODO: if here, mixed block. Further classify. Split into blocks
206 # if it cannot be classified.
207
208 raise NotImplementedError("mixed block")
207 cls = PREFIX[prefix]
208 return [cls(
209 line_num=self.line_num,
210 filename=self.filename,
211 lines=[line[len(prefix):] for line in self.lines],
212 )]
213 if any(line.startswith(prefix) for line in self.lines):
214 contains_subblocks = True
215 break
216
217 if not contains_subblocks:
218 return [InterveningMarkdown(
219 line_num=self.line_num,
220 filename=self.filename,
221 lines=self.lines
222 )]
223
224 # If we got here, it's a mixed block.
225 # TODO: Further classify.
226
227 # Split into individual blocks.
228 prefix = ''
229 blocks = []
230
231 def next(block):
232 if block is not None:
233 blocks.extend(block.classify())
234 return Block(line_num=self.line_num,
235 filename=self.filename)
236
237 block = next(None)
238
239 for line in self.lines:
240 line_prefix = line[:2]
241 if line_prefix != prefix:
242 block = next(block)
243 block.append(line)
244
245 next(block)
246
247 return blocks
209248
210249
211250 class LiterateCode(Block):
285324 """
286325 state = 'toplevel'
287326 blocks = []
288 block = None
289327 line_num = 1
290328
291329 def next(block):
292330 if block is not None:
293 # TODO refine the block by calling block.classify()
294 blocks.append(block)
331 blocks.extend(block.classify())
295332 return Block(line_num=line_num,
296333 filename=self.filename)
297334
298 block = next(block)
335 block = next(None)
299336
300337 for line in self.lines:
301338 if state == 'toplevel':
314351 block.append(line)
315352 line_num += 1
316353
317 block = next(block)
354 next(block)
318355
319356 self.blocks = blocks
320357
331368 >>> d = Document()
332369 >>> d.append(u'This is a test file.')
333370 >>> d.append(u' -> Tests for functionality "Parse Thing"')
334 >>> d.append(u" | This is some test input.")
371 >>> d.append(u" | This is some test body.")
335372 >>> d.append(u" | It extends over two lines.")
336373 >>> d.append(u' ? Expected Error')
337374 >>> d.append(u' | Test with input')
342379 >>> d.append(u' -> Tests for functionality "Run Thing"')
343380 >>> d.append(u" | Thing")
344381 >>> d.append(u' ? Oops')
382 >>> d.parse_lines_to_blocks()
383 >>> [b.__class__.__name__ for b in d.blocks]
384 ['InterveningMarkdown', 'Pragma', 'TestBody', 'ExpectedError',
385 'TestBody', 'TestInput', 'ExpectedResult',
386 'TestInput', 'ExpectedResult', 'Pragma', 'TestBody', 'ExpectedError']
345387 >>> tests = d.parse_blocks_to_tests(funs)
346388 >>> [t.body for t in tests]
347 [u'This is some test input.\nIt extends over two lines.',
389 [u'This is some test body.\nIt extends over two lines.',
348390 u'Test with input', u'Test with input', u'Thing']
349391 >>> [t.input_block for t in tests]
350392 [None, TestInput(u' + ', line_num=7, filename=None),