File indexing completed on 2024-07-14 14:27:10

0001 /*
0002     SPDX-FileCopyrightText: 2010 Thomas McGuire <mcguire@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "codectest.h"
0007 
0008 #include <QTest>
0009 
0010 #include <QDir>
0011 
0012 #include "../src/kcodecs.h"
0013 
0014 using namespace KCodecs;
0015 
0016 QTEST_MAIN(CodecTest)
0017 
0018 enum Mode {
0019     Decode,
0020     Encode,
0021 };
0022 Q_DECLARE_METATYPE(Mode)
0023 
0024 void CodecTest::testCodecs_data()
0025 {
0026     QTest::addColumn<QByteArray>("input");
0027     QTest::addColumn<QByteArray>("expResult");
0028     QTest::addColumn<QByteArray>("codecName");
0029     QTest::addColumn<QString>("tag");
0030     QTest::addColumn<Mode>("mode");
0031 
0032     QDir codecBaseDir(QFINDTESTDATA("data"));
0033     const QStringList lst = codecBaseDir.entryList(QStringList(), QDir::Dirs | QDir::NoDotAndDotDot, QDir::NoSort);
0034     for (const QString &dir : lst) {
0035         if (dir.toLower().startsWith(QLatin1String("codec_"))) {
0036             const QString codecName = dir.right(dir.size() - 6);
0037             QDir codecDir(codecBaseDir.path() + QLatin1String("/") + dir);
0038             const QStringList lst2 = codecDir.entryList(QStringList(), QDir::Files, QDir::NoSort);
0039             for (const QString &file : lst2) {
0040                 if (file.toLower().endsWith(QLatin1String(".expected"))) {
0041                     const QString dataFileNameBase = file.left(file.size() - 9);
0042                     QFile dataFile(codecDir.path() + QLatin1Char('/') + dataFileNameBase);
0043                     QFile expectedFile(codecDir.path() + QLatin1Char('/') + file);
0044                     QVERIFY(dataFile.open(QIODevice::ReadOnly));
0045                     QVERIFY(expectedFile.open(QIODevice::ReadOnly));
0046 
0047                     Mode mode = Decode;
0048                     if (file.contains(QLatin1String("-decode"))) {
0049                         mode = Decode;
0050                     } else if (file.contains(QLatin1String("-encode"))) {
0051                         mode = Encode;
0052                     }
0053 
0054                     const QByteArray data = dataFile.readAll();
0055                     const QByteArray expected = expectedFile.readAll();
0056 
0057                     const QString tag = codecName + QLatin1Char('/') + dataFileNameBase;
0058                     QTest::newRow(tag.toLatin1().constData()) << data << expected << codecName.toLatin1() << tag << mode;
0059 
0060                     dataFile.close();
0061                     expectedFile.close();
0062                 }
0063             }
0064         }
0065     }
0066 }
0067 
0068 void CodecTest::testCodecs()
0069 {
0070     QFETCH(QByteArray, input);
0071     QFETCH(QByteArray, expResult);
0072     QFETCH(QByteArray, codecName);
0073     QFETCH(QString, tag);
0074     QFETCH(Mode, mode);
0075 
0076     Codec *codec = Codec::codecForName(codecName);
0077     QVERIFY(codec);
0078 
0079     QStringList blacklistedTags;
0080     if (blacklistedTags.contains(tag)) {
0081         QEXPECT_FAIL(tag.toLatin1().constData(), "Codec broken", Continue);
0082     }
0083 
0084     QByteArray result;
0085     if (mode == Decode) {
0086         result = codec->decode(input, Codec::NewlineLF);
0087     } else {
0088         result = codec->encode(input, Codec::NewlineLF);
0089     }
0090 
0091     // More usable version of QCOMPARE(result, expResult), in case the difference is at the end...
0092     if (result != expResult) {
0093         const QList<QByteArray> lines = result.split('\n');
0094         const QList<QByteArray> expLines = expResult.split('\n');
0095         if (lines.count() == expLines.count()) {
0096             QCOMPARE(result.split('\n'), expResult.split('\n'));
0097         }
0098     }
0099     QCOMPARE(result, expResult);
0100 }
0101 
0102 #include "moc_codectest.cpp"