Debian Codegraph @

tests/test_decorators.py

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