File indexing completed on 2024-04-28 15:30:08

0001 /*
0002     This file is part of the KDE libraries
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "cursorwords_test.h"
0008 
0009 #include <kateconfig.h>
0010 #include <katedocument.h>
0011 #include <kateglobal.h>
0012 #include <kateview.h>
0013 
0014 #include <QTest>
0015 
0016 QTEST_MAIN(CursorWordsTest)
0017 
0018 using namespace KTextEditor;
0019 
0020 struct DocAndView {
0021     DocumentPrivate *doc;
0022     ViewPrivate *view;
0023 
0024     ~DocAndView()
0025     {
0026         delete view;
0027         delete doc;
0028     }
0029 };
0030 
0031 DocAndView createDocAndView(const QString &text, int line, int column)
0032 {
0033     auto doc = new DocumentPrivate();
0034     doc->config()->setCamelCursor(false);
0035     auto view = new ViewPrivate(doc, nullptr);
0036     doc->setText(text);
0037     view->setCursorPosition({line, column});
0038     return {doc, view};
0039 }
0040 
0041 CursorWordsTest::CursorWordsTest(QObject *parent)
0042     : QObject(parent)
0043 {
0044     EditorPrivate::enableUnitTestMode();
0045 }
0046 
0047 CursorWordsTest::~CursorWordsTest()
0048 {
0049 }
0050 
0051 void CursorWordsTest::testMoveToNextWordSingleLine()
0052 {
0053     { // single space between words
0054 
0055         auto [doc, view] = createDocAndView("foo bar quzzi", 0, 0);
0056 
0057         view->wordRight();
0058 
0059         QCOMPARE(view->cursorPosition(), Cursor(0, 4));
0060 
0061         view->wordRight();
0062 
0063         QCOMPARE(view->cursorPosition(), Cursor(0, 8));
0064 
0065         view->wordRight();
0066 
0067         QCOMPARE(view->cursorPosition(), Cursor(0, 13));
0068     }
0069 
0070     { // cursor inside multiple spaces between words
0071 
0072         auto [doc, view] = createDocAndView("  -  1234  xyz", 0, 1); // cursor at second space
0073 
0074         view->wordRight();
0075 
0076         QCOMPARE(view->cursorPosition(), Cursor(0, 2)); // just before "-"
0077 
0078         view->wordRight();
0079 
0080         QCOMPARE(view->cursorPosition(), Cursor(0, 5));
0081 
0082         view->wordRight();
0083 
0084         QCOMPARE(view->cursorPosition(), Cursor(0, 11));
0085     }
0086 }
0087 
0088 void CursorWordsTest::testMoveToPrevWordSingleLine()
0089 {
0090     { // single space between words
0091 
0092         auto [doc, view] = createDocAndView("foo bar quzzi", 0, 8); // cursor at the start of "quzzi"
0093 
0094         view->wordLeft();
0095 
0096         QCOMPARE(view->cursorPosition(), Cursor(0, 4));
0097 
0098         view->wordLeft();
0099 
0100         QCOMPARE(view->cursorPosition(), Cursor(0, 0));
0101     }
0102 
0103     { // cursor inside multiple spaces between words
0104 
0105         auto [doc, view] = createDocAndView("  12  -  ", 0, 8); // cursor at the last space
0106 
0107         view->wordLeft();
0108 
0109         QCOMPARE(view->cursorPosition(), Cursor(0, 6));
0110 
0111         view->wordLeft();
0112 
0113         QCOMPARE(view->cursorPosition(), Cursor(0, 2));
0114     }
0115 }
0116 
0117 // clang-format off
0118 #define COMPARE_CHAR_AND_CURSOR(view, expectedCursor, expectedCharacter)                  \
0119     QCOMPARE(view->document()->characterAt(view->cursorPosition()), expectedCharacter);   \
0120     QCOMPARE(view->cursorPosition(), expectedCursor)
0121 // clang-format on
0122 
0123 void CursorWordsTest::testMoveToWordsMultipleLines()
0124 {
0125     auto [doc, view] = createDocAndView("hello  there...\n\tno  one answers.", 0, 0);
0126 
0127     view->wordRight();
0128     COMPARE_CHAR_AND_CURSOR(view, Cursor(0, 7), 't');
0129 
0130     view->wordRight();
0131     COMPARE_CHAR_AND_CURSOR(view, Cursor(0, 12), '.');
0132 
0133     view->wordRight();
0134     COMPARE_CHAR_AND_CURSOR(view, Cursor(0, 15), '\0'); // end of line
0135 
0136     view->wordRight();
0137     COMPARE_CHAR_AND_CURSOR(view, Cursor(1, 1), 'n');
0138 
0139     view->wordRight();
0140     COMPARE_CHAR_AND_CURSOR(view, Cursor(1, 5), 'o');
0141 
0142     view->wordRight();
0143     COMPARE_CHAR_AND_CURSOR(view, Cursor(1, 9), 'a');
0144 
0145     view->wordRight();
0146     COMPARE_CHAR_AND_CURSOR(view, Cursor(1, 16), '.');
0147 
0148     view->wordLeft();
0149     COMPARE_CHAR_AND_CURSOR(view, Cursor(1, 9), 'a');
0150 
0151     view->wordLeft();
0152     view->wordLeft();
0153     COMPARE_CHAR_AND_CURSOR(view, Cursor(1, 1), 'n');
0154 
0155     view->wordLeft();
0156     COMPARE_CHAR_AND_CURSOR(view, Cursor(0, 15), '\0');
0157 
0158     view->wordLeft();
0159     COMPARE_CHAR_AND_CURSOR(view, Cursor(0, 12), '.');
0160 }
0161 
0162 #include "moc_cursorwords_test.cpp"