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

pull/4055/head
Damien George 2018-08-16 14:43:35 +10:00
rodzic a3de776486
commit f774614110
10 zmienionych plików z 210 dodań i 0 usunięć

Wyświetl plik

@ -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()

Wyświetl plik

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

Wyświetl plik

@ -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()

Wyświetl plik

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

Wyświetl plik

@ -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()

Wyświetl plik

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

Wyświetl plik

@ -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()

Wyświetl plik

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

Wyświetl plik

@ -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()

Wyświetl plik

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