tests: Add tests for SyntaxError, TypeError, and other missing things.
This is intended to improve coverage of the test suite.
This commit is contained in:
parent
44f65c0e2f
commit
214179b430
|
@ -11,3 +11,12 @@ print(i[-1])
|
||||||
print(len(array.array('h')))
|
print(len(array.array('h')))
|
||||||
print(array.array('i'))
|
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")
|
||||||
|
|
|
@ -14,3 +14,6 @@ print(array('i', bytearray(4)))
|
||||||
# convert from other arrays
|
# convert from other arrays
|
||||||
print(array('H', array('b', [1, 2])))
|
print(array('H', array('b', [1, 2])))
|
||||||
print(array('b', array('I', [1, 2])))
|
print(array('b', array('I', [1, 2])))
|
||||||
|
|
||||||
|
# construct from something with unknown length
|
||||||
|
print(array('i', (i for i in range(10))))
|
||||||
|
|
|
@ -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)
|
|
@ -41,3 +41,8 @@ print(l)
|
||||||
l = bytearray(x)
|
l = bytearray(x)
|
||||||
#del l[:-3]
|
#del l[:-3]
|
||||||
print(l)
|
print(l)
|
||||||
|
|
||||||
|
# slice assignment that extends the array
|
||||||
|
b = bytearray(2)
|
||||||
|
b[2:] = bytearray(10)
|
||||||
|
print(b)
|
||||||
|
|
|
@ -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()
|
|
@ -0,0 +1,5 @@
|
||||||
|
# test str of function
|
||||||
|
|
||||||
|
def f():
|
||||||
|
pass
|
||||||
|
print(str(f)[:8])
|
|
@ -81,3 +81,9 @@ test('1 1', 16)
|
||||||
|
|
||||||
# check that we don't parse this as a floating point number
|
# check that we don't parse this as a floating point number
|
||||||
print(0x1e+1)
|
print(0x1e+1)
|
||||||
|
|
||||||
|
# can't convert list to int
|
||||||
|
try:
|
||||||
|
int([])
|
||||||
|
except TypeError:
|
||||||
|
print("TypeError")
|
||||||
|
|
|
@ -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)
|
|
@ -32,3 +32,6 @@ except ValueError:
|
||||||
print("Raised ValueError")
|
print("Raised ValueError")
|
||||||
else:
|
else:
|
||||||
print("Did not raise ValueError")
|
print("Did not raise ValueError")
|
||||||
|
|
||||||
|
# 3rd argument to index greater than length of list
|
||||||
|
print([1, 2].index(1, 0, 4))
|
||||||
|
|
|
@ -25,6 +25,10 @@ m = memoryview(b'1234')
|
||||||
print(list(m[1:]))
|
print(list(m[1:]))
|
||||||
print(list(m[1:-1]))
|
print(list(m[1:-1]))
|
||||||
|
|
||||||
|
# this tests get_buffer of memoryview
|
||||||
|
m = memoryview(bytearray(2))
|
||||||
|
print(bytearray(m))
|
||||||
|
|
||||||
import array
|
import array
|
||||||
a = array.array('i', [1, 2, 3, 4])
|
a = array.array('i', [1, 2, 3, 4])
|
||||||
m = memoryview(a)
|
m = memoryview(a)
|
||||||
|
|
|
@ -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)
|
|
@ -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")
|
|
@ -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")
|
|
@ -16,6 +16,8 @@ print(+(1j))
|
||||||
print(-(1 + 2j))
|
print(-(1 + 2j))
|
||||||
|
|
||||||
# binary ops
|
# binary ops
|
||||||
|
print(1j + False)
|
||||||
|
print(1j + True)
|
||||||
print(1j + 2)
|
print(1j + 2)
|
||||||
print(1j + 2j)
|
print(1j + 2j)
|
||||||
print(1j - 2)
|
print(1j - 2)
|
||||||
|
@ -30,3 +32,12 @@ ans = 1j ** 2.5j; print("%.5g %.5g" % (ans.real, ans.imag))
|
||||||
# builtin abs
|
# builtin abs
|
||||||
print(abs(1j))
|
print(abs(1j))
|
||||||
print("%.5g" % abs(1j + 2))
|
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")
|
||||||
|
|
|
@ -20,6 +20,12 @@ try:
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
print("ZeroDivisionError")
|
print("ZeroDivisionError")
|
||||||
|
|
||||||
|
# can't convert list to float
|
||||||
|
try:
|
||||||
|
float([])
|
||||||
|
except TypeError:
|
||||||
|
print("TypeError")
|
||||||
|
|
||||||
# test constant float with more than 255 chars
|
# test constant float with more than 255 chars
|
||||||
x = 1.84728699436059052516398251149631771898472869943605905251639825114963177189847286994360590525163982511496317718984728699436059052516398251149631771898472869943605905251639825114963177189847286994360590525163982511496317718984728699436059052516398251149631771898472869943605905251639825114963177189
|
x = 1.84728699436059052516398251149631771898472869943605905251639825114963177189847286994360590525163982511496317718984728699436059052516398251149631771898472869943605905251639825114963177189847286994360590525163982511496317718984728699436059052516398251149631771898472869943605905251639825114963177189
|
||||||
print("%.5f" % x)
|
print("%.5f" % x)
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
|
|
||||||
i = 1 << 65
|
i = 1 << 65
|
||||||
|
|
||||||
|
# convert bignum to float on rhs
|
||||||
|
print("%.5g" % (2.0 * i))
|
||||||
|
|
||||||
# this should convert to float
|
# this should convert to float
|
||||||
print("%.5g" % (i / 5))
|
print("%.5g" % (i / 5))
|
||||||
|
|
||||||
|
|
|
@ -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:
|
def test_syntax(code):
|
||||||
exec("a = const(x)")
|
try:
|
||||||
except SyntaxError:
|
exec(code)
|
||||||
print("SyntaxError")
|
except SyntaxError:
|
||||||
|
print("SyntaxError")
|
||||||
|
|
||||||
|
# argument not a constant
|
||||||
|
test_syntax("a = const(x)")
|
||||||
|
|
||||||
|
# redefined constant
|
||||||
|
test_syntax("A = const(1); A = const(2)")
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
SyntaxError
|
SyntaxError
|
||||||
|
SyntaxError
|
||||||
|
|
|
@ -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")
|
|
@ -0,0 +1,2 @@
|
||||||
|
SyntaxError
|
||||||
|
SyntaxError
|
|
@ -1,10 +1,31 @@
|
||||||
|
# tests for natively compiled functions
|
||||||
|
|
||||||
|
# basic test
|
||||||
@micropython.native
|
@micropython.native
|
||||||
def native_test(x):
|
def native_test(x):
|
||||||
print(1, [], x)
|
print(1, [], x)
|
||||||
|
|
||||||
native_test(2)
|
native_test(2)
|
||||||
|
|
||||||
# check that GC doesn't collect the native function
|
# check that GC doesn't collect the native function
|
||||||
import gc
|
import gc
|
||||||
gc.collect()
|
gc.collect()
|
||||||
native_test(3)
|
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)
|
||||||
|
|
|
@ -1,2 +1,6 @@
|
||||||
1 [] 2
|
1 [] 2
|
||||||
1 [] 3
|
1 [] 3
|
||||||
|
3
|
||||||
|
6
|
||||||
|
True
|
||||||
|
False
|
||||||
|
|
|
@ -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")
|
|
@ -0,0 +1,2 @@
|
||||||
|
SyntaxError
|
||||||
|
SyntaxError
|
Loading…
Reference in New Issue