##// END OF EJS Templates
Fix some doctests for Python 2
Thomas Kluyver -
Show More
@@ -1,161 +1,158
1 """Simple example using doctests.
1 """Simple example using doctests.
2
2
3 This file just contains doctests both using plain python and IPython prompts.
3 This file just contains doctests both using plain python and IPython prompts.
4 All tests should be loaded by nose.
4 All tests should be loaded by nose.
5 """
5 """
6 from IPython.utils.py3compat import doctest_refactor_print
6 from __future__ import print_function
7
7
8 def pyfunc():
8 def pyfunc():
9 """Some pure python tests...
9 """Some pure python tests...
10
10
11 >>> pyfunc()
11 >>> pyfunc()
12 'pyfunc'
12 'pyfunc'
13
13
14 >>> import os
14 >>> import os
15
15
16 >>> 2+3
16 >>> 2+3
17 5
17 5
18
18
19 >>> for i in range(3):
19 >>> for i in range(3):
20 ... print(i, end=' ')
20 ... print(i, end=' ')
21 ... print(i+1, end=' ')
21 ... print(i+1, end=' ')
22 ...
22 ...
23 0 1 1 2 2 3
23 0 1 1 2 2 3
24 """
24 """
25 return 'pyfunc'
25 return 'pyfunc'
26
26
27 @doctest_refactor_print
28 def ipfunc():
27 def ipfunc():
29 """Some ipython tests...
28 """Some ipython tests...
30
29
31 In [1]: import os
30 In [1]: import os
32
31
33 In [3]: 2+3
32 In [3]: 2+3
34 Out[3]: 5
33 Out[3]: 5
35
34
36 In [26]: for i in range(3):
35 In [26]: for i in range(3):
37 ....: print(i, end=' ')
36 ....: print(i, end=' ')
38 ....: print(i+1, end=' ')
37 ....: print(i+1, end=' ')
39 ....:
38 ....:
40 0 1 1 2 2 3
39 0 1 1 2 2 3
41
40
42
41
43 Examples that access the operating system work:
42 Examples that access the operating system work:
44
43
45 In [1]: !echo hello
44 In [1]: !echo hello
46 hello
45 hello
47
46
48 In [2]: !echo hello > /tmp/foo_iptest
47 In [2]: !echo hello > /tmp/foo_iptest
49
48
50 In [3]: !cat /tmp/foo_iptest
49 In [3]: !cat /tmp/foo_iptest
51 hello
50 hello
52
51
53 In [4]: rm -f /tmp/foo_iptest
52 In [4]: rm -f /tmp/foo_iptest
54
53
55 It's OK to use '_' for the last result, but do NOT try to use IPython's
54 It's OK to use '_' for the last result, but do NOT try to use IPython's
56 numbered history of _NN outputs, since those won't exist under the
55 numbered history of _NN outputs, since those won't exist under the
57 doctest environment:
56 doctest environment:
58
57
59 In [7]: 'hi'
58 In [7]: 'hi'
60 Out[7]: 'hi'
59 Out[7]: 'hi'
61
60
62 In [8]: print(repr(_))
61 In [8]: print(repr(_))
63 'hi'
62 'hi'
64
63
65 In [7]: 3+4
64 In [7]: 3+4
66 Out[7]: 7
65 Out[7]: 7
67
66
68 In [8]: _+3
67 In [8]: _+3
69 Out[8]: 10
68 Out[8]: 10
70
69
71 In [9]: ipfunc()
70 In [9]: ipfunc()
72 Out[9]: 'ipfunc'
71 Out[9]: 'ipfunc'
73 """
72 """
74 return 'ipfunc'
73 return 'ipfunc'
75
74
76
75
77 def ranfunc():
76 def ranfunc():
78 """A function with some random output.
77 """A function with some random output.
79
78
80 Normal examples are verified as usual:
79 Normal examples are verified as usual:
81 >>> 1+3
80 >>> 1+3
82 4
81 4
83
82
84 But if you put '# random' in the output, it is ignored:
83 But if you put '# random' in the output, it is ignored:
85 >>> 1+3
84 >>> 1+3
86 junk goes here... # random
85 junk goes here... # random
87
86
88 >>> 1+2
87 >>> 1+2
89 again, anything goes #random
88 again, anything goes #random
90 if multiline, the random mark is only needed once.
89 if multiline, the random mark is only needed once.
91
90
92 >>> 1+2
91 >>> 1+2
93 You can also put the random marker at the end:
92 You can also put the random marker at the end:
94 # random
93 # random
95
94
96 >>> 1+2
95 >>> 1+2
97 # random
96 # random
98 .. or at the beginning.
97 .. or at the beginning.
99
98
100 More correct input is properly verified:
99 More correct input is properly verified:
101 >>> ranfunc()
100 >>> ranfunc()
102 'ranfunc'
101 'ranfunc'
103 """
102 """
104 return 'ranfunc'
103 return 'ranfunc'
105
104
106
105
107 def random_all():
106 def random_all():
108 """A function where we ignore the output of ALL examples.
107 """A function where we ignore the output of ALL examples.
109
108
110 Examples:
109 Examples:
111
110
112 # all-random
111 # all-random
113
112
114 This mark tells the testing machinery that all subsequent examples should
113 This mark tells the testing machinery that all subsequent examples should
115 be treated as random (ignoring their output). They are still executed,
114 be treated as random (ignoring their output). They are still executed,
116 so if a they raise an error, it will be detected as such, but their
115 so if a they raise an error, it will be detected as such, but their
117 output is completely ignored.
116 output is completely ignored.
118
117
119 >>> 1+3
118 >>> 1+3
120 junk goes here...
119 junk goes here...
121
120
122 >>> 1+3
121 >>> 1+3
123 klasdfj;
122 klasdfj;
124
123
125 >>> 1+2
124 >>> 1+2
126 again, anything goes
125 again, anything goes
127 blah...
126 blah...
128 """
127 """
129 pass
128 pass
130
129
131 @doctest_refactor_print
132 def iprand():
130 def iprand():
133 """Some ipython tests with random output.
131 """Some ipython tests with random output.
134
132
135 In [7]: 3+4
133 In [7]: 3+4
136 Out[7]: 7
134 Out[7]: 7
137
135
138 In [8]: print('hello')
136 In [8]: print('hello')
139 world # random
137 world # random
140
138
141 In [9]: iprand()
139 In [9]: iprand()
142 Out[9]: 'iprand'
140 Out[9]: 'iprand'
143 """
141 """
144 return 'iprand'
142 return 'iprand'
145
143
146 @doctest_refactor_print
147 def iprand_all():
144 def iprand_all():
148 """Some ipython tests with fully random output.
145 """Some ipython tests with fully random output.
149
146
150 # all-random
147 # all-random
151
148
152 In [7]: 1
149 In [7]: 1
153 Out[7]: 99
150 Out[7]: 99
154
151
155 In [8]: print('hello')
152 In [8]: print('hello')
156 world
153 world
157
154
158 In [9]: iprand_all()
155 In [9]: iprand_all()
159 Out[9]: 'junk'
156 Out[9]: 'junk'
160 """
157 """
161 return 'iprand_all'
158 return 'iprand_all'
@@ -1,33 +1,34
1 """Simple example using doctests.
1 """Simple example using doctests.
2
2
3 This file just contains doctests both using plain python and IPython prompts.
3 This file just contains doctests both using plain python and IPython prompts.
4 All tests should be loaded by nose.
4 All tests should be loaded by nose.
5 """
5 """
6 from __future__ import print_function
6
7
7 def pyfunc():
8 def pyfunc():
8 """Some pure python tests...
9 """Some pure python tests...
9
10
10 >>> pyfunc()
11 >>> pyfunc()
11 'pyfunc'
12 'pyfunc'
12
13
13 >>> import os
14 >>> import os
14
15
15 >>> 2+3
16 >>> 2+3
16 5
17 5
17
18
18 >>> for i in range(3):
19 >>> for i in range(3):
19 ... print(i, end=' ')
20 ... print(i, end=' ')
20 ... print(i+1, end=' ')
21 ... print(i+1, end=' ')
21 ...
22 ...
22 0 1 1 2 2 3
23 0 1 1 2 2 3
23 """
24 """
24 return 'pyfunc'
25 return 'pyfunc'
25
26
26
27
27 def ipyfunc2():
28 def ipyfunc2():
28 """Some pure python tests...
29 """Some pure python tests...
29
30
30 >>> 1+1
31 >>> 1+1
31 2
32 2
32 """
33 """
33 return 'pyfunc2'
34 return 'pyfunc2'
General Comments 0
You need to be logged in to leave comments. Login now