~ubuntu-branches/ubuntu/vivid/kate/vivid-proposed

« back to all changes in this revision

Viewing changes to tests/movingcursor_test.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-12-04 16:49:41 UTC
  • mfrom: (1.6.6)
  • Revision ID: package-import@ubuntu.com-20141204164941-l3qbvsly83hhlw2v
Tags: 4:14.11.97-0ubuntu1
* New upstream release
* Update build-deps and use pkg-kde v3 for Qt 5 build
* kate-data now kate5-data for co-installability

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of the KDE libraries
2
 
   Copyright (C) 2010 Dominik Haumann <dhaumann kde org>
3
 
 
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.
8
 
 
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.
13
 
 
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.
18
 
*/
19
 
 
20
 
#include "movingcursor_test.h"
21
 
#include "moc_movingcursor_test.cpp"
22
 
 
23
 
#include <qtest_kde.h>
24
 
 
25
 
#include <katedocument.h>
26
 
#include <ktexteditor/movingcursor.h>
27
 
 
28
 
using namespace KTextEditor;
29
 
 
30
 
QTEST_KDEMAIN(MovingCursorTest, GUI)
31
 
 
32
 
namespace QTest {
33
 
    template<>
34
 
    char *toString(const KTextEditor::Cursor &cursor)
35
 
    {
36
 
        QByteArray ba = "Cursor[" + QByteArray::number(cursor.line())
37
 
        + ", " + QByteArray::number(cursor.column()) + "]";
38
 
        return qstrdup(ba.data());
39
 
    }
40
 
}
41
 
 
42
 
MovingCursorTest::MovingCursorTest()
43
 
  : QObject()
44
 
{
45
 
}
46
 
 
47
 
MovingCursorTest::~MovingCursorTest()
48
 
{
49
 
}
50
 
 
51
 
// tests:
52
 
// - MovingCursor with StayOnInsert
53
 
// - MovingCursor with MoveOnInsert
54
 
void MovingCursorTest::testMovingCursor()
55
 
{
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);
60
 
 
61
 
  // verify initial conditions
62
 
  QVERIFY(!invalid->isValid());
63
 
  QCOMPARE(moveOnInsert->toCursor(), Cursor(0, 0));
64
 
  QCOMPARE(stayOnInsert->toCursor(), Cursor(0, 0));
65
 
 
66
 
  // insert some text
67
 
  doc.insertText(Cursor(0, 0), "\n"
68
 
                                "1\n"
69
 
                                "22");
70
 
 
71
 
  // check new cursor positions
72
 
  QCOMPARE(moveOnInsert->toCursor(), Cursor(2, 2));
73
 
  QCOMPARE(stayOnInsert->toCursor(), Cursor(0, 0));
74
 
 
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));
80
 
  doc.undo();
81
 
  QCOMPARE(stayOnInsert->toCursor(), Cursor(1, 1));
82
 
 
83
 
  // position still at (1, 1). insert text at cursor
84
 
  doc.insertText(Cursor(1, 1), "test");
85
 
  QCOMPARE(stayOnInsert->toCursor(), Cursor(1, 1));
86
 
  doc.undo();
87
 
  QCOMPARE(stayOnInsert->toCursor(), Cursor(1, 1));
88
 
 
89
 
  //
90
 
  // same tests with the moveOnInsert cursor
91
 
  //
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));
97
 
  doc.undo();
98
 
  QCOMPARE(moveOnInsert->toCursor(), Cursor(1, 1));
99
 
 
100
 
  // position still at (1, 1). insert text at cursor
101
 
  doc.insertText(Cursor(1, 1), "test");
102
 
  QCOMPARE(moveOnInsert->toCursor(), Cursor(1, 5));
103
 
  doc.undo();
104
 
  QCOMPARE(moveOnInsert->toCursor(), Cursor(1, 1));
105
 
 
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));
112
 
}
113
 
 
114
 
// tests:
115
 
// - atStartOfDocument
116
 
// - atStartOfLine
117
 
// - atEndOfDocument
118
 
// - atEndOfLine
119
 
// - move forward with Wrap
120
 
// - move forward with NoWrap
121
 
// - move backward
122
 
// - gotoNextLine
123
 
// - gotoPreviousLine
124
 
void MovingCursorTest::testConvenienceApi()
125
 
{
126
 
  KateDocument doc (false, false, false);
127
 
  doc.setText("\n"
128
 
              "1\n"
129
 
              "22\n"
130
 
              "333\n"
131
 
              "4444\n"
132
 
              "55555");
133
 
 
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());
141
 
 
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());
151
 
 
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());
156
 
 
157
 
  // advance 7 characters to position (3, 3)
158
 
  QVERIFY(moving->move(7));   // at 3, 3
159
 
  QCOMPARE(moving->toCursor(), Cursor(3, 3));
160
 
 
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));
166
 
 
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));
171
 
 
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());
177
 
 
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));
182
 
}
183
 
 
184
 
void MovingCursorTest::testOperators()
185
 
{
186
 
    KateDocument doc (false, false, false);
187
 
    doc.setText("--oo--\n"
188
 
                "--oo--\n"
189
 
                "--oo--");
190
 
 
191
 
    // create lots of cursors for comparison
192
 
    Cursor invalid = Cursor::invalid();
193
 
    Cursor c02(0, 2);
194
 
    Cursor c04(0, 4);
195
 
    Cursor c14(1, 4);
196
 
 
197
 
    MovingCursor* m02 = doc.newMovingCursor(Cursor(0, 2));
198
 
    MovingCursor* m04 = doc.newMovingCursor(Cursor(0, 4));
199
 
    MovingCursor* m14 = doc.newMovingCursor(Cursor(1, 4));
200
 
 
201
 
    // invalid comparison
202
 
    QVERIFY(invalid == invalid);
203
 
    QVERIFY(invalid <= c02);
204
 
    QVERIFY(invalid < c02);
205
 
    QVERIFY(!(invalid > c02));
206
 
    QVERIFY(!(invalid >= c02));
207
 
 
208
 
    QVERIFY(!(invalid == *m02));
209
 
    QVERIFY(invalid <= *m02);
210
 
    QVERIFY(invalid < *m02);
211
 
    QVERIFY(!(invalid > *m02));
212
 
    QVERIFY(!(invalid >= *m02));
213
 
 
214
 
    QVERIFY(!(*m02 == invalid));
215
 
    QVERIFY(!(*m02 <= invalid));
216
 
    QVERIFY(!(*m02 < invalid));
217
 
    QVERIFY(*m02 > invalid);
218
 
    QVERIFY(*m02 >= invalid);
219
 
 
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));
227
 
 
228
 
    QVERIFY(!(*m02 == *m04));
229
 
    QVERIFY(*m02 <= *m04);
230
 
    QVERIFY(!(*m02 >= *m04));
231
 
    QVERIFY(*m02 < *m04);
232
 
    QVERIFY(!(*m02 > *m04));
233
 
    QVERIFY(*m02 != *m04);
234
 
 
235
 
    QVERIFY(!(*m04 == *m02));
236
 
    QVERIFY(!(*m04 <= *m02));
237
 
    QVERIFY(*m04 >= *m02);
238
 
    QVERIFY(!(*m04 < *m02));
239
 
    QVERIFY(*m04 > *m02);
240
 
    QVERIFY(*m04 != *m02);
241
 
 
242
 
    QVERIFY(!(*m02 == *m14));
243
 
    QVERIFY(*m02 <= *m14);
244
 
    QVERIFY(!(*m02 >= *m14));
245
 
    QVERIFY(*m02 < *m14);
246
 
    QVERIFY(!(*m02 > *m14));
247
 
    QVERIFY(*m02 != *m14);
248
 
 
249
 
    QVERIFY(!(*m14 == *m02));
