File indexing completed on 2024-07-21 12:06:18

0001 /*
0002     SPDX-FileCopyrightText: 2021 Friedrich W. H. Kossebau <kossebau@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <kusasciitextcodec.h>
0008 // Qt
0009 #include <QTest>
0010 
0011 class KUsAsciiTextCodecTest : public QObject
0012 {
0013     Q_OBJECT
0014 
0015 private Q_SLOTS:
0016     void initTestCase();
0017 
0018     void testBrokenBuiltinEncoding();
0019     void testEncoding();
0020     void testDecoding();
0021 
0022 private:
0023     QTextCodec *qtCodec = nullptr;
0024     QTextCodec *ourCodec = nullptr;
0025 };
0026 
0027 void KUsAsciiTextCodecTest::initTestCase()
0028 {
0029     // default coming from Qt, if built with ICU
0030     qtCodec = QTextCodec::codecForName("US-ASCII");
0031 
0032     // QTextCodec constructor of the baseclass registers the instance, also for ownership.
0033     // By the previous query for a US-ASCII codec the default one though
0034     // is cached for further look-ups, so we have to remember our own explicitely.
0035     ourCodec = new KUsAsciiTextCodec;
0036 }
0037 
0038 void KUsAsciiTextCodecTest::testBrokenBuiltinEncoding()
0039 {
0040     if (!qtCodec) {
0041         QSKIP("Qt without built-in US-ASCII codec");
0042     }
0043 
0044     const QString failUnicodeText = QStringLiteral("Testnäme");
0045 
0046     QTextCodec::ConverterState failConverterState;
0047 
0048     // Only interested in what failConverterState will be changed to
0049     qtCodec->fromUnicode(failUnicodeText.constData(), failUnicodeText.length(), &failConverterState);
0050 
0051     // Bug seems fixed in Qt6
0052 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 7)
0053     // confirm the broken result 0, which rather should be 1
0054     QCOMPARE(failConverterState.invalidChars, 1);
0055 #else
0056     QCOMPARE(failConverterState.invalidChars, 0);
0057 #endif
0058 }
0059 
0060 void KUsAsciiTextCodecTest::testEncoding()
0061 {
0062     // compatible text
0063     const QString successUnicodeText = QStringLiteral("Testname");
0064 
0065     QTextCodec::ConverterState successConverterState;
0066 
0067     const QByteArray successEncoded8Bit = ourCodec->fromUnicode(successUnicodeText.constData(), successUnicodeText.length(), &successConverterState);
0068 
0069     const QByteArray successExpected8Bit = QByteArrayLiteral("Testname");
0070     QCOMPARE(successConverterState.invalidChars, 0);
0071     QCOMPARE(successEncoded8Bit, successExpected8Bit);
0072 
0073     // incompatible text
0074     const QString failUnicodeText = QStringLiteral("Testnäme");
0075 
0076     QTextCodec::ConverterState failConverterState;
0077 
0078     const QByteArray failEncoded8Bit = ourCodec->fromUnicode(failUnicodeText.constData(), failUnicodeText.length(), &failConverterState);
0079 
0080     const QByteArray failExpected8Bit = QByteArrayLiteral("Testn?me");
0081     QCOMPARE(failConverterState.invalidChars, 1);
0082     QCOMPARE(failEncoded8Bit, failExpected8Bit);
0083 }
0084 
0085 void KUsAsciiTextCodecTest::testDecoding()
0086 {
0087     // compatible text
0088     const QByteArray success8BitString = QByteArrayLiteral("Testname");
0089 
0090     QTextCodec::ConverterState successConverterState;
0091 
0092     const QString successUnicodeString = ourCodec->toUnicode(success8BitString.constData(), success8BitString.length(), &successConverterState);
0093 
0094     const QString successExpectedString = QStringLiteral("Testname");
0095     QCOMPARE(successConverterState.invalidChars, 0);
0096     QCOMPARE(successUnicodeString, successExpectedString);
0097 
0098     // incompatible text, with "ä" in latin1
0099     /* clang-format off */
0100     const QByteArray fail8BitString = QByteArrayLiteral("Testn""\xE4""me");
0101     /* clang-format on */
0102 
0103     QTextCodec::ConverterState failConverterState;
0104 
0105     const QString failUnicodeString = ourCodec->toUnicode(fail8BitString.constData(), fail8BitString.length(), &failConverterState);
0106 
0107     const QString failExpectedString = QStringLiteral("Testn?me");
0108     QCOMPARE(failConverterState.invalidChars, 1);
0109     QCOMPARE(failUnicodeString, failExpectedString);
0110 }
0111 
0112 QTEST_GUILESS_MAIN(KUsAsciiTextCodecTest)
0113 
0114 #include "kusasciitextcodectest.moc"