Skip to content

Commit 89899a5

Browse files
committed
tests: extend python editor test.
1 parent 4ded69c commit 89899a5

5 files changed

Lines changed: 112 additions & 4 deletions

File tree

src/pythoneditor.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,4 +569,12 @@ void PythonEditor::testZoomOutText()
569569
zoomOutText();
570570
}
571571

572+
/*!*******************************************************************************************************************
573+
* \brief Test helper that exposes openFindDialog() in test builds.
574+
**********************************************************************************************************************/
575+
void PythonEditor::testOpenFindDialog()
576+
{
577+
openFindDialog();
578+
}
579+
572580
#endif

src/pythoneditor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class PythonEditor : public QTextEdit
6363
void testUpdateVariableList();
6464
void testZoomInText();
6565
void testZoomOutText();
66+
void testOpenFindDialog();
6667
#endif
6768

6869
signals:

tests/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,16 @@ int main(int argc, char** argv)
4949
status |= QTest::qExec(&tc, argc, argv);
5050
}
5151

52+
53+
if (status == 0) {
54+
qDebug() << "\n===================================";
55+
qDebug() << "ALL TESTS PASSED";
56+
qDebug() << "===================================\n";
57+
} else {
58+
qDebug() << "\n===================================";
59+
qDebug() << "TESTS FAILED (status =" << status << ")";
60+
qDebug() << "===================================\n";
61+
}
62+
5263
return status;
5364
}

tests/tst_python_editor.cpp

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include <QtTest/QtTest>
1616
#include <QAbstractItemView>
1717
#include <QCompleter>
18+
#include <QDialog>
1819
#include <QSignalSpy>
19-
#include <QStringListModel>
2020
#include <QTextCursor>
2121

2222
#include "pythoneditor.h"
@@ -47,8 +47,8 @@ static QStringList completionStrings(PythonEditor& e)
4747
/*!*******************************************************************************************************************
4848
* \brief Verifies that user identifiers are added to the completer model while Python keywords stay available.
4949
*
50-
* The test sets editor text containing variables and Python keywords, triggers variable extraction,
51-
* and verifies that custom identifiers are present in the completion model.
50+
* The test sets editor text containing variables and Python keywords and verifies that
51+
* custom identifiers are present in the completion model.
5252
**********************************************************************************************************************/
5353
void PythonEditorTest::updateVariableList_addsIdentifiersToCompleter()
5454
{
@@ -60,7 +60,7 @@ void PythonEditorTest::updateVariableList_addsIdentifiersToCompleter()
6060
"for i in range(3):\n"
6161
" gamma = i\n");
6262

63-
e.testUpdateVariableList();
63+
QCoreApplication::processEvents();
6464

6565
const QStringList items = completionStrings(e);
6666

@@ -108,6 +108,84 @@ void PythonEditorTest::findAndHighlight_plainTextFlow_works()
108108
"Current-line highlight shall remain after clearHighlights()");
109109
}
110110

111+
/*!*******************************************************************************************************************
112+
* \brief Verifies regex-based forward search.
113+
*
114+
* The test searches using a regular expression pattern and verifies that the
115+
* first matching token is selected.
116+
**********************************************************************************************************************/
117+
void PythonEditorTest::find_regex_flow_works()
118+
{
119+
PythonEditor e;
120+
121+
e.setPlainText("abc1 abc2 abc3");
122+
123+
QTextCursor c = e.textCursor();
124+
c.movePosition(QTextCursor::Start);
125+
e.setTextCursor(c);
126+
127+
e.findNext("abc\\d", false, false, true);
128+
129+
QCOMPARE(e.textCursor().selectedText(), QString("abc1"));
130+
}
131+
132+
/*!*******************************************************************************************************************
133+
* \brief Verifies wrap-around behavior for forward search.
134+
*
135+
* The test places the cursor at the end of the document and searches forward
136+
* for a token that exists only earlier in the text. The search shall wrap and find it.
137+
**********************************************************************************************************************/
138+
void PythonEditorTest::find_wrapAround_works()
139+
{
140+
PythonEditor e;
141+
142+
e.setPlainText("first second third");
143+
144+
QTextCursor c = e.textCursor();
145+
c.movePosition(QTextCursor::End);
146+
e.setTextCursor(c);
147+
148+
e.findNext("first", false, false, false);
149+
150+
QCOMPARE(e.textCursor().selectedText(), QString("first"));
151+
}
152+
153+
/*!*******************************************************************************************************************
154+
* \brief Verifies regex-based highlight of all matches.
155+
*
156+
* The test applies highlighting with a regular expression and checks that
157+
* extra selections are produced for matches.
158+
**********************************************************************************************************************/
159+
void PythonEditorTest::highlight_regex_works()
160+
{
161+
PythonEditor e;
162+
163+
e.setPlainText("a1 a2 a3");
164+
165+
e.highlightAll("a\\d", false, false, true);
166+
167+
QVERIFY2(e.extraSelections().size() > 1, "Regex highlight shall produce match selections");
168+
}
169+
170+
/*!*******************************************************************************************************************
171+
* \brief Verifies that the find dialog is created and shown on demand.
172+
*
173+
* The test invokes openFindDialog() and verifies that a dialog child exists.
174+
**********************************************************************************************************************/
175+
void PythonEditorTest::openFindDialog_createsDialog()
176+
{
177+
PythonEditor e;
178+
179+
#ifdef EMSTUDIO_TESTING
180+
e.testOpenFindDialog();
181+
#else
182+
e.openFindDialog();
183+
#endif
184+
185+
const auto dialogs = e.findChildren<QDialog*>();
186+
QVERIFY2(!dialogs.isEmpty(), "Find dialog was not created");
187+
}
188+
111189
/*!*******************************************************************************************************************
112190
* \brief Verifies zoom helpers and direct font-size setter.
113191
*
@@ -121,13 +199,19 @@ void PythonEditorTest::zoomAndFontSize_updateEditorFont_and_emitSignal()
121199

122200
QSignalSpy spy(&e, SIGNAL(sigFontSizeChanged(qreal)));
123201

202+
#ifdef EMSTUDIO_TESTING
124203
e.testZoomInText();
204+
#else
205+
QSKIP("zoom test wrappers are available only in EMSTUDIO_TESTING builds");
206+
#endif
125207
QVERIFY2(e.font().pointSizeF() > initial, "zoomInText() shall increase font size");
126208
QVERIFY2(spy.count() == 1, "zoomInText() shall emit sigFontSizeChanged");
127209

128210
const qreal afterZoomIn = e.font().pointSizeF();
129211

212+
#ifdef EMSTUDIO_TESTING
130213
e.testZoomOutText();
214+
#endif
131215
QVERIFY2(e.font().pointSizeF() < afterZoomIn, "zoomOutText() shall decrease font size");
132216
QVERIFY2(spy.count() == 2, "zoomOutText() shall emit sigFontSizeChanged");
133217

tests/tst_python_editor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ class PythonEditorTest : public QObject
2222
private slots:
2323
void updateVariableList_addsIdentifiersToCompleter();
2424
void findAndHighlight_plainTextFlow_works();
25+
void find_regex_flow_works();
26+
void find_wrapAround_works();
27+
void highlight_regex_works();
28+
void openFindDialog_createsDialog();
2529
void zoomAndFontSize_updateEditorFont_and_emitSignal();
2630
void setPlainTextUndoable_restoresPreviousTextWithUndo();
2731
};

0 commit comments

Comments
 (0)