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