Show More
@@ -1,219 +1,219 b'' | |||||
1 | """Tests for the decorators we've created for IPython. |
|
1 | """Tests for the decorators we've created for IPython. | |
2 | """ |
|
2 | """ | |
3 |
|
3 | |||
4 | # Module imports |
|
4 | # Module imports | |
5 | # Std lib |
|
5 | # Std lib | |
6 | import inspect |
|
6 | import inspect | |
7 | import sys |
|
7 | import sys | |
8 | import unittest |
|
8 | import unittest | |
9 |
|
9 | |||
10 | # Third party |
|
10 | # Third party | |
11 | import nose.tools as nt |
|
11 | import nose.tools as nt | |
12 |
|
12 | |||
13 | # Our own |
|
13 | # Our own | |
14 | from IPython.testing import decorators as dec |
|
14 | from IPython.testing import decorators as dec | |
15 | from IPython.testing.ipunittest import ParametricTestCase |
|
15 | from IPython.testing.ipunittest import ParametricTestCase | |
16 |
|
16 | |||
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 | # Utilities |
|
18 | # Utilities | |
19 |
|
19 | |||
20 | # Note: copied from OInspect, kept here so the testing stuff doesn't create |
|
20 | # Note: copied from OInspect, kept here so the testing stuff doesn't create | |
21 | # circular dependencies and is easier to reuse. |
|
21 | # circular dependencies and is easier to reuse. | |
22 | def getargspec(obj): |
|
22 | def getargspec(obj): | |
23 | """Get the names and default values of a function's arguments. |
|
23 | """Get the names and default values of a function's arguments. | |
24 |
|
24 | |||
25 | A tuple of four things is returned: (args, varargs, varkw, defaults). |
|
25 | A tuple of four things is returned: (args, varargs, varkw, defaults). | |
26 | 'args' is a list of the argument names (it may contain nested lists). |
|
26 | 'args' is a list of the argument names (it may contain nested lists). | |
27 | 'varargs' and 'varkw' are the names of the * and ** arguments or None. |
|
27 | 'varargs' and 'varkw' are the names of the * and ** arguments or None. | |
28 | 'defaults' is an n-tuple of the default values of the last n arguments. |
|
28 | 'defaults' is an n-tuple of the default values of the last n arguments. | |
29 |
|
29 | |||
30 | Modified version of inspect.getargspec from the Python Standard |
|
30 | Modified version of inspect.getargspec from the Python Standard | |
31 | Library.""" |
|
31 | Library.""" | |
32 |
|
32 | |||
33 | if inspect.isfunction(obj): |
|
33 | if inspect.isfunction(obj): | |
34 | func_obj = obj |
|
34 | func_obj = obj | |
35 | elif inspect.ismethod(obj): |
|
35 | elif inspect.ismethod(obj): | |
36 | func_obj = obj.im_func |
|
36 | func_obj = obj.im_func | |
37 | else: |
|
37 | else: | |
38 | raise TypeError, 'arg is not a Python function' |
|
38 | raise TypeError, 'arg is not a Python function' | |
39 | args, varargs, varkw = inspect.getargs(func_obj.func_code) |
|
39 | args, varargs, varkw = inspect.getargs(func_obj.func_code) | |
40 | return args, varargs, varkw, func_obj.func_defaults |
|
40 | return args, varargs, varkw, func_obj.func_defaults | |
41 |
|
41 | |||
42 | #----------------------------------------------------------------------------- |
|
42 | #----------------------------------------------------------------------------- | |
43 | # Testing functions |
|
43 | # Testing functions | |
44 |
|
44 | |||
45 | @dec.as_unittest |
|
45 | @dec.as_unittest | |
46 | def trivial(): |
|
46 | def trivial(): | |
47 | """A trivial test""" |
|
47 | """A trivial test""" | |
48 | pass |
|
48 | pass | |
49 |
|
49 | |||
50 | # Some examples of parametric tests. |
|
50 | # Some examples of parametric tests. | |
51 |
|
51 | |||
52 | def is_smaller(i,j): |
|
52 | def is_smaller(i,j): | |
53 | assert i<j,"%s !< %s" % (i,j) |
|
53 | assert i<j,"%s !< %s" % (i,j) | |
54 |
|
54 | |||
55 | class Tester(ParametricTestCase): |
|
55 | class Tester(ParametricTestCase): | |
56 |
|
56 | |||
57 | def test_parametric(self): |
|
57 | def test_parametric(self): | |
58 | yield is_smaller(3, 4) |
|
58 | yield is_smaller(3, 4) | |
59 | x, y = 1, 2 |
|
59 | x, y = 1, 2 | |
60 | yield is_smaller(x, y) |
|
60 | yield is_smaller(x, y) | |
61 |
|
61 | |||
62 | @dec.parametric |
|
62 | @dec.parametric | |
63 | def test_par_standalone(): |
|
63 | def test_par_standalone(): | |
64 | yield is_smaller(3, 4) |
|
64 | yield is_smaller(3, 4) | |
65 | x, y = 1, 2 |
|
65 | x, y = 1, 2 | |
66 | yield is_smaller(x, y) |
|
66 | yield is_smaller(x, y) | |
67 |
|
67 | |||
68 |
|
68 | |||
69 | @dec.skip |
|
69 | @dec.skip | |
70 | def test_deliberately_broken(): |
|
70 | def test_deliberately_broken(): | |
71 | """A deliberately broken test - we want to skip this one.""" |
|
71 | """A deliberately broken test - we want to skip this one.""" | |
72 | 1/0 |
|
72 | 1/0 | |
73 |
|
73 | |||
74 | @dec.skip('Testing the skip decorator') |
|
74 | @dec.skip('Testing the skip decorator') | |
75 | def test_deliberately_broken2(): |
|
75 | def test_deliberately_broken2(): | |
76 | """Another deliberately broken test - we want to skip this one.""" |
|
76 | """Another deliberately broken test - we want to skip this one.""" | |
77 | 1/0 |
|
77 | 1/0 | |
78 |
|
78 | |||
79 |
|
79 | |||
80 | # Verify that we can correctly skip the doctest for a function at will, but |
|
80 | # Verify that we can correctly skip the doctest for a function at will, but | |
81 | # that the docstring itself is NOT destroyed by the decorator. |
|
81 | # that the docstring itself is NOT destroyed by the decorator. | |
82 | @dec.skip_doctest |
|
82 | @dec.skip_doctest | |
83 | def doctest_bad(x,y=1,**k): |
|
83 | def doctest_bad(x,y=1,**k): | |
84 | """A function whose doctest we need to skip. |
|
84 | """A function whose doctest we need to skip. | |
85 |
|
85 | |||
86 | >>> 1+1 |
|
86 | >>> 1+1 | |
87 | 3 |
|
87 | 3 | |
88 | """ |
|
88 | """ | |
89 | print 'x:',x |
|
89 | print 'x:',x | |
90 | print 'y:',y |
|
90 | print 'y:',y | |
91 | print 'k:',k |
|
91 | print 'k:',k | |
92 |
|
92 | |||
93 |
|
93 | |||
94 | def call_doctest_bad(): |
|
94 | def call_doctest_bad(): | |
95 | """Check that we can still call the decorated functions. |
|
95 | """Check that we can still call the decorated functions. | |
96 |
|
96 | |||
97 | >>> doctest_bad(3,y=4) |
|
97 | >>> doctest_bad(3,y=4) | |
98 | x: 3 |
|
98 | x: 3 | |
99 | y: 4 |
|
99 | y: 4 | |
100 | k: {} |
|
100 | k: {} | |
101 | """ |
|
101 | """ | |
102 | pass |
|
102 | pass | |
103 |
|
103 | |||
104 |
|
104 | |||
105 | def test_skip_dt_decorator(): |
|
105 | def test_skip_dt_decorator(): | |
106 | """Doctest-skipping decorator should preserve the docstring. |
|
106 | """Doctest-skipping decorator should preserve the docstring. | |
107 | """ |
|
107 | """ | |
108 | # Careful: 'check' must be a *verbatim* copy of the doctest_bad docstring! |
|
108 | # Careful: 'check' must be a *verbatim* copy of the doctest_bad docstring! | |
109 | check = """A function whose doctest we need to skip. |
|
109 | check = """A function whose doctest we need to skip. | |
110 |
|
110 | |||
111 | >>> 1+1 |
|
111 | >>> 1+1 | |
112 | 3 |
|
112 | 3 | |
113 | """ |
|
113 | """ | |
114 | # Fetch the docstring from doctest_bad after decoration. |
|
114 | # Fetch the docstring from doctest_bad after decoration. | |
115 | val = doctest_bad.__doc__ |
|
115 | val = doctest_bad.__doc__ | |
116 |
|
116 | |||
117 |
assert |
|
117 | nt.assert_equal(check,val,"doctest_bad docstrings don't match") | |
|
118 | ||||
118 |
|
119 | |||
119 | # Doctest skipping should work for class methods too |
|
120 | # Doctest skipping should work for class methods too | |
120 |
class |
|
121 | class FooClass(object): | |
121 | """Foo |
|
122 | """FooClass | |
122 |
|
123 | |||
123 | Example: |
|
124 | Example: | |
124 |
|
125 | |||
125 | >>> 1+1 |
|
126 | >>> 1+1 | |
126 | 2 |
|
127 | 2 | |
127 | """ |
|
128 | """ | |
128 |
|
129 | |||
129 | @dec.skip_doctest |
|
130 | @dec.skip_doctest | |
130 | def __init__(self,x): |
|
131 | def __init__(self,x): | |
131 |
"""Make a |
|
132 | """Make a FooClass. | |
132 |
|
133 | |||
133 | Example: |
|
134 | Example: | |
134 |
|
135 | |||
135 |
>>> f = |
|
136 | >>> f = FooClass(3) | |
136 | junk |
|
137 | junk | |
137 | """ |
|
138 | """ | |
138 |
print 'Making a |
|
139 | print 'Making a FooClass.' | |
139 | self.x = x |
|
140 | self.x = x | |
140 |
|
141 | |||
141 | @dec.skip_doctest |
|
142 | @dec.skip_doctest | |
142 | def bar(self,y): |
|
143 | def bar(self,y): | |
143 | """Example: |
|
144 | """Example: | |
144 |
|
145 | |||
145 |
>>> f |
|
146 | >>> ff = FooClass(3) | |
146 | >>> f.bar(0) |
|
147 | >>> ff.bar(0) | |
147 | boom! |
|
148 | boom! | |
148 | >>> 1/0 |
|
149 | >>> 1/0 | |
149 | bam! |
|
150 | bam! | |
150 | """ |
|
151 | """ | |
151 | return 1/y |
|
152 | return 1/y | |
152 |
|
153 | |||
153 | def baz(self,y): |
|
154 | def baz(self,y): | |
154 | """Example: |
|
155 | """Example: | |
155 |
|
156 | |||
156 |
>>> f |
|
157 | >>> ff2 = FooClass(3) | |
157 |
Making a |
|
158 | Making a FooClass. | |
158 | >>> f.baz(3) |
|
159 | >>> ff2.baz(3) | |
159 | True |
|
160 | True | |
160 | """ |
|
161 | """ | |
161 | return self.x==y |
|
162 | return self.x==y | |
162 |
|
163 | |||
163 |
|
164 | |||
164 |
|
||||
165 | def test_skip_dt_decorator2(): |
|
165 | def test_skip_dt_decorator2(): | |
166 | """Doctest-skipping decorator should preserve function signature. |
|
166 | """Doctest-skipping decorator should preserve function signature. | |
167 | """ |
|
167 | """ | |
168 | # Hardcoded correct answer |
|
168 | # Hardcoded correct answer | |
169 | dtargs = (['x', 'y'], None, 'k', (1,)) |
|
169 | dtargs = (['x', 'y'], None, 'k', (1,)) | |
170 | # Introspect out the value |
|
170 | # Introspect out the value | |
171 | dtargsr = getargspec(doctest_bad) |
|
171 | dtargsr = getargspec(doctest_bad) | |
172 | assert dtargsr==dtargs, \ |
|
172 | assert dtargsr==dtargs, \ | |
173 | "Incorrectly reconstructed args for doctest_bad: %s" % (dtargsr,) |
|
173 | "Incorrectly reconstructed args for doctest_bad: %s" % (dtargsr,) | |
174 |
|
174 | |||
175 |
|
175 | |||
176 | @dec.skip_linux |
|
176 | @dec.skip_linux | |
177 | def test_linux(): |
|
177 | def test_linux(): | |
178 | nt.assert_not_equals(sys.platform,'linux2',"This test can't run under linux") |
|
178 | nt.assert_not_equals(sys.platform,'linux2',"This test can't run under linux") | |
179 |
|
179 | |||
180 | @dec.skip_win32 |
|
180 | @dec.skip_win32 | |
181 | def test_win32(): |
|
181 | def test_win32(): | |
182 | nt.assert_not_equals(sys.platform,'win32',"This test can't run under windows") |
|
182 | nt.assert_not_equals(sys.platform,'win32',"This test can't run under windows") | |
183 |
|
183 | |||
184 | @dec.skip_osx |
|
184 | @dec.skip_osx | |
185 | def test_osx(): |
|
185 | def test_osx(): | |
186 | nt.assert_not_equals(sys.platform,'darwin',"This test can't run under osx") |
|
186 | nt.assert_not_equals(sys.platform,'darwin',"This test can't run under osx") | |
187 |
|
187 | |||
188 |
|
188 | |||
189 | # Verify that the same decorators work for methods. |
|
189 | # Verify that the same decorators work for methods. | |
190 | # Note: this code is identical to that in test_decorators_trial, but that one |
|
190 | # Note: this code is identical to that in test_decorators_trial, but that one | |
191 | # uses twisted's unittest, not the one from the stdlib, which we are using |
|
191 | # uses twisted's unittest, not the one from the stdlib, which we are using | |
192 | # here. While somewhat redundant, we want to check both with the stdlib and |
|
192 | # here. While somewhat redundant, we want to check both with the stdlib and | |
193 | # with twisted, so the duplication is OK. |
|
193 | # with twisted, so the duplication is OK. | |
194 | class TestDecoratorsTrial(unittest.TestCase): |
|
194 | class TestDecoratorsTrial(unittest.TestCase): | |
195 |
|
195 | |||
196 | @dec.skip() |
|
196 | @dec.skip() | |
197 | def test_deliberately_broken(self): |
|
197 | def test_deliberately_broken(self): | |
198 | """A deliberately broken test - we want to skip this one.""" |
|
198 | """A deliberately broken test - we want to skip this one.""" | |
199 | 1/0 |
|
199 | 1/0 | |
200 |
|
200 | |||
201 | @dec.skip('Testing the skip decorator') |
|
201 | @dec.skip('Testing the skip decorator') | |
202 | def test_deliberately_broken2(self): |
|
202 | def test_deliberately_broken2(self): | |
203 | """Another deliberately broken test - we want to skip this one.""" |
|
203 | """Another deliberately broken test - we want to skip this one.""" | |
204 | 1/0 |
|
204 | 1/0 | |
205 |
|
205 | |||
206 | @dec.skip_linux |
|
206 | @dec.skip_linux | |
207 | def test_linux(self): |
|
207 | def test_linux(self): | |
208 | self.assertNotEquals(sys.platform, 'linux2', |
|
208 | self.assertNotEquals(sys.platform, 'linux2', | |
209 | "This test can't run under linux") |
|
209 | "This test can't run under linux") | |
210 |
|
210 | |||
211 | @dec.skip_win32 |
|
211 | @dec.skip_win32 | |
212 | def test_win32(self): |
|
212 | def test_win32(self): | |
213 | self.assertNotEquals(sys.platform, 'win32', |
|
213 | self.assertNotEquals(sys.platform, 'win32', | |
214 | "This test can't run under windows") |
|
214 | "This test can't run under windows") | |
215 |
|
215 | |||
216 | @dec.skip_osx |
|
216 | @dec.skip_osx | |
217 | def test_osx(self): |
|
217 | def test_osx(self): | |
218 | self.assertNotEquals(sys.platform, 'darwin', |
|
218 | self.assertNotEquals(sys.platform, 'darwin', | |
219 | "This test can't run under osx") |
|
219 | "This test can't run under osx") |
General Comments 0
You need to be logged in to leave comments.
Login now