File indexing completed on 2024-12-22 04:28:10

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "plaintexteditfindbartest.h"
0008 #include "plaintexteditor/plaintexteditfindbar.h"
0009 #include "widgets/findutils.h"
0010 #include <QPlainTextEdit>
0011 #include <QTest>
0012 
0013 QTEST_MAIN(PlainTextEditFindBarTest)
0014 Q_DECLARE_METATYPE(TextCustomEditor::TextEditFindBarBase::FindFlags)
0015 PlainTextEditFindBarTest::PlainTextEditFindBarTest(QObject *parent)
0016     : QObject{parent}
0017 {
0018 }
0019 
0020 void PlainTextEditFindBarTest::shouldHaveDefaultValues()
0021 {
0022     QPlainTextEdit edit;
0023     TextCustomEditor::PlainTextEditFindBar w(&edit);
0024     QVERIFY(w.text().isEmpty());
0025 }
0026 
0027 class PlainTextEditFindBarExample : public TextCustomEditor::PlainTextEditFindBar
0028 {
0029 public:
0030     explicit PlainTextEditFindBarExample(QPlainTextEdit *view, QWidget *parent = nullptr)
0031         : PlainTextEditFindBar(view, parent)
0032     {
0033     }
0034     ~PlainTextEditFindBarExample() override = default;
0035     [[nodiscard]] bool searchInDocument(const QString &text, TextEditFindBarBase::FindFlags searchOptions) override
0036     {
0037         return TextCustomEditor::PlainTextEditFindBar::searchInDocument(text, searchOptions);
0038     }
0039 
0040     [[nodiscard]] bool searchInDocument(const QRegularExpression &regExp, TextEditFindBarBase::FindFlags searchOptions) override
0041     {
0042         return TextCustomEditor::PlainTextEditFindBar::searchInDocument(regExp, searchOptions);
0043     }
0044 };
0045 
0046 void PlainTextEditFindBarTest::shouldSearchText_data()
0047 {
0048     QTest::addColumn<QString>("text");
0049     QTest::addColumn<QString>("searchText");
0050     QTest::addColumn<TextCustomEditor::TextEditFindBarBase::FindFlags>("flags");
0051     QTest::addColumn<bool>("found");
0052 
0053     QTest::newRow("empty") << QString() << QStringLiteral("blabla") << TextCustomEditor::TextEditFindBarBase::FindFlags() << false;
0054     {
0055         TextCustomEditor::TextEditFindBarBase::FindFlags flags;
0056         flags |= TextCustomEditor::TextEditFindBarBase::FindRespectDiacritics;
0057         QTest::newRow("empty-1") << QString() << QStringLiteral("blabla") << flags << false;
0058     }
0059     {
0060         TextCustomEditor::TextEditFindBarBase::FindFlags flags;
0061         flags |= TextCustomEditor::TextEditFindBarBase::FindCaseSensitively;
0062         QTest::newRow("empty-2") << QString() << QStringLiteral("blabla") << flags << false;
0063     }
0064     {
0065         TextCustomEditor::TextEditFindBarBase::FindFlags flags;
0066         flags |= TextCustomEditor::TextEditFindBarBase::FindWholeWords;
0067         QTest::newRow("findwholewords-1") << QStringLiteral("bla bla") << QStringLiteral("bla") << flags << true;
0068 
0069         QTest::newRow("findwholewords-2") << QStringLiteral("blabla") << QStringLiteral("bla") << flags << false;
0070     }
0071     {
0072         TextCustomEditor::TextEditFindBarBase::FindFlags flags;
0073         flags |= TextCustomEditor::TextEditFindBarBase::FindRespectDiacritics;
0074         QTest::newRow("find-diacritics-1") << QStringLiteral("numéro numéro") << QStringLiteral("numero") << flags << false;
0075 
0076         QTest::newRow("find-diacritics-2") << QStringLiteral("numéro numéro") << QStringLiteral("numéro") << flags << true;
0077         QTest::newRow("find-diacritics-3") << QStringLiteral("numéro numéro il est là. Numero numéro") << QStringLiteral("numero") << flags << true;
0078     }
0079 }
0080 
0081 void PlainTextEditFindBarTest::shouldSearchText()
0082 {
0083     QFETCH(QString, text);
0084     QFETCH(QString, searchText);
0085     QFETCH(TextCustomEditor::TextEditFindBarBase::FindFlags, flags);
0086     QFETCH(bool, found);
0087 
0088     QPlainTextEdit edit;
0089     PlainTextEditFindBarExample w(&edit);
0090     edit.setPlainText(text);
0091 
0092     QCOMPARE(w.searchInDocument(searchText, flags), found);
0093 }
0094 
0095 void PlainTextEditFindBarTest::shouldReplaceAllText_data()
0096 {
0097     QTest::addColumn<QString>("text");
0098     QTest::addColumn<QString>("searchText");
0099     QTest::addColumn<QString>("replaceText");
0100     QTest::addColumn<TextCustomEditor::TextEditFindBarBase::FindFlags>("flags");
0101     QTest::addColumn<int>("nbElement");
0102     QTest::addColumn<QString>("resultStr");
0103 
0104     QTest::newRow("empty") << QString() << QStringLiteral("blabla") << QStringLiteral("replace") << TextCustomEditor::TextEditFindBarBase::FindFlags() << 0
0105                            << QString();
0106 
0107     {
0108         TextCustomEditor::TextEditFindBarBase::FindFlags flags;
0109         flags |= TextCustomEditor::TextEditFindBarBase::FindWholeWords;
0110         QTest::newRow("wholewords") << QStringLiteral("bla bla") << QStringLiteral("bla") << QStringLiteral("replace") << flags << 2
0111                                     << QStringLiteral("replace replace");
0112     }
0113     {
0114         TextCustomEditor::TextEditFindBarBase::FindFlags flags;
0115         flags |= TextCustomEditor::TextEditFindBarBase::FindWholeWords;
0116         flags |= TextCustomEditor::TextEditFindBarBase::FindRespectDiacritics;
0117         QTest::newRow("wholewords-diacritics") << QStringLiteral("réunion reunion réunion") << QStringLiteral("réunion") << QStringLiteral("replace") << flags
0118                                                << 2 << QStringLiteral("replace reunion replace");
0119     }
0120     {
0121         TextCustomEditor::TextEditFindBarBase::FindFlags flags;
0122         flags |= TextCustomEditor::TextEditFindBarBase::FindWholeWords;
0123         QTest::newRow("wholewords-no-diacritics") << QStringLiteral("réunion reunion réunion") << QStringLiteral("réunion") << QStringLiteral("replace")
0124                                                   << flags << 3 << QStringLiteral("replace replace replace");
0125     }
0126     {
0127         TextCustomEditor::TextEditFindBarBase::FindFlags flags;
0128         flags |= TextCustomEditor::TextEditFindBarBase::FindWholeWords;
0129         QTest::newRow("wholewords-no-diacritics-2") << QStringLiteral("réunion réunion réunion") << QStringLiteral("réunion") << QStringLiteral("replace")
0130                                                     << flags << 3 << QStringLiteral("replace replace replace");
0131     }
0132     {
0133         TextCustomEditor::TextEditFindBarBase::FindFlags flags;
0134         flags |= TextCustomEditor::TextEditFindBarBase::FindWholeWords;
0135         QTest::newRow("wholewords-no-diacritics-3") << QStringLiteral("voiture voiture voiture") << QStringLiteral("réunion") << QStringLiteral("replace")
0136                                                     << flags << 0 << QStringLiteral("voiture voiture voiture");
0137     }
0138     {
0139         TextCustomEditor::TextEditFindBarBase::FindFlags flags;
0140         flags |= TextCustomEditor::TextEditFindBarBase::FindWholeWords;
0141         QTest::newRow("wholewords-no-diacritics-3") << QStringLiteral("voiture voiture il est là. voiture") << QStringLiteral("voiture")
0142                                                     << QStringLiteral("replace") << flags << 3 << QStringLiteral("replace replace il est là. replace");
0143     }
0144     {
0145         TextCustomEditor::TextEditFindBarBase::FindFlags flags;
0146         flags |= TextCustomEditor::TextEditFindBarBase::FindWholeWords;
0147         QTest::newRow("wholewords-no-diacritics-4") << QStringLiteral("réunion réunion . Test... réunion") << QStringLiteral("réunion")
0148                                                     << QStringLiteral("replace") << flags << 3 << QStringLiteral("replace replace . Test... replace");
0149     }
0150 }
0151 
0152 void PlainTextEditFindBarTest::shouldReplaceAllText()
0153 {
0154     QFETCH(QString, text);
0155     QFETCH(QString, searchText);
0156     QFETCH(QString, replaceText);
0157     QFETCH(TextCustomEditor::TextEditFindBarBase::FindFlags, flags);
0158     QFETCH(int, nbElement);
0159     QFETCH(QString, resultStr);
0160 
0161     QPlainTextEdit edit;
0162     edit.setPlainText(text);
0163     PlainTextEditFindBarExample w(&edit);
0164     const int nbElementResult = TextCustomEditor::FindUtils::replaceAll(&edit, searchText, replaceText, flags);
0165     QCOMPARE(edit.toPlainText(), resultStr);
0166     QCOMPARE(nbElementResult, nbElement);
0167 }
0168 
0169 #include "moc_plaintexteditfindbartest.cpp"