95 void functionEntryAndExit_functionReturn_construct(); |
95 void functionEntryAndExit_functionReturn_construct(); |
96 void functionEntryAndExit_functionReturn_call(); |
96 void functionEntryAndExit_functionReturn_call(); |
97 void functionEntryAndExit_objectCall(); |
97 void functionEntryAndExit_objectCall(); |
98 void positionChange_1(); |
98 void positionChange_1(); |
99 void positionChange_2(); |
99 void positionChange_2(); |
|
100 void positionChange_3(); |
100 void exceptionThrowAndCatch(); |
101 void exceptionThrowAndCatch(); |
101 void eventOrder_assigment(); |
102 void eventOrder_assigment(); |
102 void eventOrder_functionDefinition(); |
103 void eventOrder_functionDefinition(); |
103 void eventOrder_throwError(); |
104 void eventOrder_throwError(); |
104 void eventOrder_throwAndCatch(); |
105 void eventOrder_throwAndCatch(); |
1620 QEXPECT_FAIL("", "With JSC-based back-end, column number is always reported as 1", Continue); |
1623 QEXPECT_FAIL("", "With JSC-based back-end, column number is always reported as 1", Continue); |
1621 QCOMPARE(spy->at(1).columnNumber, 20); |
1624 QCOMPARE(spy->at(1).columnNumber, 20); |
1622 } |
1625 } |
1623 delete spy; |
1626 delete spy; |
1624 } |
1627 } |
|
1628 |
|
1629 void tst_QScriptEngineAgent::positionChange_3() |
|
1630 { |
|
1631 QScriptEngine eng; |
|
1632 eng.evaluate("function some_function1(a) {\n a++; \n return a + 12; } \n some_function1(42);", "function1.qs", 12); |
|
1633 QScriptValue some_function2 = eng.evaluate("(function (b) {\n b--; \n return b + 11; })", "function2.qs", 21); |
|
1634 some_function2.call(QScriptValue(), QScriptValueList() << 2 ); |
|
1635 |
|
1636 // Test that the agent work, even if installed after the function has been evaluated. |
|
1637 ScriptEngineSpy *spy = new ScriptEngineSpy(&eng, ~(ScriptEngineSpy::IgnorePositionChange)); |
|
1638 { |
|
1639 spy->clear(); |
|
1640 QScriptValue v = eng.evaluate("some_function1(15)"); |
|
1641 QCOMPARE(v.toInt32(), (15+1+12)); |
|
1642 QCOMPARE(spy->count(), 3); |
|
1643 |
|
1644 // some_function1() |
|
1645 QCOMPARE(spy->at(0).type, ScriptEngineEvent::PositionChange); |
|
1646 QVERIFY(spy->at(0).scriptId != -1); |
|
1647 QCOMPARE(spy->at(0).lineNumber, 1); |
|
1648 |
|
1649 // a++ |
|
1650 QCOMPARE(spy->at(1).type, ScriptEngineEvent::PositionChange); |
|
1651 QVERIFY(spy->at(1).scriptId != spy->at(0).scriptId); |
|
1652 QCOMPARE(spy->at(1).lineNumber, 13); |
|
1653 // return a + 12 |
|
1654 QCOMPARE(spy->at(2).type, ScriptEngineEvent::PositionChange); |
|
1655 QVERIFY(spy->at(2).scriptId == spy->at(1).scriptId); |
|
1656 QCOMPARE(spy->at(2).lineNumber, 14); |
|
1657 } |
|
1658 |
|
1659 { |
|
1660 spy->clear(); |
|
1661 QScriptValue v = some_function2.call(QScriptValue(), QScriptValueList() << 89 ); |
|
1662 QCOMPARE(v.toInt32(), (89-1+11)); |
|
1663 QCOMPARE(spy->count(), 2); |
|
1664 |
|
1665 // b-- |
|
1666 QCOMPARE(spy->at(0).type, ScriptEngineEvent::PositionChange); |
|
1667 QVERIFY(spy->at(0).scriptId != -1); |
|
1668 QCOMPARE(spy->at(0).lineNumber, 22); |
|
1669 // return b + 11 |
|
1670 QCOMPARE(spy->at(1).type, ScriptEngineEvent::PositionChange); |
|
1671 QVERIFY(spy->at(1).scriptId == spy->at(0).scriptId); |
|
1672 QCOMPARE(spy->at(1).lineNumber, 23); |
|
1673 } |
|
1674 |
|
1675 QVERIFY(!eng.hasUncaughtException()); |
|
1676 } |
|
1677 |
1625 |
1678 |
1626 void tst_QScriptEngineAgent::exceptionThrowAndCatch() |
1679 void tst_QScriptEngineAgent::exceptionThrowAndCatch() |
1627 { |
1680 { |
1628 QScriptEngine eng; |
1681 QScriptEngine eng; |
1629 ScriptEngineSpy *spy = new ScriptEngineSpy(&eng, ~(ScriptEngineSpy::IgnoreExceptionThrow |
1682 ScriptEngineSpy *spy = new ScriptEngineSpy(&eng, ~(ScriptEngineSpy::IgnoreExceptionThrow |
2377 |
2430 |
2378 QCOMPARE(spy->at(4).type, ScriptEngineEvent::ScriptUnload); |
2431 QCOMPARE(spy->at(4).type, ScriptEngineEvent::ScriptUnload); |
2379 QCOMPARE(spy->at(4).scriptId, spy->at(0).scriptId); |
2432 QCOMPARE(spy->at(4).scriptId, spy->at(0).scriptId); |
2380 } |
2433 } |
2381 |
2434 |
|
2435 class BacktraceSpy : public QScriptEngineAgent |
|
2436 { |
|
2437 public: |
|
2438 BacktraceSpy(QScriptEngine *engine, const QStringList &expectedbacktrace, int breakpoint) |
|
2439 : QScriptEngineAgent(engine), expectedbacktrace(expectedbacktrace), breakpoint(breakpoint), ok(false) {} |
|
2440 |
|
2441 QStringList expectedbacktrace; |
|
2442 int breakpoint; |
|
2443 bool ok; |
|
2444 |
|
2445 protected: |
|
2446 |
|
2447 void exceptionThrow(qint64 , const QScriptValue &, bool) |
|
2448 { check(); } |
|
2449 |
|
2450 void positionChange(qint64 , int lineNumber, int ) |
|
2451 { |
|
2452 if (lineNumber == breakpoint) |
|
2453 check(); |
|
2454 } |
|
2455 |
|
2456 private: |
|
2457 void check() |
|
2458 { |
|
2459 QCOMPARE(engine()->currentContext()->backtrace(), expectedbacktrace); |
|
2460 ok = true; |
|
2461 } |
|
2462 }; |
|
2463 |
|
2464 |
|
2465 void tst_QScriptEngineAgent::backtraces_data() |
|
2466 { |
|
2467 QTest::addColumn<QString>("code"); |
|
2468 QTest::addColumn<int>("breakpoint"); |
|
2469 QTest::addColumn<QStringList>("expectedbacktrace"); |
|
2470 |
|
2471 { |
|
2472 QString source( |
|
2473 "function foo() {\n" |
|
2474 " var a = 5\n" |
|
2475 "}\n" |
|
2476 "foo('hello', { })\n" |
|
2477 "var r = 0;"); |
|
2478 |
|
2479 QStringList expected; |
|
2480 expected |
|
2481 << "foo('hello', [object Object]) at filename.js:2" |
|
2482 << "<global>() at filename.js:4"; |
|
2483 QTest::newRow("simple breakpoint") << source << 2 << expected; |
|
2484 } |
|
2485 |
|
2486 { |
|
2487 QString source( |
|
2488 "function foo() {\n" |
|
2489 " error = err\n" //this must throw |
|
2490 "}\n" |
|
2491 "foo('hello', { })\n" |
|
2492 "var r = 0;"); |
|
2493 |
|
2494 QStringList expected; |
|
2495 expected |
|
2496 << "foo('hello', [object Object]) at filename.js:2" |
|
2497 << "<global>() at filename.js:4"; |
|
2498 QTest::newRow("throw because of error") << source << -100 << expected; |
|
2499 } |
|
2500 } |
|
2501 |
|
2502 void tst_QScriptEngineAgent::backtraces() |
|
2503 { |
|
2504 QFETCH(QString, code); |
|
2505 QFETCH(int, breakpoint); |
|
2506 QFETCH(QStringList, expectedbacktrace); |
|
2507 |
|
2508 QScriptEngine eng; |
|
2509 BacktraceSpy *spy = new BacktraceSpy(&eng, expectedbacktrace, breakpoint); |
|
2510 eng.setAgent(spy); |
|
2511 QLatin1String filename("filename.js"); |
|
2512 eng.evaluate(code, filename); |
|
2513 QVERIFY(spy->ok); |
|
2514 } |
|
2515 |
2382 QTEST_MAIN(tst_QScriptEngineAgent) |
2516 QTEST_MAIN(tst_QScriptEngineAgent) |
2383 #include "tst_qscriptengineagent.moc" |
2517 #include "tst_qscriptengineagent.moc" |