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
373 lines (300 loc) · 8.01 KB
/
test_kernel.py
File metadata and controls
373 lines (300 loc) · 8.01 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import os
import weakref
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 .conftest 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 kernel.plot_settings['backend'] == 'pngcairo'
assert kernel.plot_settings['format'] == 'png'
assert 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 '1.0' in text
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 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 os.path.exists('sine-cosine.png')
# Multiple line statement
code = """
set output 'tan.png'
plot tan(x)
set output 'tan2.png'
replot
"""
kernel.do_execute(code)
assert os.path.exists('tan.png')
assert os.path.exists('tan2.png')
remove_files('sine.png', 'sine-cosine.png')
remove_files('tan.png', 'tan2.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 '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 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 '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 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 text.count('Display Data') == 1
# With output
code = """
set terminal pncairo
set output 'multiplot-sin-cos.png'
set multiplot layout 2, 1
plot sin(x)
plot cos(x)
unset multiplot
unset output
"""
kernel.do_execute(code)
assert os.path.exists('multiplot-sin-cos.png')
remove_files('multiplot-sin-cos.png')
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 '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 '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 '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 ' ^' 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 'warning' in text.lower()
def test_data_block():
kernel = get_kernel(GnuplotKernel)
# Good data block
code = """
$DATA << EOD
# x y
1 1
2 2
3 3
4 4
EOD
plot $DATA
"""
kernel.do_execute(code)
text = get_log_text(kernel)
assert text.count('Display Data') == 1
clear_log_text(kernel)
# Badly terminated data block
bad_code = """
$DATA << EOD
# x y
1 1
2 2
3 3
4 4
EODX
plot $DATA
"""
kernel.do_execute(bad_code)
text = get_log_text(kernel)
assert 'Error' in text
clear_log_text(kernel)
# Good code should work after the bad_code
kernel.do_execute(code)
text = get_log_text(kernel)
assert text.count('Display Data') == 1
# magics #
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 '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 os.path.exists('cosine.png')
clear_log_text(kernel)
remove_files('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 not os.path.exists('sine+cosine.png')
code = """
unset key
"""
kernel.do_execute(code)
assert os.path.exists('sine+cosine.png')
remove_files('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 not os.path.exists('sine+sine.png')
# Bad inline backend
# metakernel messes this exception!!
# with assert_raises(ValueError):
# kernel.call_magic('%gnuplot inline qt')
# fixture tests #
def test_remove_files():
"""
This test create a file. Next test tests that it
is deleted
"""
filename = 'antigravit.txt'
# Create file
# make sure it exis
with open(filename, 'w'):
pass
assert os.path.exists(filename)
remove_files(filename)
assert not os.path.exists(filename)