tests: Add tests to exercise lexer; and some more complex number tests.

This commit is contained in:
Damien George 2015-04-04 23:16:22 +01:00
parent 9dd3640464
commit 97abe22963
6 changed files with 122 additions and 3 deletions

View File

@ -1,4 +1,12 @@
# literals
print(b'123')
print(br'123')
print(rb'123')
print(b'\u1234')
# construction
print(bytes())
print(bytes(b'abc'))
a = b"123"
print(a)

40
tests/basics/lexer.py Normal file
View File

@ -0,0 +1,40 @@
# test the lexer
# __debug__ is a special symbol
print(type(__debug__))
# short input
exec("")
exec("\n")
exec("\n\n")
exec("\r")
exec("\r\r")
print(eval("1"))
print(eval("12"))
print(eval("123"))
print(eval("1\n"))
print(eval("12\n"))
print(eval("123\n"))
print(eval("1\r"))
print(eval("12\r"))
print(eval("123\r"))
# lots of indentation
def a(x):
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
print(x)
a(1)

View File

@ -1,17 +1,27 @@
# basic strings
print(str())
# literals
print('abc')
print(r'abc')
print(u'abc')
print(repr('\a\b\t\n\v\f\r'))
print('\z') # unrecognised escape char
# construction
print(str())
print(str('abc'))
# inplace addition
x = 'abc'
print(x)
x += 'def'
print(x)
# binary ops
print('123' + "456")
print('123' * 5)
# subscription
print('abc'[1])
print('abc'[-1])
try:

View File

@ -4,9 +4,28 @@ def test_syntax(code):
try:
exec(code)
print("no SyntaxError")
except IndentationError:
print("IndentationError")
except SyntaxError:
print("SyntaxError")
# non-newline after line-continuation character (lexer error)
test_syntax("a \\a\n")
# dedent mismatch (lexer error)
test_syntax("def f():\n a\n a\n")
# unclosed string (lexer error)
test_syntax("'abc")
# invalid (lexer error)
test_syntax("!")
test_syntax("$")
test_syntax("`")
# bad indentation (lexer error)
test_syntax(" a\n")
# can't assign to literals
test_syntax("1 = 2")
test_syntax("'' = 1")

View File

@ -25,10 +25,17 @@ print(1j - 2j)
print(1j * 2)
print(1j * 2j)
print(1j / 2)
print((1j / 2j).real)
print(1j / (1 + 2j))
ans = 0j ** 0; print("%.5g %.5g" % (ans.real, ans.imag))
ans = 0j ** 0j; print("%.5g %.5g" % (ans.real, ans.imag))
ans = 1j ** 2.5; print("%.5g %.5g" % (ans.real, ans.imag))
ans = 1j ** 2.5j; print("%.5g %.5g" % (ans.real, ans.imag))
# comparison
print(1j == 1)
print(1j == 1j)
# builtin abs
print(abs(1j))
print("%.5g" % abs(1j + 2))
@ -44,3 +51,33 @@ try:
1j + []
except TypeError:
print("TypeError")
# unsupported unary op
try:
~(1j)
except TypeError:
print("TypeError")
# unsupported binary op
try:
1j // 2
except TypeError:
print("TypeError")
# unsupported binary op
try:
1j < 2j
except TypeError:
print("TypeError")
# zero division
try:
1j / 0
except ZeroDivisionError:
print("ZeroDivisionError")
# zero division via power
try:
0j ** 1j
except ZeroDivisionError:
print("ZeroDivisionError")

View File

@ -1,5 +1,10 @@
# test basic float capabilities
# literals
print(.12)
print(1.)
print(1.2)
# float construction
print(float(1.2))