160 void translateScript(); |
160 void translateScript(); |
161 void translateWithInvalidArgs_data(); |
161 void translateWithInvalidArgs_data(); |
162 void translateWithInvalidArgs(); |
162 void translateWithInvalidArgs(); |
163 void translationContext_data(); |
163 void translationContext_data(); |
164 void translationContext(); |
164 void translationContext(); |
|
165 void translateScriptIdBased(); |
165 void functionScopes(); |
166 void functionScopes(); |
166 void nativeFunctionScopes(); |
167 void nativeFunctionScopes(); |
167 void evaluateProgram(); |
168 void evaluateProgram(); |
168 void collectGarbageAfterConnect(); |
169 void collectGarbageAfterConnect(); |
169 void promoteThisObjectToQObjectInConstructor(); |
170 void promoteThisObjectToQObjectInConstructor(); |
4384 } |
4385 } |
4385 QVERIFY(!global.property("qsTranslate").isValid()); |
4386 QVERIFY(!global.property("qsTranslate").isValid()); |
4386 QVERIFY(!global.property("QT_TRANSLATE_NOOP").isValid()); |
4387 QVERIFY(!global.property("QT_TRANSLATE_NOOP").isValid()); |
4387 QVERIFY(!global.property("qsTr").isValid()); |
4388 QVERIFY(!global.property("qsTr").isValid()); |
4388 QVERIFY(!global.property("QT_TR_NOOP").isValid()); |
4389 QVERIFY(!global.property("QT_TR_NOOP").isValid()); |
|
4390 QVERIFY(!global.property("qsTrId").isValid()); |
|
4391 QVERIFY(!global.property("QT_TRID_NOOP").isValid()); |
4389 QVERIFY(!globalOrig.property("String").property("prototype").property("arg").isValid()); |
4392 QVERIFY(!globalOrig.property("String").property("prototype").property("arg").isValid()); |
4390 |
4393 |
4391 eng.installTranslatorFunctions(); |
4394 eng.installTranslatorFunctions(); |
4392 QVERIFY(global.property("qsTranslate").isFunction()); |
4395 QVERIFY(global.property("qsTranslate").isFunction()); |
4393 QVERIFY(global.property("QT_TRANSLATE_NOOP").isFunction()); |
4396 QVERIFY(global.property("QT_TRANSLATE_NOOP").isFunction()); |
4394 QVERIFY(global.property("qsTr").isFunction()); |
4397 QVERIFY(global.property("qsTr").isFunction()); |
4395 QVERIFY(global.property("QT_TR_NOOP").isFunction()); |
4398 QVERIFY(global.property("QT_TR_NOOP").isFunction()); |
|
4399 QVERIFY(global.property("qsTrId").isFunction()); |
|
4400 QVERIFY(global.property("QT_TRID_NOOP").isFunction()); |
4396 QVERIFY(globalOrig.property("String").property("prototype").property("arg").isFunction()); |
4401 QVERIFY(globalOrig.property("String").property("prototype").property("arg").isFunction()); |
4397 |
4402 |
4398 if (useCustomGlobalObject) { |
4403 if (useCustomGlobalObject) { |
4399 QVERIFY(!globalOrig.property("qsTranslate").isValid()); |
4404 QVERIFY(!globalOrig.property("qsTranslate").isValid()); |
4400 QVERIFY(!globalOrig.property("QT_TRANSLATE_NOOP").isValid()); |
4405 QVERIFY(!globalOrig.property("QT_TRANSLATE_NOOP").isValid()); |
4401 QVERIFY(!globalOrig.property("qsTr").isValid()); |
4406 QVERIFY(!globalOrig.property("qsTr").isValid()); |
4402 QVERIFY(!globalOrig.property("QT_TR_NOOP").isValid()); |
4407 QVERIFY(!globalOrig.property("QT_TR_NOOP").isValid()); |
|
4408 QVERIFY(!globalOrig.property("qsTrId").isValid()); |
|
4409 QVERIFY(!globalOrig.property("QT_TRID_NOOP").isValid()); |
4403 } |
4410 } |
4404 |
4411 |
4405 { |
4412 { |
4406 QScriptValue ret = eng.evaluate("qsTr('foo')"); |
4413 QScriptValue ret = eng.evaluate("qsTr('foo')"); |
4407 QVERIFY(ret.isString()); |
4414 QVERIFY(ret.isString()); |
4424 } |
4431 } |
4425 { |
4432 { |
4426 QScriptValue ret = eng.evaluate("'foo%0'.arg('bar')"); |
4433 QScriptValue ret = eng.evaluate("'foo%0'.arg('bar')"); |
4427 QVERIFY(ret.isString()); |
4434 QVERIFY(ret.isString()); |
4428 QCOMPARE(ret.toString(), QString::fromLatin1("foobar")); |
4435 QCOMPARE(ret.toString(), QString::fromLatin1("foobar")); |
|
4436 } |
|
4437 |
|
4438 { |
|
4439 QScriptValue ret = eng.evaluate("qsTrId('foo')"); |
|
4440 QVERIFY(ret.isString()); |
|
4441 QCOMPARE(ret.toString(), QString::fromLatin1("foo")); |
|
4442 } |
|
4443 { |
|
4444 QScriptValue ret = eng.evaluate("QT_TRID_NOOP('foo')"); |
|
4445 QVERIFY(ret.isString()); |
|
4446 QCOMPARE(ret.toString(), QString::fromLatin1("foo")); |
4429 } |
4447 } |
4430 } |
4448 } |
4431 |
4449 |
4432 static QScriptValue callQsTr(QScriptContext *ctx, QScriptEngine *eng) |
4450 static QScriptValue callQsTr(QScriptContext *ctx, QScriptEngine *eng) |
4433 { |
4451 { |
4535 QTest::newRow("qsTranslate('foo', 123)") << "qsTranslate('foo', 123)" << "Error: qsTranslate(): second argument (text) must be a string"; |
4553 QTest::newRow("qsTranslate('foo', 123)") << "qsTranslate('foo', 123)" << "Error: qsTranslate(): second argument (text) must be a string"; |
4536 QTest::newRow("qsTranslate('foo', 'bar', 123)") << "qsTranslate('foo', 'bar', 123)" << "Error: qsTranslate(): third argument (comment) must be a string"; |
4554 QTest::newRow("qsTranslate('foo', 'bar', 123)") << "qsTranslate('foo', 'bar', 123)" << "Error: qsTranslate(): third argument (comment) must be a string"; |
4537 QTest::newRow("qsTranslate('foo', 'bar', 'baz', 123)") << "qsTranslate('foo', 'bar', 'baz', 123)" << "Error: qsTranslate(): fourth argument (encoding) must be a string"; |
4555 QTest::newRow("qsTranslate('foo', 'bar', 'baz', 123)") << "qsTranslate('foo', 'bar', 'baz', 123)" << "Error: qsTranslate(): fourth argument (encoding) must be a string"; |
4538 QTest::newRow("qsTranslate('foo', 'bar', 'baz', 'zab', 'rab')") << "qsTranslate('foo', 'bar', 'baz', 'zab', 'rab')" << "Error: qsTranslate(): fifth argument (n) must be a number"; |
4556 QTest::newRow("qsTranslate('foo', 'bar', 'baz', 'zab', 'rab')") << "qsTranslate('foo', 'bar', 'baz', 'zab', 'rab')" << "Error: qsTranslate(): fifth argument (n) must be a number"; |
4539 QTest::newRow("qsTranslate('foo', 'bar', 'baz', 'zab', 123)") << "qsTranslate('foo', 'bar', 'baz', 'zab', 123)" << "Error: qsTranslate(): invalid encoding 'zab'"; |
4557 QTest::newRow("qsTranslate('foo', 'bar', 'baz', 'zab', 123)") << "qsTranslate('foo', 'bar', 'baz', 'zab', 123)" << "Error: qsTranslate(): invalid encoding 'zab'"; |
|
4558 |
|
4559 QTest::newRow("qsTrId()") << "qsTrId()" << "Error: qsTrId() requires at least one argument"; |
|
4560 QTest::newRow("qsTrId(123)") << "qsTrId(123)" << "TypeError: qsTrId(): first argument (id) must be a string"; |
|
4561 QTest::newRow("qsTrId('foo', 'bar')") << "qsTrId('foo', 'bar')" << "TypeError: qsTrId(): second argument (n) must be a number"; |
4540 } |
4562 } |
4541 |
4563 |
4542 void tst_QScriptEngine::translateWithInvalidArgs() |
4564 void tst_QScriptEngine::translateWithInvalidArgs() |
4543 { |
4565 { |
4544 QFETCH(QString, expression); |
4566 QFETCH(QString, expression); |
4594 QScriptValue ret = engine.evaluate(QString::fromLatin1("qsTr('%0')").arg(text), path); |
4616 QScriptValue ret = engine.evaluate(QString::fromLatin1("qsTr('%0')").arg(text), path); |
4595 QVERIFY(ret.isString()); |
4617 QVERIFY(ret.isString()); |
4596 QCOMPARE(ret.toString(), expectedTranslation); |
4618 QCOMPARE(ret.toString(), expectedTranslation); |
4597 |
4619 |
4598 QCoreApplication::instance()->removeTranslator(&translator); |
4620 QCoreApplication::instance()->removeTranslator(&translator); |
|
4621 } |
|
4622 |
|
4623 void tst_QScriptEngine::translateScriptIdBased() |
|
4624 { |
|
4625 QScriptEngine engine; |
|
4626 |
|
4627 QTranslator translator; |
|
4628 translator.load(":/translations/idtranslatable_la"); |
|
4629 QCoreApplication::instance()->installTranslator(&translator); |
|
4630 engine.installTranslatorFunctions(); |
|
4631 |
|
4632 QString fileName = QString::fromLatin1("idtranslatable.js"); |
|
4633 |
|
4634 QHash<QString, QString> expectedTranslations; |
|
4635 expectedTranslations["qtn_foo_bar"] = "First string"; |
|
4636 expectedTranslations["qtn_needle"] = "Second string"; |
|
4637 expectedTranslations["qtn_haystack"] = "Third string"; |
|
4638 expectedTranslations["qtn_bar_baz"] = "Fourth string"; |
|
4639 |
|
4640 QHash<QString, QString>::const_iterator it; |
|
4641 for (it = expectedTranslations.constBegin(); it != expectedTranslations.constEnd(); ++it) { |
|
4642 for (int x = 0; x < 2; ++x) { |
|
4643 QString fn; |
|
4644 if (x) |
|
4645 fn = fileName; |
|
4646 // Top-level |
|
4647 QCOMPARE(engine.evaluate(QString::fromLatin1("qsTrId('%0')") |
|
4648 .arg(it.key()), fn).toString(), |
|
4649 it.value()); |
|
4650 QCOMPARE(engine.evaluate(QString::fromLatin1("QT_TRID_NOOP('%0')") |
|
4651 .arg(it.key()), fn).toString(), |
|
4652 it.key()); |
|
4653 // From function |
|
4654 QCOMPARE(engine.evaluate(QString::fromLatin1("(function() { return qsTrId('%0'); })()") |
|
4655 .arg(it.key()), fn).toString(), |
|
4656 it.value()); |
|
4657 QCOMPARE(engine.evaluate(QString::fromLatin1("(function() { return QT_TRID_NOOP('%0'); })()") |
|
4658 .arg(it.key()), fn).toString(), |
|
4659 it.key()); |
|
4660 } |
|
4661 } |
|
4662 |
|
4663 // Plural form |
|
4664 QCOMPARE(engine.evaluate("qsTrId('qtn_bar_baz', 10)").toString(), |
|
4665 QString::fromLatin1("10 fooish bar(s) found")); |
|
4666 QCOMPARE(engine.evaluate("qsTrId('qtn_foo_bar', 10)").toString(), |
|
4667 QString::fromLatin1("qtn_foo_bar")); // Doesn't have plural |
4599 } |
4668 } |
4600 |
4669 |
4601 void tst_QScriptEngine::functionScopes() |
4670 void tst_QScriptEngine::functionScopes() |
4602 { |
4671 { |
4603 QScriptEngine eng; |
4672 QScriptEngine eng; |