summaryrefslogtreecommitdiff
path: root/test/gjs-tests.cpp
diff options
context:
space:
mode:
authorEvan Welsh <contact@evanwelsh.com>2021-02-05 21:51:41 -0800
committerPhilip Chimento <philip.chimento@gmail.com>2021-02-08 19:49:06 -0800
commit81e3d313b40c618a924df07f1b147ca8909cf2eb (patch)
tree80399edd30364058b3a2a492c7ce0f0b2932bba1 /test/gjs-tests.cpp
parent97c2fd83488d567c1d83481478ac7bee2335505b (diff)
downloadgjs-81e3d313b40c618a924df07f1b147ca8909cf2eb.tar.gz
Add dynamic import support to scripts
To allow import() from scripts we must set script privates with an object similar to the ModulePrivate class.
Diffstat (limited to 'test/gjs-tests.cpp')
-rw-r--r--test/gjs-tests.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index cd79d100..d8a32102 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -112,6 +112,81 @@ gjstest_test_func_gjs_context_construct_eval(void)
g_object_unref (context);
}
+static void gjstest_test_func_gjs_context_eval_dynamic_import() {
+ GjsAutoUnref<GjsContext> gjs = gjs_context_new();
+ GError* error = NULL;
+ int status;
+
+ bool ok = gjs_context_eval(gjs, R"js(
+ import('system')
+ .catch(logError)
+ .finally(() => imports.mainloop.quit());
+ imports.mainloop.run();
+ )js",
+ -1, "<main>", &status, &error);
+
+ g_assert_true(ok);
+ g_assert_no_error(error);
+}
+
+static void gjstest_test_func_gjs_context_eval_dynamic_import_relative() {
+ GjsAutoUnref<GjsContext> gjs = gjs_context_new();
+ GError* error = NULL;
+ int status;
+
+ bool ok = g_file_set_contents("num.js", "export default 77;", -1, &error);
+
+ g_assert_true(ok);
+ g_assert_no_error(error);
+
+ ok = gjs_context_eval(gjs, R"js(
+ let num;
+ import('./num.js')
+ .then(module => (num = module.default))
+ .catch(logError)
+ .finally(() => imports.mainloop.quit());
+ imports.mainloop.run();
+ num;
+ )js",
+ -1, "<main>", &status, &error);
+
+ g_assert_true(ok);
+ g_assert_no_error(error);
+ g_assert_cmpint(status, ==, 77);
+
+ g_unlink("num.js");
+}
+
+static void gjstest_test_func_gjs_context_eval_dynamic_import_bad() {
+ GjsAutoUnref<GjsContext> gjs = gjs_context_new();
+ GError* error = NULL;
+ int status;
+
+ g_test_expect_message("Gjs", G_LOG_LEVEL_WARNING,
+ "*Unknown module: 'badmodule'*");
+
+ bool ok = gjs_context_eval(gjs, R"js(
+ let isBad = false;
+ import('badmodule')
+ .catch(err => {
+ logError(err);
+ isBad = true;
+ })
+ .finally(() => imports.mainloop.quit());
+ imports.mainloop.run();
+
+ if (isBad) imports.system.exit(10);
+ )js",
+ -1, "<main>", &status, &error);
+
+ g_assert_false(ok);
+ g_assert_cmpuint(status, ==, 10);
+
+ g_test_assert_expected_messages();
+
+ g_clear_error(&error);
+}
+
static void gjstest_test_func_gjs_context_eval_non_zero_terminated(void) {
GjsAutoUnref<GjsContext> gjs = gjs_context_new();
GError* error = NULL;
@@ -765,6 +840,12 @@ main(int argc,
g_test_add_func("/gjs/context/construct/destroy", gjstest_test_func_gjs_context_construct_destroy);
g_test_add_func("/gjs/context/construct/eval", gjstest_test_func_gjs_context_construct_eval);
+ g_test_add_func("/gjs/context/eval/dynamic-import",
+ gjstest_test_func_gjs_context_eval_dynamic_import);
+ g_test_add_func("/gjs/context/eval/dynamic-import/relative",
+ gjstest_test_func_gjs_context_eval_dynamic_import_relative);
+ g_test_add_func("/gjs/context/eval/dynamic-import/bad",
+ gjstest_test_func_gjs_context_eval_dynamic_import_bad);
g_test_add_func("/gjs/context/eval/non-zero-terminated",
gjstest_test_func_gjs_context_eval_non_zero_terminated);
g_test_add_func("/gjs/context/exit", gjstest_test_func_gjs_context_exit);