forked from has2k1/gnuplot_kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_kernel.py
More file actions
312 lines (247 loc) · 7.16 KB
/
test_kernel.py
File metadata and controls
312 lines (247 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import os
import weakref
from nose.tools import (assert_equal, assert_true, assert_false,
with_setup)
from metakernel.tests.utils import (get_kernel, get_log_text,
clear_log_text)
from gnuplot_kernel import GnuplotKernel
from gnuplot_kernel.magics import GnuplotMagic
from .utils import remove_files
# Note: Empty lines after indented triple quoted may
# lead to empty statements which could obscure the
# final output.
# All kernels are registered to ensure a
# thorough cleanup
_get_kernel = get_kernel
KERNELS = weakref.WeakSet()
def get_kernel(klass=None):
"""
Create & add to registry of live kernels
"""
if klass:
kernel = _get_kernel(klass)
else:
kernel = _get_kernel()
KERNELS.add(kernel)
return kernel
def teardown():
"""
Shutdown all live kernels
"""
while True:
try:
kernel = KERNELS.pop()
except KeyError:
break
kernel.do_shutdown(restart=False)
# Normal workflow tests #
def test_inline_magic():
kernel = get_kernel(GnuplotKernel)
# gnuplot line magic changes the plot settings
kernel.call_magic('%gnuplot pngcairo size 560, 420')
assert_equal(kernel.plot_settings['backend'],
'pngcairo')
assert_equal(kernel.plot_settings['format'],
'png')
assert_equal(kernel.plot_settings['termspec'],
'pngcairo size 560, 420')
def test_print():
kernel = get_kernel(GnuplotKernel)
code = "print cos(0)"
kernel.do_execute(code)
text = get_log_text(kernel)
assert_true('1.0' in text)
@with_setup(teardown=remove_files(('sine.png',
'sine-cosine.png')))
def test_file_plots():
kernel = get_kernel(GnuplotKernel)
kernel.call_magic('%gnuplot pngcairo size 560, 420')
# With a non-inline terminal plot gets created
code = """
set output 'sine.png'
plot sin(x)
"""
kernel.do_execute(code)
assert_true(os.path.exists('sine.png'))
clear_log_text(kernel)
# Multiple line statement
code = """
set output 'sine-cosine.png'
plot sin(x),\
cos(x)
"""
kernel.do_execute(code)
assert_true(os.path.exists('sine-cosine.png'))
def test_inline_plots():
kernel = get_kernel(GnuplotKernel)
kernel.call_magic('%gnuplot inline')
# inline plot creates data
code = """
plot sin(x)
"""
kernel.do_execute(code)
text = get_log_text(kernel)
assert_true('Display Data' in text)
clear_log_text(kernel)
# multiple plot statements data
code = """
plot sin(x)
plot cos(x)
"""
kernel.do_execute(code)
text = get_log_text(kernel)
assert_true(text.count('Display Data') == 2)
clear_log_text(kernel)
# svg
kernel.call_magic('%gnuplot inline svg')
code = """
plot tan(x)
"""
kernel.do_execute(code)
text = get_log_text(kernel)
assert_true('Display Data' in text)
clear_log_text(kernel)
def test_plot_abbreviations():
kernel = get_kernel(GnuplotKernel)
# Short names for the plot statements can be used
# to create inline plots
code = """
plot sin(x)
p cos(x)
rep
unset key
sp x**2+y**2, x**2-y**2
"""
kernel.do_execute(code)
text = get_log_text(kernel)
assert_true(text.count('Display Data') == 4)
def test_multiplot():
kernel = get_kernel(GnuplotKernel)
# multiplot
code = """
set multiplot layout 2,1
plot sin(x)
plot cos(x)
unset multiplot
"""
kernel.do_execute(code)
text = get_log_text(kernel)
assert_true(text.count('Display Data') == 1)
def test_help():
kernel = get_kernel(GnuplotKernel)
# The help commands should not get
# stuck in pagers.
# Fancy notebook help
code = 'terminal?'
kernel.do_execute(code)
text = get_log_text(kernel).lower()
assert_true('subtopic' in text)
clear_log_text(kernel)
# help by gnuplot statement
code = 'help print'
kernel.do_execute(code)
text = get_log_text(kernel).lower()
assert_true('syntax' in text)
clear_log_text(kernel)
def test_badinput():
kernel = get_kernel(GnuplotKernel)
# No code that endswith a backslash
code = 'plot sin(x),\\'
kernel.do_execute(code)
text = get_log_text(kernel)
assert_true('backslash' in text)
def test_gnuplot_error_message():
kernel = get_kernel(GnuplotKernel)
# The error messages gets to the kernel
code = 'plot [1,2][] sin(x)'
kernel.do_execute(code)
text = get_log_text(kernel)
assert_true(' ^' in text)
def test_bad_prompt():
kernel = get_kernel(GnuplotKernel)
# Anything other than 'gnuplot> '
# is a bad prompt
code = 'set multiplot'
kernel.do_execute(code)
text = get_log_text(kernel)
assert_true('warning' in text.lower())
# magics #
@with_setup(teardown=remove_files('cosine.png'))
def test_cell_magic():
# To simulate '%load_ext gnuplot_kernel';
# create a main kernel, a gnuplot kernel and
# a gnuplot magic that uses the gnuplot kernel.
# Then manually register the gnuplot magic into
# the main kernel.
kernel = get_kernel()
gkernel = GnuplotKernel()
gmagic = GnuplotMagic(gkernel)
gkernel.makeSubkernel(kernel)
kernel.line_magics['gnuplot'] = gmagic
kernel.cell_magics['gnuplot'] = gmagic
# inline output
code = """%%gnuplot
plot cos(x)
"""
kernel.do_execute(code)
assert_true('Display Data' in get_log_text(kernel))
clear_log_text(kernel)
# file output
kernel.call_magic('%gnuplot pngcairo size 560,420')
code = """%%gnuplot
set output 'cosine.png'
plot cos(x)
"""
kernel.do_execute(code)
assert_true(os.path.exists('cosine.png'))
clear_log_text(kernel)
@with_setup(teardown=remove_files('sine+cosine.png'))
def test_reset_cell_magic():
kernel = get_kernel(GnuplotKernel)
# Use reset statements that have testable effect
code = """%%reset
set output 'sine+cosine.png'
plot sin(x) + cos(x)
"""
kernel.call_magic(code)
assert_false(os.path.exists('sine+cosine.png'))
code = """
unset key
"""
kernel.do_execute(code)
assert_true(os.path.exists('sine+cosine.png'))
def test_reset_line_magic():
kernel = get_kernel(GnuplotKernel)
# Create a reset
code = """%%reset
set output 'sine+sine.png'
plot sin(x) + sin(x)
"""
kernel.call_magic(code)
# Remove the reset, execute some code and
# make sure there are no effects
kernel.call_magic('%reset')
code = """
unset key
"""
kernel.do_execute(code)
assert_false(os.path.exists('sine+sine.png'))
# Bad inline backend
# metakernel messes this exception!!
# with assert_raises(ValueError):
# kernel.call_magic('%gnuplot inline qt')
# utils tests #
def test_remove_files():
filename = './antigravity.txt'
func = remove_files(filename)
# Create file
# Verify it exists
# Remove it
# Verify it does not exist
# Remove it again, nothing happens
with open(filename, 'w'):
pass
assert_true(os.path.exists(filename))
func()
assert_false(os.path.exists(filename))
func()