250
 
    QVERIFY(!(*m14 <= *m02));
251
 
    QVERIFY(*m14 >= *m02);
252
 
    QVERIFY(!(*m14 < *m02));
253
 
    QVERIFY(*m14 > *m02);
254
 
    QVERIFY(*m14 != *m02);
255
 
 
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));
263
 
 
264
 
    QVERIFY(!(*m02 == c04));
265
 
    QVERIFY(*m02 <= c04);
266
 
    QVERIFY(!(*m02 >= c04));
267
 
    QVERIFY(*m02 < c04);
268
 
    QVERIFY(!(*m02 > c04));
269
 
    QVERIFY(*m02 != c04);
270
 
 
271
 
    QVERIFY(!(*m04 == c02));
272
 
    QVERIFY(!(*m04 <= c02));
273
 
    QVERIFY(*m04 >= c02);
274
 
    QVERIFY(!(*m04 < c02));
275
 
    QVERIFY(*m04 > c02);
276
 
    QVERIFY(*m04 != c02);
277
 
 
278
 
    QVERIFY(!(*m02 == c14));
279
 
    QVERIFY(*m02 <= c14);
280
 
    QVERIFY(!(*m02 >= c14));
281
 
    QVERIFY(*m02 < c14);
282
 
    QVERIFY(!(*m02 > c14));
283
 
    QVERIFY(*m02 != c14);
284
 
 
285
 
    QVERIFY(!(*m14 == c02));
286
 
    QVERIFY(!(*m14 <= c02));
287
 
    QVERIFY(*m14 >= c02);
288
 
    QVERIFY(!(*m14 < c02));
289
 
    QVERIFY(*m14 > c02);
290
 
    QVERIFY(*m14 != c02);
291
 
 
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));
299
 
 
300
 
    QVERIFY(!(c02 == *m04));
301
 
    QVERIFY(c02 <= *m04);
302
 
    QVERIFY(!(c02 >= *m04));
303
 
    QVERIFY(c02 < *m04);
304
 
    QVERIFY(!(c02 > *m04));
305
 
    QVERIFY(c02 != *m04);
306
 
 
307
 
    QVERIFY(!(c04 == *m02));
308
 
    QVERIFY(!(c04 <= *m02));
309
 
    QVERIFY(c04 >= *m02);
310
 
    QVERIFY(!(c04 < *m02));
311
 
    QVERIFY(c04 > *m02);
312
 
    QVERIFY(c04 != *m02);
313
 
 
314
 
    QVERIFY(!(c02 == *m14));
315
 
    QVERIFY(c02 <= *m14);
316
 
    QVERIFY(!(c02 >= *m14));
317
 
    QVERIFY(c02 < *m14);
318
 
    QVERIFY(!(c02 > *m14));
319
 
    QVERIFY(c02 != *m14);
320
 
 
321
 
    QVERIFY(!(c14 == *m02));
322
 
    QVERIFY(!(c14 <= *m02));
323
 
    QVERIFY(c14 >= *m02);
324
 
    QVERIFY(!(c14 < *m02));
325
 
    QVERIFY(c14 > *m02);
326
 
    QVERIFY(c14 != *m02);
327
 
}
328
 
 
329
 
void MovingCursorTest::testInvalidMovingCursor()
330
 
{
331
 
  KateDocument* doc = new KateDocument(false, false, false);
332
 
 
333
 
  // add invalid MovingCursor. Inserts c into KateBuffer::m_invalidCursors
334
 
  MovingCursor* c = doc->newMovingCursor(Cursor(-1, -1));
335
 
  QVERIFY(Cursor(-1, -1) == *c);
336
 
 
337
 
  c->setPosition(Cursor(0, 0));
338
 
  QVERIFY(Cursor(0, 0) == *c);
339
 
 
340
 
  // now c should be removed from KateBuffer::m_invalidCursors
341
 
  delete c;
342
 
 
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
345
 
  delete doc;
346
 
}