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

`This is intended to improve coverage of the test suite.`
parent 44f65c0e
 ... ... @@ -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', ))) # bad typecode try: array.array('X') except ValueError: print("ValueError")
 ... ... @@ -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))))
 # 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) #del l[:-3] print(l) # slice assignment that extends the array b = bytearray(2) b[2:] = bytearray(10) print(b)
 # 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()
 # 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 print(0x1e+1) # can't convert list to int try: int([]) except TypeError: print("TypeError")
 # 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") else: 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:-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) ... ...
 # 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", TypeError) test_exc("1 = 1", TypeError) test_exc("del 1", 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)
 # 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**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**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")
 # 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)) # 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")
 ... ... @@ -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)
 ... ... @@ -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)) ... ...
 # 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)")
 SyntaxError SyntaxError
 # 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")
 SyntaxError SyntaxError
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!