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