summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/basics/import_instance_method.py38
-rw-r--r--tests/ports/webassembly/asyncio_top_level_await.mjs36
-rw-r--r--tests/ports/webassembly/asyncio_top_level_await.mjs.exp8
-rw-r--r--tests/ports/webassembly/register_js_module.js39
-rw-r--r--tests/ports/webassembly/register_js_module.js.exp10
5 files changed, 131 insertions, 0 deletions
diff --git a/tests/basics/import_instance_method.py b/tests/basics/import_instance_method.py
new file mode 100644
index 000000000..d25b70ac5
--- /dev/null
+++ b/tests/basics/import_instance_method.py
@@ -0,0 +1,38 @@
+# Test importing a method from a class instance.
+# This is not a common thing to do, but ensures MicroPython has the same semantics as CPython.
+
+import sys
+
+if not hasattr(sys, "modules"):
+ print("SKIP")
+ raise SystemExit
+
+
+class A:
+ def __init__(self, value):
+ self.value = value
+
+ def meth(self):
+ return self.value
+
+ def meth_with_arg(self, a):
+ return [self.value, a]
+
+
+# Register a class instance as the module "mod".
+sys.modules["mod"] = A(1)
+
+# Try importing it as a module.
+import mod
+
+print(mod.meth())
+print(mod.meth_with_arg(2))
+
+# Change the module.
+sys.modules["mod"] = A(3)
+
+# Try importing it using "from ... import".
+from mod import meth, meth_with_arg
+
+print(meth())
+print(meth_with_arg(4))
diff --git a/tests/ports/webassembly/asyncio_top_level_await.mjs b/tests/ports/webassembly/asyncio_top_level_await.mjs
index 234b7a6ce..fedf10d9c 100644
--- a/tests/ports/webassembly/asyncio_top_level_await.mjs
+++ b/tests/ports/webassembly/asyncio_top_level_await.mjs
@@ -88,3 +88,39 @@ print("top-level end")
`);
console.log("finished");
+
+/**********************************************************/
+// Top-level await's on a JavaScript function that throws.
+
+console.log("= TEST 4 ==========");
+
+globalThis.jsFail = async () => {
+ console.log("jsFail");
+ throw new Error("jsFail");
+};
+
+await mp.runPythonAsync(`
+import asyncio
+import js
+
+# Test top-level catching from a failed JS await.
+try:
+ await js.jsFail()
+except Exception as er:
+ print("caught exception:", type(er), type(er.args[0]), er.args[1:])
+
+async def main():
+ try:
+ await js.jsFail()
+ except Exception as er:
+ print("caught exception:", type(er), type(er.args[0]), er.args[1:])
+
+# Test top-level waiting on a coro that catches.
+await main()
+
+# Test top-level waiting on a task that catches.
+t = asyncio.create_task(main())
+await t
+`);
+
+console.log("finished");
diff --git a/tests/ports/webassembly/asyncio_top_level_await.mjs.exp b/tests/ports/webassembly/asyncio_top_level_await.mjs.exp
index 66fefd2dc..f6720b5ab 100644
--- a/tests/ports/webassembly/asyncio_top_level_await.mjs.exp
+++ b/tests/ports/webassembly/asyncio_top_level_await.mjs.exp
@@ -17,3 +17,11 @@ top-level wait task
task end
top-level end
finished
+= TEST 4 ==========
+jsFail
+caught exception: <class 'JsException'> <class 'JsProxy'> ('Error', 'jsFail')
+jsFail
+caught exception: <class 'JsException'> <class 'JsProxy'> ('Error', 'jsFail')
+jsFail
+caught exception: <class 'JsException'> <class 'JsProxy'> ('Error', 'jsFail')
+finished
diff --git a/tests/ports/webassembly/register_js_module.js b/tests/ports/webassembly/register_js_module.js
index b512f2c0d..f58d12e50 100644
--- a/tests/ports/webassembly/register_js_module.js
+++ b/tests/ports/webassembly/register_js_module.js
@@ -1,6 +1,45 @@
+// Test the registerJsModule() public API method.
+
import(process.argv[2]).then((mp) => {
mp.loadMicroPython().then((py) => {
+ // Simple module.
py.registerJsModule("js_module", { y: 2 });
py.runPython("import js_module; print(js_module); print(js_module.y)");
+
+ // Module with functions.
+ // In particular test how "this" behaves.
+ py.registerJsModule("js_module2", {
+ yes: true,
+ add1(x) {
+ return x + 1;
+ },
+ getThis() {
+ return this;
+ },
+ });
+
+ console.log("====");
+
+ // Test using simple import.
+ py.runPython(`
+import js_module2
+
+print(js_module2.yes)
+print(js_module2.add1(1))
+print(js_module2.getThis())
+print(js_module2.getThis().yes)
+`);
+
+ console.log("====");
+
+ // Test using "from ... import".
+ py.runPython(`
+from js_module2 import yes, add1, getThis
+
+print(yes)
+print(add1(2))
+print(getThis())
+print(getThis().yes)
+`);
});
});
diff --git a/tests/ports/webassembly/register_js_module.js.exp b/tests/ports/webassembly/register_js_module.js.exp
index 6e2bad3ce..34bfd345d 100644
--- a/tests/ports/webassembly/register_js_module.js.exp
+++ b/tests/ports/webassembly/register_js_module.js.exp
@@ -1,2 +1,12 @@
<JsProxy 2>
2
+====
+True
+2
+<JsProxy 3>
+True
+====
+True
+3
+<JsProxy 3>
+True