tests: Add tests for SyntaxError, TypeError, and other missing things.

This is intended to improve coverage of the test suite.
pull/1164/merge
Damien George 2015-03-25 23:10:09 +00:00
rodzic 44f65c0e2f
commit 214179b430
24 zmienionych plików z 296 dodań i 6 usunięć

Wyświetl plik

@ -11,3 +11,12 @@ print(i[-1])
print(len(array.array('h')))
print(array.array('i'))
# bool operator acting on arrays
print(bool(array.array('i')))
print(bool(array.array('i', [1])))
# bad typecode
try:
array.array('X')
except ValueError:
print("ValueError")

Wyświetl plik

@ -14,3 +14,6 @@ print(array('i', bytearray(4)))
# convert from other arrays
print(array('H', array('b', [1, 2])))
print(array('b', array('I', [1, 2])))
# construct from something with unknown length
print(array('i', (i for i in range(10))))

Wyświetl plik

@ -0,0 +1,11 @@
# tests for bool objects
# basic logic
print(not False)
print(not True)
print(False and True)
print(False or True)
# unary operators
print(+True)
print(-True)

Wyświetl plik

@ -41,3 +41,8 @@ print(l)
l = bytearray(x)
#del l[:-3]
print(l)
# slice assignment that extends the array
b = bytearray(2)
b[2:] = bytearray(10)
print(b)

Wyświetl plik

@ -0,0 +1,9 @@
# test closure with lots of closed over variables
def f():
a, b, c, d, e, f, g, h = [i for i in range(8)]
def x():
print(a, b, c, d, e, f, g, h)
x()
f()

Wyświetl plik

@ -0,0 +1,5 @@
# test str of function
def f():
pass
print(str(f)[:8])

Wyświetl plik

@ -81,3 +81,9 @@ test('1 1', 16)
# check that we don't parse this as a floating point number
print(0x1e+1)
# can't convert list to int
try:
int([])
except TypeError:
print("TypeError")

Wyświetl plik

@ -0,0 +1,7 @@
# test basic int operations
# test conversion of bool on RHS of binary op
a = False
print(1 + a)
a = True
print(1 + a)

Wyświetl plik

@ -32,3 +32,6 @@ except ValueError:
print("Raised ValueError")
else:
print("Did not raise ValueError")
# 3rd argument to index greater than length of list
print([1, 2].index(1, 0, 4))

Wyświetl plik

@ -25,6 +25,10 @@ m = memoryview(b'1234')
print(list(m[1:]))
print(list(m[1:-1]))
# this tests get_buffer of memoryview
m = memoryview(bytearray(2))
print(bytearray(m))
import array
a = array.array('i', [1, 2, 3, 4])
m = memoryview(a)

Wyświetl plik

@ -0,0 +1,43 @@
# test errors from bad operations (unary, binary, etc)
def test_exc(code, exc):
try:
exec(code)
print("no exception")
except exc:
print("right exception")
except:
print("wrong exception")
# unsupported unary operators
test_exc("~None", TypeError)
test_exc("~[]", TypeError)
test_exc("~bytearray()", TypeError)
# unsupported binary operators
test_exc("False in True", TypeError)
test_exc("1 * {}", TypeError)
test_exc("1 in 1", TypeError)
test_exc("bytearray() // 2", TypeError)
test_exc("m = memoryview(bytearray())\nm += bytearray()", TypeError)
# object with buffer protocol needed on rhs
test_exc("bytearray(1) + 1", TypeError)
test_exc("(1 << 70) in 1", TypeError)
# unsupported subscription
test_exc("1[0]", TypeError)
test_exc("1[0] = 1", TypeError)
test_exc("del 1[0]", TypeError)
# not callable
test_exc("1()", TypeError)
# not an iterator
test_exc("next(1)", TypeError)
# must be an exception type
test_exc("raise 1", TypeError)
# no such name in import
test_exc("from sys import youcannotimportmebecauseidontexist", ImportError)

Wyświetl plik

