Browse Source

tests/micropython: Add tests for try and with blocks under native/viper.

pull/4055/head
Damien George 6 years ago
parent
commit
f774614110
  1. 39
      tests/micropython/native_try.py
  2. 7
      tests/micropython/native_try.py.exp
  3. 34
      tests/micropython/native_try_deep.py
  4. 9
      tests/micropython/native_try_deep.py.exp
  5. 28
      tests/micropython/native_with.py
  6. 9
      tests/micropython/native_with.py.exp
  7. 40
      tests/micropython/viper_try.py
  8. 7
      tests/micropython/viper_try.py.exp
  9. 28
      tests/micropython/viper_with.py
  10. 9
      tests/micropython/viper_with.py.exp

39
tests/micropython/native_try.py

@ -0,0 +1,39 @@
# test native try handling
# basic try-finally
@micropython.native
def f():
try:
fail
finally:
print('finally')
try:
f()
except NameError:
print('NameError')
# nested try-except with try-finally
@micropython.native
def f():
try:
try:
fail
finally:
print('finally')
except NameError:
print('NameError')
f()
# check that locals written to in try blocks keep their values
@micropython.native
def f():
a = 100
try:
print(a)
a = 200
fail
except NameError:
print(a)
a = 300
print(a)
f()

7
tests/micropython/native_try.py.exp

@ -0,0 +1,7 @@
finally
NameError
finally
NameError
100
200
300

34
tests/micropython/native_try_deep.py

@ -0,0 +1,34 @@
# test native try handling
# deeply nested try (9 deep)
@micropython.native
def f():
try:
try:
try:
try:
try:
try:
try:
try:
try:
raise ValueError
finally:
print(8)
finally:
print(7)
finally:
print(6)
finally:
print(5)
finally:
print(4)
finally:
print(3)
finally:
print(2)
finally:
print(1)
except ValueError:
print('ValueError')
f()

9
tests/micropython/native_try_deep.py.exp

@ -0,0 +1,9 @@
8
7
6
5
4
3
2
1
ValueError

28
tests/micropython/native_with.py

@ -0,0 +1,28 @@
# test with handling within a native function
class C:
def __init__(self):
print('__init__')
def __enter__(self):
print('__enter__')
def __exit__(self, a, b, c):
print('__exit__', a, b, c)
# basic with
@micropython.native
def f():
with C():
print(1)
f()
# nested with and try-except
@micropython.native
def f():
try:
with C():
print(1)
fail
print(2)
except NameError:
print('NameError')
f()

9
tests/micropython/native_with.py.exp

@ -0,0 +1,9 @@
__init__
__enter__
1
__exit__ None None None
__init__
__enter__
1
__exit__ <class 'NameError'> name 'fail' is not defined None
NameError

40
tests/micropython/viper_try.py

@ -0,0 +1,40 @@
# test try handling within a viper function
# basic try-finally
@micropython.viper
def f():
try:
fail
finally:
print('finally')
try:
f()
except NameError:
print('NameError')
# nested try-except with try-finally
@micropython.viper
def f():
try:
try:
fail
finally:
print('finally')
except NameError:
print('NameError')
f()
# check that locals written to in try blocks keep their values
@micropython.viper
def f():
a = 100
try:
print(a)
a = 200
fail
except NameError:
print(a)
a = 300
print(a)
f()

7
tests/micropython/viper_try.py.exp

@ -0,0 +1,7 @@
finally
NameError
finally
NameError
100
200
300

28
tests/micropython/viper_with.py

@ -0,0 +1,28 @@
# test with handling within a viper function
class C:
def __init__(self):
print('__init__')
def __enter__(self):
print('__enter__')
def __exit__(self, a, b, c):
print('__exit__', a, b, c)
# basic with
@micropython.viper
def f():
with C():
print(1)
f()
# nested with and try-except
@micropython.viper
def f():
try:
with C():
print(1)
fail
print(2)
except NameError:
print('NameError')
f()

9
tests/micropython/viper_with.py.exp

@ -0,0 +1,9 @@
__init__
__enter__
1
__exit__ None None None
__init__
__enter__
1
__exit__ <class 'NameError'> name 'fail' is not defined None
NameError
Loading…
Cancel
Save