[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [kate] part: KTE::DocumentCursor: add unit test (same as MovingCursor test)
From: Dominik Haumann <dhdev () gmx ! de>
Date: 2012-07-03 8:04:48
Message-ID: 20120703080448.211E3A60A6 () git ! kde ! org
[Download RAW message or body]
Git commit 86b794da87abd4ae62557218d72e814bcb0d4b55 by Dominik Haumann.
Committed on 03/07/2012 at 10:00.
Pushed by dhaumann into branch 'master'.
KTE::DocumentCursor: add unit test (same as MovingCursor test)
M +3 -1 part/document/documentcursor.h
M +11 -0 part/tests/CMakeLists.txt
A +280 -0 part/tests/kte_documentcursor.cpp [License: LGPL (v2+)]
A +41 -0 part/tests/kte_documentcursor.h [License: LGPL (v2+)]
http://commits.kde.org/kate/86b794da87abd4ae62557218d72e814bcb0d4b55
diff --git a/part/document/documentcursor.h b/part/document/documentcursor.h
index 56634af..161eeaa 100644
--- a/part/document/documentcursor.h
+++ b/part/document/documentcursor.h
@@ -25,6 +25,8 @@
#include <ktexteditor/cursor.h>
#include <ktexteditor/document.h>
+#include "katepartprivate_export.h"
+
namespace KTextEditor {
/**
@@ -64,7 +66,7 @@ namespace KTextEditor {
* \todo KDE5: move to ktexteditor interface, use it in
* MovingCursor::move() to avoid code duplication
*/
-class DocumentCursor
+class KATEPART_TESTS_EXPORT DocumentCursor
{
//
// sub types
diff --git a/part/tests/CMakeLists.txt b/part/tests/CMakeLists.txt
index 5bb15bf..80d3c23 100644
--- a/part/tests/CMakeLists.txt
+++ b/part/tests/CMakeLists.txt
@@ -281,6 +281,17 @@ target_link_libraries( bug286887_test
katepartinterfaces
)
+########### KTextEditor::DocumentCursor test ###############
+
+kde4_add_unit_test(kte_documentcursor_test TESTNAME kte-documentcursor_test kte_documentcursor.cpp)
+
+target_link_libraries( kte_documentcursor_test
+ ${KDE4_KDEUI_LIBS}
+ ${QT_QTTEST_LIBRARY}
+ ${KATE_TEST_LINK_LIBS}
+ katepartinterfaces
+)
+
endif(NOT MINGW)
# encoding tets
diff --git a/part/tests/kte_documentcursor.cpp b/part/tests/kte_documentcursor.cpp
new file mode 100644
index 0000000..f3361fe
--- /dev/null
+++ b/part/tests/kte_documentcursor.cpp
@@ -0,0 +1,280 @@
+/* This file is part of the KDE libraries
+ Copyright (C) 2012 Dominik Haumann <dhaumann kde org>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+*/
+
+#include "kte_documentcursor.h"
+#include "moc_kte_documentcursor.cpp"
+
+#include <qtest_kde.h>
+
+#include <katedocument.h>
+#include <katecodefolding.h>
+#include <katebuffer.h>
+#include <kateglobal.h>
+#include <kateview.h>
+#include <kateconfig.h>
+#include <katesearchbar.h>
+#include <ktexteditor/movingrange.h>
+
+#include <documentcursor.h>
+
+QTEST_KDEMAIN(DocumentCursorTest, GUI)
+
+using namespace KTextEditor;
+
+DocumentCursorTest::DocumentCursorTest()
+ : QObject()
+{
+}
+
+DocumentCursorTest::~DocumentCursorTest()
+{
+}
+
+void DocumentCursorTest::initTestCase()
+{
+ KateGlobal::self()->incRef();
+}
+
+void DocumentCursorTest::cleanupTestCase()
+{
+ KateGlobal::self()->decRef();
+}
+
+// tests:
+// - atStartOfDocument
+// - atStartOfLine
+// - atEndOfDocument
+// - atEndOfLine
+// - move forward with Wrap
+// - move forward with NoWrap
+// - move backward
+// - gotoNextLine
+// - gotoPreviousLine
+void DocumentCursorTest::testConvenienceApi()
+{
+ KateDocument doc (false, false, false);
+ doc.setText("\n"
+ "1\n"
+ "22\n"
+ "333\n"
+ "4444\n"
+ "55555");
+
+ // check start and end of document
+ DocumentCursor startOfDoc(&doc); startOfDoc.setPosition(Cursor(0, 0));
+ DocumentCursor endOfDoc(&doc); endOfDoc.setPosition(Cursor(5, 5));
+ QVERIFY(startOfDoc.atStartOfDocument());
+ QVERIFY(startOfDoc.atStartOfLine());
+ QVERIFY(endOfDoc.atEndOfDocument());
+ QVERIFY(endOfDoc.atEndOfLine());
+
+ // set cursor to (2, 2) and then move to the left two times
+ DocumentCursor moving(&doc); moving.setPosition(Cursor(2, 2));
+ QVERIFY(moving.atEndOfLine()); // at 2, 2
+ QVERIFY(moving.move(-1)); // at 2, 1
+ QCOMPARE(moving.toCursor(), Cursor(2, 1));
+ QVERIFY(!moving.atEndOfLine());
+ QVERIFY(moving.move(-1)); // at 2, 0
+ QCOMPARE(moving.toCursor(), Cursor(2, 0));
+ QVERIFY(moving.atStartOfLine());
+
+ // now move again to the left, should wrap to (1, 1)
+ QVERIFY(moving.move(-1)); // at 1, 1
+ QCOMPARE(moving.toCursor(), Cursor(1, 1));
+ QVERIFY(moving.atEndOfLine());
+
+ // advance 7 characters to position (3, 3)
+ QVERIFY(moving.move(7)); // at 3, 3
+ QCOMPARE(moving.toCursor(), Cursor(3, 3));
+
+ // advance 20 characters in NoWrap mode, then go back 10 characters
+ QVERIFY(moving.move(20, DocumentCursor::NoWrap)); // at 3, 23
+ QCOMPARE(moving.toCursor(), Cursor(3, 23));
+ QVERIFY(moving.move(-10)); // at 3, 13
+ QCOMPARE(moving.toCursor(), Cursor(3, 13));
+
+ // still at invalid text position. move one char to wrap around
+ QVERIFY(!moving.isValidTextPosition()); // at 3, 13
+ QVERIFY(moving.move(1)); // at 4, 0
+ QCOMPARE(moving.toCursor(), Cursor(4, 0));
+
+ // moving 11 characters in wrap mode moves to (5, 6), which is not a valid
+ // text position anymore. Hence, moving should be rejected.
+ QVERIFY(!moving.move(11));
+ QVERIFY(moving.move(10));
+ QVERIFY(moving.atEndOfDocument());
+
+ // try to move to next line, which fails. then go to previous line
+ QVERIFY(!moving.gotoNextLine());
+ QVERIFY(moving.gotoPreviousLine());
+ QCOMPARE(moving.toCursor(), Cursor(4, 0));
+}
+
+void DocumentCursorTest::testOperators()
+{
+ KateDocument doc (false, false, false);
+ doc.setText("--oo--\n"
+ "--oo--\n"
+ "--oo--");
+
+ // create lots of cursors for comparison
+ Cursor invalid = Cursor::invalid();
+ Cursor c02(0, 2);
+ Cursor c04(0, 4);
+ Cursor c14(1, 4);
+
+ DocumentCursor m02(&doc);
+ DocumentCursor m04(&doc);
+ DocumentCursor m14(&doc);
+
+ QVERIFY(m02 == invalid);
+ QVERIFY(m04 == invalid);
+ QVERIFY(m14 == invalid);
+
+ m02.setPosition(Cursor(0, 2));
+ m04.setPosition(Cursor(0, 4));
+ m14.setPosition(Cursor(1, 4));
+
+ // invalid comparison
+ QVERIFY(invalid == invalid);
+ QVERIFY(invalid <= c02);
+ QVERIFY(invalid < c02);
+ QVERIFY(!(invalid > c02));
+ QVERIFY(!(invalid >= c02));
+
+ QVERIFY(!(invalid == m02));
+ QVERIFY(invalid <= m02);
+ QVERIFY(invalid < m02);
+ QVERIFY(!(invalid > m02));
+ QVERIFY(!(invalid >= m02));
+
+ QVERIFY(!(m02 == invalid));
+ QVERIFY(!(m02 <= invalid));
+ QVERIFY(!(m02 < invalid));
+ QVERIFY(m02 > invalid);
+ QVERIFY(m02 >= invalid);
+
+ // DocumentCursor <-> DocumentCursor
+ QVERIFY(m02 == m02);
+ QVERIFY(m02 <= m02);
+ QVERIFY(m02 >= m02);
+ QVERIFY(!(m02 < m02));
+ QVERIFY(!(m02 > m02));
+ QVERIFY(!(m02 != m02));
+
+ QVERIFY(!(m02 == m04));
+ QVERIFY(m02 <= m04);
+ QVERIFY(!(m02 >= m04));
+ QVERIFY(m02 < m04);
+ QVERIFY(!(m02 > m04));
+ QVERIFY(m02 != m04);
+
+ QVERIFY(!(m04 == m02));
+ QVERIFY(!(m04 <= m02));
+ QVERIFY(m04 >= m02);
+ QVERIFY(!(m04 < m02));
+ QVERIFY(m04 > m02);
+ QVERIFY(m04 != m02);
+
+ QVERIFY(!(m02 == m14));
+ QVERIFY(m02 <= m14);
+ QVERIFY(!(m02 >= m14));
+ QVERIFY(m02 < m14);
+ QVERIFY(!(m02 > m14));
+ QVERIFY(m02 != m14);
+
+ QVERIFY(!(m14 == m02));
+ QVERIFY(!(m14 <= m02));
+ QVERIFY(m14 >= m02);
+ QVERIFY(!(m14 < m02));
+ QVERIFY(m14 > m02);
+ QVERIFY(m14 != m02);
+
+ // DocumentCursor <-> Cursor
+ QVERIFY(m02 == c02);
+ QVERIFY(m02 <= c02);
+ QVERIFY(m02 >= c02);
+ QVERIFY(!(m02 < c02));
+ QVERIFY(!(m02 > c02));
+ QVERIFY(!(m02 != c02));
+
+ QVERIFY(!(m02 == c04));
+ QVERIFY(m02 <= c04);
+ QVERIFY(!(m02 >= c04));
+ QVERIFY(m02 < c04);
+ QVERIFY(!(m02 > c04));
+ QVERIFY(m02 != c04);
+
+ QVERIFY(!(m04 == c02));
+ QVERIFY(!(m04 <= c02));
+ QVERIFY(m04 >= c02);
+ QVERIFY(!(m04 < c02));
+ QVERIFY(m04 > c02);
+ QVERIFY(m04 != c02);
+
+ QVERIFY(!(m02 == c14));
+ QVERIFY(m02 <= c14);
+ QVERIFY(!(m02 >= c14));
+ QVERIFY(m02 < c14);
+ QVERIFY(!(m02 > c14));
+ QVERIFY(m02 != c14);
+
+ QVERIFY(!(m14 == c02));
+ QVERIFY(!(m14 <= c02));
+ QVERIFY(m14 >= c02);
+ QVERIFY(!(m14 < c02));
+ QVERIFY(m14 > c02);
+ QVERIFY(m14 != c02);
+
+ // Cursor <-> DocumentCursor
+ QVERIFY(c02 == m02);
+ QVERIFY(c02 <= m02);
+ QVERIFY(c02 >= m02);
+ QVERIFY(!(c02 < m02));
+ QVERIFY(!(c02 > m02));
+ QVERIFY(!(c02 != m02));
+
+ QVERIFY(!(c02 == m04));
+ QVERIFY(c02 <= m04);
+ QVERIFY(!(c02 >= m04));
+ QVERIFY(c02 < m04);
+ QVERIFY(!(c02 > m04));
+ QVERIFY(c02 != m04);
+
+ QVERIFY(!(c04 == m02));
+ QVERIFY(!(c04 <= m02));
+ QVERIFY(c04 >= m02);
+ QVERIFY(!(c04 < m02));
+ QVERIFY(c04 > m02);
+ QVERIFY(c04 != m02);
+
+ QVERIFY(!(c02 == m14));
+ QVERIFY(c02 <= m14);
+ QVERIFY(!(c02 >= m14));
+ QVERIFY(c02 < m14);
+ QVERIFY(!(c02 > m14));
+ QVERIFY(c02 != m14);
+
+ QVERIFY(!(c14 == m02));
+ QVERIFY(!(c14 <= m02));
+ QVERIFY(c14 >= m02);
+ QVERIFY(!(c14 < m02));
+ QVERIFY(c14 > m02);
+ QVERIFY(c14 != m02);
+}
diff --git a/part/tests/kte_documentcursor.h b/part/tests/kte_documentcursor.h
new file mode 100644
index 0000000..ccb2283
--- /dev/null
+++ b/part/tests/kte_documentcursor.h
@@ -0,0 +1,41 @@
+/* This file is part of the KDE libraries
+ Copyright (C) 2012 Dominik Haumann <dhaumann kde org>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+*/
+
+#ifndef KTE_DOCUMENTCURSOR_TEST_H
+#define KTE_DOCUMENTCURSOR_TEST_H
+
+#include <QtCore/QObject>
+
+class DocumentCursorTest : public QObject
+{
+ Q_OBJECT
+
+public:
+ DocumentCursorTest();
+ ~DocumentCursorTest();
+
+private Q_SLOTS:
+ void initTestCase();
+ void cleanupTestCase();
+
+ void testConvenienceApi();
+ void testOperators();
+};
+
+#endif
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic