File indexing completed on 2024-12-22 04:28:22

0001 /*
0002   SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "voskspeechtotextinfotest.h"
0008 #include "voskspeechtotextinfo.h"
0009 #include <QJsonDocument>
0010 #include <QTest>
0011 QTEST_GUILESS_MAIN(VoskSpeechToTextInfoTest)
0012 VoskSpeechToTextInfoTest::VoskSpeechToTextInfoTest(QObject *parent)
0013     : QObject{parent}
0014 {
0015 }
0016 
0017 void VoskSpeechToTextInfoTest::shouldHaveDefaultValues()
0018 {
0019     VoskSpeechToTextInfo w;
0020     QVERIFY(w.identifier().isEmpty());
0021     QVERIFY(w.langText().isEmpty());
0022     QVERIFY(w.md5().isEmpty());
0023     QVERIFY(w.version().isEmpty());
0024     QVERIFY(w.url().isEmpty());
0025     QCOMPARE(w.size(), 0);
0026     QVERIFY(w.name().isEmpty());
0027     QVERIFY(w.type().isEmpty());
0028     QVERIFY(!w.obsolete());
0029 }
0030 
0031 void VoskSpeechToTextInfoTest::shouldParseJson_data()
0032 {
0033     QTest::addColumn<QString>("fileName");
0034     QTest::addColumn<VoskSpeechToTextInfo>("result");
0035     QTest::addColumn<bool>("isValid");
0036     QTest::newRow("empty") << QStringLiteral("empty") << VoskSpeechToTextInfo() << false;
0037     VoskSpeechToTextInfo info;
0038     info.setLangText(QStringLiteral("Ukrainian"));
0039     info.setIdentifier(QStringLiteral("ua"));
0040     info.setMd5(QStringLiteral("138fb6e39f858619527030f064c0a8fc"));
0041     info.setObsolete(false);
0042     info.setSize(371048965);
0043     info.setName(QStringLiteral("vosk-model-uk-v3"));
0044     info.setType(QStringLiteral("big"));
0045     info.setUrl(QStringLiteral("https://alphacephei.com/vosk/models/vosk-model-uk-v3.zip"));
0046     info.setVersion(QStringLiteral("v3"));
0047     QTest::newRow("test1") << QStringLiteral("test1") << info << true;
0048 }
0049 
0050 void VoskSpeechToTextInfoTest::shouldParseJson()
0051 {
0052     QFETCH(QString, fileName);
0053     QFETCH(VoskSpeechToTextInfo, result);
0054     QFETCH(bool, isValid);
0055     const QString originalJsonFile = QLatin1String(VOSK_DATA_DIR) + QStringLiteral("/info/") + fileName + QStringLiteral(".json");
0056     QFile f(originalJsonFile);
0057     QVERIFY(f.open(QIODevice::ReadOnly));
0058     const QByteArray content = f.readAll();
0059     f.close();
0060     const QJsonDocument doc = QJsonDocument::fromJson(content);
0061     const QJsonObject fields = doc.object();
0062     VoskSpeechToTextInfo parser;
0063     parser.parse(fields);
0064     const bool compare = (parser == result);
0065     if (!compare) {
0066         qDebug() << " Parser " << parser;
0067         qDebug() << " result " << result;
0068     }
0069     QVERIFY(compare);
0070     QCOMPARE(parser.isValid(), isValid);
0071 }
0072 
0073 #include "moc_voskspeechtotextinfotest.cpp"