##// END OF EJS Templates
Complete first pass on testing system. All tests pass on my box. Whew....
Fernando Perez -
Show More
@@ -61,6 +61,8 b' from IPython import platutils'
61 61 import IPython.generics
62 62 import IPython.ipapi
63 63 from IPython.ipapi import UsageError
64 from IPython.testing import decorators as testdec
65
64 66 #***************************************************************************
65 67 # Utility functions
66 68 def on_off(tag):
@@ -522,7 +524,7 b' Currently the magic system has the following functions:\\n"""'
522 524 rc.automagic = not rc.automagic
523 525 print '\n' + Magic.auto_status[rc.automagic]
524 526
525
527 @testdec.skip_doctest
526 528 def magic_autocall(self, parameter_s = ''):
527 529 """Make functions callable without having to type parentheses.
528 530
@@ -551,8 +553,9 b' Currently the magic system has the following functions:\\n"""'
551 553 2 -> Active always. Even if no arguments are present, the callable
552 554 object is called:
553 555
554 In [4]: callable
555 ------> callable()
556 In [2]: float
557 ------> float()
558 Out[2]: 0.0
556 559
557 560 Note that even with autocall off, you can still use '/' at the start of
558 561 a line to treat the first argument on the command line as a function
@@ -561,6 +564,8 b' Currently the magic system has the following functions:\\n"""'
561 564 In [8]: /str 43
562 565 ------> str(43)
563 566 Out[8]: '43'
567
568 # all-random (note for auto-testing)
564 569 """
565 570
566 571 rc = self.shell.rc
@@ -1243,12 +1248,13 b' Currently the magic system has the following functions:\\n"""'
1243 1248
1244 1249 self.shell.debugger(force=True)
1245 1250
1251 @testdec.skip_doctest
1246 1252 def magic_prun(self, parameter_s ='',user_mode=1,
1247 1253 opts=None,arg_lst=None,prog_ns=None):
1248 1254
1249 1255 """Run a statement through the python code profiler.
1250 1256
1251 Usage:\\
1257 Usage:
1252 1258 %prun [options] statement
1253 1259
1254 1260 The given statement (which doesn't require quote marks) is run via the
@@ -1293,16 +1299,16 b' Currently the magic system has the following functions:\\n"""'
1293 1299 abbreviation is unambiguous. The following are the keys currently
1294 1300 defined:
1295 1301
1296 Valid Arg Meaning\\
1297 "calls" call count\\
1298 "cumulative" cumulative time\\
1299 "file" file name\\
1300 "module" file name\\
1301 "pcalls" primitive call count\\
1302 "line" line number\\
1303 "name" function name\\
1304 "nfl" name/file/line\\
1305 "stdname" standard name\\
1302 Valid Arg Meaning
1303 "calls" call count
1304 "cumulative" cumulative time
1305 "file" file name
1306 "module" file name
1307 "pcalls" primitive call count
1308 "line" line number
1309 "name" function name
1310 "nfl" name/file/line
1311 "stdname" standard name
1306 1312 "time" internal time
1307 1313
1308 1314 Note that all sorts on statistics are in descending order (placing
@@ -1328,8 +1334,10 b' Currently the magic system has the following functions:\\n"""'
1328 1334 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
1329 1335 contains profiler specific options as described here.
1330 1336
1331 You can read the complete documentation for the profile module with:\\
1332 In []: import profile; profile.help() """
1337 You can read the complete documentation for the profile module with::
1338
1339 In [1]: import profile; profile.help()
1340 """
1333 1341
1334 1342 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
1335 1343 # protect user quote marks
@@ -1413,6 +1421,7 b' Currently the magic system has the following functions:\\n"""'
1413 1421 else:
1414 1422 return None
1415 1423
1424 @testdec.skip_doctest
1416 1425 def magic_run(self, parameter_s ='',runner=None):
1417 1426 """Run the named file inside IPython as a program.
1418 1427
@@ -1709,6 +1718,7 b' Currently the magic system has the following functions:\\n"""'
1709 1718 self.shell.safe_execfile(f,self.shell.user_ns,
1710 1719 self.shell.user_ns,islog=1)
1711 1720
1721 @testdec.skip_doctest
1712 1722 def magic_timeit(self, parameter_s =''):
1713 1723 """Time execution of a Python statement or expression
1714 1724
@@ -1736,7 +1746,8 b' Currently the magic system has the following functions:\\n"""'
1736 1746 Default: 3
1737 1747
1738 1748
1739 Examples:\\
1749 Examples:
1750
1740 1751 In [1]: %timeit pass
1741 1752 10000000 loops, best of 3: 53.3 ns per loop
1742 1753
@@ -1820,7 +1831,8 b' Currently the magic system has the following functions:\\n"""'
1820 1831 units[order])
1821 1832 if tc > tc_min:
1822 1833 print "Compiler time: %.2f s" % tc
1823
1834
1835 @testdec.skip_doctest
1824 1836 def magic_time(self,parameter_s = ''):
1825 1837 """Time execution of a Python statement or expression.
1826 1838
@@ -1912,6 +1924,7 b' Currently the magic system has the following functions:\\n"""'
1912 1924 print "Compiler : %.2f s" % tc
1913 1925 return out
1914 1926
1927 @testdec.skip_doctest
1915 1928 def magic_macro(self,parameter_s = ''):
1916 1929 """Define a set of input lines as a macro for future re-execution.
1917 1930
@@ -1941,17 +1954,17 b' Currently the magic system has the following functions:\\n"""'
1941 1954
1942 1955 For example, if your history contains (%hist prints it):
1943 1956
1944 44: x=1\\
1945 45: y=3\\
1946 46: z=x+y\\
1947 47: print x\\
1948 48: a=5\\
1949 49: print 'x',x,'y',y\\
1957 44: x=1
1958 45: y=3
1959 46: z=x+y
1960 47: print x
1961 48: a=5
1962 49: print 'x',x,'y',y
1950 1963
1951 1964 you can create a macro with lines 44 through 47 (included) and line 49
1952 1965 called my_macro with:
1953 1966
1954 In []: %macro my_macro 44-47 49
1967 In [55]: %macro my_macro 44-47 49
1955 1968
1956 1969 Now, typing `my_macro` (without quotes) will re-execute all this code
1957 1970 in one pass.
@@ -1972,7 +1985,7 b' Currently the magic system has the following functions:\\n"""'
1972 1985 can obtain similar results by explicitly executing slices from your
1973 1986 input history with:
1974 1987
1975 In []: exec In[44:48]+In[49]"""
1988 In [60]: exec In[44:48]+In[49]"""
1976 1989
1977 1990 opts,args = self.parse_options(parameter_s,'r',mode='list')
1978 1991 if not args:
@@ -2043,6 +2056,7 b' Currently the magic system has the following functions:\\n"""'
2043 2056 """Alias to %edit."""
2044 2057 return self.magic_edit(parameter_s)
2045 2058
2059 @testdec.skip_doctest
2046 2060 def magic_edit(self,parameter_s='',last_call=['','']):
2047 2061 """Bring up an editor and execute the resulting code.
2048 2062
@@ -2136,47 +2150,47 b' Currently the magic system has the following functions:\\n"""'
2136 2150 This is an example of creating a simple function inside the editor and
2137 2151 then modifying it. First, start up the editor:
2138 2152
2139 In []: ed\\
2140 Editing... done. Executing edited code...\\
2141 Out[]: 'def foo():\\n print "foo() was defined in an editing session"\\n'
2153 In [1]: ed
2154 Editing... done. Executing edited code...
2155 Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
2142 2156
2143 2157 We can then call the function foo():
2144 2158
2145 In []: foo()\\
2159 In [2]: foo()
2146 2160 foo() was defined in an editing session
2147 2161
2148 2162 Now we edit foo. IPython automatically loads the editor with the
2149 2163 (temporary) file where foo() was previously defined:
2150 2164
2151 In []: ed foo\\
2165 In [3]: ed foo
2152 2166 Editing... done. Executing edited code...
2153 2167
2154 2168 And if we call foo() again we get the modified version:
2155 2169
2156 In []: foo()\\
2170 In [4]: foo()
2157 2171 foo() has now been changed!
2158 2172
2159 2173 Here is an example of how to edit a code snippet successive
2160 2174 times. First we call the editor:
2161 2175
2162 In []: ed\\
2163 Editing... done. Executing edited code...\\
2164 hello\\
2165 Out[]: "print 'hello'\\n"
2176 In [5]: ed
2177 Editing... done. Executing edited code...
2178 hello
2179 Out[5]: "print 'hello'n"
2166 2180
2167 2181 Now we call it again with the previous output (stored in _):
2168 2182
2169 In []: ed _\\
2170 Editing... done. Executing edited code...\\
2171 hello world\\
2172 Out[]: "print 'hello world'\\n"
2183 In [6]: ed _
2184 Editing... done. Executing edited code...
2185 hello world
2186 Out[6]: "print 'hello world'n"
2173 2187
2174 2188 Now we call it with the output #8 (stored in _8, also as Out[8]):
2175 2189
2176 In []: ed _8\\
2177 Editing... done. Executing edited code...\\
2178 hello again\\
2179 Out[]: "print 'hello again'\\n"
2190 In [7]: ed _8
2191 Editing... done. Executing edited code...
2192 hello again
2193 Out[7]: "print 'hello again'n"
2180 2194
2181 2195
2182 2196 Changing the default editor hook:
@@ -2473,7 +2487,8 b' Defaulting color scheme to \'NoColor\'"""'
2473 2487
2474 2488 #......................................................................
2475 2489 # Functions to implement unix shell-type things
2476
2490
2491 @testdec.skip_doctest
2477 2492 def magic_alias(self, parameter_s = ''):
2478 2493 """Define an alias for a system command.
2479 2494
@@ -2489,18 +2504,18 b' Defaulting color scheme to \'NoColor\'"""'
2489 2504 You can use the %l specifier in an alias definition to represent the
2490 2505 whole line when the alias is called. For example:
2491 2506
2492 In [2]: alias all echo "Input in brackets: <%l>"\\
2493 In [3]: all hello world\\
2507 In [2]: alias all echo "Input in brackets: <%l>"
2508 In [3]: all hello world
2494 2509 Input in brackets: <hello world>
2495 2510
2496 2511 You can also define aliases with parameters using %s specifiers (one
2497 2512 per parameter):
2498 2513
2499 In [1]: alias parts echo first %s second %s\\
2500 In [2]: %parts A B\\
2501 first A second B\\
2502 In [3]: %parts A\\
2503 Incorrect number of arguments: 2 expected.\\
2514 In [1]: alias parts echo first %s second %s
2515 In [2]: %parts A B
2516 first A second B
2517 In [3]: %parts A
2518 Incorrect number of arguments: 2 expected.
2504 2519 parts is an alias to: 'echo first %s second %s'
2505 2520
2506 2521 Note that %l and %s are mutually exclusive. You can only use one or
@@ -2513,11 +2528,11 b' Defaulting color scheme to \'NoColor\'"""'
2513 2528 IPython for variable expansion. If you want to access a true shell
2514 2529 variable, an extra $ is necessary to prevent its expansion by IPython:
2515 2530
2516 In [6]: alias show echo\\
2517 In [7]: PATH='A Python string'\\
2518 In [8]: show $PATH\\
2519 A Python string\\
2520 In [9]: show $$PATH\\
2531 In [6]: alias show echo
2532 In [7]: PATH='A Python string'
2533 In [8]: show $PATH
2534 A Python string
2535 In [9]: show $$PATH
2521 2536 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
2522 2537
2523 2538 You can use the alias facility to acess all of $PATH. See the %rehash
@@ -2832,7 +2847,7 b' Defaulting color scheme to \'NoColor\'"""'
2832 2847 header = 'Directory history (kept in _dh)',
2833 2848 start=ini,stop=fin)
2834 2849
2835
2850 @testdec.skip_doctest
2836 2851 def magic_sc(self, parameter_s=''):
2837 2852 """Shell capture - execute a shell command and capture its output.
2838 2853
@@ -2876,29 +2891,31 b' Defaulting color scheme to \'NoColor\'"""'
2876 2891
2877 2892 For example:
2878 2893
2894 # all-random
2895
2879 2896 # Capture into variable a
2880 In []: sc a=ls *py
2897 In [1]: sc a=ls *py
2881 2898
2882 2899 # a is a string with embedded newlines
2883 In []: a
2884 Out[]: 'setup.py\nwin32_manual_post_install.py'
2900 In [2]: a
2901 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
2885 2902
2886 2903 # which can be seen as a list:
2887 In []: a.l
2888 Out[]: ['setup.py', 'win32_manual_post_install.py']
2904 In [3]: a.l
2905 Out[3]: ['setup.py', 'win32_manual_post_install.py']
2889 2906
2890 2907 # or as a whitespace-separated string:
2891 In []: a.s
2892 Out[]: 'setup.py win32_manual_post_install.py'
2908 In [4]: a.s
2909 Out[4]: 'setup.py win32_manual_post_install.py'
2893 2910
2894 2911 # a.s is useful to pass as a single command line:
2895 In []: !wc -l $a.s
2912 In [5]: !wc -l $a.s
2896 2913 146 setup.py
2897 2914 130 win32_manual_post_install.py
2898 2915 276 total
2899 2916
2900 2917 # while the list form is useful to loop over:
2901 In []: for f in a.l:
2918 In [6]: for f in a.l:
2902 2919 ...: !wc -l $f
2903 2920 ...:
2904 2921 146 setup.py
@@ -2908,13 +2925,13 b' Defaulting color scheme to \'NoColor\'"""'
2908 2925 the sense that you can equally invoke the .s attribute on them to
2909 2926 automatically get a whitespace-separated string from their contents:
2910 2927
2911 In []: sc -l b=ls *py
2928 In [7]: sc -l b=ls *py
2912 2929
2913 In []: b
2914 Out[]: ['setup.py', 'win32_manual_post_install.py']
2930 In [8]: b
2931 Out[8]: ['setup.py', 'win32_manual_post_install.py']
2915 2932
2916 In []: b.s
2917 Out[]: 'setup.py win32_manual_post_install.py'
2933 In [9]: b.s
2934 Out[9]: 'setup.py win32_manual_post_install.py'
2918 2935
2919 2936 In summary, both the lists and strings used for ouptut capture have
2920 2937 the following special attributes:
@@ -1010,7 +1010,7 b' class InteractiveShell(object,Magic):'
1010 1010 hello
1011 1011
1012 1012 In [10]: _ip.IP.complete('x.l')
1013 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1013 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip'] # random
1014 1014 """
1015 1015
1016 1016 complete = self.Completer.complete
@@ -88,8 +88,8 b' class Struct:'
88 88 initialization): keys can't be numbers. But numeric keys can be used and
89 89 accessed using the dictionary syntax. Again, an example:
90 90
91 This doesn't work:
92 py> s=Struct(4='hi') #doctest: +IGNORE_EXCEPTION_DETAIL
91 This doesn't work (prompt changed to avoid confusing the test system):
92 ->> s=Struct(4='hi')
93 93 Traceback (most recent call last):
94 94 ...
95 95 SyntaxError: keyword can't be an expression
@@ -1,3 +1,5 b''
1 from __future__ import with_statement
2
1 3 #def test_simple():
2 4 if 0:
3 5
1 NO CONTENT: file renamed from IPython/testing/plugin/decorator_msim.py to IPython/testing/decorator_msim.py
@@ -19,7 +19,7 b' import inspect'
19 19 # Third-party imports
20 20
21 21 # This is Michele Simionato's decorator module, also kept verbatim.
22 from decorator_msim import decorator
22 from decorator_msim import decorator, update_wrapper
23 23
24 24 # Grab the numpy-specific decorators which we keep in a file that we
25 25 # occasionally update from upstream: decorators_numpy.py is an IDENTICAL copy
@@ -113,23 +113,12 b' def make_label_dec(label,ds=None):'
113 113 #-----------------------------------------------------------------------------
114 114 # Decorators for public use
115 115
116 def skip_doctest(func):
117 """Decorator - mark a function for skipping its doctest.
116 skip_doctest = make_label_dec('skip_doctest',
117 """Decorator - mark a function or method for skipping its doctest.
118 118
119 119 This decorator allows you to mark a function whose docstring you wish to
120 120 omit from testing, while preserving the docstring for introspection, help,
121 etc."""
122
123 # We just return the function unmodified, but the wrapping has the effect
124 # of making the doctest plugin skip the doctest.
125 def wrapper(*a,**k):
126 return func(*a,**k)
127
128 # Here we use plain 'decorator' and not apply_wrapper, because we don't
129 # need all the nose-protection machinery (functions containing doctests
130 # can't be full-blown nose tests, so we don't need to prserve
131 # setup/teardown).
132 return decorator(wrapper,func)
121 etc.""")
133 122
134 123
135 124 def skip(func):
1 NO CONTENT: file renamed from IPython/testing/plugin/decorators_numpy.py to IPython/testing/decorators_numpy.py
@@ -7,7 +7,7 b' NOSE=nosetests -vvs --with-ipdoctest --doctest-tests --doctest-extension=txt'
7 7
8 8 #--with-color
9 9
10 SRC=ipdoctest.py setup.py decorators.py
10 SRC=ipdoctest.py setup.py ../decorators.py
11 11
12 12 plugin: IPython_doctest_plugin.egg-info
13 13
@@ -32,7 +32,13 b' iptest: plugin'
32 32 $(NOSE) IPython
33 33
34 34 deco:
35 $(NOSE0) decorators.py
35 $(NOSE0) IPython.testing.decorators
36
37 mtest: plugin
38 $(NOSE) -x IPython.Magic
39
40 ipipe: plugin
41 $(NOSE) -x IPython.Extensions.ipipe
36 42
37 43 sr: rtest std
38 44
@@ -158,6 +158,16 b' def is_extension_module(filename):'
158 158 return os.path.splitext(filename)[1].lower() in ('.so','.pyd')
159 159
160 160
161 class nodoc(object):
162 def __init__(self,obj):
163 self.obj = obj
164
165 def __getattribute__(self,key):
166 if key == '__doc__':
167 return None
168 else:
169 return getattr(object.__getattribute__(self,'obj'),key)
170
161 171 # Modified version of the one in the stdlib, that fixes a python bug (doctests
162 172 # not found in extension modules, http://bugs.python.org/issue3158)
163 173 class DocTestFinder(doctest.DocTestFinder):
@@ -196,6 +206,10 b' class DocTestFinder(doctest.DocTestFinder):'
196 206 add them to `tests`.
197 207 """
198 208
209 if hasattr(obj,"skip_doctest"):
210 #print 'SKIPPING DOCTEST FOR:',obj # dbg
211 obj = nodoc(obj)
212
199 213 doctest.DocTestFinder._find(self,tests, obj, name, module,
200 214 source_lines, globs, seen)
201 215
@@ -591,6 +605,12 b' class IPDocTestRunner(doctest.DocTestRunner,object):'
591 605 # for all examples, most of which won't be calling %run anyway).
592 606 _run_ns_sync.test_globs = test.globs
593 607
608 # dbg
609 ## print >> sys.stderr, "Test:",test
610 ## for ex in test.examples:
611 ## print >> sys.stderr, ex.source
612 ## print >> sys.stderr, 'Want:\n',ex.want,'\n--'
613
594 614 return super(IPDocTestRunner,self).run(test,
595 615 compileflags,out,clear_globs)
596 616
@@ -5,7 +5,7 b' import inspect'
5 5 # Third party
6 6
7 7 # Our own
8 import decorators as dec
8 from IPython.testing import decorators as dec
9 9
10 10 #-----------------------------------------------------------------------------
11 11 # Utilities
@@ -55,7 +55,65 b' def doctest_bad(x,y=1,**k):'
55 55 >>> 1+1
56 56 3
57 57 """
58 z=2
58 print 'x:',x
59 print 'y:',y
60 print 'k:',k
61
62
63 def call_doctest_bad():
64 """Check that we can still call the decorated functions.
65
66 >>> doctest_bad(3,y=4)
67 x: 3
68 y: 4
69 k: {}
70 """
71 pass
72
73
74 # Doctest skipping should work for class methods too
75 class foo(object):
76 """Foo
77
78 Example:
79
80 >>> 1+1
81 2
82 """
83
84 @dec.skip_doctest
85 def __init__(self,x):
86 """Make a foo.
87
88 Example:
89
90 >>> f = foo(3)
91 junk
92 """
93 print 'Making a foo.'
94 self.x = x
95
96 @dec.skip_doctest
97 def bar(self,y):
98 """Example:
99
100 >>> f = foo(3)
101 >>> f.bar(0)
102 boom!
103 >>> 1/0
104 bam!
105 """
106 return 1/y
107
108 def baz(self,y):
109 """Example:
110
111 >>> f = foo(3)
112 Making a foo.
113 >>> f.baz(3)
114 True
115 """
116 return self.x==y
59 117
60 118
61 119 def test_skip_dt_decorator():
@@ -24,4 +24,5 b' are trapped first by Python itself.'
24 24 """
25 25
26 26 import IPython.Shell
27
27 28 IPython.Shell.start().mainloop()
General Comments 0
You need to be logged in to leave comments. Login now