summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-08-16 14:43:35 +1000
committerDamien George <damien.p.george@gmail.com>2018-08-17 14:11:36 +1000
commitf7746141106a5caa1b02c08d4e083260d2b9e1c1 (patch)
treeb42f16e5f227817cbd91acaeac71c5b119f41fcf
parenta3de776486396a4bf4f5233ce18143bd0fd81cac (diff)
tests/micropython: Add tests for try and with blocks under native/viper.
-rw-r--r--tests/micropython/native_try.py39
-rw-r--r--tests/micropython/native_try.py.exp7
-rw-r--r--tests/micropython/native_try_deep.py34
-rw-r--r--tests/micropython/native_try_deep.py.exp9
-rw-r--r--tests/micropython/native_with.py28
-rw-r--r--tests/micropython/native_with.py.exp9
-rw-r--r--tests/micropython/viper_try.py40
-rw-r--r--tests/micropython/viper_try.py.exp7
-rw-r--r--tests/micropython/viper_with.py28
-rw-r--r--tests/micropython/viper_with.py.exp9
10 files changed, 210 insertions, 0 deletions
diff --git a/tests/micropython/native_try.py b/tests/micropython/native_try.py
new file mode 100644
index 000000000..2e41bf2ea
--- /dev/null
+++ b/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()
diff --git a/tests/micropython/native_try.py.exp b/tests/micropython/native_try.py.exp
new file mode 100644
index 000000000..96596ce5f
--- /dev/null
+++ b/tests/micropython/native_try.py.exp
@@ -0,0 +1,7 @@
+finally
+NameError
+finally
+NameError
+100
+200
+300
diff --git a/tests/micropython/native_try_deep.py b/tests/micropython/native_try_deep.py
new file mode 100644
index 000000000..7fac4f0f3
--- /dev/null
+++ b/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()
diff --git a/tests/micropython/native_try_deep.py.exp b/tests/micropython/native_try_deep.py.exp
new file mode 100644
index 000000000..84c6beae3
--- /dev/null
+++ b/tests/micropython/native_try_deep.py.exp
@@ -0,0 +1,9 @@
+8
+7
+6
+5
+4
+3
+2
+1
+ValueError
diff --git a/tests/micropython/native_with.py b/tests/micropython/native_with.py
new file mode 100644
index 000000000..343f3e8d3
--- /dev/null
+++ b/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()
diff --git a/tests/micropython/native_with.py.exp b/tests/micropython/native_with.py.exp
new file mode 100644
index 000000000..6eef7822f
--- /dev/null
+++ b/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
diff --git a/tests/micropython/viper_try.py b/tests/micropython/viper_try.py
new file mode 100644
index 000000000..d75b3418e
--- /dev/null
+++ b/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()
+
diff --git a/tests/micropython/viper_try.py.exp b/tests/micropython/viper_try.py.exp
new file mode 100644
index 000000000..96596ce5f
--- /dev/null
+++ b/tests/micropython/viper_try.py.exp
@@ -0,0 +1,7 @@
+finally
+NameError
+finally
+NameError
+100
+200
+300
diff --git a/tests/micropython/viper_with.py b/tests/micropython/viper_with.py
new file mode 100644
index 000000000..2bc3c4f1b
--- /dev/null
+++ b/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()
diff --git a/tests/micropython/viper_with.py.exp b/tests/micropython/viper_with.py.exp
new file mode 100644
index 000000000..6eef7822f
--- /dev/null
+++ b/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