File indexing completed on 2024-05-12 05:55:43

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2023 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "char8stringvalidatortest.hpp"
0010 
0011 // sut
0012 #include <char8stringvalidator.hpp>
0013 #include <char8stringparser.hpp>
0014 // Okteta core
0015 #include <Okteta/CharCodec>
0016 // Qt
0017 #include <QTest>
0018 
0019 
0020 void Char8StringValidatorTest::testValidate_data()
0021 {
0022     QTest::addColumn<QString>("string");
0023     QTest::addColumn<QString>("codecName");
0024     QTest::addColumn<QValidator::State>("expectedValidateResult");
0025     QTest::addColumn<QChar>("expectedParseResult");
0026 
0027     const QString iso8859_1 = QStringLiteral("ISO-8859-1");
0028     const QString usAscii = QStringLiteral("US-ASCII");
0029     const QChar dummyByte;
0030 
0031     QTest::newRow("emptystring")
0032         << QString() << iso8859_1 << QValidator::Intermediate << dummyByte;
0033     QTest::newRow("A-ISO-8859-1")
0034         << QStringLiteral("A") << iso8859_1 << QValidator::Acceptable << QChar::fromLatin1('A');
0035     QTest::newRow("z-ISO-8859-1")
0036         << QStringLiteral("z") << iso8859_1 << QValidator::Acceptable << QChar::fromLatin1('z');
0037     QTest::newRow("€-ISO-8859-1")
0038         << QStringLiteral("€") << iso8859_1 << QValidator::Invalid << dummyByte;
0039     QTest::newRow("AA-ISO-8859-1")
0040         << QStringLiteral("AA") << iso8859_1 << QValidator::Invalid << dummyByte;
0041     QTest::newRow("backslash-ISO-8859-1")
0042         << QStringLiteral("\\") << iso8859_1 << QValidator::Acceptable << QChar::fromLatin1('\\');
0043     QTest::newRow("newline-ISO-8859-1")
0044         << QStringLiteral("\\n") << iso8859_1 << QValidator::Acceptable << QChar::fromLatin1('\n');
0045     QTest::newRow("newline-n-ISO-8859-1")
0046         << QStringLiteral("\\nn") << iso8859_1 << QValidator::Invalid << dummyByte;
0047     QTest::newRow("newline-ISO-8859-1")
0048         << QStringLiteral("\\foo") << iso8859_1 << QValidator::Invalid << dummyByte;
0049     QTest::newRow("escaped-hex-x-US-ASCII")
0050         << QStringLiteral("\\x") << usAscii << QValidator::Intermediate << dummyByte;
0051     QTest::newRow("escaped-hex-x2-US-ASCII")
0052         << QStringLiteral("\\x2") << usAscii << QValidator::Acceptable << QChar::fromLatin1('\x02');
0053     QTest::newRow("escaped-hex-x02-US-ASCII")
0054         << QStringLiteral("\\x02") << usAscii << QValidator::Acceptable << QChar::fromLatin1('\x02');
0055     QTest::newRow("escaped-hex-x2g-US-ASCII")
0056         << QStringLiteral("\\x2g") << usAscii << QValidator::Invalid << dummyByte;
0057     QTest::newRow("escaped-hex-x20-US-ASCII")
0058         << QStringLiteral("\\x20") << usAscii << QValidator::Acceptable << QChar::fromLatin1(' ');
0059     QTest::newRow("escaped-hex-undefined-US-ASCII")
0060         << QStringLiteral("\\xFF") << usAscii << QValidator::Invalid << dummyByte;
0061     QTest::newRow("escaped-octal-0-US-ASCII")
0062         << QStringLiteral("\\0") << usAscii << QValidator::Acceptable << QChar::fromLatin1('\0');
0063     QTest::newRow("escaped-octal-40-US-ASCII")
0064         << QStringLiteral("\\40") << usAscii << QValidator::Acceptable << QChar::fromLatin1('\40');
0065     QTest::newRow("escaped-octal-040-US-ASCII")
0066         << QStringLiteral("\\040") << usAscii << QValidator::Acceptable << QChar::fromLatin1('\40');
0067     QTest::newRow("escaped-octal-undefined-US-ASCII")
0068         << QStringLiteral("\\377") << usAscii << QValidator::Invalid << dummyByte;
0069     QTest::newRow("escaped-octal-outofbounds-US-ASCII")
0070         << QStringLiteral("\\444") << usAscii << QValidator::Invalid << dummyByte;
0071 }
0072 
0073 void Char8StringValidatorTest::testValidate()
0074 {
0075     QFETCH(const QString, string);
0076     QFETCH(QString, codecName);
0077     QFETCH(const QValidator::State, expectedValidateResult);
0078     QFETCH(QChar, expectedParseResult);
0079 
0080     Okteta::CharCodec* codec = Okteta::CharCodec::createCodec(codecName);
0081 
0082     QVERIFY(codec != nullptr);
0083     QCOMPARE(codec->name(), codecName);
0084 
0085     Okteta::Char8StringParser parser(codec);
0086     Okteta::Char8StringValidator validator(&parser);
0087 
0088     int dummyCursorPos = 0;
0089     QString input(string);
0090 
0091     QValidator::State validateResult = validator.validate(input, dummyCursorPos);
0092 
0093     QCOMPARE(validateResult, expectedValidateResult);
0094 
0095     if (validateResult == QValidator::Acceptable) {
0096         QChar parseResult;
0097         parser.evaluate(&parseResult, string);
0098 
0099         QCOMPARE(parseResult, expectedParseResult);
0100     }
0101 
0102     // clean up
0103     delete codec;
0104 }
0105 
0106 QTEST_GUILESS_MAIN(Char8StringValidatorTest)
0107 
0108 #include "moc_char8stringvalidatortest.cpp"