git @ Cat's Eye Technologies Falderal / 01f9e15
Convert the majority of the tests. Chris Pressey 3 years ago
2 changed file(s) with 244 addition(s) and 186 deletion(s). Raw diff Collapse all Expand all
191191 def __unicode__(self):
192192 return unicode(repr(self))
193193
194 def __eq__(self, other):
195 return (
196 self.__class__ == other.__class__ and
197 self.lines == other.lines and
198 self.line_num == other.line_num and
199 self.filename == other.filename
200 )
201
194202 def location(self):
195203 filename = self.filename
196204 if filename is None:
44 from unittest import TestCase
55
66 from falderal.objects import (
7 Block, InterveningText,
7 Block, Pragma,
8 ParseState, InterveningText,
89 Document,
910 Functionality, ShellImplementation,
10 Test, OutputOutcome,
11 Test, OutputOutcome, ErrorOutcome,
12 FalderalSyntaxError,
1113 )
1214
1315
4042 [u'->', u'| ', u'? ', '', u'| ', u'+ ', u'= ']
4143 )
4244
43 #>>> b = Block()
44 #>>> b.append(u'-> This is a pragma.')
45 #>>> b.append(u'-> which extends over two lines')
46 #>>> print b.classify(ParseState())
47 #Pragma(line_num=1)
48 #
49 #>>> f = Functionality('foo')
50 #>>> b = Block()
51 #>>> b.append(u'| Test body here.')
52 #>>> b.append(u'= Expected result here.')
53 #>>> print b.classify(ParseState(current_functionality=f))
54 #Test(body_block=Block(line_num=1), input_block=None,
55 # expectation=OutputOutcome(u'Expected result here.'),
56 # functionality=Functionality('foo'), desc_block=None,
57 # body=u'Test body here.', input=None)
58 #
59 #>>> b = Block()
60 #>>> b.append(u'| Test body here.')
61 #>>> b.append(u'? Expected error here.')
62 #>>> print b.classify(ParseState(current_functionality=f))
63 #Test(body_block=Block(line_num=1), input_block=None,
64 # expectation=ErrorOutcome(u'Expected error here.'),
65 # functionality=Functionality('foo'), desc_block=None,
66 # body=u'Test body here.', input=None)
45 def test_classify_block_pragma(self):
46 b = Block()
47 b.append(u'-> This is a pragma.')
48 b.append(u'-> which extends over two lines')
49 result = b.classify(ParseState())
50 self.assertIsInstance(result, Pragma)
51 self.assertEqual(result.lines, [u' This is a pragma.', u' which extends over two lines'])
52 self.assertEqual(result.line_num, 1)
53
54 def test_classify_block_success_test(self):
55 f = Functionality('foo')
56 b = Block()
57 b.append(u'| Test body here.')
58 b.append(u'= Expected result here.')
59 result = b.classify(ParseState(current_functionality=f))
60 self.assertIsInstance(result, Test)
61 #self.assertEqual(result.body_block, None)
62 self.assertEqual(result.input_block, None)
63 self.assertEqual(result.expectation, OutputOutcome(u'Expected result here.'))
64 self.assertEqual(result.functionality, f)
65 self.assertEqual(result.desc_block, None)
66 self.assertEqual(result.body, u'Test body here.')
67 self.assertEqual(result.input, None)
68
69 def test_classify_block_error_test(self):
70 f = Functionality('foo')
71 b = Block()
72 b.append(u'| Test body here.')
73 b.append(u'? Expected error here.')
74 result = b.classify(ParseState(current_functionality=f))
75 self.assertIsInstance(result, Test)
76 #self.assertEqual(result.body_block, None)
77 self.assertEqual(result.input_block, None)
78 self.assertEqual(result.expectation, ErrorOutcome(u'Expected error here.'))
79 self.assertEqual(result.functionality, f)
80 self.assertEqual(result.desc_block, None)
81 self.assertEqual(result.body, u'Test body here.')
82 self.assertEqual(result.input, None)
6783
6884
6985 class DocumentTestCase(TestCase):
144160 [u'This is some test body.\nIt extends over two lines.',
145161 u'Test with input', u'Test with input', u'Thing']
146162 )
147 #>>> [t.input_block for t in tests]
148 #[None, Block(line_num=8), Block(line_num=12), None]
149 #>>> tests[1].input_block.text()
150 #u'input-for-test'
151 #>>> tests[2].input_block.text()
152 #u'Other input-for-test'
153 #>>> [t.expectation for t in tests]
154 #[ErrorOutcome(u'Expected Error'),
155 # OutputOutcome(u'Expected result on output'),
156 # OutputOutcome(u'Other Expected result on output'),
157 # ErrorOutcome(u'Oops')]
158 #>>> [t.functionality.name for t in tests]
159 #[u'Parse Thing', u'Parse Thing', u'Parse Thing', u'Run Thing']
160 #>>> sorted(functionalities.keys())
161 #[u'Parse Thing', u'Run Thing']
162
163 #>>> d = Document()
164 #>>> d.append(u" | This is some test body.")
165 #>>> d.append(u' = Expected')
166 #>>> d.extract_tests({})
167 #Traceback (most recent call last):
168 #...
169 #FalderalSyntaxError: line 1: functionality under test not specified
170 #
171 #>>> d = Document()
172 #>>> d.append(u'This is a test file.')
173 #>>> d.append(u' ? Expected Error')
174 #>>> d.extract_tests({})
175 #Traceback (most recent call last):
176 #...
177 #FalderalSyntaxError: line 2: expectation must be preceded by test body or test input
178 #
179 #>>> d = Document()
180 #>>> d.append(u' -> Hello, this is pragma')
181 #>>> d.append(u' = Expected')
182 #>>> d.extract_tests({})
183 #Traceback (most recent call last):
184 #...
185 #FalderalSyntaxError: line 1: incorrectly formatted test block
186 #
187 #>>> d = Document()
188 #>>> d.append(u' | This is test')
189 #>>> d.append(u'This is text')
190 #>>> d.extract_tests({})
191 #Traceback (most recent call last):
192 #...
193 #FalderalSyntaxError: line 1: test body must be followed by expectation or test input
194 #
195 #>>> d = Document()
196 #>>> d.append(u' -> Hello, this is pragma')
197 #>>> d.append(u' + Input to where exactly?')
198 #>>> d.extract_tests({})
199 #Traceback (most recent call last):
200 #...
201 #FalderalSyntaxError: line 1: incorrectly formatted test block
202 #
203 #>>> d = Document()
204 #>>> funs = {}
205 #>>> d.append(u' -> Functionality "Parse Stuff" is implemented by '
206 #... u'shell command "parse"')
207 #>>> d.append(u'')
208 #>>> d.append(u' -> Functionality "Parse Stuff" is')
209 #>>> d.append(u' -> implemented by shell command "pxxxy"')
210 #>>> tests = d.extract_tests(funs)
211 #>>> len(funs.keys())
212 #1
213 #>>> [i for i in funs["Parse Stuff"].implementations]
214 #[ShellImplementation(u'parse'), ShellImplementation(u'pxxxy')]
163 #self.assertEqual(
164 # [t.input_block for t in tests],
165 # [None, Block(line_num=8), Block(line_num=12), None]
166 #)
167 self.assertEqual(
168 tests[1].input_block.text(),
169 u'input-for-test'
170 )
171 self.assertEqual(
172 tests[2].input_block.text(),
173 u'Other input-for-test'
174 )
175 self.assertEqual(
176 [t.expectation for t in tests],
177 [ErrorOutcome(u'Expected Error'),
178 OutputOutcome(u'Expected result on output'),
179 OutputOutcome(u'Other Expected result on output'),
180 ErrorOutcome(u'Oops')]
181 )
182 self.assertEqual(
183 [t.functionality.name for t in tests],
184 [u'Parse Thing', u'Parse Thing', u'Parse Thing', u'Run Thing']
185 )
186 self.assertEqual(
187 sorted(functionalities.keys()),
188 [u'Parse Thing', u'Run Thing']
189 )
190
191 def test_no_functionality_under_test(self):
192 d = Document()
193 d.append(u" | This is some test body.")
194 d.append(u' = Expected')
195 with self.assertRaises(FalderalSyntaxError) as ar:
196 d.extract_tests({})
197 self.assertEqual(str(ar.exception), "line 1: functionality under test not specified")
198
199 def test_expectation_in_bad_place(self):
200 d = Document()
201 d.append(u'This is a test file.')
202 d.append(u' ? Expected Error')
203 with self.assertRaises(FalderalSyntaxError) as ar:
204 d.extract_tests({})
205 self.assertEqual(str(ar.exception), "line 2: expectation must be preceded by test body or test input")
206
207 def test_badly_formatted_test_block(self):
208 d = Document()
209 d.append(u' -> Hello, this is pragma')
210 d.append(u' = Expected')
211 with self.assertRaises(FalderalSyntaxError) as ar:
212 d.extract_tests({})
213 self.assertEqual(str(ar.exception), "line 1: incorrectly formatted test block")
214
215 def test_body_not_followed_by_anything_sensible(self):
216 d = Document()
217 d.append(u' | This is test')
218 d.append(u'This is text')
219 with self.assertRaises(FalderalSyntaxError) as ar:
220 d.extract_tests({})
221 self.assertEqual(str(ar.exception), "line 1: test body must be followed by expectation or test input")
222
223 def test_another_badly_formatted_block(self):
224 d = Document()
225 d.append(u' -> Hello, this is pragma')
226 d.append(u' + Input to where exactly?')
227 with self.assertRaises(FalderalSyntaxError) as ar:
228 d.extract_tests({})
229 self.assertEqual(str(ar.exception), "line 1: incorrectly formatted test block")
230
231 def test_parse_functionalities(self):
232 d = Document()
233 funs = {}
234 d.append(u' -> Functionality "Parse Stuff" is implemented by '
235 u'shell command "parse"')
236 d.append(u'')
237 d.append(u' -> Functionality "Parse Stuff" is')
238 d.append(u' -> implemented by shell command "pxxxy"')
239 tests = d.extract_tests(funs)
240 self.assertEqual(list(funs.keys()), ['Parse Stuff'])
241 self.assertEqual(
242 [repr(i) for i in funs["Parse Stuff"].implementations],
243 ["ShellImplementation(u'parse')", "ShellImplementation(u'pxxxy')"]
244 )
215245
216246
217247 class ShellImplementationTestCase(TestCase):
219249 i = ShellImplementation('cat')
220250 self.assertEqual(i.run(body=u'text'), OutputOutcome(u'text'))
221251
222 #>>> i = ShellImplementation('cat fhofhofhf')
223 #>>> i.run(body=u'text')
224 #ErrorOutcome(u'cat: fhofhofhf: No such file or directory')
225 #
226 #>>> i = ShellImplementation('cat %(test-body-file)')
227 #>>> i.run(body=u'text')
228 #OutputOutcome(u'text')
229 #
230 #>>> i = ShellImplementation("echo '%(test-body-text)'")
231 #>>> i.run(body=u'text')
232 #OutputOutcome(u'text')
233 #
234 #>>> i = ShellImplementation('cat >%(output-file)')
235 #>>> i.run(body=u'text')
236 #OutputOutcome(u'text')
237 #
238 #>>> i = ShellImplementation("echo '%(test-body-text)' '%(test-input-text)'")
239 #>>> i.run(body=u'text', input=u'zzrk')
240 #OutputOutcome(u'text zzrk')
241 #
242 #Here the body is sent to cat's stdin, but cat ignores it.
243 #
244 #>>> i = ShellImplementation('cat >%(output-file) <%(test-input-file)')
245 #>>> i.run(body=u'text', input=u'zzrk')
246 #OutputOutcome(u'zzrk')
247
248
249 def TestsTestCase(TestCase):
252 def test_cat_file(self):
253 i = ShellImplementation('cat fhofhofhf')
254 self.assertEqual(i.run(body=u'text'), ErrorOutcome(u'cat: fhofhofhf: No such file or directory'))
255
256 def test_cat_test_body_file(self):
257 i = ShellImplementation('cat %(test-body-file)')
258 self.assertEqual(i.run(body=u'text'), OutputOutcome(u'text'))
259
260 def test_cat_test_body_text(self):
261 i = ShellImplementation("echo '%(test-body-text)'")
262 self.assertEqual(i.run(body=u'text'), OutputOutcome(u'text'))
263
264 def test_cat_output_file(self):
265 i = ShellImplementation('cat >%(output-file)')
266 self.assertEqual(i.run(body=u'text'), OutputOutcome(u'text'))
267
268 def test_echo(self):
269 i = ShellImplementation("echo '%(test-body-text)' '%(test-input-text)'")
270 self.assertEqual(i.run(body=u'text', input=u'zzrk'), OutputOutcome(u'text zzrk'))
271
272 def test_cat_stdin(self):
273 # Here the body is sent to cat's stdin, but cat ignores it.
274 i = ShellImplementation('cat >%(output-file) <%(test-input-file)')
275 self.assertEqual(i.run(body=u'text', input=u'zzrk'), OutputOutcome(u'zzrk'))
276
277
278 def TestTestCase(TestCase):
250279 def test_test_contents(self):
251280 b = Block()
252281 b.append(u'foo')
266295 ['success']
267296 )
268297
269 #>>> f = Functionality('Cat File')
270 #>>> f.add_implementation(CallableImplementation(lambda x, y: x))
271 #>>> t = Test(body=u'foo', expectation=OutputOutcome(u'bar'),
272 #... functionality=f)
273 #>>> [r.short_description() for r in t.run()]
274 #["expected OutputOutcome(u'bar'), got OutputOutcome(u'foo')"]
275 #
276 #>>> f = Functionality('Cat File')
277 #>>> f.add_implementation(CallableImplementation(lambda x, y: x))
278 #>>> t = Test(body=u'foo', expectation=ErrorOutcome(u'foo'),
279 #... functionality=f)
280 #>>> [r.short_description() for r in t.run()]
281 #["expected ErrorOutcome(u'foo'), got OutputOutcome(u'foo')"]
282 #
283 #>>> f = Functionality('Cat File')
284 #>>> def e(x, y):
285 #... raise ValueError(x)
286 #>>> f.add_implementation(CallableImplementation(e))
287 #>>> t = Test(body=u'foo', expectation=ErrorOutcome(u'foo'),
288 #... functionality=f)
289 #>>> [r.short_description() for r in t.run()]
290 #['success']
291 #
292 #>>> f = Functionality('Cat File')
293 #>>> def e(x, y):
294 #... raise ValueError(x)
295 #>>> f.add_implementation(CallableImplementation(e))
296 #>>> t = Test(body=u'foo', expectation=ErrorOutcome(u'bar'),
297 #... functionality=f)
298 #>>> [r.short_description() for r in t.run()]
299 #["expected ErrorOutcome(u'bar'), got ErrorOutcome(u'foo')"]
300 #
301 #>>> f = Functionality('Cat File')
302 #>>> def e(x, y):
303 #... raise ValueError(x)
304 #>>> f.add_implementation(CallableImplementation(e))
305 #>>> t = Test(body=u'foo', expectation=OutputOutcome(u'foo'),
306 #... functionality=f)
307 #>>> [r.short_description() for r in t.run()]
308 #["expected OutputOutcome(u'foo'), got ErrorOutcome(u'foo')"]
309 #
310 #>>> f = Functionality('Cat File with Input')
311 #>>> f.add_implementation(CallableImplementation(lambda x, y: x + y))
312 #>>> t = Test(body=u'foo', input=u'bar', expectation=OutputOutcome(u'foobar'),
313 #... functionality=f)
314 #>>> [r.short_description() for r in t.run()]
315 #['success']
316 #
317 #A functionality can have multiple implementations. We test them all.
318 #
319 #>>> f = Functionality('Cat File')
320 #>>> def c1(body, input):
321 #... return body
322 #>>> def c2(body, input):
323 #... return body + '...'
324 #>>> def c3(body, input):
325 #... raise ValueError(body)
326 #>>> for c in (c1, c2, c3):
327 #... f.add_implementation(CallableImplementation(c))
328 #>>> t = Test(body=u'foo', expectation=OutputOutcome(u'foo'),
329 #... functionality=f)
330 #>>> [r.short_description() for r in t.run()]
331 #['success', "expected OutputOutcome(u'foo'), got OutputOutcome(u'foo...')",
332 # "expected OutputOutcome(u'foo'), got ErrorOutcome(u'foo')"]
298 def test_tests_2(self):
299 f = Functionality('Cat File')
300 f.add_implementation(CallableImplementation(lambda x, y: x))
301 t = Test(body=u'foo', expectation=OutputOutcome(u'bar'),
302 functionality=f)
303 self.assertEqual(
304 [r.short_description() for r in t.run()],
305 ["expected OutputOutcome(u'bar'), got OutputOutcome(u'foo')"]
306 )
307
308 def test_tests_3(self):
309 f = Functionality('Cat File')
310 f.add_implementation(CallableImplementation(lambda x, y: x))
311 t = Test(body=u'foo', expectation=ErrorOutcome(u'foo'),
312 functionality=f)
313 self.assertEqual(
314 [r.short_description() for r in t.run()],
315 ["expected ErrorOutcome(u'foo'), got OutputOutcome(u'foo')"]
316 )
317
318 def test_tests_4(self):
319 f = Functionality('Cat File')
320 def e(x, y):
321 raise ValueError(x)
322 f.add_implementation(CallableImplementation(e))
323 t = Test(body=u'foo', expectation=ErrorOutcome(u'foo'),
324 functionality=f)
325 self.assertEqual(
326 [r.short_description() for r in t.run()],
327 ['success']
328 )
329
330 def test_tests_5(self):
331 f = Functionality('Cat File')
332 def e(x, y):
333 raise ValueError(x)
334 f.add_implementation(CallableImplementation(e))
335 t = Test(body=u'foo', expectation=ErrorOutcome(u'bar'),
336 functionality=f)
337 self.assertEqual(
338 [r.short_description() for r in t.run()],
339 ["expected ErrorOutcome(u'bar'), got ErrorOutcome(u'foo')"]
340 )
341
342 def test_tests_6(self):
343 f = Functionality('Cat File')
344 def e(x, y):
345 raise ValueError(x)
346 f.add_implementation(CallableImplementation(e))
347 t = Test(body=u'foo', expectation=OutputOutcome(u'foo'),
348 functionality=f)
349 self.assertEqual(
350 [r.short_description() for r in t.run()],
351 ["expected OutputOutcome(u'foo'), got ErrorOutcome(u'foo')"]
352 )
353
354 def test_tests_7(self):
355 f = Functionality('Cat File with Input')
356 f.add_implementation(CallableImplementation(lambda x, y: x + y))
357 t = Test(body=u'foo', input=u'bar', expectation=OutputOutcome(u'foobar'),
358 functionality=f)
359 self.assertEqual(
360 [r.short_description() for r in t.run()],
361 ['success']
362 )
363
364 def test_functionality_with_multiple_implementations(self):
365 # A functionality can have multiple implementations. We test them all.
366
367 f = Functionality('Cat File')
368 def c1(body, input):
369 return body
370 def c2(body, input):
371 return body + '...'
372 def c3(body, input):
373 raise ValueError(body)
374 for c in (c1, c2, c3):
375 f.add_implementation(CallableImplementation(c))
376 t = Test(body=u'foo', expectation=OutputOutcome(u'foo'),
377 functionality=f)
378 self.assertEqual(
379 [r.short_description() for r in t.run()],
380 ['success', "expected OutputOutcome(u'foo'), got OutputOutcome(u'foo...')",
381 "expected OutputOutcome(u'foo'), got ErrorOutcome(u'foo')"]
382 )
333383
334384
335385 if __name__ == '__main__':