From 9d39dd98295136d633c05f28654b021aee48d147 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Aur=C3=A9lien=20G=C3=A2teau?= <agateau@kde.org>
Date: Thu, 24 Oct 2013 18:20:56 +0200
Subject: [PATCH 1/2] Start writing unit test for KTranscript

---
 tier2/ki18n/autotests/CMakeLists.txt      |  1 +
 tier2/ki18n/autotests/ktranscripttest.cpp | 84 +++++++++++++++++++++++++++++++
 tier2/ki18n/autotests/ktranscripttest.h   | 46 +++++++++++++++++
 3 files changed, 131 insertions(+)
 create mode 100644 tier2/ki18n/autotests/ktranscripttest.cpp
 create mode 100644 tier2/ki18n/autotests/ktranscripttest.h

diff --git a/tier2/ki18n/autotests/CMakeLists.txt b/tier2/ki18n/autotests/CMakeLists.txt
index 49f46a1..ea22bda 100644
--- a/tier2/ki18n/autotests/CMakeLists.txt
+++ b/tier2/ki18n/autotests/CMakeLists.txt
@@ -13,5 +13,6 @@ endmacro()
 
 ki18n_unit_tests(
     klocalizedstringtest
+    ktranscripttest
 )
 
diff --git a/tier2/ki18n/autotests/ktranscripttest.cpp b/tier2/ki18n/autotests/ktranscripttest.cpp
new file mode 100644
index 0000000..fdde23c
--- /dev/null
+++ b/tier2/ki18n/autotests/ktranscripttest.cpp
@@ -0,0 +1,84 @@
+/*
+Copyright 2013 Aurélien Gâteau <agateau@kde.org>
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) version 3, or any
+later version accepted by the membership of KDE e.V. (or its
+successor approved by the membership of KDE e.V.), which shall
+act as a proxy defined in Section 6 of version 3 of the license.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#include <ktranscripttest.h>
+
+#include <QtTest/QtTest>
+
+
+#include <ktranscript_p.h>
+
+QTEST_MAIN(KTranscriptTest)
+
+extern "C"
+{
+    typedef KTranscript *(*InitFunc)();
+}
+
+KTranscriptTest::KTranscriptTest()
+: m_transcript(0)
+{
+}
+
+// FIXME: Duplicated from klocalizedstring.cpp. Should be shared.
+static QString findKTranscriptPlugin()
+{
+    QStringList nameFilters;
+    QString pluginSubdir = QLatin1String("kf5");
+    QString pluginName = QLatin1String("ktranscript");
+    nameFilters.append(pluginName + QLatin1String(".*"));
+    Q_FOREACH (const QString &dirPath, QCoreApplication::libraryPaths()) {
+        QString dirPathKf = dirPath + QLatin1Char('/') + pluginSubdir;
+        if (!QDir(dirPathKf).entryList(nameFilters).isEmpty()) {
+            return dirPathKf + QLatin1Char('/') + pluginName;
+        }
+    }
+    return QString();
+}
+
+void KTranscriptTest::initTestCase()
+{
+    QString pluginPath = findKTranscriptPlugin();
+    QVERIFY(!pluginPath.isEmpty());
+
+    m_library.setFileName(pluginPath);
+}
+
+void KTranscriptTest::init()
+{
+    QVERIFY(m_library.load());
+    InitFunc initf = (InitFunc) m_library.resolve("load_transcript");
+    QVERIFY(initf);
+    m_transcript = initf();
+    QVERIFY(m_transcript);
+}
+
+void KTranscriptTest::cleanup()
+{
+    // load_transcript() returns a Q_GLOBAL_STATIC instance. The only way to
+    // ensure we get a new instance is to unload the library.
+    QVERIFY(m_library.unload());
+    m_transcript = 0;
+}
+
+void KTranscriptTest::test()
+{
+}
+
+#include <ktranscripttest.h>
diff --git a/tier2/ki18n/autotests/ktranscripttest.h b/tier2/ki18n/autotests/ktranscripttest.h
new file mode 100644
index 0000000..8ee0d3a
--- /dev/null
+++ b/tier2/ki18n/autotests/ktranscripttest.h
@@ -0,0 +1,46 @@
+/*
+Copyright 2013 Aurélien Gâteau <agateau@kde.org>
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) version 3, or any
+later version accepted by the membership of KDE e.V. (or its
+successor approved by the membership of KDE e.V.), which shall
+act as a proxy defined in Section 6 of version 3 of the license.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#ifndef KTRANSCRIPTTEST_H
+#define KTRANSCRIPTTEST_H
+
+#include <QLibrary>
+#include <QObject>
+
+class KTranscript;
+
+class KTranscriptTest : public QObject
+{
+    Q_OBJECT
+public:
+    KTranscriptTest();
+
+private Q_SLOTS:
+    void initTestCase();
+    void init();
+    void cleanup();
+
+    void test();
+
+private:
+    QLibrary m_library;
+    KTranscript *m_transcript;
+};
+
+#endif /* KTRANSCRIPTTEST_H */
-- 
1.8.1.2


