summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tests/auto/qscriptengine/tst_qscriptengine.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/tests/auto/qscriptengine/tst_qscriptengine.cpp b/tests/auto/qscriptengine/tst_qscriptengine.cpp
index 25ee00f..c00bde6 100644
--- a/tests/auto/qscriptengine/tst_qscriptengine.cpp
+++ b/tests/auto/qscriptengine/tst_qscriptengine.cpp
@@ -44,6 +44,7 @@
#include <qscriptengine.h>
#include <qscriptengineagent.h>
+#include <qscriptprogram.h>
#include <qscriptvalueiterator.h>
#include <qgraphicsitem.h>
#include <qstandarditemmodel.h>
@@ -151,6 +152,7 @@ private slots:
void installTranslatorFunctions();
void functionScopes();
void nativeFunctionScopes();
+ void compileAndEvaluate();
void qRegExpInport_data();
void qRegExpInport();
@@ -4289,6 +4291,85 @@ void tst_QScriptEngine::nativeFunctionScopes()
}
}
+void tst_QScriptEngine::compileAndEvaluate()
+{
+ QScriptEngine eng;
+
+ {
+ QString code("1 + 2");
+ QString fileName("hello.js");
+ int lineNumber(123);
+ QScriptProgram program = eng.compile(code, fileName, lineNumber);
+ QVERIFY(program.isValid());
+ QCOMPARE(program.sourceCode(), code);
+ QCOMPARE(program.fileName(), fileName);
+ QCOMPARE(program.lineNumber(), lineNumber);
+
+ QScriptValue expected = eng.evaluate(code);
+ QScriptValue ret = eng.evaluate(program);
+ QVERIFY(ret.equals(expected));
+ }
+
+ // Program that accesses variable in the scope
+ {
+ QScriptProgram program = eng.compile("a");
+ QVERIFY(program.isValid());
+ {
+ QScriptValue ret = eng.evaluate(program);
+ QVERIFY(ret.isError());
+ QCOMPARE(ret.toString(), QString::fromLatin1("ReferenceError: Can't find variable: a"));
+ }
+
+ QScriptValue obj = eng.newObject();
+ obj.setProperty("a", 123);
+ QScriptContext *ctx = eng.currentContext();
+ ctx->pushScope(obj);
+ {
+ QScriptValue ret = eng.evaluate(program);
+ QVERIFY(!ret.isError());
+ QVERIFY(ret.equals(obj.property("a")));
+ }
+
+ obj.setProperty("a", QScriptValue());
+ {
+ QScriptValue ret = eng.evaluate(program);
+ QVERIFY(ret.isError());
+ }
+
+ QScriptValue obj2 = eng.newObject();
+ obj2.setProperty("a", 456);
+ ctx->pushScope(obj2);
+ {
+ QScriptValue ret = eng.evaluate(program);
+ QVERIFY(!ret.isError());
+ QVERIFY(ret.equals(obj2.property("a")));
+ }
+
+ ctx->popScope();
+ }
+
+ // Program that creates closure
+ {
+ QScriptProgram program = eng.compile("(function() { var count = 0; return function() { return count++; }; })");
+ QVERIFY(program.isValid());
+ QScriptValue createCounter = eng.evaluate(program);
+ QVERIFY(createCounter.isFunction());
+ QScriptValue counter = createCounter.call();
+ QVERIFY(counter.isFunction());
+ {
+ QScriptValue ret = counter.call();
+ QVERIFY(ret.isNumber());
+ }
+ QScriptValue counter2 = createCounter.call();
+ QVERIFY(counter2.isFunction());
+ QVERIFY(!counter2.equals(counter));
+ {
+ QScriptValue ret = counter2.call();
+ QVERIFY(ret.isNumber());
+ }
+ }
+}
+
static QRegExp minimal(QRegExp r) { r.setMinimal(true); return r; }
void tst_QScriptEngine::qRegExpInport_data()