File indexing completed on 2024-11-17 03:42:09

0001 /*
0002   This file is part of the KTextTemplate library
0003 
0004   SPDX-FileCopyrightText: 2009, 2010 Stephen Kelly <steveire@gmail.com>
0005 
0006   SPDX-License-Identifier: LGPL-2.1-or-later
0007 
0008 */
0009 
0010 #ifndef SAFESTRINGTEST_H
0011 #define SAFESTRINGTEST_H
0012 
0013 #include <QObject>
0014 #include <QTest>
0015 
0016 #include "safestring.h"
0017 
0018 using namespace KTextTemplate;
0019 
0020 class TestSafeString : public QObject
0021 {
0022     Q_OBJECT
0023 private Q_SLOTS:
0024     void testCombining();
0025     void testAppending();
0026     void testChopping();
0027     void testReplacing();
0028 };
0029 
0030 void TestSafeString::testCombining()
0031 {
0032     auto string1 = QStringLiteral("this & that");
0033     auto string2 = QStringLiteral(" over & under");
0034     SafeString safeString1unsafe(string1);
0035     SafeString safeString2unsafe(string2);
0036     SafeString safeString1safe(string1, SafeString::IsSafe);
0037     SafeString safeString2safe(string2, SafeString::IsSafe);
0038 
0039     SafeString combined;
0040     combined = safeString1safe + safeString2safe;
0041     QVERIFY(combined.isSafe());
0042     combined = safeString1safe + safeString1unsafe;
0043     QVERIFY(!combined.isSafe());
0044     combined = safeString1safe + string1;
0045     QVERIFY(!combined.isSafe());
0046     combined = safeString1unsafe + safeString1safe;
0047     QVERIFY(!combined.isSafe());
0048     combined = safeString1unsafe + safeString2unsafe;
0049     QVERIFY(!combined.isSafe());
0050     combined = safeString1unsafe + string1;
0051     QVERIFY(!combined.isSafe());
0052 }
0053 
0054 void TestSafeString::testAppending()
0055 {
0056     auto string1 = QStringLiteral("this & that");
0057     SafeString safeString1unsafe(string1);
0058     SafeString safeString1safe(string1, SafeString::IsSafe);
0059     SafeString safeString2safe(string1, SafeString::IsSafe);
0060 
0061     SafeString result;
0062     result = safeString1safe.get().append(safeString1safe);
0063     QVERIFY(safeString1safe.isSafe());
0064     QVERIFY(result.isSafe());
0065     QCOMPARE(result.get(), safeString1safe.get());
0066     result = safeString1safe.get().append(string1);
0067     QVERIFY(!safeString1safe.isSafe());
0068     QVERIFY(!result.isSafe());
0069     QCOMPARE(result.get(), safeString1safe.get());
0070     result = safeString1unsafe.get().append(string1);
0071     QVERIFY(!safeString1unsafe.isSafe());
0072     QVERIFY(!result.isSafe());
0073     QCOMPARE(result.get(), safeString1unsafe.get());
0074 }
0075 
0076 void TestSafeString::testChopping()
0077 {
0078     auto string1 = QStringLiteral("this & that");
0079     SafeString safeString1unsafe(string1);
0080     SafeString safeString1safe(string1, SafeString::IsSafe);
0081 
0082     safeString1safe.get().chop(4);
0083     QVERIFY(!safeString1safe.isSafe());
0084     QCOMPARE(QStringLiteral("this & "), QString(safeString1safe.get()));
0085     safeString1unsafe.get().chop(4);
0086     QVERIFY(!safeString1unsafe.isSafe());
0087     QCOMPARE(QStringLiteral("this & "), QString(safeString1unsafe.get()));
0088 }
0089 
0090 void TestSafeString::testReplacing()
0091 {
0092     auto string1 = QStringLiteral("x&\ny");
0093     SafeString safeString1safe(string1, SafeString::IsSafe);
0094 
0095     auto result = safeString1safe.get().replace(QLatin1Char('\n'), QStringLiteral("<br />"));
0096     QVERIFY(!result.isSafe());
0097     QVERIFY(!safeString1safe.isSafe());
0098     QCOMPARE(result.get(), safeString1safe.get());
0099 }
0100 
0101 QTEST_MAIN(TestSafeString)
0102 #include "testsafestring.moc"
0103 
0104 #endif