Commit cb623f43 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Continue to implement autotests

parent ac29a776
Pipeline #144476 passed with stages
in 2 minutes and 18 seconds
......@@ -57,22 +57,49 @@ int FindUtils::replaceAll(QTextEdit *view, const QString &str, const QString &re
int FindUtils::replaceAll(QPlainTextEdit *view, const QString &str, const QString &replaceStr, TextEditFindBarBase::FindFlags searchOptions)
{
auto document = view->document();
view->textCursor().beginEditBlock();
QTextCursor c(document);
int count = 0;
// Ignoring FindBackward when replacing all
const QTextDocument::FindFlags flags = FindUtils::convertTextEditFindFlags(searchOptions) & ~QTextDocument::FindBackward;
while (!c.isNull()) {
c = document->find(str, c, flags);
if (!c.isNull()) {
// find() selects found text, and insertText() replaces selection
c.insertText(replaceStr);
count++;
} else {
break;
if (searchOptions & TextEditFindBarBase::FindFlag::FindRespectDiacritics) {
view->textCursor().beginEditBlock();
QTextCursor c(document);
while (!c.isNull()) {
c = document->find(str, c, flags);
if (!c.isNull()) {
// find() selects found text, and insertText() replaces selection
c.insertText(replaceStr);
count++;
} else {
break;
}
}
view->textCursor().endEditBlock();
} else {
const QString toPlainTextWithoutRespectDiacritics{FindUtils::normalize(view->toPlainText())};
const QString searchStrWithoutRespectDiacritics{FindUtils::normalize(str)};
QTextDocument documentWithoutRespectDiacritics(toPlainTextWithoutRespectDiacritics);
QTextCursor documentWithoutRespectDiacriticsTextCursor(&documentWithoutRespectDiacritics);
QTextCursor docCusor(view->textCursor());
documentWithoutRespectDiacriticsTextCursor.setPosition(docCusor.position());
view->textCursor().beginEditBlock();
QTextCursor c(document);
while (!documentWithoutRespectDiacriticsTextCursor.isNull()) {
documentWithoutRespectDiacriticsTextCursor = document->find(searchStrWithoutRespectDiacritics, documentWithoutRespectDiacriticsTextCursor, flags);
if (!documentWithoutRespectDiacriticsTextCursor.isNull()) {
c.setPosition(documentWithoutRespectDiacriticsTextCursor.selectionStart());
c.setPosition(documentWithoutRespectDiacriticsTextCursor.selectionEnd(), QTextCursor::KeepAnchor);
// find() selects found text, and insertText() replaces selection
c.insertText(replaceStr);
count++;
} else {
break;
}
}
view->textCursor().endEditBlock();
}
view->textCursor().endEditBlock();
return count;
}
......
......@@ -99,26 +99,29 @@ void PlainTextEditFindBarTest::shouldReplaceAllText_data()
QTest::addColumn<QString>("replaceText");
QTest::addColumn<KPIMTextEdit::TextEditFindBarBase::FindFlags>("flags");
QTest::addColumn<int>("nbElement");
QTest::addColumn<QString>("resultStr");
QTest::newRow("empty") << QString() << QStringLiteral("blabla") << QStringLiteral("replace") << KPIMTextEdit::TextEditFindBarBase::FindFlags() << 0;
QTest::newRow("empty") << QString() << QStringLiteral("blabla") << QStringLiteral("replace") << KPIMTextEdit::TextEditFindBarBase::FindFlags() << 0
<< QString();
{
KPIMTextEdit::TextEditFindBarBase::FindFlags flags;
flags |= KPIMTextEdit::TextEditFindBarBase::FindWholeWords;
QTest::newRow("wholewords") << QStringLiteral("bla bla") << QStringLiteral("bla") << QStringLiteral("replace") << flags << 2;
QTest::newRow("wholewords") << QStringLiteral("bla bla") << QStringLiteral("bla") << QStringLiteral("replace") << flags << 2
<< QStringLiteral("replace replace");
}
{
KPIMTextEdit::TextEditFindBarBase::FindFlags flags;
flags |= KPIMTextEdit::TextEditFindBarBase::FindWholeWords;
flags |= KPIMTextEdit::TextEditFindBarBase::FindRespectDiacritics;
QTest::newRow("wholewords-diacritics") << QStringLiteral("réunion reunion réunion") << QStringLiteral("réunion") << QStringLiteral("replace") << flags
<< 2;
<< 2 << QStringLiteral("replace reunion replace");
}
{
KPIMTextEdit::TextEditFindBarBase::FindFlags flags;
flags |= KPIMTextEdit::TextEditFindBarBase::FindWholeWords;
QTest::newRow("wholewords-no-diacritics") << QStringLiteral("réunion reunion réunion") << QStringLiteral("réunion") << QStringLiteral("replace")
<< flags << 3;
<< flags << 3 << QStringLiteral("replace replace replace");
}
}
......@@ -129,6 +132,7 @@ void PlainTextEditFindBarTest::shouldReplaceAllText()
QFETCH(QString, replaceText);
QFETCH(KPIMTextEdit::TextEditFindBarBase::FindFlags, flags);
QFETCH(int, nbElement);
QFETCH(QString, resultStr);
QPlainTextEdit edit;
edit.setPlainText(text);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment