forked from ikotler/pythonect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_eval.py
More file actions
508 lines (285 loc) · 16.9 KB
/
test_eval.py
File metadata and controls
508 lines (285 loc) · 16.9 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# Copyright (c) 2012-2013, Itzik Kotler
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# * Neither the name of the author nor the names of its contributors may
# be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
try:
# If < Python 2.7, use the backported unittest2 package
import unittest2 as unittest
except ImportError:
# Probably > Python 2.7, use unittest
import unittest
import os
import sys
# Local imports
import pythonect
def _installed_module(name):
try:
__import__(name)
return True
except ImportError:
return False
class TestPythonect(unittest.TestCase):
def setUp(self):
self.input = None
def test_literal_hex(self):
self.assertEqual(pythonect.eval('0x01', {}, {}), 1)
def test_sub_expr_literal_hex(self):
self.assertEqual(pythonect.eval('`0x01`', {}, {}), 1)
def test_literal_bin(self):
self.assertEqual(pythonect.eval('0b1', {}, {}), 1)
def test_literal_float(self):
self.assertEqual(pythonect.eval('1.0', {}, {}), 1)
def test_literal_int(self):
self.assertEqual(pythonect.eval('1', {}, {}), 1)
def test_python_stmt_import(self):
self.assertEqual(pythonect.eval('import math', {}, {}), self.input)
def test_sub_expr_python_stmt_import(self):
self.assertEqual(pythonect.eval('`import math`', {}, {}), self.input)
def test_python_stmt_assignment(self):
self.assertEqual(pythonect.eval('x = 1', {}, {}), self.input)
def test_sub_expr_python_stmt_assignment(self):
self.assertEqual(pythonect.eval('`x = 1`', {}, {}), self.input)
def test_python_expr_int(self):
self.assertEqual(pythonect.eval('1 + 1', {}, {}), 2)
def test_sub_expr_python_expr_int(self):
self.assertEqual(pythonect.eval('`1 + 1`', {}, {}), 2)
def test_python_expr_str_1(self):
self.assertEqual(pythonect.eval('"Hello World"', {}, {}), "Hello World")
def test_sub_expr_python_expr_str_1(self):
self.assertEqual(pythonect.eval('`"Hello World"`', {}, {}), "Hello World")
def test_python_expr_str_2(self):
self.assertEqual(pythonect.eval("'Hello World'", {}, {}), "Hello World")
def test_python_expr_str_3(self):
self.assertEqual(pythonect.eval('"Hello \'W\'orld"', {}, {}), "Hello 'W'orld")
def test_python_expr_str_4(self):
self.assertEqual(pythonect.eval("'Hello \"W\"orld'", {}, {}), 'Hello "W"orld')
def test_python_expr_list(self):
self.assertEqual(pythonect.eval('[[1, 2, 3]]', {}, {}), [1, 2, 3])
def test_sub_expr_python_expr_list(self):
self.assertEqual(pythonect.eval('`[[1, 2, 3]]`', {}, {}), [1, 2, 3])
def test_python_true_expr_literal_eq_literal(self):
self.assertEqual(pythonect.eval('1 == 1', {}, {}), self.input)
def test_sub_expr_python_true_expr_literal_eq_literal(self):
self.assertEqual(pythonect.eval('`1 == 1`', {}, {}), self.input)
def test_python_false_expr_literal_neq_literal(self):
self.assertEqual(pythonect.eval('1 == 0', {}, {}), False)
def test_python_true_expr_underscore_eq_underscore(self):
self.assertEqual(pythonect.eval('_ == _', {}, {}), self.input)
def test_python_false_expr_underscore_neq_repr_underscore(self):
self.assertEqual(pythonect.eval('_ == repr(_)', {}, {}), False)
def test_literal_int_async_none(self):
self.assertEqual(pythonect.eval('1 -> None', {}, {}), 1)
def test_literal_int_sync_none(self):
self.assertEqual(pythonect.eval('1 | None', {}, {}), 1)
def test_literal_int_async_dict(self):
self.assertEqual(pythonect.eval('1 -> {1: "One", 2: "Two"}', {}, {}), "One")
def test_literal_int_sync_dict(self):
self.assertEqual(pythonect.eval('1 | {1: "One", 2: "Two"}', {}, {}), "One")
def test_literal_str_async_autoload(self):
self.assertEqual(pythonect.eval('"Hello world" -> string.split', {}, {}), ["Hello", "world"])
def test_literal_str_sync_autoload(self):
self.assertItemsEqual(pythonect.eval('"Hello world" | string.split', {}, {}), ["Hello", "world"])
def test_literal_int_async_literal_int(self):
self.assertEqual(pythonect.eval('1 -> 1', {}, {}), 1)
def test_literal_int_sync_literal_int(self):
self.assertEqual(pythonect.eval('1 | 1', {}, {}), 1)
def test_literal_int_async_literal_array_int_float(self):
self.assertItemsEqual(pythonect.eval('1 -> [int,float]', {}, {}), [1, 1.0])
def test_literal_int_sync_literal_array_int_float(self):
self.assertEqual(pythonect.eval('1 | [int,float]', {}, {}), [1, 1.0])
def test_python_stmt_assignment_async_literal_int(self):
self.assertEqual(pythonect.eval('[x = 0] -> 1', {}, {}), 1)
def test_python_stmt_assignment_sync_literal_int(self):
self.assertEqual(pythonect.eval('[x = 0] | 1', {}, {}), 1)
def test_python_stmt_assignment_sync_variable(self):
self.assertEqual(pythonect.eval('[x = 0] | x', {}, {}), 0)
def test_python_stmt_assignment_async_variable(self):
self.assertEqual(pythonect.eval('[x = 0] -> x', {}, {}), 0)
def test_literal_array_int_int_sync_none(self):
self.assertEqual(pythonect.eval('[1, 2] | None', {}, {}), [1, 2])
def test_literal_array_int_int_async_none(self):
self.assertItemsEqual(pythonect.eval('[1, 2] -> None', {}, {}), [1, 2])
def test_literal_array_int_int_sync_dict(self):
self.assertEqual(pythonect.eval('[1, 2] | {1: "One", 2: "Two"}', {}, {}), ["One", "Two"])
def test_literal_array_int_int_async_dict(self):
self.assertItemsEqual(pythonect.eval('[1, 2] -> {1: "One", 2: "Two"}', {}, {}), ["One", "Two"])
def test_literal_array_int_str_async_none(self):
self.assertItemsEqual(pythonect.eval('[1, "Hello"] -> None', {}, {}), [1, "Hello"])
def test_literal_array_int_str_sync_none(self):
self.assertEqual(pythonect.eval('[1, "Hello"] | None', {}, {}), [1, "Hello"])
def test_literal_array_int_str_async_int(self):
self.assertItemsEqual(pythonect.eval('[1, "Hello"] -> 1', {}, {}), [1, 1])
def test_literal_array_int_str_sync_int(self):
self.assertEqual(pythonect.eval('[1, "Hello"] | 1', {}, {}), [1, 1])
def test_literal_array_int_int_sync_literal_int(self):
self.assertEqual(pythonect.eval('[1, 2] | 1', {}, {}), [1, 1])
def test_literal_array_int_int_async_literal_int(self):
self.assertEqual(pythonect.eval('[1, 2] -> 1', {}, {}), [1, 1])
def test_literal_array_int_int_sync_literal_array_int_int(self):
self.assertEqual(pythonect.eval('[1, 2] | [3, 4]', {}, {}), [3, 4, 3, 4])
def test_literal_array_int_int_async_literal_array_int_int(self):
self.assertItemsEqual(pythonect.eval('[1, 2] -> [3, 4]', {}, {}), [3, 3, 4, 4])
def test_literal_int_async_stmt_single_return_value_function_async_single_return_value_function(self):
self.assertEqual(pythonect.eval('1 -> def foobar(x): return x+1 -> foobar', {}, {}), 2)
def test_literal_int_async_stmt_single_return_value_function_sync_single_return_value_function(self):
self.assertEqual(pythonect.eval('1 -> def foobar(x): return x+1 | foobar', {}, {}), 2)
def test_literal_int_sync_stmt_single_return_value_function_async_single_return_value_function(self):
self.assertEqual(pythonect.eval('1 | def foobar(x): return x+1 -> foobar', {}, {}), 2)
def test_literal_int_sync_stmt_single_return_value_function_sync_single_return_value_function(self):
self.assertEqual(pythonect.eval('1 | def foobar(x): return x+1 | foobar', {}, {}), 2)
def test_literal_int_async_stmt_multiple_return_value_function_async_multiple_return_value_function(self):
self.assertItemsEqual(pythonect.eval('1 -> def foobar(x): return [x,x+1] -> foobar', {}, {}), [1, 2])
def test_literal_int_async_stmt_multiple_return_value_function_sync_multiple_return_value_function(self):
self.assertEqual(pythonect.eval('1 -> def foobar(x): return [x,x+1] | foobar', {}, {}), [1, 2])
def test_literal_int_sync_stmt_multiple_return_value_function_async_multiple_return_value_function(self):
self.assertEqual(pythonect.eval('1 | def foobar(x): return [x,x+1] -> foobar', {}, {}), [1, 2])
def test_literal_int_sync_stmt_multiple_return_value_function_sync_multiple_return_value_function(self):
self.assertEqual(pythonect.eval('1 | def foobar(x): return [x,x+1] | foobar', {}, {}), [1, 2])
def test_literal_int_async_stmt_generator_return_value_function_async_generator_return_value_function(self):
self.assertItemsEqual(pythonect.eval('1 -> def foobar(x): yield x; yield x+1 -> foobar', {}, {}), [1, 2])
def test_literal_int_async_stmt_generator_return_value_function_sync_generator_return_value_function(self):
self.assertEqual(pythonect.eval('1 -> def foobar(x): yield x; yield x+1 | foobar', {}, {}), [1, 2])
def test_literal_int_sync_stmt_generator_return_value_function_async_generator_return_value_function(self):
self.assertEqual(pythonect.eval('1 | def foobar(x): yield x; yield x+1 -> foobar', {}, {}), [1, 2])
def test_literal_int_sync_stmt_generator_return_value_function_sync_generator_return_value_function(self):
self.assertEqual(pythonect.eval('1 | def foobar(x): yield x; yield x+1 | foobar', {}, {}), [1, 2])
def test_singlethread_program_async(self):
self.assertEqual(pythonect.eval('import threading -> x = threading.current_thread().name -> y = threading.current_thread().name -> x == y', {}, {}), None)
def test_singlethread_program_sync(self):
self.assertEqual(pythonect.eval('import threading | x = threading.current_thread().name | y = threading.current_thread().name | x == y', {}, {}), None)
def test_multithread_program_async(self):
r_array = pythonect.eval('import threading -> [threading.current_thread().name, threading.current_thread().name]', {}, {})
self.assertEqual(r_array[0] != r_array[1], True)
@unittest.skipIf(not _installed_module('multiprocessing'), 'Current Python implementation does not support multiprocessing')
def test_multithread_program_sync(self):
r_array = pythonect.eval('import threading | [threading.current_thread().name, threading.current_thread().name]', {}, {})
self.assertEqual(r_array[0] != r_array[1], True)
# @unittest.skipIf(not _installed_module('multiprocessing'), 'Current Python implementation does not support multiprocessing')
# def test_multiprocess_program_async(self):
#
# try:
#
# self.assertEqual(pythonect.eval('import multiprocessing -> start_pid = multiprocessing.current_process().pid -> start_pid -> str & -> current_pid = multiprocessing.current_process().pid -> 1 -> current_pid != start_pid', {}, {}), 1)
#
# except OSError as e:
#
# return 1
# @unittest.skipIf(not _installed_module('multiprocessing'), 'Current Python implementation does not support multiprocessing')
# def test_multiprocess_program_sync(self):
#
# try:
#
# self.assertEqual(pythonect.eval('import multiprocessing | start_pid = multiprocessing.current_process().pid | start_pid | str & | current_pid = multiprocessing.current_process().pid | 1 | current_pid != start_pid', {}, {}), 1)
#
# except OSError as e:
#
# return 1
def test_pseudo_none_const_as_url(self):
self.assertEqual(pythonect.eval('def foobar(x): return x+1 -> 1 -> foobar@None', {}, {}), 2)
def test_pseudo_none_str_as_url(self):
self.assertEqual(pythonect.eval('def foobar(x): return x+1 -> 1 -> foobar@"None"', {}, {}), 2)
def test_pseudo_none_value_fcn_return_value_as_url(self):
self.assertEqual(pythonect.eval('def ret_none(): return None -> def foobar(x): return x+1 -> 1 -> foobar@ret_none()', {}, {}), 2)
def test_pseudo_none_str_fcn_return_value_as_url(self):
self.assertEqual(pythonect.eval('def ret_none(): return "None" -> def foobar(x): return x+1 -> 1 -> foobar@ret_none()', {}, {}), 2)
def test_pythonect_eval_fcn(self):
self.assertEqual(pythonect.eval("eval('1->1', {}, {})", {}, {}), 1)
def test_python_eval_within_pythonect_program(self):
self.assertEqual(pythonect.eval("__eval__('1')", {}, {}), 1)
def test_void_function(self):
self.assertEqual(pythonect.eval("def void_foobar(): return 2 -> 1 -> void_foobar", {}, {}), 2)
############################################################
# Ticket numbers in this file can be looked up by visiting #
# http://github.com/ikotler/pythonect/issues/<number> #
############################################################
# Bug #11
def test_autloader_within_array(self):
self.assertItemsEqual(pythonect.eval('"Hello world" | [string.split]', {}, {}), ["Hello", "world"])
# Bug #14
def test_print_like_statement(self):
self.assertItemsEqual(pythonect.eval('range(1,10) -> print("Thread A")', {}, {}), [1, 2, 3, 4, 5, 6, 7, 8, 9])
def test_multiple_stateful_x_eq_5_statement(self):
locals_ = {}
globals_ = {}
pythonect.eval('xrange(1, 10) -> x = _', globals_, locals_)
self.assertEqual('x' not in locals_ and 'x' not in globals_, True)
# i.e.
# >>> xrange(x, 10) -> x = _
# >>> x
# NameError: name 'x' is not defined
def test_stateful_x_eq_5_statement(self):
locals_ = {}
globals_ = {}
pythonect.eval('x = 5', globals_, locals_)
self.assertEqual(pythonect.eval('1 -> [x == 5]', globals_, locals_), 1)
# i.e.
# >>> x = 5
# >>> 1 -> [x == 5]
# 1
# Bug #16
def test_typeerror_exception_not_due_to_eval(self):
import sys
import os
orig_stderr = sys.stderr
sys.stderr = open(os.devnull, 'w')
self.assertEqual(pythonect.eval('1 -> socket.socket(socket.AF_INET, socket.SOCK_STREAM) -> _.connect("A","B")', {'sys': sys}, {'sys': sys}), False)
sys.stderr = orig_stderr
# Bug #21
def test_list_with_str_with_comma(self):
self.assertEqual(pythonect.eval('["Hello, world"]', {}, {}), 'Hello, world')
# Bug #27
# @unittest.skipIf(not _installed_module('multiprocessing'), 'Current Python implementation does not support multiprocessing')
# def test_multi_processing_and_multi_threading(self):
#
# try:
#
# self.assertEqual(pythonect.eval('"Hello, world" -> [print, print &]', {}, {}), ['Hello, world', 'Hello, world'])
#
# except OSError as e:
#
# # i.e. OSError: [Errno 13] Permission denied
#
# return 1
# Bug #30
def test_non_string_literals_in_list(self):
self.assertEqual(pythonect.eval('[1,2,3] -> _ + 1', {}, {}), [2, 3, 4])
# Feature #35
def test_eval_with_expressions_list_as_input(self):
expressions = pythonect.parse('"Hello, world" -> 1 | 2')
self.assertEqual(pythonect.eval(expressions, {}, {}), 2)
# Enhancement #45
def test_literal_dict_as_input(self):
self.assertEqual(pythonect.eval('{"foobar": "foobar"}', {}, {}), {"foobar": "foobar"})
def test_dict_as_return_value_as_input(self):
self.assertEqual(pythonect.eval("def foobar(): return {'foobar': 'foobar'} -> foobar() -> print", {}, {}), {"foobar": "foobar"})
# Bug #48
def test_print_B_in_ABC(self):
self.assertEqual(pythonect.eval('1 -> print "B" in "ABC"', {}, {}), 1)
def test_print_2_is_2(self):
self.assertEqual(pythonect.eval('1 -> print 2 is 2', {}, {}), 1)
# Bug #69
def test_alt_print__fcn(self):
self.assertEqual(pythonect.eval('1 -> print', {}, {'print_': lambda x: 123}), 123)