Changeset View
Changeset View
Standalone View
Standalone View
autotests/src/variable_test.cpp
- This file was added.
1 | /* This file is part of the KDE project | ||||
---|---|---|---|---|---|
2 | * | ||||
3 | * Copyright 2019 Dominik Haumann <dhaumann@kde.org> | ||||
4 | * | ||||
5 | * This library is free software; you can redistribute it and/or | ||||
6 | * modify it under the terms of the GNU Library General Public | ||||
7 | * License as published by the Free Software Foundation; either | ||||
8 | * version 2 of the License, or (at your option) any later version. | ||||
9 | * | ||||
10 | * This library is distributed in the hope that it will be useful, | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | * Library General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU Library General Public License | ||||
16 | * along with this library; see the file COPYING.LIB. If not, write to | ||||
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | * Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | #include "variable_test.h" | ||||
21 | #include "moc_variable_test.cpp" | ||||
22 | | ||||
23 | #include <kateglobal.h> | ||||
24 | #include <ktexteditor/editor.h> | ||||
25 | #include <ktexteditor/document.h> | ||||
26 | #include <ktexteditor/view.h> | ||||
27 | | ||||
28 | #include <QtTest> | ||||
29 | | ||||
30 | using namespace KTextEditor; | ||||
31 | | ||||
32 | QTEST_MAIN(VariableTest) | ||||
33 | | ||||
34 | VariableTest::VariableTest() | ||||
35 | : QObject() | ||||
36 | { | ||||
37 | KTextEditor::EditorPrivate::enableUnitTestMode(); | ||||
38 | } | ||||
39 | | ||||
40 | VariableTest::~VariableTest() | ||||
41 | { | ||||
42 | } | ||||
43 | | ||||
44 | void VariableTest::testReturnValues() | ||||
45 | { | ||||
46 | auto editor = KTextEditor::Editor::instance(); | ||||
47 | | ||||
48 | const QString name = QStringLiteral("Document:"); | ||||
49 | auto func = [](const QStringView&, KTextEditor::View*) { return QString(); }; | ||||
50 | | ||||
51 | // exact matches | ||||
52 | QVERIFY(!editor->unregisterVariableMatch(name)); | ||||
53 | QVERIFY(editor->registerVariableMatch(name, "Document Text", func)); | ||||
54 | QVERIFY(!editor->registerVariableMatch(name, "Document Text", func)); | ||||
55 | QVERIFY(editor->unregisterVariableMatch(name)); | ||||
56 | QVERIFY(!editor->unregisterVariableMatch(name)); | ||||
57 | | ||||
58 | // prefix matches | ||||
59 | QVERIFY(!editor->unregisterVariablePrefix(name)); | ||||
60 | QVERIFY(editor->registerVariablePrefix(name, "Document Text", func)); | ||||
61 | QVERIFY(!editor->registerVariablePrefix(name, "Document Text", func)); | ||||
62 | QVERIFY(editor->unregisterVariablePrefix(name)); | ||||
63 | QVERIFY(!editor->unregisterVariablePrefix(name)); | ||||
64 | } | ||||
65 | | ||||
66 | void VariableTest::testExactMatch_data() | ||||
67 | { | ||||
68 | QTest::addColumn<QString>("text"); | ||||
69 | QTest::addColumn<QString>("expected"); | ||||
70 | QTest::addColumn<QString>("expectedText"); | ||||
71 | | ||||
72 | QTest::newRow("World") << "World" << "World" << "World"; | ||||
73 | QTest::newRow("Smart World") << "Smart World" << "Smart World" << "Smart World"; | ||||
74 | } | ||||
75 | | ||||
76 | void VariableTest::testExactMatch() | ||||
77 | { | ||||
78 | QFETCH(QString, text); | ||||
79 | QFETCH(QString, expected); | ||||
80 | QFETCH(QString, expectedText); | ||||
81 | | ||||
82 | auto editor = KTextEditor::Editor::instance(); | ||||
83 | auto doc = editor->createDocument(nullptr); | ||||
84 | auto view = doc->createView(nullptr); | ||||
85 | doc->setText(text); | ||||
86 | | ||||
87 | const QString name = QStringLiteral("Doc:Text"); | ||||
88 | auto func = [](const QStringView&, KTextEditor::View* view) { | ||||
89 | return view->document()->text(); | ||||
90 | }; | ||||
91 | | ||||
92 | QVERIFY(editor->registerVariableMatch(name, "Document Text", func)); | ||||
93 | | ||||
94 | // expandVariable | ||||
95 | QString output; | ||||
96 | QVERIFY(editor->expandVariable(QStringLiteral("Doc:Text"), view, output)); | ||||
97 | QCOMPARE(output, expected); | ||||
98 | | ||||
99 | // expandText | ||||
100 | editor->expandText(QStringLiteral("Hello %{Doc:Text}!"), view, output); | ||||
101 | QCOMPARE(output, QStringLiteral("Hello ") + expectedText + QLatin1Char('!')); | ||||
102 | | ||||
103 | editor->expandText(QStringLiteral("Hello %{Doc:Text} %{Doc:Text}!"), view, output); | ||||
104 | QCOMPARE(output, QStringLiteral("Hello ") + expectedText + QLatin1Char(' ') + expectedText + QLatin1Char('!')); | ||||
105 | | ||||
106 | QVERIFY(editor->unregisterVariableMatch("Doc:Text")); | ||||
107 | | ||||
108 | delete doc; | ||||
109 | } | ||||
110 | | ||||
111 | void VariableTest::testPrefixMatch() | ||||
112 | { | ||||
113 | auto editor = KTextEditor::Editor::instance(); | ||||
114 | | ||||
115 | const QString prefix = QStringLiteral("Mirror:"); | ||||
116 | auto func = [](const QStringView& text, KTextEditor::View*) { | ||||
117 | QStringView rest = text.right(text.size() - 7); | ||||
118 | QString out; | ||||
119 | for (auto it = rest.rbegin(); it != rest.rend(); ++it) { | ||||
120 | out += *it; | ||||
121 | } | ||||
122 | return out; | ||||
123 | }; | ||||
124 | | ||||
125 | QVERIFY(editor->registerVariablePrefix(prefix, "Reverse text", func)); | ||||
126 | | ||||
127 | QString output; | ||||
128 | QVERIFY(editor->expandVariable(QStringLiteral("Mirror:12345"), nullptr, output)); | ||||
129 | QCOMPARE(output, QStringLiteral("54321")); | ||||
130 | | ||||
131 | editor->expandText(QStringLiteral("Countdown: %{Mirror:12345}"), nullptr, output); | ||||
132 | QCOMPARE(output, QStringLiteral("Countdown: 54321")); | ||||
133 | | ||||
134 | // Test recursive expansion | ||||
135 | editor->expandText(QStringLiteral("Countup: %{Mirror:%{Mirror:12345}}"), nullptr, output); | ||||
136 | QCOMPARE(output, QStringLiteral("Countup: 12345")); | ||||
137 | | ||||
138 | QVERIFY(editor->unregisterVariablePrefix(prefix)); | ||||
139 | } | ||||
140 | | ||||
141 | void VariableTest::testRecursiveMatch() | ||||
142 | { | ||||
143 | auto editor = KTextEditor::Editor::instance(); | ||||
144 | auto doc = editor->createDocument(nullptr); | ||||
145 | auto view = doc->createView(nullptr); | ||||
146 | doc->setText(QStringLiteral("Text")); | ||||
147 | | ||||
148 | const QString name = QStringLiteral("Doc:Text"); | ||||
149 | auto func = [](const QStringView&, KTextEditor::View* view) { | ||||
150 | return view->document()->text(); | ||||
151 | }; | ||||
152 | QVERIFY(editor->registerVariableMatch(name, "Document Text", func)); | ||||
153 | | ||||
154 | // Test recursive expansion | ||||
155 | doc->setText(QStringLiteral("Text")); | ||||
156 | QString output; | ||||
157 | editor->expandText(QStringLiteral("Hello %{Doc:%{Doc:Text}}!"), view, output); | ||||
158 | QCOMPARE(output, QStringLiteral("Hello Text!")); | ||||
159 | | ||||
160 | QVERIFY(editor->unregisterVariableMatch(name)); | ||||
161 | delete doc; | ||||
162 | } | ||||
163 | | ||||
164 | // kate: indent-mode cstyle; indent-width 4; replace-tabs on; |