File indexing completed on 2024-05-12 05:09:57
0001 /*************************************************************************** 0002 Copyright (C) 2009 Robby Stephenson <robby@periapsis.org> 0003 ***************************************************************************/ 0004 0005 /*************************************************************************** 0006 * * 0007 * This program is free software; you can redistribute it and/or * 0008 * modify it under the terms of the GNU General Public License as * 0009 * published by the Free Software Foundation; either version 2 of * 0010 * the License or (at your option) version 3 or any later version * 0011 * accepted by the membership of KDE e.V. (or its successor approved * 0012 * by the membership of KDE e.V.), which shall act as a proxy * 0013 * defined in Section 14 of version 3 of the license. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, * 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0018 * GNU General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 0022 * * 0023 ***************************************************************************/ 0024 0025 #undef QT_NO_CAST_FROM_ASCII 0026 0027 #include "entitytest.h" 0028 0029 #include "../utils/string_utils.h" 0030 #include "../utils/mapvalue.h" 0031 0032 #include <KLocalizedString> 0033 0034 #include <QTest> 0035 #include <QRegularExpression> 0036 #include <QJsonDocument> 0037 #include <QJsonObject> 0038 0039 QTEST_APPLESS_MAIN( EntityTest ) 0040 0041 #define QSL(x) QStringLiteral(x) 0042 0043 void EntityTest::initTestCase() { 0044 KLocalizedString::setApplicationDomain("tellico"); 0045 } 0046 0047 void EntityTest::testEntities() { 0048 QFETCH(QByteArray, data); 0049 QFETCH(QString, expectedString); 0050 0051 QCOMPARE(Tellico::decodeHTML(data), expectedString); 0052 } 0053 0054 void EntityTest::testEntities_data() { 0055 QTest::addColumn<QByteArray>("data"); 0056 QTest::addColumn<QString>("expectedString"); 0057 0058 QTest::newRow("robby") << QByteArray("robby") << QSL("robby"); 0059 QTest::newRow("&fake;") << QByteArray("&fake;") << QSL("&fake;"); 0060 QTest::newRow("0") << QByteArray("0") << QSL("0"); 0061 QTest::newRow("robby0robb") << QByteArray("robby0robby") << QSL("robby0robby"); 0062 } 0063 0064 void EntityTest::testAccents() { 0065 QFETCH(QString, inputString); 0066 QFETCH(QString, expectedString); 0067 0068 QCOMPARE(Tellico::removeAccents(inputString), expectedString); 0069 } 0070 0071 void EntityTest::testAccents_data() { 0072 QTest::addColumn<QString>("inputString"); 0073 QTest::addColumn<QString>("expectedString"); 0074 0075 QTest::newRow("robby") << QSL("robby") << QSL("robby"); 0076 QTest::newRow("jose") << QSL("José Guzmán") << QSL("Jose Guzman"); 0077 QTest::newRow("inarritu") << QSL("Alejandro González Iñárritu") << QSL("Alejandro Gonzalez Inarritu"); 0078 QTest::newRow("harakiri") << QSL("'Shitsurakuen': jôbafuku onna harakiri") << QSL("'Shitsurakuen': jobafuku onna harakiri"); 0079 QTest::newRow("svet") << QSL("Tmavomodrý Svět") << QSL("Tmavomodry Svet"); 0080 QTest::newRow("russian") << QSL("Возвращение Супермена") << QSL("Возвращение Супермена"); 0081 QTest::newRow("chinese") << QSL("湖南科学技术出版社") << QSL("湖南科学技术出版社"); 0082 } 0083 0084 void EntityTest::testI18nReplace() { 0085 QFETCH(QString, inputString); 0086 QFETCH(QString, expectedString); 0087 0088 QCOMPARE(Tellico::i18nReplace(inputString), expectedString); 0089 } 0090 0091 void EntityTest::testI18nReplace_data() { 0092 QTest::addColumn<QString>("inputString"); 0093 QTest::addColumn<QString>("expectedString"); 0094 0095 QTest::newRow("robby") << QSL("robby") << QSL("robby"); 0096 QTest::newRow("basic1") << QSL("<i18n>robby</i18n>") << QSL("robby"); 0097 QTest::newRow("basic2") << QSL("<i18n>robby davy</i18n>") << QSL("robby davy"); 0098 QTest::newRow("basic3") << QSL("\n <i18n>robby</i18n> \n davy\n") << QSL("robby davy\n"); 0099 // KDE bug 254863 0100 QTest::newRow("bug254863") << QSL("<i18n>Cer&ca</i18n>") << QSL("Cer&ca"); 0101 QTest::newRow("multiple") << QSL("<i18n>robby</i18n> davy <i18n>jason</i18n>") << QSL("robby davy jason"); 0102 QTest::newRow("bracket") << QSL("<i18n>robby <robby></i18n>") << QSL("robby <robby>"); 0103 } 0104 0105 void EntityTest::testMinutes() { 0106 QFETCH(int, seconds); 0107 QFETCH(QString, minutesString); 0108 0109 QCOMPARE(Tellico::minutes(seconds), minutesString); 0110 } 0111 0112 void EntityTest::testMinutes_data() { 0113 QTest::addColumn<int>("seconds"); 0114 QTest::addColumn<QString>("minutesString"); 0115 0116 QTest::newRow("1") << 1 << QSL("0:01"); 0117 QTest::newRow("60") << 60 << QSL("1:00"); 0118 QTest::newRow("600") << 600 << QSL("10:00"); 0119 QTest::newRow("0") << 0 << QSL("0:00"); 0120 QTest::newRow("120") << 120 << QSL("2:00"); 0121 } 0122 0123 void EntityTest::testObfuscate() { 0124 QString s(QStringLiteral("1q!Q+=% f")); 0125 QByteArray b = Tellico::obfuscate(s); 0126 // qDebug() << s << b; 0127 QCOMPARE(s, Tellico::reverseObfuscate(b)); 0128 } 0129 0130 void EntityTest::testControlCodes() { 0131 QFETCH(QString, string); 0132 QFETCH(QString, result); 0133 0134 QCOMPARE(Tellico::removeControlCodes(string), result); 0135 } 0136 0137 void EntityTest::testControlCodes_data() { 0138 QTest::addColumn<QString>("string"); 0139 QTest::addColumn<QString>("result"); 0140 0141 QTest::newRow("basic test") << QSL("basic test") << QSL("basic test"); 0142 QTest::newRow("basic test1") << (QSL("basic test") + QChar(1)) << QSL("basic test"); 0143 QTest::newRow("nl") << QSL("new\nline") << QSL("new\nline"); 0144 QTest::newRow("cr") << QSL("new\rline") << QSL("new\rline"); 0145 QTest::newRow("tab") << QSL("new\ttab") << QSL("new\ttab"); 0146 QTest::newRow("hex5") << QSL("hex5\x5") << QSL("hex5"); 0147 QTest::newRow("hexD") << QSL("hexD\xD") << QSL("hexD\xD"); 0148 } 0149 0150 void EntityTest::testBug254863() { 0151 QString input(QLatin1String("\\n<div>\\n\\n<i18n>check&replace</i18n></div>\\n")); 0152 auto output = Tellico::i18nReplace(input); 0153 QVERIFY(!output.contains(QLatin1String("i18n>"))); 0154 QVERIFY(!output.contains(QLatin1String("&replace"))); 0155 QVERIFY(!output.contains(QRegularExpression(QLatin1String("\\s$")))); 0156 } 0157 0158 void EntityTest::testMapValue() { 0159 auto data(QByteArrayLiteral(R"( 0160 { 0161 "num": 10, 0162 "item":"value", 0163 "items": [ 0164 {"title":"title"}, 0165 {"title":"title2"} 0166 ], 0167 "chain": { 0168 "chain2": { 0169 "chain3": { 0170 "chain4":"value" 0171 } 0172 } 0173 }, 0174 "list":["value1", "value2", "value3"], 0175 "list2":{"list":["value1", "value2", "value3"]} 0176 } 0177 )")); 0178 QJsonDocument doc = QJsonDocument::fromJson(data); 0179 QVERIFY(!doc.isNull()); 0180 auto map = doc.object().toVariantMap(); 0181 QCOMPARE(Tellico::mapValue(map, "num"), QStringLiteral("10")); 0182 QCOMPARE(Tellico::mapValue(map, "item"), QStringLiteral("value")); 0183 QCOMPARE(Tellico::mapValue(map, "items", "title"), QStringLiteral("title; title2")); 0184 QCOMPARE(Tellico::mapValue(map, "chain", "chain2", "chain3", "chain4"), QStringLiteral("value")); 0185 QCOMPARE(Tellico::mapValue(map, "list"), QStringLiteral("value1; value2; value3")); 0186 QCOMPARE(Tellico::mapValue(map, "list2", "list"), QStringLiteral("value1; value2; value3")); 0187 }