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