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