From f58f3aaafc07cd84f71fb51972d8f178cbbea229 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Aur=C3=A9lien=20G=C3=A2teau?= <agateau@kde.org>
Date: Thu, 24 Oct 2013 18:21:43 +0200
Subject: [PATCH 2/2] First actual tests

---
 tier2/ki18n/autotests/ktranscripttest.cpp | 39 +++++++++++++++++++++++++++++++
 tier2/ki18n/autotests/ktranscripttest.h   |  1 +
 tier2/ki18n/autotests/test.js             |  7 ++++++
 3 files changed, 47 insertions(+)
 create mode 100644 tier2/ki18n/autotests/test.js

diff --git a/tier2/ki18n/autotests/ktranscripttest.cpp b/tier2/ki18n/autotests/ktranscripttest.cpp
index fdde23c..ff6da93 100644
--- a/tier2/ki18n/autotests/ktranscripttest.cpp
+++ b/tier2/ki18n/autotests/ktranscripttest.cpp
@@ -21,6 +21,7 @@ License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 
 #include <QtTest/QtTest>
 
+#include <QDebug>
 
 #include <ktranscript_p.h>
 
@@ -77,8 +78,46 @@ void KTranscriptTest::cleanup()
     m_transcript = 0;
 }
 
+void KTranscriptTest::test_data()
+{
+    QTest::addColumn<QVariantList>("argv");
+    QTest::addColumn<QString>("expected");
+
+    QTest::newRow("test_basic") << (QVariantList() << "test_basic" << "foo") << "foo bar";
+    QTest::newRow("test_msgtrf") << (QVariantList() << "test_msgtrf") << "ordinary";
+}
+
 void KTranscriptTest::test()
 {
+    QFETCH(QVariantList, argv);
+    QFETCH(QString, expected);
+
+    QString language = "fr";
+    QString country = "fr";
+    QString msgctxt = "msgctxt";
+    QHash<QString, QString> dynamicContext;
+    QString msgid = "msgid";
+    QStringList subs;
+    QList<QVariant> values;
+    QString ordinaryTranslation = "ordinary";
+
+    QString testJs = QFINDTESTDATA("test.js");
+    QList<QStringList> modules;
+    modules << (QStringList() << testJs << language);
+
+    QString error;
+    bool fallback;
+    QString result = m_transcript->eval(
+        argv, language, country,
+        msgctxt, dynamicContext, msgid,
+        subs, values, ordinaryTranslation,
+        modules, error, fallback);
+
+    if (!error.isEmpty()) {
+        QFAIL(qPrintable(error));
+    }
+    QVERIFY(!fallback);
+    QCOMPARE(result, expected);
 }
 
 #include <ktranscripttest.h>
diff --git a/tier2/ki18n/autotests/ktranscripttest.h b/tier2/ki18n/autotests/ktranscripttest.h
index 8ee0d3a..53f3ce0 100644
--- a/tier2/ki18n/autotests/ktranscripttest.h
+++ b/tier2/ki18n/autotests/ktranscripttest.h
@@ -36,6 +36,7 @@ private Q_SLOTS:
     void init();
     void cleanup();
 
+    void test_data();
     void test();
 
 private:
diff --git a/tier2/ki18n/autotests/test.js b/tier2/ki18n/autotests/test.js
new file mode 100644
index 0000000..ac06e27
--- /dev/null
+++ b/tier2/ki18n/autotests/test.js
@@ -0,0 +1,7 @@
+Ts.setcall("test_basic", function(arg) {
+    return arg + " bar";
+});
+
+Ts.setcall("test_msgtrf", function() {
+    return Ts.msgstrf();
+});
-- 
1.8.1.2

