git @ Cat's Eye Technologies Falderal / 83a36f6
Begin converting doctests to unit tests. Chris Pressey 3 years ago
4 changed file(s) with 343 addition(s) and 314 deletion(s). Raw diff Collapse all Expand all
2626 parser.add_option("-d", "--dump",
2727 action="store_true", default=False,
2828 help="print out info about parsed tests, don't run them")
29 parser.add_option("-t", "--test",
30 action="store_true", default=False,
31 help="run internal tests only and exit")
3229 parser.add_option("-v", "--verbose",
3330 action="store_true", default=False,
3431 help="print out info about each test as it is run")
3532
3633 (options, args) = parser.parse_args(args[1:])
37
38 if options.test:
39 import doctest
40 import falderal.objects
41 (failure_count, test_count) = \
42 doctest.testmod(falderal.objects,
43 optionflags=doctest.NORMALIZE_WHITESPACE)
44 if failure_count > 0:
45 return 1
46 else:
47 return 0
4834
4935 # load Documents and create Falderal Tests from them
5036 documents = []
4848
4949 def __repr__(self):
5050 return '%s(%r)' % (self.__class__.__name__, self.text)
51
52 def __eq__(self, other):
53 return self.__class__ == other.__class__ and self.text == other.text
5154
5255
5356 class OutputOutcome(Outcome):
142145
143146 class Block(object):
144147 """A segment of a Falderal-formatted file.
145
146 >>> b = Block()
147 >>> b.append(u'line 1')
148 >>> b.append(u'line 2')
149 >>> print b.text()
150 line 1
151 line 2
152 >>> print b.text(seperator='')
153 line 1line 2
154 >>> print b.deconstruct()
155 [('', [u'line 1', u'line 2'])]
156
157 >>> b = Block()
158 >>> b.append(u'-> This is a pragma.')
159 >>> b.append(u"| This is some test input.")
160 >>> b.append(u"| It extends over two lines.")
161 >>> b.append(u'? Expected Error')
162 >>> b.append(u'Plain text')
163 >>> b.append(u'More plain text')
164 >>> b.append(u'| Test with input')
165 >>> b.append(u'+ input-for-test')
166 >>> b.append(u'= Expected result on output')
167 >>> b.append(u'= which extends over two lines')
168 >>> print [pair[0] for pair in b.deconstruct()]
169 [u'->', u'| ', u'? ', '', u'| ', u'+ ', u'= ']
170
171 >>> b = Block()
172 >>> b.append(u'-> This is a pragma.')
173 >>> b.append(u'-> which extends over two lines')
174 >>> print b.classify(ParseState())
175 Pragma(line_num=1)
176
177 >>> f = Functionality('foo')
178 >>> b = Block()
179 >>> b.append(u'| Test body here.')
180 >>> b.append(u'= Expected result here.')
181 >>> print b.classify(ParseState(current_functionality=f))
182 Test(body_block=Block(line_num=1), input_block=None,
183 expectation=OutputOutcome(u'Expected result here.'),
184 functionality=Functionality('foo'), desc_block=None,
185 body=u'Test body here.', input=None)
186
187 >>> b = Block()
188 >>> b.append(u'| Test body here.')
189 >>> b.append(u'? Expected error here.')
190 >>> print b.classify(ParseState(current_functionality=f))
191 Test(body_block=Block(line_num=1), input_block=None,
192 expectation=ErrorOutcome(u'Expected error here.'),
193 functionality=Functionality('foo'), desc_block=None,
194 body=u'Test body here.', input=None)
195148
196149 """
197150
437390 self.lines.append(line)
438391
439392 def parse_lines_to_blocks(self):
440 r"""Parse the lines of the Document into Blocks.
441
442 >>> d = Document()
443 >>> d.append(u'This is a test file.')
444 >>> d.append(u' -> This is a pragma.')
445 >>> d.append(u'')
446 >>> d.append(u" | This is some test input.\n")
447 >>> d.append(u" | It extends over two lines.")
448 >>> d.append(u' ? Expected Error')
449 >>> d.append(u'')
450 >>> d.append(u' | Test with input')
451 >>> d.append(u' + input-for-test')
452 >>> d.append(u' = Expected result on output')
453 >>> blocks = d.parse_lines_to_blocks()
454 >>> [block.lines for block in blocks if isinstance(block, InterveningText)]
455 [[u'This is a test file.'], [u''], [u'']]
456 >>> [b.__class__.__name__ for b in blocks]
457 ['InterveningText', 'Block', 'InterveningText', 'Block', 'InterveningText', 'Block']
458 >>> [b.line_num for b in blocks]
459 [1, 2, 3, 4, 7, 8]
393 """Parse the lines of the Document into Blocks.
460394
461395 """
462396 indent = None
528462 return tests
529463
530464 def extract_tests(self, functionalities):
531 r"""Extract all Tests from this Document.
532
533 >>> functionalities = {}
534 >>> d = Document()
535 >>> d.append(u"This is a text file.")
536 >>> d.append(u'It contains NO tests.')
537 >>> d.extract_tests(functionalities)
538 []
539
540 >>> d = Document()
541 >>> d.append(u'This is a test file.')
542 >>> d.append(u' -> Tests for functionality "Parse Thing"')
543 >>> d.append(u'')
544 >>> d.append(u" | This is some test body.")
545 >>> d.append(u' = Expected result')
546 >>> d.extract_tests(functionalities)
547 [Test(body_block=Block(line_num=4), input_block=None,
548 expectation=OutputOutcome(u'Expected result'),
549 functionality=Functionality(u'Parse Thing'),
550 desc_block=InterveningText(line_num=1),
551 body=u'This is some test body.', input=None)]
552
553 >>> d = Document()
554 >>> d.append(u'This is a test file.')
555 >>> d.append(u' -> Tests for functionality "Parse Thing"')
556 >>> d.append(u'')
557 >>> d.append(u" | This is some test body.")
558 >>> d.append(u" | It extends over two lines.")
559 >>> d.append(u' ? Expected Error')
560 >>> d.append(u'')
561 >>> d.append(u' | Test with input')
562 >>> d.append(u' + input-for-test')
563 >>> d.append(u' = Expected result on output')
564 >>> d.append(u'')
565 >>> d.append(u' + Other input-for-test')
566 >>> d.append(u' = Other Expected result on output')
567 >>> d.append(u'')
568 >>> d.append(u' -> Tests for functionality "Run Thing"')
569 >>> d.append(u'')
570 >>> d.append(u" | Thing")
571 >>> d.append(u' ? Oops')
572 >>> tests = d.extract_tests(functionalities)
573 >>> [t.body for t in tests]
574 [u'This is some test body.\nIt extends over two lines.',
575 u'Test with input', u'Test with input', u'Thing']
576 >>> [t.input_block for t in tests]
577 [None, Block(line_num=8), Block(line_num=12), None]
578 >>> tests[1].input_block.text()
579 u'input-for-test'
580 >>> tests[2].input_block.text()
581 u'Other input-for-test'
582 >>> [t.expectation for t in tests]
583 [ErrorOutcome(u'Expected Error'),
584 OutputOutcome(u'Expected result on output'),
585 OutputOutcome(u'Other Expected result on output'),
586 ErrorOutcome(u'Oops')]
587 >>> [t.functionality.name for t in tests]
588 [u'Parse Thing', u'Parse Thing', u'Parse Thing', u'Run Thing']
589 >>> sorted(functionalities.keys())
590 [u'Parse Thing', u'Run Thing']
591
592 >>> d = Document()
593 >>> d.append(u" | This is some test body.")
594 >>> d.append(u' = Expected')
595 >>> d.extract_tests({})
596 Traceback (most recent call last):
597 ...
598 FalderalSyntaxError: line 1: functionality under test not specified
599
600 >>> d = Document()
601 >>> d.append(u'This is a test file.')
602 >>> d.append(u' ? Expected Error')
603 >>> d.extract_tests({})
604 Traceback (most recent call last):
605 ...
606 FalderalSyntaxError: line 2: expectation must be preceded by test body or test input
607
608 >>> d = Document()
609 >>> d.append(u' -> Hello, this is pragma')
610 >>> d.append(u' = Expected')
611 >>> d.extract_tests({})
612 Traceback (most recent call last):
613 ...
614 FalderalSyntaxError: line 1: incorrectly formatted test block
615
616 >>> d = Document()
617 >>> d.append(u' | This is test')
618 >>> d.append(u'This is text')
619 >>> d.extract_tests({})
620 Traceback (most recent call last):
621 ...
622 FalderalSyntaxError: line 1: test body must be followed by expectation or test input
623
624 >>> d = Document()
625 >>> d.append(u' -> Hello, this is pragma')
626 >>> d.append(u' + Input to where exactly?')
627 >>> d.extract_tests({})
628 Traceback (most recent call last):
629 ...
630 FalderalSyntaxError: line 1: incorrectly formatted test block
631
632 >>> d = Document()
633 >>> funs = {}
634 >>> d.append(u' -> Functionality "Parse Stuff" is implemented by '
635 ... u'shell command "parse"')
636 >>> d.append(u'')
637 >>> d.append(u' -> Functionality "Parse Stuff" is')
638 >>> d.append(u' -> implemented by shell command "pxxxy"')
639 >>> tests = d.extract_tests(funs)
640 >>> len(funs.keys())
641 1
642 >>> [i for i in funs["Parse Stuff"].implementations]
643 [ShellImplementation(u'parse'), ShellImplementation(u'pxxxy')]
465 """Extract all Tests from this Document.
644466
645467 """
646468 blocks = self.parse_lines_to_blocks()
722544 return u'shell command "%s"' % self.command
723545
724546 def run(self, body=None, input=None):
725 r"""
726 >>> i = ShellImplementation('cat')
727 >>> i.run(body=u'text')
728 OutputOutcome(u'text')
729
730 >>> i = ShellImplementation('cat fhofhofhf')
731 >>> i.run(body=u'text')
732 ErrorOutcome(u'cat: fhofhofhf: No such file or directory')
733
734 >>> i = ShellImplementation('cat %(test-body-file)')
735 >>> i.run(body=u'text')
736 OutputOutcome(u'text')
737
738 >>> i = ShellImplementation("echo '%(test-body-text)'")
739 >>> i.run(body=u'text')
740 OutputOutcome(u'text')
741
742 >>> i = ShellImplementation('cat >%(output-file)')
743 >>> i.run(body=u'text')
744 OutputOutcome(u'text')
745
746 >>> i = ShellImplementation("echo '%(test-body-text)' '%(test-input-text)'")
747 >>> i.run(body=u'text', input=u'zzrk')
748 OutputOutcome(u'text zzrk')
749
750 Here the body is sent to cat's stdin, but cat ignores it.
751
752 >>> i = ShellImplementation('cat >%(output-file) <%(test-input-file)')
753 >>> i.run(body=u'text', input=u'zzrk')
754 OutputOutcome(u'zzrk')
755
756 """
757547 # expand variables in the command
758548 test_filename = None
759549 output_filename = None
862652 a body and/or input may be passed alone.
863653
864654 TODO: maybe write a helper function for that instead.
865
866 >>> b = Block()
867 >>> b.append(u'foo')
868 >>> b.append(u'bar')
869 >>> i = Block()
870 >>> i.append(u'green')
871 >>> t = Test(body_block=b, input_block=i)
872 >>> print t.body
873 foo
874 bar
875 >>> print t.input
876 green
877655
878656 """
879657 def __init__(self, body_block=None, input_block=None, expectation=None,
907685 """Returns a list of Results, one for each implementation of
908686 the functionality being tested.
909687
910 >>> f = Functionality('Cat File')
911 >>> f.add_implementation(CallableImplementation(lambda x, y: x))
912 >>> t = Test(body=u'foo', expectation=OutputOutcome(u'foo'),
913 ... functionality=f)
914 >>> [r.short_description() for r in t.run()]
915 ['success']
916
917 >>> f = Functionality('Cat File')
918 >>> f.add_implementation(CallableImplementation(lambda x, y: x))
919 >>> t = Test(body=u'foo', expectation=OutputOutcome(u'bar'),
920 ... functionality=f)
921 >>> [r.short_description() for r in t.run()]
922 ["expected OutputOutcome(u'bar'), got OutputOutcome(u'foo')"]
923
924 >>> f = Functionality('Cat File')
925 >>> f.add_implementation(CallableImplementation(lambda x, y: x))
926 >>> t = Test(body=u'foo', expectation=ErrorOutcome(u'foo'),
927 ... functionality=f)
928 >>> [r.short_description() for r in t.run()]
929 ["expected ErrorOutcome(u'foo'), got OutputOutcome(u'foo')"]
930
931 >>> f = Functionality('Cat File')
932 >>> def e(x, y):
933 ... raise ValueError(x)
934 >>> f.add_implementation(CallableImplementation(e))
935 >>> t = Test(body=u'foo', expectation=ErrorOutcome(u'foo'),
936 ... functionality=f)
937 >>> [r.short_description() for r in t.run()]
938 ['success']
939
940 >>> f = Functionality('Cat File')
941 >>> def e(x, y):
942 ... raise ValueError(x)
943 >>> f.add_implementation(CallableImplementation(e))
944 >>> t = Test(body=u'foo', expectation=ErrorOutcome(u'bar'),
945 ... functionality=f)
946 >>> [r.short_description() for r in t.run()]
947 ["expected ErrorOutcome(u'bar'), got ErrorOutcome(u'foo')"]
948
949 >>> f = Functionality('Cat File')
950 >>> def e(x, y):
951 ... raise ValueError(x)
952 >>> f.add_implementation(CallableImplementation(e))
953 >>> t = Test(body=u'foo', expectation=OutputOutcome(u'foo'),
954 ... functionality=f)
955 >>> [r.short_description() for r in t.run()]
956 ["expected OutputOutcome(u'foo'), got ErrorOutcome(u'foo')"]
957
958 >>> f = Functionality('Cat File with Input')
959 >>> f.add_implementation(CallableImplementation(lambda x, y: x + y))
960 >>> t = Test(body=u'foo', input=u'bar', expectation=OutputOutcome(u'foobar'),
961 ... functionality=f)
962 >>> [r.short_description() for r in t.run()]
963 ['success']
964
965 A functionality can have multiple implementations. We test them all.
966
967 >>> f = Functionality('Cat File')
968 >>> def c1(body, input):
969 ... return body
970 >>> def c2(body, input):
971 ... return body + '...'
972 >>> def c3(body, input):
973 ... raise ValueError(body)
974 >>> for c in (c1, c2, c3):
975 ... f.add_implementation(CallableImplementation(c))
976 >>> t = Test(body=u'foo', expectation=OutputOutcome(u'foo'),
977 ... functionality=f)
978 >>> [r.short_description() for r in t.run()]
979 ['success', "expected OutputOutcome(u'foo'), got OutputOutcome(u'foo...')",
980 "expected OutputOutcome(u'foo'), got ErrorOutcome(u'foo')"]
981
982688 """
983689 results = []
984690 for implementation in self.functionality.implementations:
0 # Note: these are unit tests for py-falderal itself,
1 # not tests that Falderal can understand.
2
3 import unittest
4 from unittest import TestCase
5
6 from falderal.objects import (
7 Block, InterveningText,
8 Document,
9 Functionality, ShellImplementation,
10 Test, OutputOutcome,
11 )
12
13
14 class BlockTestCase(TestCase):
15 def test_block(self):
16 b = Block()
17 b.append(u'line 1')
18 b.append(u'line 2')
19 self.assertEqual(b.text(), "line 1\nline 2")
20 self.assertEqual(b.text(seperator=''), "line 1line 2")
21 self.assertEqual(
22 b.deconstruct(),
23 [('', [u'line 1', u'line 2'])]
24 )
25
26 def test_deconstruct_block(self):
27 b = Block()
28 b.append(u'-> This is a pragma.')
29 b.append(u"| This is some test input.")
30 b.append(u"| It extends over two lines.")
31 b.append(u'? Expected Error')
32 b.append(u'Plain text')
33 b.append(u'More plain text')
34 b.append(u'| Test with input')
35 b.append(u'+ input-for-test')
36 b.append(u'= Expected result on output')
37 b.append(u'= which extends over two lines')
38 self.assertEqual(
39 [pair[0] for pair in b.deconstruct()],
40 [u'->', u'| ', u'? ', '', u'| ', u'+ ', u'= ']
41 )
42
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)
67
68
69 class DocumentTestCase(TestCase):
70 def test_document(self):
71 d = Document()
72 d.append(u'This is a test file.')
73 d.append(u' -> This is a pragma.')
74 d.append(u'')
75 d.append(u" | This is some test input.\n")
76 d.append(u" | It extends over two lines.")
77 d.append(u' ? Expected Error')
78 d.append(u'')
79 d.append(u' | Test with input')
80 d.append(u' + input-for-test')
81 d.append(u' = Expected result on output')
82 blocks = d.parse_lines_to_blocks()
83 self.assertEqual(
84 [block.lines for block in blocks if isinstance(block, InterveningText)],
85 [[u'This is a test file.'], [u''], [u'']]
86 )
87 self.assertEqual(
88 [b.__class__.__name__ for b in blocks],
89 ['InterveningText', 'Block', 'InterveningText', 'Block', 'InterveningText', 'Block']
90 )
91 self.assertEqual(
92 [b.line_num for b in blocks],
93 [1, 2, 3, 4, 7, 8]
94 )
95
96 def test_extract_tests_empty(self):
97 d = Document()
98 d.append(u"This is a text file.")
99 d.append(u'It contains NO tests.')
100 functionalities = {}
101 self.assertEqual(d.extract_tests(functionalities), [])
102
103 def test_extract_tests_basic(self):
104 d = Document()
105 d.append(u'This is a test file.')
106 d.append(u' -> Tests for functionality "Parse Thing"')
107 d.append(u'')
108 d.append(u" | This is some test body.")
109 d.append(u' = Expected result')
110 functionalities = {}
111 self.assertEqual(
112 repr(d.extract_tests(functionalities)),
113 "[Test(body_block=Block(line_num=4), input_block=None, "
114 "expectation=OutputOutcome(u'Expected result'), "
115 "functionality=Functionality(u'Parse Thing'), "
116 "desc_block=InterveningText(line_num=1), "
117 "body=u'This is some test body.', input=None)]"
118 )
119
120 def test_extract_tests_more(self):
121 d = Document()
122 d.append(u'This is a test file.')
123 d.append(u' -> Tests for functionality "Parse Thing"')
124 d.append(u'')
125 d.append(u" | This is some test body.")
126 d.append(u" | It extends over two lines.")
127 d.append(u' ? Expected Error')
128 d.append(u'')
129 d.append(u' | Test with input')
130 d.append(u' + input-for-test')
131 d.append(u' = Expected result on output')
132 d.append(u'')
133 d.append(u' + Other input-for-test')
134 d.append(u' = Other Expected result on output')
135 d.append(u'')
136 d.append(u' -> Tests for functionality "Run Thing"')
137 d.append(u'')
138 d.append(u" | Thing")
139 d.append(u' ? Oops')
140 functionalities = {}
141 tests = d.extract_tests(functionalities)
142 self.assertEqual(
143 [t.body for t in tests],
144 [u'This is some test body.\nIt extends over two lines.',
145 u'Test with input', u'Test with input', u'Thing']
146 )
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')]
215
216
217 class ShellImplementationTestCase(TestCase):
218 def test_cat(self):
219 i = ShellImplementation('cat')
220 self.assertEqual(i.run(body=u'text'), OutputOutcome(u'text'))
221
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):
250 def test_test_contents(self):
251 b = Block()
252 b.append(u'foo')
253 b.append(u'bar')
254 i = Block()
255 i.append(u'green')
256 t = Test(body_block=b, input_block=i)
257 self.assertEqual(t.body, "foo\nbar")
258 self.assertEqual(t.input, "green")
259
260 def test_tests_1(self):
261 f = Functionality('Cat File')
262 f.add_implementation(CallableImplementation(lambda x, y: x))
263 t = Test(body=u'foo', expectation=OutputOutcome(u'foo'), functionality=f)
264 self.assertEqual(
265 [r.short_description() for r in t.run()]
266 ['success']
267 )
268
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')"]
333
334
335 if __name__ == '__main__':
336 unittest.main()
11
22 # Really crude test harness for py-falderal itself...
33
4 bin/falderal -v -t || exit 1
4 PYTHONPATH=src python src/falderal/tests.py -v || exit 1
55
66 cd tests
77