1
/* This file is part of the KDE libraries
2
Copyright (C) 2010 Dominik Haumann <dhaumann kde org>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
Boston, MA 02110-1301, USA.
20
#include "movingcursor_test.h"
21
#include "moc_movingcursor_test.cpp"
23
#include <qtest_kde.h>
25
#include <katedocument.h>
26
#include <ktexteditor/movingcursor.h>
28
using namespace KTextEditor;
30
QTEST_KDEMAIN(MovingCursorTest, GUI)
34
char *toString(const KTextEditor::Cursor &cursor)
36
QByteArray ba = "Cursor[" + QByteArray::number(cursor.line())
37
+ ", " + QByteArray::number(cursor.column()) + "]";
38
return qstrdup(ba.data());
42
MovingCursorTest::MovingCursorTest()
47
MovingCursorTest::~MovingCursorTest()
52
// - MovingCursor with StayOnInsert
53
// - MovingCursor with MoveOnInsert
54
void MovingCursorTest::testMovingCursor()
56
KateDocument doc (false, false, false);
57
MovingCursor* invalid = doc.newMovingCursor(Cursor::invalid());
58
MovingCursor* moveOnInsert = doc.newMovingCursor(Cursor(0, 0), MovingCursor::MoveOnInsert);
59
MovingCursor* stayOnInsert = doc.newMovingCursor(Cursor(0, 0), MovingCursor::StayOnInsert);
61
// verify initial conditions
62
QVERIFY(!invalid->isValid());
63
QCOMPARE(moveOnInsert->toCursor(), Cursor(0, 0));
64
QCOMPARE(stayOnInsert->toCursor(), Cursor(0, 0));
67
doc.insertText(Cursor(0, 0), "\n"
71
// check new cursor positions
72
QCOMPARE(moveOnInsert->toCursor(), Cursor(2, 2));
73
QCOMPARE(stayOnInsert->toCursor(), Cursor(0, 0));
75
// set position to (1, 1) and insert text before cursor
76
stayOnInsert->setPosition(Cursor(1, 1));
77
QCOMPARE(stayOnInsert->toCursor(), Cursor(1, 1));
78
doc.insertText(Cursor(1, 0), "test");
79
QCOMPARE(stayOnInsert->toCursor(), Cursor(1, 5));
81
QCOMPARE(stayOnInsert->toCursor(), Cursor(1, 1));
83
// position still at (1, 1). insert text at cursor
84
doc.insertText(Cursor(1, 1), "test");
85
QCOMPARE(stayOnInsert->toCursor(), Cursor(1, 1));
87
QCOMPARE(stayOnInsert->toCursor(), Cursor(1, 1));
90
// same tests with the moveOnInsert cursor
92
// set position to (1, 1) and insert text before cursor
93
moveOnInsert->setPosition(Cursor(1, 1));
94
QCOMPARE(moveOnInsert->toCursor(), Cursor(1, 1));
95
doc.insertText(Cursor(1, 0), "test");
96
QCOMPARE(moveOnInsert->toCursor(), Cursor(1, 5));
98
QCOMPARE(moveOnInsert->toCursor(), Cursor(1, 1));
100
// position still at (1, 1). insert text at cursor
101
doc.insertText(Cursor(1, 1), "test");
102
QCOMPARE(moveOnInsert->toCursor(), Cursor(1, 5));
104
QCOMPARE(moveOnInsert->toCursor(), Cursor(1, 1));
106
// set both cursors to (2, 1) then delete text range that contains cursors
107
moveOnInsert->setPosition(Cursor(2, 1));
108
stayOnInsert->setPosition(Cursor(2, 1));
109
doc.removeText(Range(Cursor(2, 0), Cursor(2, 2)));
110
QCOMPARE(moveOnInsert->toCursor(), Cursor(2, 0));
111
QCOMPARE(moveOnInsert->toCursor(), Cursor(2, 0));
115
// - atStartOfDocument
119
// - move forward with Wrap
120
// - move forward with NoWrap
123
// - gotoPreviousLine
124
void MovingCursorTest::testConvenienceApi()
126
KateDocument doc (false, false, false);
134
// check start and end of document
135
MovingCursor *startOfDoc = doc.newMovingCursor(Cursor(0, 0));
136
MovingCursor *endOfDoc = doc.newMovingCursor(Cursor(5, 5));
137
QVERIFY(startOfDoc->atStartOfDocument());
138
QVERIFY(startOfDoc->atStartOfLine());
139
QVERIFY(endOfDoc->atEndOfDocument());
140
QVERIFY(endOfDoc->atEndOfLine());
142
// set cursor to (2, 2) and then move to the left two times
143
MovingCursor *moving = doc.newMovingCursor(Cursor(2, 2));
144
QVERIFY(moving->atEndOfLine()); // at 2, 2
145
QVERIFY(moving->move(-1)); // at 2, 1
146
QCOMPARE(moving->toCursor(), Cursor(2, 1));
147
QVERIFY(!moving->atEndOfLine());
148
QVERIFY(moving->move(-1)); // at 2, 0
149
QCOMPARE(moving->toCursor(), Cursor(2, 0));
150
QVERIFY(moving->atStartOfLine());
152
// now move again to the left, should wrap to (1, 1)
153
QVERIFY(moving->move(-1)); // at 1, 1
154
QCOMPARE(moving->toCursor(), Cursor(1, 1));
155
QVERIFY(moving->atEndOfLine());
157
// advance 7 characters to position (3, 3)
158
QVERIFY(moving->move(7)); // at 3, 3
159
QCOMPARE(moving->toCursor(), Cursor(3, 3));
161
// advance 20 characters in NoWrap mode, then go back 10 characters
162
QVERIFY(moving->move(20, MovingCursor::NoWrap)); // at 3, 23
163
QCOMPARE(moving->toCursor(), Cursor(3, 23));
164
QVERIFY(moving->move(-10)); // at 3, 13
165
QCOMPARE(moving->toCursor(), Cursor(3, 13));
167
// still at invalid text position. move one char to wrap around
168
QVERIFY(!moving->isValidTextPosition()); // at 3, 13
169
QVERIFY(moving->move(1)); // at 4, 0
170
QCOMPARE(moving->toCursor(), Cursor(4, 0));
172
// moving 11 characters in wrap mode moves to (5, 6), which is not a valid
173
// text position anymore. Hence, moving should be rejected.
174
QVERIFY(!moving->move(11));
175
QVERIFY(moving->move(10));
176
QVERIFY(moving->atEndOfDocument());
178
// try to move to next line, which fails. then go to previous line
179
QVERIFY(!moving->gotoNextLine());
180
QVERIFY(moving->gotoPreviousLine());
181
QCOMPARE(moving->toCursor(), Cursor(4, 0));
184
void MovingCursorTest::testOperators()
186
KateDocument doc (false, false, false);
187
doc.setText("--oo--\n"
191
// create lots of cursors for comparison
192
Cursor invalid = Cursor::invalid();
197
MovingCursor* m02 = doc.newMovingCursor(Cursor(0, 2));
198
MovingCursor* m04 = doc.newMovingCursor(Cursor(0, 4));
199
MovingCursor* m14 = doc.newMovingCursor(Cursor(1, 4));
201
// invalid comparison
202
QVERIFY(invalid == invalid);
203
QVERIFY(invalid <= c02);
204
QVERIFY(invalid < c02);
205
QVERIFY(!(invalid > c02));
206
QVERIFY(!(invalid >= c02));
208
QVERIFY(!(invalid == *m02));
209
QVERIFY(invalid <= *m02);
210
QVERIFY(invalid < *m02);
211
QVERIFY(!(invalid > *m02));
212
QVERIFY(!(invalid >= *m02));
214
QVERIFY(!(*m02 == invalid));
215
QVERIFY(!(*m02 <= invalid));
216
QVERIFY(!(*m02 < invalid));
217
QVERIFY(*m02 > invalid);
218
QVERIFY(*m02 >= invalid);
220
// MovingCursor <-> MovingCursor
221
QVERIFY(*m02 == *m02);
222
QVERIFY(*m02 <= *m02);
223
QVERIFY(*m02 >= *m02);
224
QVERIFY(!(*m02 < *m02));
225
QVERIFY(!(*m02 > *m02));
226
QVERIFY(!(*m02 != *m02));
228
QVERIFY(!(*m02 == *m04));
229
QVERIFY(*m02 <= *m04);
230
QVERIFY(!(*m02 >= *m04));
231
QVERIFY(*m02 < *m04);
232
QVERIFY(!(*m02 > *m04));
233
QVERIFY(*m02 != *m04);
235
QVERIFY(!(*m04 == *m02));
236
QVERIFY(!(*m04 <= *m02));
237
QVERIFY(*m04 >= *m02);
238
QVERIFY(!(*m04 < *m02));
239
QVERIFY(*m04 > *m02);
240
QVERIFY(*m04 != *m02);
242
QVERIFY(!(*m02 == *m14));
243
QVERIFY(*m02 <= *m14);
244
QVERIFY(!(*m02 >= *m14));
245
QVERIFY(*m02 < *m14);
246
QVERIFY(!(*m02 > *m14));
247
QVERIFY(*m02 != *m14);
249
QVERIFY(!(*m14 == *m02));
250
QVERIFY(!(*m14 <= *m02));
251
QVERIFY(*m14 >= *m02);
252
QVERIFY(!(*m14 < *m02));
253
QVERIFY(*m14 > *m02);
254
QVERIFY(*m14 != *m02);
256
// MovingCursor <-> Cursor
257
QVERIFY(*m02 == c02);
258
QVERIFY(*m02 <= c02);
259
QVERIFY(*m02 >= c02);
260
QVERIFY(!(*m02 < c02));
261
QVERIFY(!(*m02 > c02));
262
QVERIFY(!(*m02 != c02));
264
QVERIFY(!(*m02 == c04));
265
QVERIFY(*m02 <= c04);
266
QVERIFY(!(*m02 >= c04));
268
QVERIFY(!(*m02 > c04));
269
QVERIFY(*m02 != c04);
271
QVERIFY(!(*m04 == c02));
272
QVERIFY(!(*m04 <= c02));
273
QVERIFY(*m04 >= c02);
274
QVERIFY(!(*m04 < c02));
276
QVERIFY(*m04 != c02);
278
QVERIFY(!(*m02 == c14));
279
QVERIFY(*m02 <= c14);
280
QVERIFY(!(*m02 >= c14));
282
QVERIFY(!(*m02 > c14));
283
QVERIFY(*m02 != c14);
285
QVERIFY(!(*m14 == c02));
286
QVERIFY(!(*m14 <= c02));
287
QVERIFY(*m14 >= c02);
288
QVERIFY(!(*m14 < c02));
290
QVERIFY(*m14 != c02);
292
// Cursor <-> MovingCursor
293
QVERIFY(c02 == *m02);
294
QVERIFY(c02 <= *m02);
295
QVERIFY(c02 >= *m02);
296
QVERIFY(!(c02 < *m02));
297
QVERIFY(!(c02 > *m02));
298
QVERIFY(!(c02 != *m02));
300
QVERIFY(!(c02 == *m04));
301
QVERIFY(c02 <= *m04);
302
QVERIFY(!(c02 >= *m04));
304
QVERIFY(!(c02 > *m04));
305
QVERIFY(c02 != *m04);
307
QVERIFY(!(c04 == *m02));
308
QVERIFY(!(c04 <= *m02));
309
QVERIFY(c04 >= *m02);
310
QVERIFY(!(c04 < *m02));
312
QVERIFY(c04 != *m02);
314
QVERIFY(!(c02 == *m14));
315
QVERIFY(c02 <= *m14);
316
QVERIFY(!(c02 >= *m14));
318
QVERIFY(!(c02 > *m14));
319
QVERIFY(c02 != *m14);
321
QVERIFY(!(c14 == *m02));
322
QVERIFY(!(c14 <= *m02));
323
QVERIFY(c14 >= *m02);
324
QVERIFY(!(c14 < *m02));
326
QVERIFY(c14 != *m02);
329
void MovingCursorTest::testInvalidMovingCursor()
331
KateDocument* doc = new KateDocument(false, false, false);
333
// add invalid MovingCursor. Inserts c into KateBuffer::m_invalidCursors
334
MovingCursor* c = doc->newMovingCursor(Cursor(-1, -1));
335
QVERIFY(Cursor(-1, -1) == *c);
337
c->setPosition(Cursor(0, 0));
338
QVERIFY(Cursor(0, 0) == *c);
340
// now c should be removed from KateBuffer::m_invalidCursors
343
// crash in bug https://bugs.kde.org/show_bug.cgi?id=248926
344
// if it crashes: c is still in KateBuffer::m_invalidCursors -> double deletion