From b1574e4845d657673e178b892ba57e398984e0b3 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Fri, 14 Aug 2009 16:12:52 +0200 Subject: more extensive backtrace test --- tests/auto/qscriptcontext/tst_qscriptcontext.cpp | 105 ++++++++++++++++++++--- 1 file changed, 93 insertions(+), 12 deletions(-) diff --git a/tests/auto/qscriptcontext/tst_qscriptcontext.cpp b/tests/auto/qscriptcontext/tst_qscriptcontext.cpp index dfa0b8b..43938b6 100644 --- a/tests/auto/qscriptcontext/tst_qscriptcontext.cpp +++ b/tests/auto/qscriptcontext/tst_qscriptcontext.cpp @@ -68,6 +68,7 @@ private slots: void evaluateInFunction(); void pushAndPopContext(); void lineNumber(); + void backtrace_data(); void backtrace(); void scopeChain(); void pushAndPopScope(); @@ -550,26 +551,106 @@ static QScriptValue getBacktrace(QScriptContext *ctx, QScriptEngine *eng) return qScriptValueFromValue(eng, ctx->backtrace()); } +static QScriptValue custom_eval(QScriptContext *ctx, QScriptEngine *eng) +{ + return eng->evaluate(ctx->argumentsObject().property(0).toString(), ctx->argumentsObject().property(1).toString()); +} + +void tst_QScriptContext::backtrace_data() +{ + QTest::addColumn("code"); + QTest::addColumn("expectedbacktrace"); + + { + QStringList expected; + expected << " (123) at -1" + << "foo ([object Object], [object global]) at testfile:2" //### object instead of 'hello' + << " () at testfile:4"; + + QString source( + "function foo() {\n" + " return bt(123);\n" + "}\n" + "foo('hello', { })" ); + + QTest::newRow("simple") << source << expected; + } + + { + QStringList expected; + QString source = QString( + "function foo(arg1 , arg2) {\n" + " return eval(\"%1\");\n" + "}\n" + "foo('hello', 456)" + ).arg("\\n \\n bt('hey'); \\n"); + + expected << " (hey) at -1" + << " () at 3" //### should be + << "foo (arg1=hello, arg2=456) at testfile:-1" //### line number should be 2 + << " () at testfile:4"; + + QTest::newRow("eval") << source << expected; + } + + { + QString eval_code( + "function bar(a) {\\n" + " return bt('m');\\n" + "}\\n" + "bar('b'); \\n"); + QString source = QString( + "function foo() {\n" + " return custom_eval(\"%1\", 'eval.js');\n" + "}\n" + "foo()" + ).arg(eval_code); + + QStringList expected; + expected << " (m) at -1" + << "bar (a=b) at eval.js:2" + << " () at eval.js:4" //### should be + << QString(" (%1, eval.js) at -1").arg(eval_code.replace("\\n", "\n")) + << "foo () at testfile:2" + << " () at testfile:4"; + + QTest::newRow("custom_eval") << source << expected; + } + { + QString f("function (a) {\n return bt(a); \n }"); + QString source = QString( + "function foo(f) {\n" + " return f('b');\n" + "}\n" + "foo(%1)" + ).arg(f); + + QStringList expected; + expected << " (b) at -1" + << " (a=b) at testfile:5" + << QString("foo (f=%1) at testfile:2").arg(f) + << " () at testfile:6"; + + QTest::newRow("closure") << source << expected; + } +} + + void tst_QScriptContext::backtrace() { + QFETCH(QString, code); + QFETCH(QStringList, expectedbacktrace); + QScriptEngine eng; eng.globalObject().setProperty("bt", eng.newFunction(getBacktrace)); + eng.globalObject().setProperty("custom_eval", eng.newFunction(custom_eval)); QString fileName = "testfile"; - QStringList expected; - expected << " (123) at -1" - << "foo ([object Object], [object global]) at testfile:2" - << " () at testfile:4"; - - QScriptValue ret = eng.evaluate( - "function foo() {\n" - " return bt(123);\n" - "}\n" - "foo('hello', { })", fileName); - + QScriptValue ret = eng.evaluate(code, fileName); + QVERIFY(!eng.hasUncaughtException()); QVERIFY(ret.isArray()); QStringList slist = qscriptvalue_cast(ret); - QCOMPARE(slist, expected); + QCOMPARE(slist, expectedbacktrace); } static QScriptValue getScopeChain(QScriptContext *ctx, QScriptEngine *eng) -- cgit v0.12