git @ Cat's Eye Technologies Falderal / bfd62aa
Move fast and break things, I guess? Part-way to new format. Chris Pressey 5 years ago
2 changed file(s) with 62 addition(s) and 42 deletion(s). Raw diff Collapse all Expand all
144144 class Block(object):
145145 """A segment of a Falderal-formatted file.
146146
147 >>> b = Block(u'| ')
148 >>> b.append(u'| test body line 1')
149 >>> b.append(u'| test body line 2')
150 >>> print b.text(prefix=True)
147 >>> b = Block()
148 >>> b.append(u'test body line 1')
149 >>> b.append(u'test body line 2')
150 >>> print b.text(prefix=u'| ')
151151 | test body line 1
152152 | test body line 2
153153 >>> print b.text()
157157 test body line 1test body line 2
158158
159159 """
160 def __init__(self, prefix, line_num=None, filename=None):
161 assert isinstance(prefix, unicode), repr(prefix)
162 self.prefix = prefix
160 def __init__(self, line_num=None, filename=None):
163161 self.lines = []
164162 self.line_num = line_num
165163 self.filename = filename
166164
167165 def __repr__(self):
168 return "%s(%r, line_num=%r, filename=%r)" % (
169 self.__class__.__name__, self.prefix, self.line_num, self.filename
166 return "%s(line_num=%r, filename=%r)" % (
167 self.__class__.__name__, self.line_num, self.filename
170168 )
171169
172170 def __unicode__(self):
180178
181179 def append(self, line):
182180 assert isinstance(line, unicode)
183 self.lines.append(line[len(self.prefix):])
184
185 def text(self, prefix=False, seperator='\n'):
186 if not prefix:
181 self.lines.append(line)
182
183 def text(self, prefix=None, seperator='\n'):
184 if prefix is None:
187185 return seperator.join(self.lines)
188186 else:
189 return seperator.join(self.prefix + line for line in self.lines)
187 assert isinstance(prefix, unicode), repr(prefix)
188 return seperator.join(prefix + line for line in self.lines)
189
190 def classify(self):
191
192 PREFIX = {
193 u'| ': TestBody,
194 u'+ ': TestInput,
195 u'? ': ExpectedError,
196 u'= ': ExpectedResult,
197 u'->': Pragma,
198 u'> ': LiterateCode,
199 }
200
201 for prefix in PREFIX.keys():
202 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")
190209
191210
192211 class LiterateCode(Block):
218237
219238
220239 ##### Documents #####
221
222 PREFIX = {
223 u' | ': TestBody,
224 u' + ': TestInput,
225 u' ? ': ExpectedError,
226 u' = ': ExpectedResult,
227 u' ->': Pragma,
228 u' > ': LiterateCode,
229 }
230240
231241
232242 class Document(object):
273283 [1, 2, 3, 5, 6, 7, 8]
274284
275285 """
276 state = '***'
286 state = 'toplevel'
277287 blocks = []
278288 block = None
279289 line_num = 1
290
291 def next(block):
292 if block is not None:
293 # TODO refine the block by calling block.classify()
294 blocks.append(block)
295 return Block(line_num=line_num,
296 filename=self.filename)
297
298 block = next(block)
299
280300 for line in self.lines:
281 found_prefix = u''
282 for prefix in PREFIX.keys():
283 if line.startswith(prefix):
284 found_prefix = prefix
285 break
286 if found_prefix == state:
287 block.append(line)
288 else:
289 state = found_prefix
290 if block is not None:
291 blocks.append(block)
292 BlockClass = PREFIX.get(state, InterveningMarkdown)
293 block = BlockClass(state, line_num=line_num,
294 filename=self.filename)
295 block.append(line)
301 if state == 'toplevel':
302 if line.startswith(u' '):
303 state = 'indented'
304 block = next(block)
305
306 elif state == 'indented':
307 if not line.startswith(u' '):
308 state = 'toplevel'
309 block = next(block)
310
311 if line.startswith(u' '):
312 line = line[4:]
313
314 block.append(line)
296315 line_num += 1
297 if block is not None:
298 blocks.append(block)
316
317 block = next(block)
318
299319 self.blocks = blocks
300320
301321 def parse_blocks_to_tests(self, functionalities):
11
22 # Really crude test harness for py-falderal itself...
33
4 bin/falderal -v -t || exit 1
4 bin/falderal -t || exit 1
55
66 cd tests
77