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