Skip to content

Commit 9dd3640

Browse files
committed
tests: Add missing tests for builtins, and many other things.
1 parent 7e758b1 commit 9dd3640

37 files changed

Lines changed: 406 additions & 14 deletions

tests/basics/builtin_chr.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# test builtin chr (whether or not we support unicode)
2+
3+
print(chr(65))
4+
5+
try:
6+
chr(0x110000)
7+
except ValueError:
8+
print("ValueError")
9+

tests/basics/builtin_compile.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,41 @@ def have_compile():
77
except NameError:
88
return False
99

10-
# global variable for compiled code to access
11-
x = 1
12-
1310
def test():
11+
global x
12+
1413
c = compile("print(x)", "file", "exec")
1514

1615
try:
1716
exec(c)
1817
except NameError:
1918
print("NameError")
2019

20+
# global variable for compiled code to access
21+
x = 1
22+
2123
exec(c)
2224

2325
exec(c, {"x":2})
2426
exec(c, {}, {"x":3})
2527

28+
# single/eval mode
29+
exec(compile('print(1 + 1)', 'file', 'single'))
30+
print(eval(compile('1 + 1', 'file', 'eval')))
31+
32+
# bad mode
33+
try:
34+
compile('1', 'file', '')
35+
except ValueError:
36+
print("ValueError")
37+
38+
# exception within compiled code
39+
try:
40+
exec(compile('noexist', 'file', 'exec'))
41+
except NameError:
42+
print("NameError")
43+
print(x) # check 'x' still exists as a global
44+
2645
if have_compile():
2746
test()
2847
else:

tests/basics/builtin_dir.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# test builtin dir
2+
3+
# dir of locals
4+
print('__name__' in dir())
5+
6+
# dir of module
7+
import sys
8+
print('platform' in dir(sys))
9+

tests/basics/builtin_divmod.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# test builtin divmod
2+
3+
print(divmod(0, 2))
4+
print(divmod(3, 4))
5+
print(divmod(20, 3))
6+
7+
try:
8+
divmod(1, 0)
9+
except ZeroDivisionError:
10+
print("ZeroDivisionError")
11+
12+
try:
13+
divmod('a', 'b')
14+
except TypeError:
15+
print("TypeError")
16+

tests/basics/builtin_minmax.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@
2323
print(max(lst, key=lambda x:-x))
2424
print(max(1, 2, 3, 4, key=lambda x:-x))
2525
print(max(4, 3, 2, 1, key=lambda x:-x))
26+
27+
# need at least 1 item in the iterable
28+
try:
29+
min([])
30+
except ValueError:
31+
print("ValueError")

tests/basics/builtin_ord.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# test builtin ord (whether or not we support unicode)
2+
3+
print(ord('a'))
4+
5+
try:
6+
ord('')
7+
except TypeError:
8+
print("TypeError")
9+

tests/basics/builtin_pow.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# test builtin pow() with integral values
2+
3+
# 2 arg version
4+
print(pow(0, 1))
5+
print(pow(1, 0))
6+
print(pow(-2, 3))
7+
print(pow(3, 8))
8+
9+
# 3 arg version
10+
print(pow(3, 4, 7))
11+
Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# test builtin property
2+
3+
# create a property object explicitly
4+
property()
5+
property(1, 2, 3)
6+
7+
# use its accessor methods
8+
p = property()
9+
p.getter(1)
10+
p.setter(2)
11+
p.deleter(3)
12+
13+
# basic use as a decorator
114
class A:
215
def __init__(self, x):
316
self._x = x
@@ -15,6 +28,7 @@ def x(self):
1528
except AttributeError:
1629
print("AttributeError")
1730

31+
# explicit use within a class
1832
class B:
1933
def __init__(self, x):
2034
self._x = x
@@ -27,13 +41,18 @@ def xset(self, value):
2741
print("x set")
2842
self._x = value
2943

30-
x = property(xget, xset)
44+
def xdel(self):
45+
print("x del")
46+
47+
x = property(xget, xset, xdel)
3148

3249
b = B(3)
3350
print(b.x)
3451
b.x = 4
3552
print(b.x)
53+
del b.x
3654

55+
# full use as a decorator
3756
class C:
3857
def __init__(self, x):
3958
self._x = x
@@ -48,7 +67,12 @@ def x(self, value):
4867
print("x set")
4968
self._x = value
5069

70+
@x.deleter
71+
def x(self):
72+
print("x del")
73+
5174
c = C(5)
5275
print(c.x)
5376
c.x = 6
5477
print(c.x)
78+
del c.x

tests/basics/builtin_slice.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# test builtin slice
2+
3+
# print slice
4+
class A:
5+
def __getitem__(self, idx):
6+
print(idx)
7+
A()[1:2:3]

tests/basics/builtin_sorted.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# test builtin sorted
2+
3+
print(sorted(set(range(100))))
4+
print(sorted(set(range(100)), key=lambda x: x + 100*(x % 2)))
5+
6+
# need to use keyword argument
7+
try:
8+
sorted([], None)
9+
except TypeError:
10+
print("TypeError")

0 commit comments

Comments
 (0)