File indexing completed on 2024-09-01 13:29:25
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2016 Dominik Haumann <dhaumann@kde.org> 0004 SPDX-FileCopyrightText: 2010 Christoph Cullmann <cullmann@kde.org> 0005 SPDX-FileCopyrightText: 2005 Hamish Rodda <rodda@kde.org> 0006 0007 SPDX-License-Identifier: LGPL-2.0-or-later 0008 */ 0009 0010 #include "range_test.h" 0011 #include "moc_range_test.cpp" 0012 0013 #include <kateconfig.h> 0014 #include <katedocument.h> 0015 #include <kateglobal.h> 0016 #include <kateview.h> 0017 #include <ktexteditor/movingcursor.h> 0018 #include <ktexteditor/movingrange.h> 0019 0020 #include <QtTestWidgets> 0021 0022 QTEST_MAIN(RangeTest) 0023 0024 #define testNewRow() (QTest::newRow(QString("line %1").arg(__LINE__).toAscii().data())) 0025 0026 RangeTest::RangeTest() 0027 : QObject() 0028 { 0029 KTextEditor::EditorPrivate::enableUnitTestMode(); 0030 } 0031 0032 RangeTest::~RangeTest() 0033 { 0034 } 0035 0036 void RangeTest::rangeCheck(KTextEditor::Range &valid) 0037 { 0038 QVERIFY(valid.isValid() && valid.start() <= valid.end()); 0039 0040 KTextEditor::Cursor before(0, 1); 0041 KTextEditor::Cursor start(0, 2); 0042 KTextEditor::Cursor end(1, 4); 0043 KTextEditor::Cursor after(1, 10); 0044 0045 KTextEditor::Range result(start, end); 0046 QVERIFY(valid.isValid() && valid.start() <= valid.end()); 0047 0048 valid.setRange(start, end); 0049 QVERIFY(valid.isValid() && valid.start() <= valid.end()); 0050 QCOMPARE(valid, result); 0051 0052 valid.setRange(end, start); 0053 QVERIFY(valid.isValid() && valid.start() <= valid.end()); 0054 QCOMPARE(valid, result); 0055 0056 valid.setStart(after); 0057 QVERIFY(valid.isValid() && valid.start() <= valid.end()); 0058 QCOMPARE(valid, KTextEditor::Range(after, after)); 0059 0060 valid = result; 0061 QCOMPARE(valid, result); 0062 0063 valid.setEnd(before); 0064 QVERIFY(valid.isValid() && valid.start() <= valid.end()); 0065 QCOMPARE(valid, KTextEditor::Range(before, before)); 0066 } 0067 0068 void RangeTest::testTextEditorRange() 0069 { 0070 // test simple range 0071 KTextEditor::Range range; 0072 rangeCheck(range); 0073 } 0074 0075 void RangeTest::testTextRange() 0076 { 0077 // test text range 0078 KTextEditor::DocumentPrivate doc; 0079 KTextEditor::MovingRange *complexRange = doc.newMovingRange(KTextEditor::Range()); 0080 KTextEditor::Range range = *complexRange; 0081 rangeCheck(range); 0082 delete complexRange; 0083 } 0084 0085 void RangeTest::testInsertText() 0086 { 0087 KTextEditor::DocumentPrivate doc; 0088 0089 // Multi-line insert 0090 KTextEditor::MovingCursor *cursor1 = doc.newMovingCursor(KTextEditor::Cursor(), KTextEditor::MovingCursor::StayOnInsert); 0091 KTextEditor::MovingCursor *cursor2 = doc.newMovingCursor(KTextEditor::Cursor(), KTextEditor::MovingCursor::MoveOnInsert); 0092 0093 doc.insertText(KTextEditor::Cursor(), QLatin1String("Test Text\nMore Test Text")); 0094 QCOMPARE(doc.documentEnd(), KTextEditor::Cursor(1, 14)); 0095 0096 QString text = doc.text(KTextEditor::Range(1, 0, 1, 14)); 0097 QCOMPARE(text, QLatin1String("More Test Text")); 0098 0099 // Check cursors and ranges have moved properly 0100 QCOMPARE(cursor1->toCursor(), KTextEditor::Cursor(0, 0)); 0101 QCOMPARE(cursor2->toCursor(), KTextEditor::Cursor(1, 14)); 0102 0103 KTextEditor::Cursor cursor3 = doc.endOfLine(1); 0104 0105 // Set up a few more lines 0106 doc.insertText(*cursor2, QLatin1String("\nEven More Test Text")); 0107 QCOMPARE(doc.documentEnd(), KTextEditor::Cursor(2, 19)); 0108 QCOMPARE(cursor3, doc.endOfLine(1)); 0109 } 0110 0111 void RangeTest::testCornerCaseInsertion() 0112 { 0113 KTextEditor::DocumentPrivate doc; 0114 0115 // lock first revision 0116 doc.lockRevision(0); 0117 0118 KTextEditor::MovingRange *rangeEdit = doc.newMovingRange(KTextEditor::Range(0, 0, 0, 0)); 0119 QCOMPARE(rangeEdit->toRange(), KTextEditor::Range(0, 0, 0, 0)); 0120 0121 doc.insertText(KTextEditor::Cursor(0, 0), QLatin1String("\n")); 0122 QCOMPARE(rangeEdit->toRange(), KTextEditor::Range(1, 0, 1, 0)); 0123 0124 // test translate 0125 KTextEditor::Range translateTest(0, 0, 0, 0); 0126 doc.transformRange(translateTest, KTextEditor::MovingRange::DoNotExpand, KTextEditor::MovingRange::AllowEmpty, 0); 0127 QCOMPARE(translateTest, KTextEditor::Range(1, 0, 1, 0)); 0128 0129 // test translate reverse 0130 KTextEditor::Range reverseTranslateTest(1, 0, 1, 0); 0131 doc.transformRange(reverseTranslateTest, KTextEditor::MovingRange::DoNotExpand, KTextEditor::MovingRange::AllowEmpty, -1, 0); 0132 QCOMPARE(reverseTranslateTest, KTextEditor::Range(0, 0, 0, 0)); 0133 } 0134 0135 void RangeTest::testCursorStringConversion() 0136 { 0137 using KTextEditor::Cursor; 0138 0139 KTextEditor::Cursor c; 0140 QCOMPARE(c.line(), 0); 0141 QCOMPARE(c.column(), 0); 0142 QCOMPARE(c.toString(), QStringLiteral("(0, 0)")); 0143 c = Cursor::fromString(QStringLiteral("(0, 0)")); 0144 QCOMPARE(c.toString(), QStringLiteral("(0, 0)")); 0145 c = Cursor::fromString(QStringLiteral("(0,0)")); 0146 QCOMPARE(c.toString(), QStringLiteral("(0, 0)")); 0147 0148 c.setPosition(-1, -1); 0149 QCOMPARE(c.toString(), QStringLiteral("(-1, -1)")); 0150 c = Cursor::fromString(QStringLiteral("(-1, -1)")); 0151 QCOMPARE(c.toString(), QStringLiteral("(-1, -1)")); 0152 c = Cursor::fromString(QStringLiteral("(-1,-1)")); 0153 QCOMPARE(c.toString(), QStringLiteral("(-1, -1)")); 0154 0155 c.setPosition(12, 42); 0156 QCOMPARE(c.toString(), QStringLiteral("(12, 42)")); 0157 c = Cursor::fromString(QStringLiteral("(12, 42)")); 0158 QCOMPARE(c.toString(), QStringLiteral("(12, 42)")); 0159 c = Cursor::fromString(QStringLiteral("( 12,42)")); 0160 QCOMPARE(c.toString(), QStringLiteral("(12, 42)")); 0161 0162 c.setPosition(12, 42); 0163 QCOMPARE(c.toString(), QStringLiteral("(12, 42)")); 0164 c = Cursor::fromString(QStringLiteral("(12, 42)")); 0165 QCOMPARE(c.toString(), QStringLiteral("(12, 42)")); 0166 0167 c.setPosition(-12, 42); 0168 QCOMPARE(c.toString(), QStringLiteral("(-12, 42)")); 0169 c = Cursor::fromString(QStringLiteral("(-12, 42)")); 0170 QCOMPARE(c.toString(), QStringLiteral("(-12, 42)")); 0171 c = Cursor::fromString(QStringLiteral("(-12, +42)")); 0172 QCOMPARE(c.toString(), QStringLiteral("(-12, 42)")); 0173 c = Cursor::fromString(QStringLiteral("( -12 , +42)")); 0174 QCOMPARE(c.toString(), QStringLiteral("(-12, 42)")); 0175 c = Cursor::fromString(QStringLiteral("(-12 , 42 )")); 0176 QCOMPARE(c.toString(), QStringLiteral("(-12, 42)")); 0177 0178 // test invalid input 0179 c = Cursor::fromString(QStringLiteral("( - 12 , + 42)")); 0180 QCOMPARE(c.toString(), QStringLiteral("(-1, -1)")); 0181 c = Cursor::fromString(QStringLiteral("(, 42)")); 0182 QCOMPARE(c.toString(), QStringLiteral("(-1, -1)")); 0183 c = Cursor::fromString(QStringLiteral("(-, -)")); 0184 QCOMPARE(c.toString(), QStringLiteral("(-1, -1)")); 0185 c = Cursor::fromString(QStringLiteral("(-, -)")); 0186 QCOMPARE(c.toString(), QStringLiteral("(-1, -1)")); 0187 c = Cursor::fromString(QStringLiteral("(-x,y)")); 0188 QCOMPARE(c.toString(), QStringLiteral("(-1, -1)")); 0189 c = Cursor::fromString(QStringLiteral("(-3,-2y)")); 0190 QCOMPARE(c.toString(), QStringLiteral("(-1, -1)")); 0191 } 0192 0193 void RangeTest::testRangeStringConversion() 0194 { 0195 using KTextEditor::Cursor; 0196 using KTextEditor::Range; 0197 0198 KTextEditor::Range r; 0199 QCOMPARE(r.start(), Cursor(0, 0)); 0200 QCOMPARE(r.end(), Cursor(0, 0)); 0201 QCOMPARE(r.toString(), QStringLiteral("[(0, 0), (0, 0)]")); 0202 0203 r = Range::fromString(QStringLiteral("[(0, 0), (0, 0)]")); 0204 QCOMPARE(r.toString(), QStringLiteral("[(0, 0), (0, 0)]")); 0205 r = Range::fromString(QStringLiteral("[(0,0),(0,0)]")); 0206 QCOMPARE(r.toString(), QStringLiteral("[(0, 0), (0, 0)]")); 0207 r = Range::fromString(QStringLiteral("[(-1, -1), (-1, -1)]")); 0208 QCOMPARE(r.toString(), QStringLiteral("[(-1, -1), (-1, -1)]")); 0209 r = Range::fromString(QStringLiteral("[(-1, -1), (0, 0)]")); 0210 QCOMPARE(r.toString(), QStringLiteral("[(-1, -1), (0, 0)]")); 0211 r = Range::fromString(QStringLiteral("[(0, 0), (-1, -1)]")); 0212 QCOMPARE(r.toString(), QStringLiteral("[(-1, -1), (0, 0)]")); // start > end -> swap 0213 0214 r = Range::fromString(QStringLiteral("[(0, 0), (12, 42)]")); 0215 QCOMPARE(r.toString(), QStringLiteral("[(0, 0), (12, 42)]")); 0216 r = Range::fromString(QStringLiteral("[(12, 42), (0, 0)]")); 0217 QCOMPARE(r.toString(), QStringLiteral("[(0, 0), (12, 42)]")); // start > end -> swap 0218 r = Range::fromString(QStringLiteral("[(12,42),(0,0)]")); 0219 QCOMPARE(r.toString(), QStringLiteral("[(0, 0), (12, 42)]")); // start > end -> swap 0220 r = Range::fromString(QStringLiteral("[(-12, -42), (0, 0)]")); 0221 QCOMPARE(r.toString(), QStringLiteral("[(-12, -42), (0, 0)]")); 0222 r = Range::fromString(QStringLiteral("[(0, 0), (-12, -42)]")); 0223 QCOMPARE(r.toString(), QStringLiteral("[(-12, -42), (0, 0)]")); // start > end -> swap 0224 0225 // invalid input 0226 r = Range::fromString(QStringLiteral("[(0:0)(-12:-42)]")); 0227 QCOMPARE(r.toString(), QStringLiteral("[(-1, -1), (-1, -1)]")); 0228 r = Range::fromString(QStringLiteral("[0,1]")); 0229 QCOMPARE(r.toString(), QStringLiteral("[(-1, -1), (-1, -1)]")); 0230 } 0231 0232 void RangeTest::testLineRangeStringConversion() 0233 { 0234 using KTextEditor::LineRange; 0235 0236 LineRange r; 0237 QCOMPARE(r.start(), 0); 0238 QCOMPARE(r.end(), 0); 0239 QCOMPARE(r.toString(), QStringLiteral("[0, 0]")); 0240 0241 r = LineRange::fromString(QStringLiteral("[0, 0]")); 0242 QCOMPARE(r.toString(), QStringLiteral("[0, 0]")); 0243 r = LineRange::fromString(QStringLiteral("[0,0]")); 0244 QCOMPARE(r.toString(), QStringLiteral("[0, 0]")); 0245 r = LineRange::fromString(QStringLiteral("[-1, -1]")); 0246 QCOMPARE(r.toString(), QStringLiteral("[-1, -1]")); 0247 r = LineRange::fromString(QStringLiteral("[-1, 0]")); 0248 QCOMPARE(r.toString(), QStringLiteral("[-1, 0]")); 0249 r = LineRange::fromString(QStringLiteral("[0, -1]")); 0250 QCOMPARE(r.toString(), QStringLiteral("[-1, 0]")); // start > end -> swap 0251 0252 r = LineRange::fromString(QStringLiteral("[12, 42]")); 0253 QCOMPARE(r.toString(), QStringLiteral("[12, 42]")); 0254 r = LineRange::fromString(QStringLiteral("[12, 0]")); 0255 QCOMPARE(r.toString(), QStringLiteral("[0, 12]")); // start > end -> swap 0256 r = LineRange::fromString(QStringLiteral("[12, 0]")); 0257 QCOMPARE(r.toString(), QStringLiteral("[0, 12]")); // start > end -> swap 0258 r = LineRange::fromString(QStringLiteral("[-12, 0]")); 0259 QCOMPARE(r.toString(), QStringLiteral("[-12, 0]")); 0260 r = LineRange::fromString(QStringLiteral("[0, -12]")); 0261 QCOMPARE(r.toString(), QStringLiteral("[-12, 0]")); // start > end -> swap 0262 0263 // invalid input 0264 r = LineRange::fromString(QStringLiteral("[0:0]")); 0265 QCOMPARE(r.toString(), QStringLiteral("[-1, -1]")); 0266 r = LineRange::fromString(QStringLiteral("[0-1]")); 0267 QCOMPARE(r.toString(), QStringLiteral("[-1, -1]")); 0268 } 0269 0270 void RangeTest::lineRangeCheck(KTextEditor::LineRange &range) 0271 { 0272 QVERIFY(range.isValid()); 0273 QVERIFY(range.start() <= range.end()); 0274 QCOMPARE(range.numberOfLines(), range.end() - range.start()); 0275 } 0276 0277 void RangeTest::testLineRange() 0278 { 0279 KTextEditor::LineRange range; 0280 QCOMPARE(range.start(), 0); 0281 QCOMPARE(range.end(), 0); 0282 lineRangeCheck(range); 0283 0284 range.setRange(3, 5); 0285 QCOMPARE(range.start(), 3); 0286 QCOMPARE(range.end(), 5); 0287 lineRangeCheck(range); 0288 0289 range.setRange(5, 3); 0290 QCOMPARE(range.start(), 3); 0291 QCOMPARE(range.end(), 5); 0292 lineRangeCheck(range); 0293 0294 range.setStart(2); 0295 QCOMPARE(range.start(), 2); 0296 QCOMPARE(range.end(), 5); 0297 lineRangeCheck(range); 0298 0299 range.setStart(6); 0300 QCOMPARE(range.start(), 6); 0301 QCOMPARE(range.end(), 6); 0302 lineRangeCheck(range); 0303 0304 range.setEnd(8); 0305 QCOMPARE(range.start(), 6); 0306 QCOMPARE(range.end(), 8); 0307 lineRangeCheck(range); 0308 0309 range.setEnd(4); 0310 QCOMPARE(range.start(), 4); 0311 QCOMPARE(range.end(), 4); 0312 lineRangeCheck(range); 0313 }