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