File indexing completed on 2024-04-14 03:54:57

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2010-2018 Dominik Haumann <dhaumann@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "revision_test.h"
0009 #include "moc_revision_test.cpp"
0010 
0011 #include <katebuffer.h>
0012 #include <katedocument.h>
0013 #include <kateglobal.h>
0014 #include <ktexteditor/cursor.h>
0015 #include <ktexteditor/range.h>
0016 
0017 #include <QTest>
0018 
0019 using namespace KTextEditor;
0020 
0021 QTEST_MAIN(RevisionTest)
0022 
0023 RevisionTest::RevisionTest()
0024     : QObject()
0025 {
0026     KTextEditor::EditorPrivate::enableUnitTestMode();
0027 }
0028 
0029 RevisionTest::~RevisionTest()
0030 {
0031 }
0032 
0033 // tests: MovingInterface
0034 // - lockRevision()
0035 // - revision()
0036 // - unlockRevision()
0037 // - transformCursor()
0038 void RevisionTest::testTransformCursor()
0039 {
0040     KTextEditor::DocumentPrivate doc;
0041 
0042     // initial saved revision of unsaved document is -1
0043     QVERIFY(doc.lastSavedRevision() == -1);
0044 
0045     // initial revision is always 0
0046     QCOMPARE(doc.revision(), (qint64)0);
0047 
0048     // one edit action -> revision now 1, last saved still -1
0049     doc.insertText(Cursor(0, 0), QStringLiteral("0000"));
0050 
0051     qint64 rev = doc.revision();
0052     QCOMPARE(rev, (qint64)1);
0053 
0054     // now lock current revision 1
0055     doc.lockRevision(rev);
0056 
0057     // wrapLine + insertText + wrapLine + insertText
0058     doc.insertText(Cursor(0, 2), QStringLiteral("\n1111\n2222"));
0059 
0060     // create some cursors, then transform them
0061     Cursor c01(0, 1);
0062     Cursor stayOnInsert(0, 2);
0063     Cursor moveOnInsert(0, 2);
0064 
0065     doc.transformCursor(c01, MovingCursor::MoveOnInsert, rev, -1);
0066     doc.transformCursor(moveOnInsert, MovingCursor::MoveOnInsert, rev, -1);
0067     doc.transformCursor(stayOnInsert, MovingCursor::StayOnInsert, rev, -1);
0068 
0069     QCOMPARE(c01, Cursor(0, 1));
0070     QCOMPARE(stayOnInsert, Cursor(0, 2));
0071     QCOMPARE(moveOnInsert, Cursor(2, 4));
0072 
0073     // free revision and lock current again
0074     doc.unlockRevision(rev);
0075     rev = doc.revision();
0076     doc.lockRevision(rev);
0077 
0078     // now undo, the cursors should move to original positions again
0079     doc.undo();
0080 
0081     // inverse transformation
0082     doc.transformCursor(c01, MovingCursor::MoveOnInsert, rev, -1);
0083     doc.transformCursor(moveOnInsert, MovingCursor::MoveOnInsert, rev, -1);
0084     doc.transformCursor(stayOnInsert, MovingCursor::StayOnInsert, rev, -1);
0085 
0086     QCOMPARE(c01, Cursor(0, 1));
0087     QCOMPARE(stayOnInsert, Cursor(0, 2));
0088     QCOMPARE(moveOnInsert, Cursor(0, 2));
0089 }
0090 
0091 // tests:
0092 // - transformRange()
0093 void RevisionTest::testTransformRange()
0094 {
0095     KTextEditor::DocumentPrivate doc;
0096 
0097     QCOMPARE(doc.revision(), (qint64)0);
0098 
0099     doc.setText(
0100         QStringLiteral("00\n"
0101                        "11"));
0102 
0103     // now lock current revision
0104     qint64 rev = doc.revision();
0105     doc.lockRevision(rev);
0106 
0107     Range r1(Cursor(0, 0), Cursor(1, 2));
0108     Range r2(Cursor(0, 1), Cursor(1, 1));
0109     Range invalidOnEmpty(Cursor(0, 1), Cursor(1, 1));
0110 
0111     // remove text
0112     doc.removeText(Range(Cursor(0, 0), Cursor(1, 2)));
0113 
0114     doc.transformRange(r1, MovingRange::ExpandLeft | MovingRange::ExpandRight, MovingRange::AllowEmpty, rev, -1);
0115     doc.transformRange(r2, MovingRange::ExpandLeft | MovingRange::ExpandRight, MovingRange::AllowEmpty, rev, -1);
0116     doc.transformRange(invalidOnEmpty, MovingRange::ExpandLeft | MovingRange::ExpandRight, MovingRange::InvalidateIfEmpty, rev, -1);
0117 
0118     QCOMPARE(r1, Range(Cursor(0, 0), Cursor(0, 0)));
0119     QCOMPARE(r2, Range(Cursor(0, 0), Cursor(0, 0)));
0120     QCOMPARE(invalidOnEmpty, Range::invalid());
0121 
0122     // free revision and lock current again
0123     doc.unlockRevision(rev);
0124     rev = doc.revision();
0125     doc.lockRevision(rev);
0126 
0127     // now undo
0128     doc.undo();
0129 
0130     // r1 should span the entire document
0131     // r2 should be empty at end of document
0132     // invalidOnEmpty should stay invalid
0133     doc.transformRange(r1, MovingRange::ExpandLeft | MovingRange::ExpandRight, MovingRange::AllowEmpty, rev, -1);
0134     doc.transformRange(r2, MovingRange::ExpandRight, MovingRange::AllowEmpty, rev, -1);
0135     doc.transformRange(invalidOnEmpty, MovingRange::ExpandLeft | MovingRange::ExpandRight, MovingRange::AllowEmpty, rev, -1);
0136 
0137     QCOMPARE(r1, Range(Cursor(0, 0), Cursor(1, 2)));
0138     QCOMPARE(r2, Range(Cursor(1, 2), Cursor(1, 2)));
0139     QCOMPARE(invalidOnEmpty, Range::invalid());
0140 }