File indexing completed on 2024-11-24 04:16:54

0001 /*
0002   SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "translatortest.h"
0008 #include "translator.h"
0009 #include <QJsonDocument>
0010 #include <QTest>
0011 QTEST_GUILESS_MAIN(TranslatorTest)
0012 TranslatorTest::TranslatorTest(QObject *parent)
0013     : QObject{parent}
0014 {
0015 }
0016 
0017 void TranslatorTest::shouldHaveDefaultValues()
0018 {
0019     Translator w;
0020     QVERIFY(w.shortName().isEmpty());
0021     QVERIFY(w.modelName().isEmpty());
0022     QVERIFY(w.source().isEmpty());
0023     QVERIFY(w.checkSum().isEmpty());
0024     QVERIFY(w.target().isEmpty());
0025     QVERIFY(w.url().isEmpty());
0026     QVERIFY(w.repository().isEmpty());
0027     QVERIFY(w.type().isEmpty());
0028     QCOMPARE(w.version(), -1);
0029     QCOMPARE(w.api(), -1);
0030     QVERIFY(!w.isValid());
0031     QVERIFY(w.remote());
0032 }
0033 
0034 void TranslatorTest::shouldParseJson_data()
0035 {
0036     QTest::addColumn<QString>("fileName");
0037     QTest::addColumn<Translator>("result");
0038     QTest::addColumn<bool>("isValid");
0039     QTest::newRow("empty") << QStringLiteral("empty") << Translator() << false;
0040 }
0041 
0042 void TranslatorTest::shouldParseJson()
0043 {
0044     QFETCH(QString, fileName);
0045     QFETCH(Translator, result);
0046     QFETCH(bool, isValid);
0047     const QString originalJsonFile = QLatin1String(BERGAMOT_DATA_DIR) + QStringLiteral("/translator/") + fileName + QStringLiteral(".json");
0048     QFile f(originalJsonFile);
0049     QVERIFY(f.open(QIODevice::ReadOnly));
0050     const QByteArray content = f.readAll();
0051     f.close();
0052     const QJsonDocument doc = QJsonDocument::fromJson(content);
0053     const QJsonObject fields = doc.object();
0054     Translator parser;
0055     parser.parse(fields);
0056     const bool compare = (parser == result);
0057     if (!compare) {
0058         qDebug() << " Parser " << parser;
0059         qDebug() << " result " << result;
0060     }
0061     QVERIFY(compare);
0062     QCOMPARE(parser.isValid(), isValid);
0063 }
0064 #include "moc_translatortest.cpp"