From f40c28f91514356155a42f44f9eb69c7faa83eec Mon Sep 17 00:00:00 2001 From: Christian Loose Date: Mon, 27 Jan 2014 12:27:38 +0100 Subject: Q(Plain)TextEdit: Add find() overload with QRegExp Add overloads to the find() methods in QPlainTextEdit and QTextEdit that find the next occurrence matching the passed regular expression. These are convenience methods that eliminate the need to use the document() method and the need to handle the QTextCursor return value. [ChangeLog][QtWidgets][QPlainTextEdit] Added find method overload using QRegExp [ChangeLog][QtWidgets][QTextEdit] Added find method overload using QRegExp Change-Id: Ia6139b771e3ae4ca02e4b8ea7fde19e5dc71b9d8 Reviewed-by: Marc Mutz --- .../widgets/widgets/qtextedit/tst_qtextedit.cpp | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp') diff --git a/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp b/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp index d06807eedb..53c76a0da6 100644 --- a/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp +++ b/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp @@ -205,6 +205,12 @@ private slots: void countTextChangedOnRemove(); +#ifndef QT_NO_REGEXP + void findWithRegExp(); + void findBackwardWithRegExp(); + void findWithRegExpReturnsFalseIfNoMoreResults(); +#endif + private: void createSelection(); int blockCount() const; @@ -2515,5 +2521,44 @@ void tst_QTextEdit::countTextChangedOnRemove() QCOMPARE(spy.count(), 1); } +#ifndef QT_NO_REGEXP +void tst_QTextEdit::findWithRegExp() +{ + ed->setHtml(QStringLiteral("arbitrary text")); + QRegExp rx("\\w{2}xt"); + + bool found = ed->find(rx); + + QVERIFY(found == true); + QCOMPARE(ed->textCursor().selectedText(), QStringLiteral("text")); +} + +void tst_QTextEdit::findBackwardWithRegExp() +{ + ed->setPlainText(QStringLiteral("arbitrary text")); + QTextCursor cursor = ed->textCursor(); + cursor.movePosition(QTextCursor::End); + ed->setTextCursor(cursor); + QRegExp rx("a\\w*t"); + + bool found = ed->find(rx, QTextDocument::FindBackward); + + QVERIFY(found == true); + QCOMPARE(ed->textCursor().selectedText(), QStringLiteral("arbit")); +} + +void tst_QTextEdit::findWithRegExpReturnsFalseIfNoMoreResults() +{ + ed->setPlainText(QStringLiteral("arbitrary text")); + QRegExp rx("t.xt"); + ed->find(rx); + + bool found = ed->find(rx); + + QVERIFY(found == false); + QCOMPARE(ed->textCursor().selectedText(), QStringLiteral("text")); +} +#endif + QTEST_MAIN(tst_QTextEdit) #include "tst_qtextedit.moc" -- cgit v1.2.3