Skip to content

Commit 97abe22

Browse files
committed
tests: Add tests to exercise lexer; and some more complex number tests.
1 parent 9dd3640 commit 97abe22

File tree

6 files changed

+121
-2
lines changed

6 files changed

+121
-2
lines changed

tests/basics/bytes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
# literals
2+
print(b'123')
3+
print(br'123')
4+
print(rb'123')
5+
print(b'\u1234')
6+
7+
# construction
18
print(bytes())
9+
print(bytes(b'abc'))
210

311
a = b"123"
412
print(a)

tests/basics/lexer.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# test the lexer
2+
3+
# __debug__ is a special symbol
4+
print(type(__debug__))
5+
6+
# short input
7+
exec("")
8+
exec("\n")
9+
exec("\n\n")
10+
exec("\r")
11+
exec("\r\r")
12+
print(eval("1"))
13+
print(eval("12"))
14+
print(eval("123"))
15+
print(eval("1\n"))
16+
print(eval("12\n"))
17+
print(eval("123\n"))
18+
print(eval("1\r"))
19+
print(eval("12\r"))
20+
print(eval("123\r"))
21+
22+
# lots of indentation
23+
def a(x):
24+
if x:
25+
if x:
26+
if x:
27+
if x:
28+
if x:
29+
if x:
30+
if x:
31+
if x:
32+
if x:
33+
if x:
34+
if x:
35+
if x:
36+
if x:
37+
if x:
38+
if x:
39+
print(x)
40+
a(1)

tests/basics/string1.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
# basic strings
22

3+
# literals
4+
print('abc')
5+
print(r'abc')
6+
print(u'abc')
7+
print(repr('\a\b\t\n\v\f\r'))
8+
print('\z') # unrecognised escape char
9+
10+
# construction
311
print(str())
12+
print(str('abc'))
413

14+
# inplace addition
515
x = 'abc'
616
print(x)
7-
817
x += 'def'
918
print(x)
1019

20+
# binary ops
1121
print('123' + "456")
12-
1322
print('123' * 5)
1423

24+
# subscription
1525
print('abc'[1])
1626
print('abc'[-1])
1727
try:

tests/basics/syntaxerror.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,28 @@ def test_syntax(code):
44
try:
55
exec(code)
66
print("no SyntaxError")
7+
except IndentationError:
8+
print("IndentationError")
79
except SyntaxError:
810
print("SyntaxError")
911

12+
# non-newline after line-continuation character (lexer error)
13+
test_syntax("a \\a\n")
14+
15+
# dedent mismatch (lexer error)
16+
test_syntax("def f():\n a\n a\n")
17+
18+
# unclosed string (lexer error)
19+
test_syntax("'abc")
20+
21+
# invalid (lexer error)
22+
test_syntax("!")
23+
test_syntax("$")
24+
test_syntax("`")
25+
26+
# bad indentation (lexer error)
27+
test_syntax(" a\n")
28+
1029
# can't assign to literals
1130
test_syntax("1 = 2")
1231
test_syntax("'' = 1")

tests/float/complex1.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@
2525
print(1j * 2)
2626
print(1j * 2j)
2727
print(1j / 2)
28+
print((1j / 2j).real)
2829
print(1j / (1 + 2j))
30+
ans = 0j ** 0; print("%.5g %.5g" % (ans.real, ans.imag))
31+
ans = 0j ** 0j; print("%.5g %.5g" % (ans.real, ans.imag))
2932
ans = 1j ** 2.5; print("%.5g %.5g" % (ans.real, ans.imag))
3033
ans = 1j ** 2.5j; print("%.5g %.5g" % (ans.real, ans.imag))
3134

35+
# comparison
36+
print(1j == 1)
37+
print(1j == 1j)
38+
3239
# builtin abs
3340
print(abs(1j))
3441
print("%.5g" % abs(1j + 2))
@@ -44,3 +51,33 @@
4451
1j + []
4552
except TypeError:
4653
print("TypeError")
54+
55+
# unsupported unary op
56+
try:
57+
~(1j)
58+
except TypeError:
59+
print("TypeError")
60+
61+
# unsupported binary op
62+
try:
63+
1j // 2
64+
except TypeError:
65+
print("TypeError")
66+
67+
# unsupported binary op
68+
try:
69+
1j < 2j
70+
except TypeError:
71+
print("TypeError")
72+
73+
# zero division
74+
try:
75+
1j / 0
76+
except ZeroDivisionError:
77+
print("ZeroDivisionError")
78+
79+
# zero division via power
80+
try:
81+
0j ** 1j
82+
except ZeroDivisionError:
83+
print("ZeroDivisionError")

tests/float/float1.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# test basic float capabilities
22

3+
# literals
4+
print(.12)
5+
print(1.)
6+
print(1.2)
7+
38
# float construction
49
print(float(1.2))
510

0 commit comments

Comments
 (0)