File indexing completed on 2024-04-28 15:19:33

0001 /*  This file is part of the KDE libraries
0002     SPDX-FileCopyrightText: 2010 Canonical Ltd
0003     SPDX-FileContributor: Aurélien Gâteau <aurelien.gateau@canonical.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 #include "test_kconfigutils.h"
0008 
0009 #include <qdebug.h>
0010 #include <qtest.h>
0011 
0012 // Local
0013 #include "kconfigutils.h"
0014 
0015 QTEST_GUILESS_MAIN(TestKConfigUtils)
0016 
0017 void TestKConfigUtils::testParseGroupString_data()
0018 {
0019     QTest::addColumn<QString>("input");
0020     QTest::addColumn<QStringList>("expected");
0021     QTest::addColumn<bool>("expectedOk");
0022 
0023     QTest::newRow("simple-group") << " group  " << (QStringList() << QStringLiteral("group")) << true;
0024 
0025     QTest::newRow("sub-group") << "[group][sub]" << (QStringList() << QStringLiteral("group") << QStringLiteral("sub")) << true;
0026 
0027     QTest::newRow("crazy-sub-group") << "[a\\ttab\\x5d[and some hex esc\\x61pe]"
0028                                      << (QStringList() << QStringLiteral("a\ttab") << QStringLiteral("and some hex escape")) << true;
0029 
0030     QTest::newRow("missing-closing-brace") << "[group][sub" << QStringList() << false;
0031 
0032     QTest::newRow("invalid-escape-string") << "[a\\z]" << QStringList() << false;
0033 }
0034 
0035 void TestKConfigUtils::testParseGroupString()
0036 {
0037     QFETCH(QString, input);
0038     QFETCH(QStringList, expected);
0039     QFETCH(bool, expectedOk);
0040 
0041     bool ok;
0042     QString error;
0043     QStringList output = KConfigUtils::parseGroupString(input, &ok, &error);
0044     QCOMPARE(output, expected);
0045     QCOMPARE(ok, expectedOk);
0046     if (ok) {
0047         QVERIFY(error.isEmpty());
0048     } else {
0049         QVERIFY(!error.isEmpty());
0050         qDebug() << error;
0051     }
0052 }
0053 
0054 void TestKConfigUtils::testUnescapeString_data()
0055 {
0056     QTest::addColumn<QString>("input");
0057     QTest::addColumn<QString>("expected");
0058     QTest::addColumn<bool>("expectedOk");
0059 
0060     QTest::newRow("plain") << "Some text"
0061                            << "Some text" << true;
0062 
0063     QTest::newRow("single-char-escapes") << "01\\s23\\t45\\n67\\r89\\\\"
0064                                          << "01 23\t45\n67\r89\\" << true;
0065 
0066     QTest::newRow("hex-escapes") << "kd\\x65"
0067                                  << "kde" << true;
0068 
0069     QTest::newRow("unfinished-hex-escape") << "kd\\x6"
0070                                            << "" << false;
0071 
0072     QTest::newRow("invalid-hex-escape") << "kd\\xzz"
0073                                         << "" << false;
0074 
0075     QTest::newRow("invalid-escape-sequence") << "Foo\\a"
0076                                              << "" << false;
0077 
0078     QTest::newRow("unfinished-escape-sequence") << "Foo\\"
0079                                                 << "" << false;
0080 }
0081 
0082 void TestKConfigUtils::testUnescapeString()
0083 {
0084     QFETCH(QString, input);
0085     QFETCH(QString, expected);
0086     QFETCH(bool, expectedOk);
0087 
0088     bool ok;
0089     QString error;
0090     QString output = KConfigUtils::unescapeString(input, &ok, &error);
0091     QCOMPARE(output, expected);
0092     QCOMPARE(ok, expectedOk);
0093     if (ok) {
0094         QVERIFY(error.isEmpty());
0095     } else {
0096         QVERIFY(!error.isEmpty());
0097         qDebug() << error;
0098     }
0099 }
0100 
0101 #include "moc_test_kconfigutils.cpp"