File indexing completed on 2025-02-16 10:04:21
0001 /* 0002 This file is part of the Kate project. 0003 0004 SPDX-FileCopyrightText: 2021 Waqar Ahmed <waqar.17a@gmail.com> 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 #include "katetextblocktest.h" 0008 0009 #include <katetextblock.h> 0010 #include <katetextbuffer.h> 0011 #include <katetextline.h> 0012 #include <katetextrange.h> 0013 0014 QTEST_MAIN(KateTextBlockTest) 0015 0016 using namespace Kate; 0017 0018 KateTextBlockTest::KateTextBlockTest(QObject *parent) 0019 : QObject(parent) 0020 { 0021 } 0022 0023 void KateTextBlockTest::basicTest() 0024 { 0025 TextBuffer buf(nullptr); 0026 TextBlock block(&buf, 0); 0027 0028 block.appendLine(QStringLiteral("First line")); 0029 QVERIFY(block.lines() == 1); 0030 0031 block.appendLine(QStringLiteral("Second line")); 0032 QVERIFY(block.lines() == 2); 0033 QVERIFY(block.startLine() == 0); 0034 QVERIFY(block.line(block.startLine())->text() == QStringLiteral("First line")); 0035 QVERIFY(block.line(block.startLine() + 1)->text() == QStringLiteral("Second line")); 0036 0037 QString text; 0038 block.text(text); 0039 QVERIFY(text == QStringLiteral("First line\nSecond line")); 0040 0041 block.clearLines(); 0042 QVERIFY(block.lines() == 0); 0043 text.clear(); 0044 block.text(text); 0045 QVERIFY(text.isEmpty()); 0046 } 0047 0048 void KateTextBlockTest::testWrap() 0049 { 0050 TextBuffer buf(nullptr); 0051 TextBlock block(&buf, 0); 0052 0053 block.appendLine(QStringLiteral("First line")); 0054 QVERIFY(block.lines() == 1); 0055 0056 int blockIdx = 0; 0057 block.wrapLine(KTextEditor::Cursor(0, 5), blockIdx); 0058 QString text; 0059 block.text(text); 0060 QVERIFY(text == QStringLiteral("First\n line")); 0061 text.clear(); 0062 0063 block.unwrapLine(1, nullptr, blockIdx); 0064 block.text(text); 0065 QVERIFY(text == QStringLiteral("First line")); 0066 text.clear(); 0067 0068 block.clearLines(); 0069 } 0070 0071 void KateTextBlockTest::testInsertRemoveText() 0072 { 0073 TextBuffer buf(nullptr); 0074 TextBlock block(&buf, 0); 0075 0076 block.appendLine(QStringLiteral("First line")); 0077 QVERIFY(block.lines() == 1); 0078 0079 block.insertText(KTextEditor::Cursor(0, 1), QStringLiteral(" ")); 0080 QString text; 0081 block.text(text); 0082 QVERIFY(text == QStringLiteral("F irst line")); 0083 text.clear(); 0084 0085 block.removeText(KTextEditor::Range(0, 1, 0, 2), text); 0086 QVERIFY(text == QStringLiteral(" ")); 0087 text.clear(); 0088 block.text(text); 0089 QVERIFY(text == QStringLiteral("First line")); 0090 0091 block.clearLines(); 0092 } 0093 0094 void KateTextBlockTest::testSplitMergeBlocks() 0095 { 0096 TextBuffer buf(nullptr); 0097 TextBlock block(&buf, 0); 0098 0099 block.appendLine(QStringLiteral("First line")); 0100 QVERIFY(block.lines() == 1); 0101 block.appendLine(QStringLiteral("Second line")); 0102 QVERIFY(block.lines() == 2); 0103 0104 // split 0105 auto newBlock = block.splitBlock(1); 0106 QVERIFY(newBlock); 0107 0108 QString text; 0109 newBlock->text(text); 0110 QVERIFY(text == QStringLiteral("\nSecond line")); 0111 text.clear(); 0112 block.text(text); 0113 QVERIFY(text == QStringLiteral("First line")); 0114 text.clear(); 0115 0116 // merge 0117 block.mergeBlock(newBlock); 0118 newBlock->text(text); 0119 QVERIFY(text == QStringLiteral("\nSecond line\nFirst line")); 0120 text.clear(); 0121 0122 newBlock->clearLines(); 0123 delete newBlock; 0124 block.clearLines(); 0125 } 0126 0127 void KateTextBlockTest::testTextRanges() 0128 { 0129 TextBuffer buf(nullptr); 0130 TextBlock block(&buf, 0); 0131 0132 block.appendLine(QStringLiteral("First line")); 0133 QVERIFY(block.lines() == 1); 0134 0135 // range over char 'i' in "First" 0136 Kate::TextRange range(buf, KTextEditor::Range(0, 1, 0, 2), KTextEditor::MovingRange::InsertBehavior::ExpandRight); 0137 0138 // update 0139 block.updateRange(&range); 0140 0141 QVERIFY(block.containsRange(&range)); 0142 0143 auto ranges = block.rangesForLine(0, nullptr, false); 0144 QVERIFY(ranges.size() == 1); 0145 QVERIFY((*ranges.first()).start() == range.start()); 0146 QVERIFY((*ranges.first()).end() == range.end()); 0147 0148 // try getting ranges with attributes 0149 QVERIFY(block.rangesForLine(0, nullptr, true).isEmpty()); 0150 0151 // remove 0152 block.removeRange(&range); 0153 QVERIFY(block.cachedRangesForLine(0).isEmpty()); 0154 0155 block.clearLines(); 0156 } 0157 0158 #include "moc_katetextblocktest.cpp"