@ -0,0 +1,89 @@
# test syntax errors
def test_syntax(code):
try:
exec(code)
print("no SyntaxError")
except SyntaxError:
print("SyntaxError")
# can't assign to literals
test_syntax("1 = 2")
test_syntax("'' = 1")
test_syntax("{} = 1")
# can't assign to comprehension
test_syntax("(i for i in a) = 1")
# can't assign to function
test_syntax("f() = 1")
# can't assign to power
test_syntax("f**2 = 1")
# can't assign to power of composite
test_syntax("f[0]**2 = 1")
# can't assign to empty tuple
test_syntax("() = 1")
# can't have multiple *x on LHS
test_syntax("*a, *b = c")
# can't do augmented assignment to tuple
test_syntax("a, b += c")
test_syntax("(a, b) += c")
# can't do augmented assignment to list
test_syntax("[a, b] += c")
# non-default argument can't follow default argument
test_syntax("def f(a=1, b): pass")
# can't delete these things
test_syntax("del ()")
test_syntax("del f()")
test_syntax("del f[0]**2")
test_syntax("del (a for a in a)")
# must be in a "loop"
test_syntax("break")
test_syntax("continue")
# must be in a function
test_syntax("return")
test_syntax("yield")
test_syntax("nonlocal a")
# errors on uPy but shouldn't
#test_syntax("global a; global a")
#test_syntax("def f():\n a = 1\n global a")
# default except must be last
test_syntax("try:\n a\nexcept:\n pass\nexcept:\n pass")
# can't have multiple * or **
test_syntax("f(*a, *b)")
test_syntax("f(**a, **b)")
# LHS of keywords must be id's
test_syntax("f(1=2)")
# non-keyword after keyword
test_syntax("f(a=1, 2)")
# doesn't error on uPy but should
#test_syntax("f(1, i for i in i)")
# all elements of dict/set must be pairs or singles
test_syntax("{1:2, 3}")
test_syntax("{1, 2:3}")
# can't mix non-bytes with bytes when concatenating
test_syntax("'abc' b'def'")
# can't reuse same name for argument
test_syntax("def f(a, a): pass")
# nonlocal must exist in outer function/class scope
test_syntax("def f():\n def g():\n nonlocal a")

Wyświetl plik

@ -0,0 +1,17 @@
# test bad exception match
try:
try:
a
except 1:
pass
except TypeError:
print("TypeError")
try:
try:
a
except (1,):
pass
except TypeError:
print("TypeError")

Wyświetl plik

@ -16,6 +16,8 @@ print(+(1j))
print(-(1 + 2j))
# binary ops
print(1j + False)
print(1j + True)
print(1j + 2)
print(1j + 2j)
print(1j - 2)
@ -30,3 +32,12 @@ ans = 1j ** 2.5j; print("%.5g %.5g" % (ans.real, ans.imag))
# builtin abs
print(abs(1j))
print("%.5g" % abs(1j + 2))
# convert bignum to complex on rhs
ans = 1j + (1 << 70); print("%.5g %.5g" % (ans.real, ans.imag))
# can't convert rhs to complex
try:
1j + []
except TypeError:
print("TypeError")

Wyświetl plik

@ -20,6 +20,12 @@ try:
except ZeroDivisionError:
print("ZeroDivisionError")
# can't convert list to float
try:
float([])
except TypeError:
print("TypeError")
# test constant float with more than 255 chars
x = 1.84728699436059052516398251149631771898472869943605905251639825114963177189847286994360590525163982511496317718984728699436059052516398251149631771898472869943605905251639825114963177189847286994360590525163982511496317718984728699436059052516398251149631771898472869943605905251639825114963177189
print("%.5f" % x)

Wyświetl plik

@ -2,6 +2,9 @@
i = 1 << 65
# convert bignum to float on rhs
print("%.5g" % (2.0 * i))
# this should convert to float
print("%.5g" % (i / 5))

Wyświetl plik

@ -1,6 +1,13 @@
# make sure syntax error works corrects for bad const definition
# make sure syntax error works correctly for bad const definition
try:
exec("a = const(x)")
except SyntaxError:
print("SyntaxError")
def test_syntax(code):
try:
exec(code)
except SyntaxError:
print("SyntaxError")
# argument not a constant
test_syntax("a = const(x)")
# redefined constant
test_syntax("A = const(1); A = const(2)")

Wyświetl plik

@ -1 +1,2 @@
SyntaxError
SyntaxError

Wyświetl plik

@ -0,0 +1,11 @@
# test syntax errors for uPy-specific decorators
def test_syntax(code):
try:
exec(code)
except SyntaxError:
print("SyntaxError")
# invalid micropython decorators
test_syntax("@micropython.a\ndef f(): pass")
test_syntax("@micropython.a.b\ndef f(): pass")

Wyświetl plik

@ -0,0 +1,2 @@
SyntaxError
SyntaxError

Wyświetl plik

@ -1,10 +1,31 @@
# tests for natively compiled functions
# basic test
@micropython.native
def native_test(x):
print(1, [], x)
native_test(2)
# check that GC doesn't collect the native function
import gc
gc.collect()
native_test(3)
# native with 2 args
@micropython.native
def f(a, b):
print(a + b)
f(1, 2)
# native with 3 args
@micropython.native
def f(a, b, c):
print(a + b + c)
f(1, 2, 3)
# check not operator
@micropython.native
def f(a):
print(not a)
f(False)
f(True)

Wyświetl plik

@ -1,2 +1,6 @@
1 [] 2
1 [] 3
3
6
True
False

Wyświetl plik

@ -0,0 +1,11 @@
# test syntax errors specific to viper code generation
def test_syntax(code):
try:
exec(code)
except SyntaxError:
print("SyntaxError")
# viper: annotations must be identifiers
test_syntax("@micropython.viper\ndef f(a:1): pass")
test_syntax("@micropython.viper\ndef f() -> 1: pass")

Wyświetl plik

@ -0,0 +1,2 @@
SyntaxError
SyntaxError