aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qjsengine/tst_qjsengine.cpp
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@qt.io>2018-08-15 16:43:13 +0200
committerSimon Hausmann <simon.hausmann@qt.io>2018-08-16 11:05:28 +0000
commitb8e9ce6b4ecda30ad838c1d3465c428591071a66 (patch)
tree0d9ef453895e305aacd0c90eed89ba8bf0de7b04 /tests/auto/qml/qjsengine/tst_qjsengine.cpp
parent67c33f2230e899a230260879a5d90dfcd8e13d8d (diff)
Add API to QJSEngine for importing ECMAScript modules
Now that the standard defines the concept of a module, it makes sense to offer a function in QJSEngine that can read files and load them. [ChangeLog][QtQml][QJSEngine] Added function to import ECMASCript modules from the file system or the Qt resource system. Change-Id: I72f8d49de948872221ac1b54fcfb066404bed9b9 Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Diffstat (limited to 'tests/auto/qml/qjsengine/tst_qjsengine.cpp')
-rw-r--r--tests/auto/qml/qjsengine/tst_qjsengine.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
index 54b11c6215..927cc16271 100644
--- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp
+++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
@@ -40,6 +40,7 @@
#include <stdlib.h>
#include <private/qv4alloca_p.h>
#include <private/qjsvalue_p.h>
+#include <QScopeGuard>
#ifdef Q_CC_MSVC
#define NO_INLINE __declspec(noinline)
@@ -223,6 +224,9 @@ private slots:
void throwError();
void mathMinMax();
+ void importModule();
+ void importModuleRelative();
+
public:
Q_INVOKABLE QJSValue throwingCppMethod();
@@ -4339,6 +4343,53 @@ void tst_QJSEngine::mathMinMax()
QVERIFY(QJSValuePrivate::getValue(&result)->isInteger());
}
+void tst_QJSEngine::importModule()
+{
+ // This is just a basic test for the API. Primary test coverage is via the ES test suite.
+ QJSEngine engine;
+ QJSValue ns = engine.importModule(QStringLiteral(":/testmodule.mjs"));
+ QCOMPARE(ns.property("value").toInt(), 42);
+ ns.property("sideEffect").call();
+
+ // Make sure that importing a second time will return the same instance.
+ QJSValue secondNamespace = engine.importModule(QStringLiteral(":/testmodule.mjs"));
+ QCOMPARE(secondNamespace.property("value").toInt(), 43);
+}
+
+void tst_QJSEngine::importModuleRelative()
+{
+ const QString oldWorkingDirectory = QDir::currentPath();
+ auto workingDirectoryGuard = qScopeGuard([oldWorkingDirectory]{
+ QDir::setCurrent(oldWorkingDirectory);
+ });
+
+ QTemporaryDir tempDir;
+ QVERIFY(tempDir.isValid());
+ QDir::setCurrent(tempDir.path());
+
+ {
+ QFile f(QStringLiteral("relativemodule.mjs"));
+ QVERIFY(f.open(QIODevice::WriteOnly|QIODevice::Truncate));
+ f.write(QByteArrayLiteral("var value = 100; export { value }; export function change() { value = value + 1 }"));
+ }
+
+ QJSEngine engine;
+
+ {
+ QJSValue module = engine.importModule(QStringLiteral("relativemodule.mjs"));
+ QVERIFY2(!module.isError(), qPrintable(module.toString()));
+ QCOMPARE(module.property("value").toInt(), 100);
+
+ module.property("change").call();
+ }
+
+ {
+ QJSValue sameModule = engine.importModule(tempDir.filePath(QStringLiteral("relativemodule.mjs")));
+ QVERIFY2(!sameModule.isError(), qPrintable(sameModule.toString()));
+ QCOMPARE(sameModule.property("value").toInt(), 101);
+ }
+}
+
QTEST_MAIN(tst_QJSEngine)
#include "tst_qjsengine.moc"