File indexing completed on 2024-07-14 03:55:43

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2010 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "plaintextsearch_test.h"
0009 #include "moc_plaintextsearch_test.cpp"
0010 
0011 #include <katedocument.h>
0012 #include <kateglobal.h>
0013 #include <kateplaintextsearch.h>
0014 
0015 #include <QTest>
0016 
0017 QTEST_MAIN(PlainTextSearchTest)
0018 
0019 QtMessageHandler PlainTextSearchTest::s_msgHandler = nullptr;
0020 
0021 void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
0022 {
0023     switch (type) {
0024     case QtDebugMsg:
0025         /* do nothing */
0026         break;
0027     default:
0028         PlainTextSearchTest::s_msgHandler(type, context, msg);
0029     }
0030 }
0031 
0032 void PlainTextSearchTest::initTestCase()
0033 {
0034     KTextEditor::EditorPrivate::enableUnitTestMode();
0035     s_msgHandler = qInstallMessageHandler(myMessageOutput);
0036 }
0037 
0038 void PlainTextSearchTest::cleanupTestCase()
0039 {
0040     qInstallMessageHandler(nullptr);
0041 }
0042 
0043 PlainTextSearchTest::PlainTextSearchTest()
0044     : QObject()
0045 {
0046 }
0047 
0048 PlainTextSearchTest::~PlainTextSearchTest()
0049 {
0050 }
0051 
0052 void PlainTextSearchTest::init()
0053 {
0054     m_doc = new KTextEditor::DocumentPrivate(false, false, nullptr, this);
0055     m_search = new KatePlainTextSearch(m_doc, Qt::CaseSensitive, false);
0056 }
0057 
0058 void PlainTextSearchTest::cleanup()
0059 {
0060     delete m_search;
0061     delete m_doc;
0062 }
0063 
0064 void PlainTextSearchTest::testSearchBackward_data()
0065 {
0066     QTest::addColumn<KTextEditor::Range>("searchRange");
0067     QTest::addColumn<KTextEditor::Range>("expectedResult");
0068 
0069     QTest::newRow("") << KTextEditor::Range(0, 0, 1, 10) << KTextEditor::Range(1, 6, 1, 10);
0070     QTest::newRow("") << KTextEditor::Range(0, 0, 1, 5) << KTextEditor::Range(1, 0, 1, 4);
0071     QTest::newRow("") << KTextEditor::Range(0, 0, 1, 0) << KTextEditor::Range(0, 10, 0, 14);
0072 }
0073 
0074 void PlainTextSearchTest::testSearchBackward()
0075 {
0076     QFETCH(KTextEditor::Range, searchRange);
0077     QFETCH(KTextEditor::Range, expectedResult);
0078 
0079     m_doc->setText(
0080         QStringLiteral("aaaa aaaa aaaa\n"
0081                        "aaaa  aaaa"));
0082 
0083     QCOMPARE(m_search->search(QLatin1String("aaaa"), searchRange, true), expectedResult);
0084 }
0085 
0086 void PlainTextSearchTest::testSingleLineDocument_data()
0087 {
0088     QTest::addColumn<KTextEditor::Range>("searchRange");
0089     QTest::addColumn<KTextEditor::Range>("forwardResult");
0090     QTest::addColumn<KTextEditor::Range>("backwardResult");
0091 
0092     QTest::newRow("[a a a a a a a a a a a a]") << KTextEditor::Range(0, 0, 0, 23) << KTextEditor::Range(0, 0, 0, 5) << KTextEditor::Range(0, 18, 0, 23);
0093     QTest::newRow("[a a a a a a a a a a a ]a") << KTextEditor::Range(0, 0, 0, 22) << KTextEditor::Range(0, 0, 0, 5) << KTextEditor::Range(0, 16, 0, 21);
0094     QTest::newRow("a[ a a a a a a a a a a a]") << KTextEditor::Range(0, 1, 0, 23) << KTextEditor::Range(0, 2, 0, 7) << KTextEditor::Range(0, 18, 0, 23);
0095     QTest::newRow("a[ a a a a a a a a a a ]a") << KTextEditor::Range(0, 1, 0, 22) << KTextEditor::Range(0, 2, 0, 7) << KTextEditor::Range(0, 16, 0, 21);
0096     QTest::newRow("[a a a a] a a a a a a a a") << KTextEditor::Range(0, 0, 0, 7) << KTextEditor::Range(0, 0, 0, 5) << KTextEditor::Range(0, 2, 0, 7);
0097     QTest::newRow("[a a a ]a a a a a a a a a") << KTextEditor::Range(0, 0, 0, 6) << KTextEditor::Range(0, 0, 0, 5) << KTextEditor::Range(0, 0, 0, 5);
0098     QTest::newRow("[a a a] a a a a a a a a a") << KTextEditor::Range(0, 0, 0, 5) << KTextEditor::Range(0, 0, 0, 5) << KTextEditor::Range(0, 0, 0, 5);
0099     QTest::newRow("[a a ]a a a a a a a a a a") << KTextEditor::Range(0, 0, 0, 4) << KTextEditor::Range::invalid() << KTextEditor::Range::invalid();
0100     QTest::newRow("a a a a a a a a [a a a a]") << KTextEditor::Range(0, 16, 0, 23) << KTextEditor::Range(0, 16, 0, 21) << KTextEditor::Range(0, 18, 0, 23);
0101     QTest::newRow("a a a a a a a a a[ a a a]") << KTextEditor::Range(0, 17, 0, 23) << KTextEditor::Range(0, 18, 0, 23) << KTextEditor::Range(0, 18, 0, 23);
0102     QTest::newRow("a a a a a a a a a [a a a]") << KTextEditor::Range(0, 18, 0, 23) << KTextEditor::Range(0, 18, 0, 23) << KTextEditor::Range(0, 18, 0, 23);
0103     QTest::newRow("a a a a a a a a a a[ a a]") << KTextEditor::Range(0, 19, 0, 23) << KTextEditor::Range::invalid() << KTextEditor::Range::invalid();
0104     QTest::newRow("a a a a a[ a a a a] a a a") << KTextEditor::Range(0, 9, 0, 17) << KTextEditor::Range(0, 10, 0, 15) << KTextEditor::Range(0, 12, 0, 17);
0105     QTest::newRow("a a a a a[ a a] a a a a a") << KTextEditor::Range(0, 9, 0, 13) << KTextEditor::Range::invalid() << KTextEditor::Range::invalid();
0106 }
0107 
0108 void PlainTextSearchTest::testSingleLineDocument()
0109 {
0110     QFETCH(KTextEditor::Range, searchRange);
0111     QFETCH(KTextEditor::Range, forwardResult);
0112     QFETCH(KTextEditor::Range, backwardResult);
0113 
0114     m_doc->setText(QStringLiteral("a a a a a a a a a a a a"));
0115 
0116     QCOMPARE(m_search->search(QLatin1String("a a a"), searchRange, false), forwardResult);
0117     QCOMPARE(m_search->search(QLatin1String("a a a"), searchRange, true), backwardResult);
0118 }
0119 
0120 void PlainTextSearchTest::testMultilineSearch_data()
0121 {
0122     QTest::addColumn<QString>("pattern");
0123     QTest::addColumn<KTextEditor::Range>("inputRange");
0124     QTest::addColumn<KTextEditor::Range>("forwardResult");
0125 
0126     QTest::newRow("") << "a a a\na a\na a a" << KTextEditor::Range(0, 0, 2, 5) << KTextEditor::Range(0, 0, 2, 5);
0127     QTest::newRow("") << "a a a\na a\na a " << KTextEditor::Range(0, 0, 2, 5) << KTextEditor::Range(0, 0, 2, 4);
0128     QTest::newRow("") << "a a a\na a\na a" << KTextEditor::Range(0, 0, 2, 5) << KTextEditor::Range(0, 0, 2, 3);
0129     QTest::newRow("") << "a a a\na a\na" << KTextEditor::Range(0, 0, 2, 5) << KTextEditor::Range(0, 0, 2, 1);
0130     QTest::newRow("") << "a a a\na a\n" << KTextEditor::Range(0, 0, 2, 5) << KTextEditor::Range(0, 0, 2, 0);
0131     QTest::newRow("") << "a a a\na a" << KTextEditor::Range(0, 0, 2, 5) << KTextEditor::Range(0, 0, 1, 3);
0132     QTest::newRow("") << "a a\na a" << KTextEditor::Range(0, 0, 2, 5) << KTextEditor::Range(0, 2, 1, 3);
0133     QTest::newRow("") << "a a\na a\na a" << KTextEditor::Range(0, 0, 2, 5) << KTextEditor::Range(0, 2, 2, 3);
0134     QTest::newRow("") << "\na a\na a" << KTextEditor::Range(0, 0, 2, 5) << KTextEditor::Range(0, 5, 2, 3);
0135     QTest::newRow("") << "\na a\n" << KTextEditor::Range(0, 0, 2, 5) << KTextEditor::Range(0, 5, 2, 0);
0136 
0137     QTest::newRow("") << "a a a\na a\na a a" << KTextEditor::Range(0, 0, 2, 4) << KTextEditor::Range::invalid();
0138     QTest::newRow("") << "a a a\na a\na a " << KTextEditor::Range(0, 0, 2, 4) << KTextEditor::Range(0, 0, 2, 4);
0139     QTest::newRow("") << "a a a\na a\n" << KTextEditor::Range(0, 0, 2, 0) << KTextEditor::Range(0, 0, 2, 0);
0140     QTest::newRow("") << "a a a\na a\n" << KTextEditor::Range(0, 0, 1, 3) << KTextEditor::Range::invalid();
0141     QTest::newRow("") << "a a\n" << KTextEditor::Range(0, 0, 1, 3) << KTextEditor::Range(0, 2, 1, 0);
0142     QTest::newRow("") << "a \n" << KTextEditor::Range(0, 0, 1, 3) << KTextEditor::Range::invalid();
0143 }
0144 
0145 void PlainTextSearchTest::testMultilineSearch()
0146 {
0147     QFETCH(QString, pattern);
0148     QFETCH(KTextEditor::Range, inputRange);
0149     QFETCH(KTextEditor::Range, forwardResult);
0150 
0151     m_doc->setText(
0152         QStringLiteral("a a a\n"
0153                        "a a\n"
0154                        "a a a"));
0155 
0156     QCOMPARE(m_search->search(pattern, inputRange, false), forwardResult);
0157 }