/* * SPDX-License-Identifier: LGPL-2.0-or-later * * Live test: run the ":"-verbs directly (OllieCommands::runVerb) on a real * KTextEditor Document/View and confirm the buffer is mutated. This is the path * the command palette uses, independent of the editor's command registry. * Requires a (headless) QApplication; run with QT_QPA_PLATFORM=offscreen. */ #include "olliecommands.h" #include #include #include #include using namespace deft; class TestOllieCommandsLive : public QObject { Q_OBJECT KTextEditor::Editor *m_editor = nullptr; KTextEditor::View *makeView(const QString &text, KTextEditor::Document *&docOut) { docOut = m_editor->createDocument(this); docOut->setText(text); return docOut->createView(nullptr); } private Q_SLOTS: void initTestCase() { m_editor = KTextEditor::Editor::instance(); QVERIFY(m_editor); } void caseUpperWholeDocument() { KTextEditor::Document *doc = nullptr; KTextEditor::View *v = makeView(QStringLiteral("helloWorld"), doc); QString msg; QVERIFY2(OllieCommands::runVerb(v, QStringLiteral("case upper"), msg), qPrintable(msg)); QCOMPARE(doc->text(), QStringLiteral("HELLOWORLD")); delete v; delete doc; } void rot13OnSelection() { KTextEditor::Document *doc = nullptr; KTextEditor::View *v = makeView(QStringLiteral("abc XYZ"), doc); v->setSelection(doc->documentRange()); QString msg; QVERIFY(OllieCommands::runVerb(v, QStringLiteral("rot13"), msg)); QCOMPARE(doc->text(), QStringLiteral("nop KLM")); delete v; delete doc; } void sortLinesWholeDocument() { KTextEditor::Document *doc = nullptr; KTextEditor::View *v = makeView(QStringLiteral("banana\napple\ncherry"), doc); QString msg; QVERIFY(OllieCommands::runVerb(v, QStringLiteral("sort"), msg)); QCOMPARE(doc->text(), QStringLiteral("apple\nbanana\ncherry")); delete v; delete doc; } void uuidInsertsAtCursor() { KTextEditor::Document *doc = nullptr; KTextEditor::View *v = makeView(QString(), doc); QString msg; QVERIFY(OllieCommands::runVerb(v, QStringLiteral("uuid"), msg)); QCOMPARE(doc->text().size(), 36); // 8-4-4-4-12 + 4 hyphens delete v; delete doc; } void base64RoundTripOnSelection() { KTextEditor::Document *doc = nullptr; KTextEditor::View *v = makeView(QStringLiteral("hello"), doc); v->setSelection(doc->documentRange()); QString msg; QVERIFY(OllieCommands::runVerb(v, QStringLiteral("b64enc"), msg)); QCOMPARE(doc->text(), QStringLiteral("aGVsbG8=")); v->setSelection(doc->documentRange()); QVERIFY(OllieCommands::runVerb(v, QStringLiteral("b64dec"), msg)); QCOMPARE(doc->text(), QStringLiteral("hello")); delete v; delete doc; } }; QTEST_MAIN(TestOllieCommandsLive) #include "test_olliecommands_live.moc"