git @ Cat's Eye Technologies Falderal / deaff26
Breaking in different ways now. Chris Pressey 5 years ago
1 changed file(s) with 60 addition(s) and 36 deletion(s). Raw diff Collapse all Expand all
154154 test body line 1test body line 2
155155
156156 """
157
157158 def __init__(self, line_num=None, filename=None, lines=None):
158159 if lines is None:
159160 lines = []
160161 self.lines = lines
161162 self.line_num = line_num
162163 self.filename = filename
163
164 def __repr__(self):
165 return "%s(line_num=%r, filename=%r)" % (
166 self.__class__.__name__, self.line_num, self.filename
167 )
168
169 def __unicode__(self):
170 return unicode(repr(self))
171
172 def location(self):
173 filename = self.filename
174 if filename is None:
175 filename = "<<input file>>"
176 return "%s, line %s" % (filename, self.line_num)
177
178 def append(self, line):
179 assert isinstance(line, unicode)
180 self.lines.append(line)
181
182 def text(self, seperator='\n'):
183 return seperator.join(self.lines)
184
185 def deconstruct(self):
186 """Return a list of pairs of (prefix, list of lines) representing
187 the contents of this Block. The pairs are in the order the runs
188 of prefixes occur in the block. The lines in the list of lines
189 have had their prefix stripped from them."""
190
191 PREFIX_MAP = {
164 self.PREFIX_MAP = {
192165 u'| ': TestBody,
193166 u'+ ': TestInput,
194167 u'? ': ExpectedError,
196169 u'->': Pragma,
197170 u'> ': LiterateCode,
198171 }
199 PREFIXES = PREFIX_MAP.keys()
172 self.PREFIXES = self.PREFIX_MAP.keys()
173
174 def __repr__(self):
175 return "%s(line_num=%r, filename=%r)" % (
176 self.__class__.__name__, self.line_num, self.filename
177 )
178
179 def __unicode__(self):
180 return unicode(repr(self))
181
182 def location(self):
183 filename = self.filename
184 if filename is None:
185 filename = "<<input file>>"
186 return "%s, line %s" % (filename, self.line_num)
187
188 def append(self, line):
189 assert isinstance(line, unicode)
190 self.lines.append(line)
191
192 def text(self, seperator='\n'):
193 return seperator.join(self.lines)
194
195 def deconstruct(self):
196 """Return a list of pairs of (prefix, list of lines) representing
197 the contents of this Block. The pairs are in the order the runs
198 of prefixes occur in the block. The lines in the list of lines
199 have had their prefix stripped from them."""
200200
201201 pairs = []
202202 prefix_state = None
203203 acc = []
204204
205 for line in lines:
205 for line in self.lines:
206206 prefix_of_line = None
207 for prefix in PREFIXES:
207 for prefix in self.PREFIXES:
208208 if line.startswith(prefix):
209209 prefix_of_line = prefix
210210 break
219219 pairs.append((prefix_state, acc))
220220
221221 return pairs
222
223 def classify(self):
224 """Return a list of Blocks of more specific classes."""
225
226 pattern = self.deconstruct()
227 pattern_prefixes = [p[0] for p in pattern]
228 if len(pattern_prefixes) == 1:
229 return [self.PREFIX_MAP[pattern_prefixes[0]](lines=self.lines)]
230
231 return []
222232
223233
224234 class LiterateCode(Block):
296306 [1, 2, 3, 5, 6, 7, 8]
297307
298308 """
299 indent = u''
309 indent = None
300310 blocks = []
301311 line_num = 1
302312 block = None
303313
304314 for line in self.lines:
315 # make sure we get a Block to start with
316 if indent is None:
317 if line.startswith(u' '):
318 indent = u''
319 else:
320 indent = u' '
321
305322 if indent == u'':
306323 if line.startswith(u' '):
307324 indent = u' '
329346 if block is not None:
330347 blocks.append(block)
331348
332 self.blocks = blocks
349 # post-process blocks
350 new_blocks = []
351 for block in blocks:
352 if isinstance(block, InterveningText):
353 new_blocks.append(block)
354 else:
355 new_blocks.extend(block.classify())
356 self.blocks = new_blocks
333357
334358 def parse_blocks_to_tests(self, functionalities):
335359 r"""Assemble a list of Tests from the blocks in this Document.
357381 >>> d.append(u' ? Oops')
358382 >>> d.parse_lines_to_blocks()
359383 >>> [b.__class__.__name__ for b in d.blocks]
360 ['InterveningMarkdown', 'Pragma', 'TestBody', 'ExpectedError',
384 ['InterveningText', 'Pragma', 'TestBody', 'ExpectedError',
361385 'TestBody', 'TestInput', 'ExpectedResult',
362386 'TestInput', 'ExpectedResult', 'Pragma', 'TestBody', 'ExpectedError']
363387 >>> tests = d.parse_blocks_to_tests(funs)
516540 )
517541 implementation = ShellImplementation(command)
518542 functionality.add_implementation(implementation)
519 elif isinstance(block, InterveningMarkdown):
543 elif isinstance(block, InterveningText):
520544 if not re.match(r'^\s*$', block.text(seperator=' ')):
521545 last_desc_block = block
522546 prev_block = block