File indexing completed on 2024-07-14 03:55:45

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2019 Dominik Haumann <dhaumann@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "variable_test.h"
0009 #include "moc_variable_test.cpp"
0010 
0011 #include <katedocument.h>
0012 #include <kateglobal.h>
0013 #include <ktexteditor/document.h>
0014 #include <ktexteditor/editor.h>
0015 #include <ktexteditor/view.h>
0016 
0017 #include <QTest>
0018 #include <QUuid>
0019 
0020 using namespace KTextEditor;
0021 
0022 QTEST_MAIN(VariableTest)
0023 
0024 VariableTest::VariableTest()
0025     : QObject()
0026 {
0027     KTextEditor::EditorPrivate::enableUnitTestMode();
0028 }
0029 
0030 VariableTest::~VariableTest()
0031 {
0032 }
0033 
0034 void VariableTest::testReturnValues()
0035 {
0036     auto editor = KTextEditor::Editor::instance();
0037 
0038     const QString name = QStringLiteral("Document:");
0039     auto func = [](const QStringView &, KTextEditor::View *) {
0040         return QString();
0041     };
0042 
0043     // exact matches
0044     QVERIFY(!editor->unregisterVariable(name));
0045     QVERIFY(editor->registerVariableMatch(name, QStringLiteral("Document Text"), func));
0046     QVERIFY(!editor->registerVariableMatch(name, QStringLiteral("Document Text"), func));
0047     QVERIFY(editor->unregisterVariable(name));
0048     QVERIFY(!editor->unregisterVariable(name));
0049 
0050     // prefix matches
0051     QVERIFY(!editor->unregisterVariable(name));
0052     QVERIFY(editor->registerVariablePrefix(name, QStringLiteral("Document Text"), func));
0053     QVERIFY(!editor->registerVariablePrefix(name, QStringLiteral("Document Text"), func));
0054     QVERIFY(editor->unregisterVariable(name));
0055     QVERIFY(!editor->unregisterVariable(name));
0056 }
0057 
0058 void VariableTest::testExactMatch_data()
0059 {
0060     QTest::addColumn<QString>("text");
0061     QTest::addColumn<QString>("expected");
0062     QTest::addColumn<QString>("expectedText");
0063 
0064     QTest::newRow("World") << QStringLiteral("World") << QStringLiteral("World") << QStringLiteral("World");
0065     QTest::newRow("Smart World") << QStringLiteral("Smart World") << QStringLiteral("Smart World") << QStringLiteral("Smart World");
0066 }
0067 
0068 void VariableTest::testExactMatch()
0069 {
0070     QFETCH(QString, text);
0071     QFETCH(QString, expected);
0072     QFETCH(QString, expectedText);
0073 
0074     auto editor = KTextEditor::Editor::instance();
0075     auto doc = editor->createDocument(nullptr);
0076     auto view = doc->createView(nullptr);
0077     doc->setText(text);
0078 
0079     const QString name = QStringLiteral("Doc:Text");
0080     auto func = [](const QStringView &, KTextEditor::View *view) {
0081         return view->document()->text();
0082     };
0083 
0084     QVERIFY(editor->registerVariableMatch(name, QStringLiteral("Document Text"), func));
0085 
0086     // expandVariable
0087     QString output;
0088     QVERIFY(editor->expandVariable(QStringLiteral("Doc:Text"), view, output));
0089     QCOMPARE(output, expected);
0090 
0091     // expandText
0092     output = editor->expandText(QStringLiteral("Hello %{Doc:Text}!"), view);
0093     QCOMPARE(output, QStringLiteral("Hello ") + expectedText + QLatin1Char('!'));
0094 
0095     output = editor->expandText(QStringLiteral("Hello %{Doc:Text} %{Doc:Text}!"), view);
0096     QCOMPARE(output, QStringLiteral("Hello ") + expectedText + QLatin1Char(' ') + expectedText + QLatin1Char('!'));
0097 
0098     QVERIFY(editor->unregisterVariable(QStringLiteral("Doc:Text")));
0099 
0100     delete doc;
0101 }
0102 
0103 void VariableTest::testPrefixMatch()
0104 {
0105     auto editor = KTextEditor::Editor::instance();
0106 
0107     const QString prefix = QStringLiteral("Mirror:");
0108     auto func = [](const QStringView &text, KTextEditor::View *) {
0109         QStringView rest = text.right(text.size() - 7);
0110         QString out;
0111         for (auto it = rest.rbegin(); it != rest.rend(); ++it) {
0112             out += *it;
0113         }
0114         return out;
0115     };
0116 
0117     QVERIFY(editor->registerVariablePrefix(prefix, QStringLiteral("Reverse text"), func));
0118 
0119     QString output;
0120     QVERIFY(editor->expandVariable(QStringLiteral("Mirror:12345"), nullptr, output));
0121     QCOMPARE(output, QStringLiteral("54321"));
0122 
0123     output = editor->expandText(QStringLiteral("Countdown: %{Mirror:12345}"), nullptr);
0124     QCOMPARE(output, QStringLiteral("Countdown: 54321"));
0125 
0126     // Test recursive expansion
0127     output = editor->expandText(QStringLiteral("Countup: %{Mirror:%{Mirror:12345}}"), nullptr);
0128     QCOMPARE(output, QStringLiteral("Countup: 12345"));
0129 
0130     QVERIFY(editor->unregisterVariable(prefix));
0131 }
0132 
0133 void VariableTest::testRecursiveMatch()
0134 {
0135     auto editor = KTextEditor::Editor::instance();
0136     auto doc = editor->createDocument(nullptr);
0137     auto view = doc->createView(nullptr);
0138     doc->setText(QStringLiteral("Text"));
0139 
0140     const QString name = QStringLiteral("Doc:Text");
0141     auto func = [](const QStringView &, KTextEditor::View *view) {
0142         return view->document()->text();
0143     };
0144     QVERIFY(editor->registerVariableMatch(name, QStringLiteral("Document Text"), func));
0145 
0146     // Test recursive expansion
0147     doc->setText(QStringLiteral("Text"));
0148     QString output = editor->expandText(QStringLiteral("Hello %{Doc:%{Doc:Text}}!"), view);
0149     QCOMPARE(output, QStringLiteral("Hello Text!"));
0150 
0151     QVERIFY(editor->unregisterVariable(name));
0152     delete doc;
0153 }
0154 
0155 void VariableTest::testBuiltins()
0156 {
0157     auto editor = KTextEditor::Editor::instance();
0158     auto doc = editor->createDocument(nullptr);
0159     doc->openUrl(QUrl::fromLocalFile(QDir::homePath() + QStringLiteral("/kate-v5.tar.gz")));
0160     doc->setText(QStringLiteral("get an edge in editing\n:-)"));
0161     auto view = doc->createView(nullptr);
0162     view->setCursorPosition(KTextEditor::Cursor(1, 2));
0163     view->show();
0164 
0165     QString out;
0166 
0167     // Test invalid ones:
0168     out = editor->expandText(QStringLiteral("%{}"), view);
0169     QCOMPARE(out, QStringLiteral("%{}"));
0170     out = editor->expandText(QStringLiteral("%{"), view);
0171     QCOMPARE(out, QStringLiteral("%{"));
0172     out = editor->expandText(QStringLiteral("%{{}"), view);
0173     QCOMPARE(out, QStringLiteral("%{{}"));
0174     out = editor->expandText(QStringLiteral("%{{}}"), view);
0175     QCOMPARE(out, QStringLiteral("%{{}}"));
0176 
0177     // Document:FileBaseName
0178     out = editor->expandText(QStringLiteral("%{Document:FileBaseName}"), view);
0179     QCOMPARE(out, QStringLiteral("kate-v5"));
0180 
0181     // Document:FileExtension
0182     out = editor->expandText(QStringLiteral("%{Document:FileExtension}"), view);
0183     QCOMPARE(out, QStringLiteral("tar.gz"));
0184 
0185     // Document:FileName
0186     out = editor->expandText(QStringLiteral("%{Document:FileName}"), view);
0187     QCOMPARE(out, QStringLiteral("kate-v5.tar.gz"));
0188 
0189     // Document:FilePath
0190     out = editor->expandText(QStringLiteral("%{Document:FilePath}"), view);
0191     QCOMPARE(out, QFileInfo(view->document()->url().toLocalFile()).absoluteFilePath());
0192 
0193     // Document:Text
0194     out = editor->expandText(QStringLiteral("%{Document:Text}"), view);
0195     QCOMPARE(out, QStringLiteral("get an edge in editing\n:-)"));
0196 
0197     // Document:Path
0198     out = editor->expandText(QStringLiteral("%{Document:Path}"), view);
0199     QCOMPARE(out, QFileInfo(doc->url().toLocalFile()).absolutePath());
0200 
0201     // Document:NativeFilePath
0202     out = editor->expandText(QStringLiteral("%{Document:NativeFilePath}"), view);
0203     QCOMPARE(out, QDir::toNativeSeparators(QFileInfo(doc->url().toLocalFile()).absoluteFilePath()));
0204 
0205     // Document:NativePath
0206     out = editor->expandText(QStringLiteral("%{Document:NativePath}"), view);
0207     QCOMPARE(out, QDir::toNativeSeparators(QFileInfo(doc->url().toLocalFile()).absolutePath()));
0208 
0209     // Document:NativePath
0210     out = editor->expandText(QStringLiteral("%{Document:NativePath}"), view);
0211     QCOMPARE(out, QDir::toNativeSeparators(QFileInfo(doc->url().toLocalFile()).absolutePath()));
0212 
0213     // Document:Cursor:Line
0214     out = editor->expandText(QStringLiteral("%{Document:Cursor:Line}"), view);
0215     QCOMPARE(out, QStringLiteral("1"));
0216 
0217     // Document:Cursor:Column
0218     out = editor->expandText(QStringLiteral("%{Document:Cursor:Column}"), view);
0219     QCOMPARE(out, QStringLiteral("2"));
0220 
0221     // Document:Cursor:XPos
0222     out = editor->expandText(QStringLiteral("%{Document:Cursor:XPos}"), view);
0223     QVERIFY(out.toInt() > 0);
0224 
0225     // Document:Cursor:YPos
0226     out = editor->expandText(QStringLiteral("%{Document:Cursor:YPos}"), view);
0227     QVERIFY(out.toInt() > 0);
0228 
0229     view->setSelection(KTextEditor::Range(1, 0, 1, 3));
0230     // Document:Selection:Text
0231     out = editor->expandText(QStringLiteral("%{Document:Selection:Text}"), view);
0232     QCOMPARE(out, QStringLiteral(":-)"));
0233 
0234     // Document:Selection:StartLine
0235     out = editor->expandText(QStringLiteral("%{Document:Selection:StartLine}"), view);
0236     QCOMPARE(out, QStringLiteral("1"));
0237 
0238     // Document:Selection:StartColumn
0239     out = editor->expandText(QStringLiteral("%{Document:Selection:StartColumn}"), view);
0240     QCOMPARE(out, QStringLiteral("0"));
0241 
0242     // Document:Selection:EndLine
0243     out = editor->expandText(QStringLiteral("%{Document:Selection:EndLine}"), view);
0244     QCOMPARE(out, QStringLiteral("1"));
0245 
0246     // Document:Selection:EndColumn
0247     out = editor->expandText(QStringLiteral("%{Document:Selection:EndColumn}"), view);
0248     QCOMPARE(out, QStringLiteral("3"));
0249 
0250     // Document:RowCount
0251     out = editor->expandText(QStringLiteral("%{Document:RowCount}"), view);
0252     QCOMPARE(out, QStringLiteral("2"));
0253 
0254     // Document:Variable:<variable>, since KF 5.78
0255     qobject_cast<KTextEditor::DocumentPrivate *>(doc)->setVariable(QStringLiteral("cow-sound"), QStringLiteral("moo"));
0256     out = editor->expandText(QStringLiteral("%{Document:Variable:cow-sound}"), view);
0257     QCOMPARE(out, QStringLiteral("moo"));
0258 
0259     // Date:Locale
0260     out = editor->expandText(QStringLiteral("%{Date:Locale}"), view);
0261     QVERIFY(!out.isEmpty());
0262 
0263     // Date:ISO
0264     out = editor->expandText(QStringLiteral("%{Date:ISO}"), view);
0265     QVERIFY(!out.isEmpty());
0266 
0267     // Date:yyyy-MM-dd
0268     out = editor->expandText(QStringLiteral("%{Date:yyyy-MM-dd}"), view);
0269     QVERIFY(QDate::fromString(out, QStringLiteral("yyyy-MM-dd")).isValid());
0270 
0271     // Time:Locale
0272     out = editor->expandText(QStringLiteral("%{Time:Locale}"), view);
0273     QVERIFY(!out.isEmpty());
0274 
0275     // Time:ISO
0276     out = editor->expandText(QStringLiteral("%{Time:ISO}"), view);
0277     QVERIFY(!out.isEmpty());
0278 
0279     // Time:hh-mm-ss
0280     out = editor->expandText(QStringLiteral("%{Time:hh-mm-ss}"), view);
0281     QVERIFY(QTime::fromString(out, QStringLiteral("hh-mm-ss")).isValid());
0282 
0283     // ENV:KTE_ENV_VAR_TEST
0284     qputenv("KTE_ENV_VAR_TEST", "KTE_ENV_VAR_TEST_VALUE");
0285     out = editor->expandText(QStringLiteral("%{ENV:KTE_ENV_VAR_TEST}"), view);
0286     QCOMPARE(out, QStringLiteral("KTE_ENV_VAR_TEST_VALUE"));
0287 
0288     // JS:<code>
0289     out = editor->expandText(QStringLiteral("%{JS:3 + %{JS:2 + 1}}"), view);
0290     QCOMPARE(out, QStringLiteral("6"));
0291 
0292     // PercentEncoded: since 5.67
0293     out = editor->expandText(QStringLiteral("%{PercentEncoded:{a&b+c=d} \"}"), view);
0294     QCOMPARE(out, QStringLiteral("%7Ba%26b%2Bc%3Dd%7D%20%22"));
0295 
0296     // UUID
0297     out = editor->expandText(QStringLiteral("%{UUID}"), view);
0298     QCOMPARE(out.count(QLatin1Char('-')), 4);
0299 }
0300 
0301 // kate: indent-mode cstyle; indent-width 4; replace-tabs on;