summaryrefslogtreecommitdiff
path: root/tests/micropython/viper_error.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-04-22 23:18:28 +0100
committerDamien George <damien.p.george@gmail.com>2015-04-22 23:18:28 +0100
commit40d43ea88dfff431dd863146dd2e62ea8c24683e (patch)
treefb9000e5d78f4fbaf63d0ea0533c56dbfb4c4a0f /tests/micropython/viper_error.py
parent5e9810396f21698718eb26a3d4125bfeeb2f8859 (diff)
tests: Add more tests for viper, including tests for ViperTypeError's.
Diffstat (limited to 'tests/micropython/viper_error.py')
-rw-r--r--tests/micropython/viper_error.py55
1 files changed, 49 insertions, 6 deletions
diff --git a/tests/micropython/viper_error.py b/tests/micropython/viper_error.py
index 514acebd7..0762f5079 100644
--- a/tests/micropython/viper_error.py
+++ b/tests/micropython/viper_error.py
@@ -1,11 +1,54 @@
-# test syntax errors specific to viper code generation
+# test syntax and type errors specific to viper code generation
-def test_syntax(code):
+def test(code):
try:
exec(code)
- except SyntaxError:
- print("SyntaxError")
+ except (SyntaxError, ViperTypeError) as e:
+ print(repr(e))
# viper: annotations must be identifiers
-test_syntax("@micropython.viper\ndef f(a:1): pass")
-test_syntax("@micropython.viper\ndef f() -> 1: pass")
+test("@micropython.viper\ndef f(a:1): pass")
+test("@micropython.viper\ndef f() -> 1: pass")
+
+# local used before type known
+test("""
+@micropython.viper
+def f():
+ print(x)
+ x = 1
+""")
+
+# type mismatch storing to local
+test("""
+@micropython.viper
+def f():
+ x = 1
+ y = []
+ x = y
+""")
+
+# can't implicitly convert type to bool
+test("""
+@micropython.viper
+def f():
+ x = ptr(0)
+ if x:
+ pass
+""")
+
+# incorrect return type
+test("@micropython.viper\ndef f() -> int: return []")
+
+# can't do binary op between incompatible types
+test("@micropython.viper\ndef f(): 1 + []")
+
+# can't load
+test("@micropython.viper\ndef f(): 1[0]")
+test("@micropython.viper\ndef f(): 1[x]")
+
+# can't store
+test("@micropython.viper\ndef f(): 1[0] = 1")
+test("@micropython.viper\ndef f(): 1[x] = 1")
+
+# must raise an object
+test("@micropython.viper\ndef f(): raise 1")