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