File indexing completed on 2024-05-05 04:35:22
0001 #include "modeltest.h" 0002 0003 #include <QTest> 0004 #include <QTemporaryFile> 0005 #include <QDebug> 0006 #include <QUrl> 0007 0008 #include <KTextEditor/Editor> 0009 #include <KTextEditor/View> 0010 0011 #include "../model.h" 0012 0013 namespace Css { 0014 0015 void ModelTest::testCompletionRange() 0016 { 0017 KTextEditor::Document* doc = KTextEditor::Editor::instance()->createDocument(nullptr); 0018 doc->setText("body{font-w:normal;}"); 0019 //01234567890123456789 0020 KTextEditor::View* view = doc->createView(nullptr); 0021 CodeCompletionModel* model = new CodeCompletionModel(doc); 0022 0023 KTextEditor::Cursor position(0, 9); 0024 KTextEditor::Range range = model->completionRange(view, position); 0025 qDebug() << range << doc->text(range); 0026 QCOMPARE(range, KTextEditor::Range(0, 5, 0, 11)); 0027 QCOMPARE(doc->text(range), QString("font-w")); 0028 0029 position = KTextEditor::Cursor(0, 10); 0030 range = model->completionRange(view, position); 0031 qDebug() << range << doc->text(range); 0032 QCOMPARE(range, KTextEditor::Range(0, 5, 0, 11)); 0033 QCOMPARE(doc->text(range), QString("font-w")); 0034 0035 0036 position = KTextEditor::Cursor(0, 11); 0037 range = model->completionRange(view, position); 0038 qDebug() << range << doc->text(range); 0039 QCOMPARE(range, KTextEditor::Range(0, 5, 0, 11)); 0040 QCOMPARE(doc->text(range), QString("font-w")); 0041 0042 doc->setText("body{font-:normal;}"); 0043 //01234567890123456789 0044 0045 position = KTextEditor::Cursor(0, 9); 0046 range = model->completionRange(view, position); 0047 qDebug() << range << doc->text(range); 0048 QCOMPARE(range, KTextEditor::Range(0, 5, 0, 10)); 0049 QCOMPARE(doc->text(range), QString("font-")); 0050 0051 position = KTextEditor::Cursor(0, 10); 0052 range = model->completionRange(view, position); 0053 qDebug() << range << doc->text(range); 0054 QCOMPARE(range, KTextEditor::Range(0, 5, 0, 10)); 0055 QCOMPARE(doc->text(range), QString("font-")); 0056 0057 doc->setText("font-"); 0058 //01234567890123456789 0059 0060 position = KTextEditor::Cursor(0, 5); 0061 range = model->completionRange(view, position); 0062 qDebug() << range << doc->text(range); 0063 QCOMPARE(range, KTextEditor::Range(0, 0, 0, 5)); 0064 QCOMPARE(doc->text(range), QString("font-")); 0065 0066 doc->setText("font-weight:normal"); 0067 //01234567890123456789 0068 0069 position = KTextEditor::Cursor(0, 12); 0070 range = model->completionRange(view, position); 0071 qDebug() << range << doc->text(range); 0072 QCOMPARE(range, KTextEditor::Range(0, 12, 0, 18)); 0073 QCOMPARE(doc->text(range), QString("normal")); 0074 0075 position = KTextEditor::Cursor(0, 15); 0076 range = model->completionRange(view, position); 0077 qDebug() << range << doc->text(range); 0078 QCOMPARE(range, KTextEditor::Range(0, 12, 0, 18)); 0079 QCOMPARE(doc->text(range), QString("normal")); 0080 delete doc; 0081 } 0082 0083 void ModelTest::testCompletionRangeSecondLine() 0084 { 0085 KTextEditor::Document* doc = KTextEditor::Editor::instance()->createDocument(nullptr); 0086 doc->setText("body{color:red;}\nbody{font-w:normal;}"); 0087 // 01234567890123456789 0088 KTextEditor::View* view = doc->createView(nullptr); 0089 CodeCompletionModel* model = new CodeCompletionModel(doc); 0090 0091 KTextEditor::Cursor position(1, 9); 0092 KTextEditor::Range range = model->completionRange(view, position); 0093 qDebug() << range << doc->text(range); 0094 QCOMPARE(range, KTextEditor::Range(1, 5, 1, 11)); 0095 QCOMPARE(doc->text(range), QString("font-w")); 0096 delete doc; 0097 } 0098 0099 void ModelTest::completionItems_data() 0100 { 0101 QTest::addColumn<QString>("type"); 0102 QTest::addColumn<QString>("text"); 0103 QTest::addColumn<QStringList>("result"); 0104 0105 // | is the cursor 0106 0107 QTest::newRow("element") << "css" 0108 << "body{font-|w:normal;}" 0109 << (QStringList() << "font-weight"); 0110 0111 QTest::newRow("new element") << "css" 0112 << "body{|}" 0113 << (QStringList() << "font-weight"); 0114 0115 QTest::newRow("new element without value") << "css" 0116 << "body{font-w|}" 0117 << (QStringList() << "font-weight"); 0118 0119 QTest::newRow("new element without value and without brace") << "css" 0120 //01234567890123456789 0121 << "body{font-w|" 0122 << (QStringList() << "font-weight"); 0123 0124 QTest::newRow("field") << "css" 0125 //01234567890123456789 0126 << "body{font-weight:|normal;}" 0127 << (QStringList() << "normal" << "bold"); 0128 0129 QTest::newRow("field without expression") << "css" 0130 << "body{font-weight:|}" 0131 << (QStringList() << "normal" << "bold"); 0132 0133 QTest::newRow("field without expression and without brace") << "css" 0134 //012345678901234567 0135 << "body{font-weight:|" 0136 << (QStringList() << "normal" << "bold"); 0137 0138 QTest::newRow("field without expression and without brace with space") << "css" 0139 //0123456789012345678 0140 << "body{font-weight: |" 0141 << (QStringList() << "normal" << "bold"); 0142 0143 QTest::newRow("second field different property2") << "css" 0144 << "body{font-weight: n|; float: left; }" 0145 << (QStringList() << "normal" << "bold"); 0146 0147 QTest::newRow("second field different property") << "css" 0148 << "body{font-weight: normal; float: |}" 0149 << (QStringList() << "right" << "left"); 0150 0151 QTest::newRow("second field without value") << "css" 0152 << "body{font-weight: |; float: left}" 0153 << (QStringList() << "normal" << "bold"); 0154 0155 QTest::newRow("second field without value without semicolon") << "css" 0156 << "body{font-weight: | float: left}" 0157 << (QStringList() << "normal" << "bold"); 0158 0159 QTest::newRow("second field") << "css" 0160 //01234567890123456789 0161 << "body{font-weight:normal; |}" 0162 << (QStringList() << "font-weight"); 0163 0164 QTest::newRow("element second line") << "css" 0165 << "body{color:red;}\nbody{font|-w:normal;}" 0166 << (QStringList() << "font-weight"); 0167 0168 QTest::newRow("selector at start") << "css" 0169 << "|body{}" 0170 << (QStringList() << "body" << "a"); 0171 0172 QTest::newRow("selector") << "css" 0173 << "body{}|" 0174 << (QStringList() << "body" << "a"); 0175 0176 QTest::newRow("selector second line") << "css" 0177 << "body{font-weight: bolder;}|\nbody{font-weight: asdf;}" 0178 << (QStringList() << "body" << "a"); 0179 0180 QTest::newRow("selector with space") << "css" 0181 << "body{} |" 0182 << (QStringList() << "body" << "a"); 0183 0184 QTest::newRow("selector separated with space") << "css" 0185 << "body |{}" 0186 << (QStringList() << "body" << "a"); 0187 0188 QTest::newRow("selector separated with comma") << "css" 0189 << "body, |{}" 0190 << (QStringList() << "body" << "a"); 0191 0192 QTest::newRow("selector separated with space without braces") << "css" 0193 << "body |" 0194 << (QStringList() << "body" << "a"); 0195 0196 QTest::newRow("selector separated with comma without braces") << "css" 0197 << "body, |" 0198 << (QStringList() << "body" << "a"); 0199 0200 QTest::newRow("selector empty text") << "css" 0201 << "|" 0202 << (QStringList() << "body" << "a"); 0203 0204 //************************** HTML 0205 0206 QTest::newRow("in empty style element") << "html" 0207 << "<html><style>|</style></html>" 0208 << (QStringList() << "body" << "a"); 0209 0210 QTest::newRow("outside style element") << "html" 0211 << "<html>|<style></style></html>" 0212 << (QStringList()); 0213 0214 QTest::newRow("empty html") << "html" 0215 << "" 0216 << (QStringList()); 0217 0218 QTest::newRow("html fields") << "html" 0219 << "<html><style>a { font-weight: b|; }</style></html>" 0220 << (QStringList() << "bold" << "normal"); 0221 } 0222 0223 void ModelTest::completionItems() 0224 { 0225 KTextEditor::Document* doc = KTextEditor::Editor::instance()->createDocument(nullptr); 0226 0227 QFETCH(QString, text); 0228 QFETCH(QString, type); 0229 0230 KTextEditor::Cursor position; 0231 QString textBeforeCursor = text.left(text.indexOf('|')); 0232 position.setLine(textBeforeCursor.count('\n')); 0233 position.setColumn(textBeforeCursor.mid(textBeforeCursor.lastIndexOf('\n')).length()); 0234 0235 text.replace('|', ""); 0236 0237 QTemporaryFile file("XXXXXXXXX."+type); 0238 file.open(); 0239 file.write(text.toUtf8()); 0240 file.close(); 0241 0242 doc->openUrl(QUrl::fromLocalFile(QDir::current().absoluteFilePath(file.fileName()))); 0243 0244 QCOMPARE(doc->mimeType(), QString("text/")+type); 0245 0246 KTextEditor::View* view = doc->createView(nullptr); 0247 CodeCompletionModel* model = new CodeCompletionModel(doc); 0248 0249 QCOMPARE(model->rowCount(), 0); 0250 model->completionInvoked(view, model->completionRange(view, position), KTextEditor::CodeCompletionModel::ManualInvocation); 0251 QStringList completions; 0252 for (int i=0; i < model->rowCount(); ++i) { 0253 completions << model->data(model->index(i, CodeCompletionModel::Name), Qt::DisplayRole).toString(); 0254 } 0255 qDebug() << "completions" << completions; 0256 QFETCH(QStringList, result); 0257 foreach (const QString &i, result) { 0258 QVERIFY(completions.contains(i)); 0259 } 0260 0261 delete doc; 0262 } 0263 0264 } 0265 0266 QTEST_MAIN(Css::ModelTest) 0267