File indexing completed on 2024-04-14 14:23:22

0001 /*
0002     SPDX-FileCopyrightText: 2013 Aurélien Gâteau <agateau@kde.org>
0003     SPDX-FileCopyrightText: 2014 Chusslove Illich <caslav.ilic@gmx.net>
0004     SPDX-FileCopyrightText: 2014 Kevin Krammer <krammer@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 #include "ktranscripttest.h"
0009 
0010 #include "testhelpers.h"
0011 
0012 #include <QTest>
0013 
0014 #include <QDebug>
0015 
0016 #include <ktranscript_p.h>
0017 
0018 QTEST_MAIN(KTranscriptTest)
0019 
0020 extern "C" {
0021 typedef KTranscript *(*InitFunc)();
0022 }
0023 
0024 KTranscriptTest::KTranscriptTest()
0025     : m_transcript(nullptr)
0026 {
0027 }
0028 
0029 void KTranscriptTest::initTestCase()
0030 {
0031     QVERIFY2(deployTestConfig(), "Could not deploy test ktranscript.ini");
0032 
0033     QString pluginPath = QStringLiteral(KTRANSCRIPT_PATH);
0034     QVERIFY2(QFile::exists(pluginPath), "Could not find ktranscript plugin");
0035 
0036     m_library.setFileName(pluginPath);
0037 
0038     QVERIFY(m_library.load());
0039     InitFunc initf = (InitFunc)m_library.resolve("load_transcript");
0040     QVERIFY(initf);
0041     m_transcript = initf();
0042     QVERIFY(m_transcript);
0043 }
0044 
0045 void KTranscriptTest::cleanupTestCase()
0046 {
0047     QVERIFY2(removeTestConfig(), "Could not remove test ktranscript.ini");
0048 }
0049 
0050 void KTranscriptTest::test_data()
0051 {
0052     QTest::addColumn<QVariantList>("argv");
0053     QTest::addColumn<bool>("fallsBack");
0054     QTest::addColumn<QString>("expected");
0055 
0056     QTest::newRow("test_basic") << QVariantList{"test_basic", "foo"} << false << "foo bar";
0057     QTest::newRow("test_unicode") << QVariantList{"test_unicode", "čгσィ九"} << false << "čгσィ九 фу";
0058     QTest::newRow("test_hascall") << QVariantList{"test_hascall", "test_basic"} << false << "yes";
0059     QTest::newRow("test_acall") << QVariantList{"test_acall", "test_basic", "qwyx"} << false << "qwyx bar";
0060     QTest::newRow("test_load") << QVariantList{"test_load"} << false << "foo blurb";
0061     QTest::newRow("test_fallback") << QVariantList{"test_fallback"} << true << "";
0062     QTest::newRow("test_msgid") << QVariantList{"test_msgid"} << false << "source-text";
0063     QTest::newRow("test_msgtrf") << QVariantList{"test_msgtrf"} << false << "translated-text";
0064     QTest::newRow("test_msgctxt") << QVariantList{"test_msgctxt"} << false << "a-context";
0065     QTest::newRow("test_msgkey") << QVariantList{"test_msgkey"} << false << "a-context|source-text";
0066     QTest::newRow("test_nsubs") << QVariantList{"test_nsubs"} << false << "2";
0067     QTest::newRow("test_subs") << QVariantList{"test_subs", 1} << false << "qwyx";
0068     QTest::newRow("test_vals") << QVariantList{"test_vals", 0, 5} << false << "50";
0069     QTest::newRow("test_dynctxt") << QVariantList{"test_dynctxt", "origin"} << false << "neverwhere";
0070     QTest::newRow("test_dbgputs") << QVariantList{"test_dbgputs"} << false << "debugged";
0071     QTest::newRow("test_warnputs") << QVariantList{"test_warnputs"} << false << "warned";
0072     QTest::newRow("test_setcallForall") << QVariantList{"test_setcallForall"} << false << "done";
0073     QTest::newRow("test_toUpperFirst") << QVariantList{"test_toUpperFirst", "...123 foo"} << false << "...123 Foo";
0074     QTest::newRow("test_toUpperFirst_unicode") << QVariantList{"test_toUpperFirst", "...123 фу"} << false << "...123 Фу";
0075     QTest::newRow("test_toLowerFirst") << QVariantList{"test_toLowerFirst", "...123 FOO"} << false << "...123 fOO";
0076     QTest::newRow("test_loadProps") << QVariantList{"test_loadProps", "cities"} << false << "loaded";
0077     QTest::newRow("test_getProp") << QVariantList{"test_getProp", "cities", "Athens", "gen"} << false << "Atine";
0078     QTest::newRow("test_setProp") << QVariantList{"test_setProp", "Oslo", "dat", "Oslou"} << false << "Oslou";
0079     QTest::newRow("test_normKey") << QVariantList{"test_normKey", "Some &Thing"} << false << "something";
0080     QTest::newRow("test_getConfString") << QVariantList{"test_getConfString", "StringKey"} << false << "StringValue";
0081     QTest::newRow("test_getConfStringWithDefault") << QVariantList{"test_getConfStringWithDefault", "NoSuchKey", "DefaultValue"} << false << "DefaultValue";
0082     QTest::newRow("test_getConfBool") << QVariantList{"test_getConfBool", "BoolKey"} << false << "true";
0083     QTest::newRow("test_getConfBoolWithDefault") << QVariantList{"test_getConfBoolWithDefault", "NoSuchKey", true} << false << "true";
0084     QTest::newRow("test_getConfNumber") << QVariantList{"test_getConfNumber", "NumberKey"} << false << "12345";
0085     QTest::newRow("test_getConfNumberWithDefault") << QVariantList{"test_getConfNumberWithDefault", "NoSuchKey", 54321} << false << "54321";
0086 }
0087 
0088 void KTranscriptTest::test()
0089 {
0090     QFETCH(QVariantList, argv);
0091     QFETCH(bool, fallsBack);
0092     QFETCH(QString, expected);
0093 
0094     QString language = "fr";
0095     QString country = "fr";
0096     QString msgctxt = "a-context";
0097     QHash<QString, QString> dynamicContext;
0098     dynamicContext.insert("origin", "neverwhere");
0099     QString msgid = "source-text";
0100     QStringList subs;
0101     subs << "10"
0102          << "qwyx";
0103     QList<QVariant> values;
0104     values << 10 << "qwyx";
0105     QString ordinaryTranslation = "translated-text";
0106 
0107     QString testJs = QFINDTESTDATA("test.js");
0108     QList<QStringList> modules;
0109     modules << (QStringList() << testJs << language);
0110 
0111     QString error;
0112     bool fallback;
0113     QString result = m_transcript->eval(argv, language, country, msgctxt, dynamicContext, msgid, subs, values, ordinaryTranslation, modules, error, fallback);
0114 
0115     if (!error.isEmpty()) {
0116         QFAIL(qPrintable(error));
0117     }
0118     if (!fallsBack) {
0119         QVERIFY(!fallback);
0120         QCOMPARE(result, expected);
0121     } else {
0122         QVERIFY(fallback);
0123     }
0124 }
0125 
0126 #include "moc_ktranscripttest.cpp"