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

Add more autotest

parent a2c2fc8c
Pipeline #144481 passed with stages
in 2 minutes and 32 seconds
......@@ -4,3 +4,8 @@ ecm_add_tests(richtexteditortest.cpp
NAME_PREFIX "kpimtextedit-richtesteditor-"
LINK_LIBRARIES KF5::PimTextEdit Qt${QT_MAJOR_VERSION}::Test Qt${QT_MAJOR_VERSION}::Widgets
)
ecm_add_tests(richtexteditfindbartest.cpp
NAME_PREFIX "kpimtextedit-plaintexteditor-"
LINK_LIBRARIES KF5::PimTextEdit Qt${QT_MAJOR_VERSION}::Test Qt${QT_MAJOR_VERSION}::Widgets
)
/*
SPDX-FileCopyrightText: 2022 Laurent Montel <montel@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "richtexteditfindbartest.h"
#include "texteditor/commonwidget/findutils.h"
#include "texteditor/richtexteditor/richtexteditfindbar.h"
#include <QPlainTextEdit>
#include <QTest>
QTEST_MAIN(RichTextEditFindBarTest)
Q_DECLARE_METATYPE(KPIMTextEdit::TextEditFindBarBase::FindFlags)
RichTextEditFindBarTest::RichTextEditFindBarTest(QObject *parent)
: QObject{parent}
{
}
void RichTextEditFindBarTest::shouldHaveDefaultValues()
{
QTextEdit edit;
KPIMTextEdit::RichTextEditFindBar w(&edit);
QVERIFY(w.text().isEmpty());
}
class RichTextEditFindBarExample : public KPIMTextEdit::RichTextEditFindBar
{
public:
explicit RichTextEditFindBarExample(QTextEdit *view, QWidget *parent = nullptr)
: RichTextEditFindBar(view, parent)
{
}
~RichTextEditFindBarExample() override = default;
Q_REQUIRED_RESULT bool searchInDocument(const QString &text, TextEditFindBarBase::FindFlags searchOptions) override
{
return KPIMTextEdit::RichTextEditFindBar::searchInDocument(text, searchOptions);
}
Q_REQUIRED_RESULT bool searchInDocument(const QRegularExpression &regExp, TextEditFindBarBase::FindFlags searchOptions) override
{
return KPIMTextEdit::RichTextEditFindBar::searchInDocument(regExp, searchOptions);
}
};
void RichTextEditFindBarTest::shouldSearchText_data()
{
QTest::addColumn<QString>("text");
QTest::addColumn<QString>("searchText");
QTest::addColumn<KPIMTextEdit::TextEditFindBarBase::FindFlags>("flags");
QTest::addColumn<bool>("found");
QTest::newRow("empty") << QString() << QStringLiteral("blabla") << KPIMTextEdit::TextEditFindBarBase::FindFlags() << false;
{
KPIMTextEdit::TextEditFindBarBase::FindFlags flags;
flags |= KPIMTextEdit::TextEditFindBarBase::FindRespectDiacritics;
QTest::newRow("empty-1") << QString() << QStringLiteral("blabla") << flags << false;
}
{
KPIMTextEdit::TextEditFindBarBase::FindFlags flags;
flags |= KPIMTextEdit::TextEditFindBarBase::FindCaseSensitively;
QTest::newRow("empty-2") << QString() << QStringLiteral("blabla") << flags << false;
}
{
KPIMTextEdit::TextEditFindBarBase::FindFlags flags;
flags |= KPIMTextEdit::TextEditFindBarBase::FindWholeWords;
QTest::newRow("findwholewords-1") << QStringLiteral("bla bla") << QStringLiteral("bla") << flags << true;
QTest::newRow("findwholewords-2") << QStringLiteral("blabla") << QStringLiteral("bla") << flags << false;
}
{
KPIMTextEdit::TextEditFindBarBase::FindFlags flags;
flags |= KPIMTextEdit::TextEditFindBarBase::FindRespectDiacritics;
QTest::newRow("find-diacritics-1") << QStringLiteral("numéro numéro") << QStringLiteral("numero") << flags << false;
QTest::newRow("find-diacritics-2") << QStringLiteral("numéro numéro") << QStringLiteral("numéro") << flags << true;
}
}
void RichTextEditFindBarTest::shouldSearchText()
{
QFETCH(QString, text);
QFETCH(QString, searchText);
QFETCH(KPIMTextEdit::TextEditFindBarBase::FindFlags, flags);
QFETCH(bool, found);
QTextEdit edit;
RichTextEditFindBarExample w(&edit);
edit.setPlainText(text);
QCOMPARE(w.searchInDocument(searchText, flags), found);
}
void RichTextEditFindBarTest::shouldReplaceAllText_data()
{
QTest::addColumn<QString>("text");
QTest::addColumn<QString>("searchText");
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
<< QString();
{
KPIMTextEdit::TextEditFindBarBase::FindFlags flags;
flags |= KPIMTextEdit::TextEditFindBarBase::FindWholeWords;
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 << 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 << QStringLiteral("replace replace replace");
}
}
void RichTextEditFindBarTest::shouldReplaceAllText()
{
QFETCH(QString, text);
QFETCH(QString, searchText);
QFETCH(QString, replaceText);
QFETCH(KPIMTextEdit::TextEditFindBarBase::FindFlags, flags);
QFETCH(int, nbElement);
QFETCH(QString, resultStr);
QTextEdit edit;
edit.setPlainText(text);
RichTextEditFindBarExample w(&edit);
QCOMPARE(KPIMTextEdit::FindUtils::replaceAll(&edit, searchText, replaceText, flags), nbElement);
}
/*
SPDX-FileCopyrightText: 2022 Laurent Montel <montel@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#pragma once
#include <QObject>
class RichTextEditFindBarTest : public QObject
{
Q_OBJECT
public:
explicit RichTextEditFindBarTest(QObject *parent = nullptr);
~RichTextEditFindBarTest() override = default;
private Q_SLOTS:
void shouldHaveDefaultValues();
void shouldSearchText_data();
void shouldSearchText();
void shouldReplaceAllText_data();
void shouldReplaceAllText();
};
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