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