OILS / opy / byterun / test_basic.py View on Github | oilshell.org

658 lines, 218 significant
1#!/usr/bin/env python2
2from __future__ import print_function
3"""Basic tests for Byterun."""
4
5import unittest
6
7import six
8
9import vmtest
10
11PY3, PY2 = six.PY3, not six.PY3
12
13
14class TestIt(vmtest.VmTestCase):
15 def test_constant(self):
16 self.assert_ok("17")
17
18 def test_globals(self):
19 self.assert_ok("""\
20 global xyz
21 xyz=2106
22
23 def abc():
24 global xyz
25 xyz+=1
26 print("Midst:",xyz)
27
28
29 print "Pre:",xyz
30 abc()
31 print "Post:",xyz
32 """)
33
34 def test_for_loop(self):
35 self.assert_ok("""\
36 out = ""
37 for i in range(5):
38 out = out + str(i)
39 print(out)
40 """)
41
42 def test_inplace_operators(self):
43 self.assert_ok("""\
44 x, y = 2, 3
45 x **= y
46 assert x == 8 and y == 3
47 x *= y
48 assert x == 24 and y == 3
49 x //= y
50 assert x == 8 and y == 3
51 x %= y
52 assert x == 2 and y == 3
53 x += y
54 assert x == 5 and y == 3
55 x -= y
56 assert x == 2 and y == 3
57 x <<= y
58 assert x == 16 and y == 3
59 x >>= y
60 assert x == 2 and y == 3
61
62 x = 0x8F
63 x &= 0xA5
64 assert x == 0x85
65 x |= 0x10
66 assert x == 0x95
67 x ^= 0x33
68 assert x == 0xA6
69 """)
70
71 if PY2:
72 def test_inplace_division(self):
73 self.assert_ok("""\
74 x, y = 24, 3
75 x /= y
76 assert x == 8 and y == 3
77 assert isinstance(x, int)
78 x /= y
79 assert x == 2 and y == 3
80 assert isinstance(x, int)
81 """)
82 elif PY3:
83 def test_inplace_division(self):
84 self.assert_ok("""\
85 x, y = 24, 3
86 x /= y
87 assert x == 8.0 and y == 3
88 assert isinstance(x, float)
89 x /= y
90 assert x == (8.0/3.0) and y == 3
91 assert isinstance(x, float)
92 """)
93
94 def test_slice(self):
95 self.assert_ok("""\
96 print("hello, world"[3:8])
97 """)
98 self.assert_ok("""\
99 print("hello, world"[:8])
100 """)
101 self.assert_ok("""\
102 print("hello, world"[3:])
103 """)
104 self.assert_ok("""\
105 print("hello, world"[:])
106 """)
107 self.assert_ok("""\
108 print("hello, world"[::-1])
109 """)
110 self.assert_ok("""\
111 print("hello, world"[3:8:2])
112 """)
113
114 def test_slice_assignment(self):
115 self.assert_ok("""\
116 l = list(range(10))
117 l[3:8] = ["x"]
118 print(l)
119 """)
120 self.assert_ok("""\
121 l = list(range(10))
122 l[:8] = ["x"]
123 print(l)
124 """)
125 self.assert_ok("""\
126 l = list(range(10))
127 l[3:] = ["x"]
128 print(l)
129 """)
130 self.assert_ok("""\
131 l = list(range(10))
132 l[:] = ["x"]
133 print(l)
134 """)
135
136 def test_slice_deletion(self):
137 self.assert_ok("""\
138 l = list(range(10))
139 del l[3:8]
140 print(l)
141 """)
142 self.assert_ok("""\
143 l = list(range(10))
144 del l[:8]
145 print(l)
146 """)
147 self.assert_ok("""\
148 l = list(range(10))
149 del l[3:]
150 print(l)
151 """)
152 self.assert_ok("""\
153 l = list(range(10))
154 del l[:]
155 print(l)
156 """)
157 self.assert_ok("""\
158 l = list(range(10))
159 del l[::2]
160 print(l)
161 """)
162
163 def test_building_stuff(self):
164 self.assert_ok("""\
165 print((1+1, 2+2, 3+3))
166 """)
167 self.assert_ok("""\
168 print([1+1, 2+2, 3+3])
169 """)
170 self.assert_ok("""\
171 print({1:1+1, 2:2+2, 3:3+3})
172 """)
173
174 def test_subscripting(self):
175 self.assert_ok("""\
176 l = list(range(10))
177 print("%s %s %s" % (l[0], l[3], l[9]))
178 """)
179 self.assert_ok("""\
180 l = list(range(10))
181 l[5] = 17
182 print(l)
183 """)
184 self.assert_ok("""\
185 l = list(range(10))
186 del l[5]
187 print(l)
188 """)
189
190 def test_generator_expression(self):
191 self.assert_ok("""\
192 x = "-".join(str(z) for z in range(5))
193 assert x == "0-1-2-3-4"
194 """)
195 # From test_regr.py
196 # This failed a different way than the previous join when genexps were
197 # broken:
198 self.assert_ok("""\
199 from textwrap import fill
200 x = set(['test_str'])
201 width = 70
202 indent = 4
203 blanks = ' ' * indent
204 res = fill(' '.join(str(elt) for elt in sorted(x)), width,
205 initial_indent=blanks, subsequent_indent=blanks)
206 print(res)
207 """)
208 def test_list_comprehension(self):
209 self.assert_ok("""\
210 x = [z*z for z in range(5)]
211 assert x == [0, 1, 4, 9, 16]
212 """)
213
214 def test_dict_comprehension(self):
215 self.assert_ok("""\
216 x = {z:z*z for z in range(5)}
217 assert x == {0:0, 1:1, 2:4, 3:9, 4:16}
218 """)
219
220 def test_set_comprehension(self):
221 self.assert_ok("""\
222 x = {z*z for z in range(5)}
223 assert x == {0, 1, 4, 9, 16}
224 """)
225
226 def test_strange_sequence_ops(self):
227 # from stdlib: test/test_augassign.py
228 self.assert_ok("""\
229 x = [1,2]
230 x += [3,4]
231 x *= 2
232
233 assert x == [1, 2, 3, 4, 1, 2, 3, 4]
234
235 x = [1, 2, 3]
236 y = x
237 x[1:2] *= 2
238 y[1:2] += [1]
239
240 assert x == [1, 2, 1, 2, 3]
241 assert x is y
242 """)
243
244 def test_unary_operators(self):
245 self.assert_ok("""\
246 x = 8
247 print(-x, ~x, not x)
248 """)
249
250 def test_attributes(self):
251 self.assert_ok("""\
252 l = lambda: 1 # Just to have an object...
253 l.foo = 17
254 print(hasattr(l, "foo"), l.foo)
255 del l.foo
256 print(hasattr(l, "foo"))
257 """)
258
259 def test_attribute_inplace_ops(self):
260 self.assert_ok("""\
261 l = lambda: 1 # Just to have an object...
262 l.foo = 17
263 l.foo -= 3
264 print(l.foo)
265 """)
266
267 def test_deleting_names(self):
268 self.assert_ok("""\
269 g = 17
270 assert g == 17
271 del g
272 g
273 """, raises=NameError)
274
275 def test_deleting_local_names(self):
276 self.assert_ok("""\
277 def f():
278 l = 23
279 assert l == 23
280 del l
281 l
282 f()
283 """, raises=NameError)
284
285 def test_import(self):
286 self.assert_ok("""\
287 import math
288 print(math.pi, math.e)
289 from math import sqrt
290 print(sqrt(2))
291 from math import *
292 print(sin(2))
293 """)
294
295 def test_classes(self):
296 self.assert_ok("""\
297 class Thing(object):
298 def __init__(self, x):
299 self.x = x
300 def meth(self, y):
301 return self.x * y
302 thing1 = Thing(2)
303 thing2 = Thing(3)
304 print(thing1.x, thing2.x)
305 print(thing1.meth(4), thing2.meth(5))
306 """)
307
308 def test_calling_methods_wrong(self):
309 self.assert_ok("""\
310 class Thing(object):
311 def __init__(self, x):
312 self.x = x
313 def meth(self, y):
314 return self.x * y
315 thing1 = Thing(2)
316 print(Thing.meth(14))
317 """, raises=TypeError)
318
319 def test_calling_subclass_methods(self):
320 self.assert_ok("""\
321 class Thing(object):
322 def foo(self):
323 return 17
324
325 class SubThing(Thing):
326 pass
327
328 st = SubThing()
329 print(st.foo())
330 """)
331
332 def test_subclass_attribute(self):
333 self.assert_ok("""\
334 class Thing(object):
335 def __init__(self):
336 self.foo = 17
337 class SubThing(Thing):
338 pass
339 st = SubThing()
340 print(st.foo)
341 """)
342
343 def test_subclass_attributes_not_shared(self):
344 self.assert_ok("""\
345 class Thing(object):
346 foo = 17
347 class SubThing(Thing):
348 foo = 25
349 st = SubThing()
350 t = Thing()
351 assert st.foo == 25
352 assert t.foo == 17
353 """)
354
355 def test_object_attrs_not_shared_with_class(self):
356 self.assert_ok("""\
357 class Thing(object):
358 pass
359 t = Thing()
360 t.foo = 1
361 Thing.foo""", raises=AttributeError)
362
363 def test_data_descriptors_precede_instance_attributes(self):
364 self.assert_ok("""\
365 class Foo(object):
366 pass
367 f = Foo()
368 f.des = 3
369 class Descr(object):
370 def __get__(self, obj, cls=None):
371 return 2
372 def __set__(self, obj, val):
373 raise NotImplementedError
374 Foo.des = Descr()
375 assert f.des == 2
376 """)
377
378 def test_instance_attrs_precede_non_data_descriptors(self):
379 self.assert_ok("""\
380 class Foo(object):
381 pass
382 f = Foo()
383 f.des = 3
384 class Descr(object):
385 def __get__(self, obj, cls=None):
386 return 2
387 Foo.des = Descr()
388 assert f.des == 3
389 """)
390
391 def test_subclass_attributes_dynamic(self):
392 self.assert_ok("""\
393 class Foo(object):
394 pass
395 class Bar(Foo):
396 pass
397 b = Bar()
398 Foo.baz = 3
399 assert b.baz == 3
400 """)
401
402 def test_attribute_access(self):
403 self.assert_ok("""\
404 class Thing(object):
405 z = 17
406 def __init__(self):
407 self.x = 23
408 t = Thing()
409 print(Thing.z)
410 print(t.z)
411 print(t.x)
412 """)
413
414 self.assert_ok("""\
415 class Thing(object):
416 z = 17
417 def __init__(self):
418 self.x = 23
419 t = Thing()
420 print(t.xyzzy)
421 """, raises=AttributeError)
422
423 def test_staticmethods(self):
424 self.assert_ok("""\
425 class Thing(object):
426 @staticmethod
427 def smeth(x):
428 print(x)
429 @classmethod
430 def cmeth(cls, x):
431 print(x)
432
433 Thing.smeth(1492)
434 Thing.cmeth(1776)
435 """)
436
437 def test_unbound_methods(self):
438 self.assert_ok("""\
439 class Thing(object):
440 def meth(self, x):
441 print(x)
442 m = Thing.meth
443 m(Thing(), 1815)
444 """)
445
446 def test_bound_methods(self):
447 self.assert_ok("""\
448 class Thing(object):
449 def meth(self, x):
450 print(x)
451 t = Thing()
452 m = t.meth
453 m(1815)
454 """)
455
456 def test_callback(self):
457 self.assert_ok("""\
458 def lcase(s):
459 return s.lower()
460 l = ["xyz", "ABC"]
461 l.sort(key=lcase)
462 print(l)
463 assert l == ["ABC", "xyz"]
464 """)
465
466 def test_unpacking(self):
467 self.assert_ok("""\
468 a, b, c = (1, 2, 3)
469 assert a == 1
470 assert b == 2
471 assert c == 3
472 """)
473
474 if PY2:
475 def test_exec_statement(self):
476 self.assert_ok("""\
477 g = {}
478 exec "a = 11" in g, g
479 assert g['a'] == 11
480 """)
481 elif PY3:
482 def test_exec_statement(self):
483 self.assert_ok("""\
484 g = {}
485 exec("a = 11", g, g)
486 assert g['a'] == 11
487 """)
488
489 def test_jump_if_true_or_pop(self):
490 self.assert_ok("""\
491 def f(a, b):
492 return a or b
493 assert f(17, 0) == 17
494 assert f(0, 23) == 23
495 assert f(0, "") == ""
496 """)
497
498 def test_jump_if_false_or_pop(self):
499 self.assert_ok("""\
500 def f(a, b):
501 return not(a and b)
502 assert f(17, 0) is True
503 assert f(0, 23) is True
504 assert f(0, "") is True
505 assert f(17, 23) is False
506 """)
507
508 def test_pop_jump_if_true(self):
509 self.assert_ok("""\
510 def f(a):
511 if not a:
512 return 'foo'
513 else:
514 return 'bar'
515 assert f(0) == 'foo'
516 assert f(1) == 'bar'
517 """)
518
519 def test_decorator(self):
520 self.assert_ok("""\
521 def verbose(func):
522 def _wrapper(*args, **kwargs):
523 return func(*args, **kwargs)
524 return _wrapper
525
526 @verbose
527 def add(x, y):
528 return x+y
529
530 add(7, 3)
531 """)
532
533 def test_multiple_classes(self):
534 # Making classes used to mix together all the class-scoped values
535 # across classes. This test would fail because A.__init__ would be
536 # over-written with B.__init__, and A(1, 2, 3) would complain about
537 # too many arguments.
538 self.assert_ok("""\
539 class A(object):
540 def __init__(self, a, b, c):
541 self.sum = a + b + c
542
543 class B(object):
544 def __init__(self, x):
545 self.x = x
546
547 a = A(1, 2, 3)
548 b = B(7)
549 print(a.sum)
550 print(b.x)
551 """)
552
553
554if PY2:
555 class TestPrinting(vmtest.VmTestCase):
556 def test_printing(self):
557 self.assert_ok("print 'hello'")
558 self.assert_ok("a = 3; print a+4")
559 self.assert_ok("""
560 print 'hi', 17, u'bye', 23,
561 print "", "\t", "the end"
562 """)
563
564 def test_printing_in_a_function(self):
565 self.assert_ok("""\
566 def fn():
567 print "hello"
568 fn()
569 print "bye"
570 """)
571
572 def test_printing_to_a_file(self):
573 self.assert_ok("""\
574 import sys
575 print >>sys.stdout, 'hello', 'there'
576 """)
577
578
579class TestLoops(vmtest.VmTestCase):
580 def test_for(self):
581 self.assert_ok("""\
582 for i in range(10):
583 print(i)
584 print("done")
585 """)
586
587 def test_break(self):
588 self.assert_ok("""\
589 for i in range(10):
590 print(i)
591 if i == 7:
592 break
593 print("done")
594 """)
595
596 def test_continue(self):
597 # fun fact: this doesn't use CONTINUE_LOOP
598 self.assert_ok("""\
599 for i in range(10):
600 if i % 3 == 0:
601 continue
602 print(i)
603 print("done")
604 """)
605
606 def test_continue_in_try_except(self):
607 self.assert_ok("""\
608 for i in range(10):
609 try:
610 if i % 3 == 0:
611 continue
612 print(i)
613 except ValueError:
614 pass
615 print("done")
616 """)
617
618 def test_continue_in_try_finally(self):
619 self.assert_ok("""\
620 for i in range(10):
621 try:
622 if i % 3 == 0:
623 continue
624 print(i)
625 finally:
626 print(".")
627 print("done")
628 """)
629
630
631class TestComparisons(vmtest.VmTestCase):
632 def test_in(self):
633 self.assert_ok("""\
634 assert "x" in "xyz"
635 assert "x" not in "abc"
636 assert "x" in ("x", "y", "z")
637 assert "x" not in ("a", "b", "c")
638 """)
639
640 def test_less(self):
641 self.assert_ok("""\
642 assert 1 < 3
643 assert 1 <= 2 and 1 <= 1
644 assert "a" < "b"
645 assert "a" <= "b" and "a" <= "a"
646 """)
647
648 def test_greater(self):
649 self.assert_ok("""\
650 assert 3 > 1
651 assert 3 >= 1 and 3 >= 3
652 assert "z" > "a"
653 assert "z" >= "a" and "z" >= "z"
654 """)
655
656
657if __name__ == '__main__':
658 unittest.main()