File indexing completed on 2024-04-21 03:57:15

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 "scriptdocument_test.h"
0009 
0010 #include "ktexteditor/cursor.h"
0011 #include <katedocument.h>
0012 #include <kateglobal.h>
0013 #include <katescriptdocument.h>
0014 #include <ktexteditor/view.h>
0015 
0016 #include <QJSEngine>
0017 #include <QTest>
0018 
0019 QTEST_MAIN(ScriptDocumentTest)
0020 
0021 QtMessageHandler ScriptDocumentTest::s_msgHandler = nullptr;
0022 
0023 void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
0024 {
0025     switch (type) {
0026     case QtDebugMsg:
0027         /* do nothing */
0028         break;
0029     default:
0030         ScriptDocumentTest::s_msgHandler(type, context, msg);
0031     }
0032 }
0033 
0034 void ScriptDocumentTest::initTestCase()
0035 {
0036     KTextEditor::EditorPrivate::enableUnitTestMode();
0037     s_msgHandler = qInstallMessageHandler(myMessageOutput);
0038 }
0039 
0040 void ScriptDocumentTest::cleanupTestCase()
0041 {
0042     qInstallMessageHandler(nullptr);
0043 }
0044 
0045 ScriptDocumentTest::ScriptDocumentTest()
0046     : QObject()
0047 {
0048 }
0049 
0050 ScriptDocumentTest::~ScriptDocumentTest()
0051 {
0052 }
0053 
0054 void ScriptDocumentTest::init()
0055 {
0056     m_doc = new KTextEditor::DocumentPrivate;
0057     m_view = m_doc->createView(nullptr);
0058     m_scriptDoc = new KateScriptDocument(nullptr, this);
0059     m_scriptDoc->setDocument(m_doc);
0060 }
0061 
0062 void ScriptDocumentTest::cleanup()
0063 {
0064     delete m_scriptDoc;
0065     delete m_view;
0066     delete m_doc;
0067 }
0068 
0069 #if 0
0070 void ScriptDocumentTest::testRfind_data()
0071 {
0072     QTest::addColumn<KTextEditor::Range>("searchRange");
0073     QTest::addColumn<KTextEditor::Range>("expectedResult");
0074 
0075     QTest::newRow("") << KTextEditor::Range(0, 0, 1, 10) << KTextEditor::Range(1,  6, 1, 10);
0076     QTest::newRow("") << KTextEditor::Range(0, 0, 1,  5) << KTextEditor::Range(1,  0, 1,  4);
0077     QTest::newRow("") << KTextEditor::Range(0, 0, 1,  0) << KTextEditor::Range(0, 10, 0, 14);
0078 }
0079 
0080 void ScriptDocumentTest::testRfind()
0081 {
0082     QFETCH(KTextEditor::Range, searchRange);
0083     QFETCH(KTextEditor::Range, expectedResult);
0084 
0085     m_doc->setText("aaaa aaaa aaaa\n"
0086                    "aaaa  aaaa");
0087 
0088     QCOMPARE(m_search->search(searchRange, "aaaa", true), expectedResult);
0089 }
0090 #endif
0091 
0092 void ScriptDocumentTest::testRfind_data()
0093 {
0094     QTest::addColumn<KTextEditor::Cursor>("searchStart");
0095     QTest::addColumn<KTextEditor::Cursor>("result");
0096 
0097     QTest::newRow("a a a a a a a a a a a a|") << KTextEditor::Cursor(0, 23) << KTextEditor::Cursor(0, 18);
0098     QTest::newRow("a a a a a a a a a a a |a") << KTextEditor::Cursor(0, 22) << KTextEditor::Cursor(0, 16);
0099     QTest::newRow("a a a a| a a a a a a a a") << KTextEditor::Cursor(0, 7) << KTextEditor::Cursor(0, 2);
0100     QTest::newRow("a a a |a a a a a a a a a") << KTextEditor::Cursor(0, 6) << KTextEditor::Cursor(0, 0);
0101     QTest::newRow("a a a| a a a a a a a a a") << KTextEditor::Cursor(0, 5) << KTextEditor::Cursor(0, 0);
0102     QTest::newRow("a a |a a a a a a a a a a") << KTextEditor::Cursor(0, 4) << KTextEditor::Cursor::invalid();
0103 }
0104 
0105 void ScriptDocumentTest::testRfind()
0106 {
0107     QFETCH(KTextEditor::Cursor, searchStart);
0108     QFETCH(KTextEditor::Cursor, result);
0109 
0110     m_scriptDoc->setText(QStringLiteral("a a a a a a a a a a a a"));
0111 
0112     KTextEditor::Cursor cursor = m_scriptDoc->rfind(searchStart, QStringLiteral("a a a"));
0113     QCOMPARE(cursor, result);
0114 }
0115 
0116 #include "moc_scriptdocument_test.cpp"