File indexing completed on 2024-12-22 04:28:12
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 "richtexteditfindbartest.h" 0008 #include "richtexteditor/richtexteditfindbar.h" 0009 #include "widgets/findutils.h" 0010 #include <QTest> 0011 #include <QTextEdit> 0012 0013 QTEST_MAIN(RichTextEditFindBarTest) 0014 Q_DECLARE_METATYPE(TextCustomEditor::TextEditFindBarBase::FindFlags) 0015 RichTextEditFindBarTest::RichTextEditFindBarTest(QObject *parent) 0016 : QObject{parent} 0017 { 0018 } 0019 0020 void RichTextEditFindBarTest::shouldHaveDefaultValues() 0021 { 0022 QTextEdit edit; 0023 TextCustomEditor::RichTextEditFindBar w(&edit); 0024 QVERIFY(w.text().isEmpty()); 0025 } 0026 0027 class RichTextEditFindBarExample : public TextCustomEditor::RichTextEditFindBar 0028 { 0029 public: 0030 explicit RichTextEditFindBarExample(QTextEdit *view, QWidget *parent = nullptr) 0031 : RichTextEditFindBar(view, parent) 0032 { 0033 } 0034 ~RichTextEditFindBarExample() override = default; 0035 [[nodiscard]] bool searchInDocument(const QString &text, TextEditFindBarBase::FindFlags searchOptions) override 0036 { 0037 return TextCustomEditor::RichTextEditFindBar::searchInDocument(text, searchOptions); 0038 } 0039 0040 [[nodiscard]] bool searchInDocument(const QRegularExpression ®Exp, TextEditFindBarBase::FindFlags searchOptions) override 0041 { 0042 return TextCustomEditor::RichTextEditFindBar::searchInDocument(regExp, searchOptions); 0043 } 0044 }; 0045 0046 void RichTextEditFindBarTest::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 } 0078 } 0079 0080 void RichTextEditFindBarTest::shouldSearchText() 0081 { 0082 QFETCH(QString, text); 0083 QFETCH(QString, searchText); 0084 QFETCH(TextCustomEditor::TextEditFindBarBase::FindFlags, flags); 0085 QFETCH(bool, found); 0086 0087 QTextEdit edit; 0088 RichTextEditFindBarExample w(&edit); 0089 edit.setPlainText(text); 0090 0091 QCOMPARE(w.searchInDocument(searchText, flags), found); 0092 } 0093 0094 void RichTextEditFindBarTest::shouldReplaceAllText_data() 0095 { 0096 QTest::addColumn<QString>("text"); 0097 QTest::addColumn<QString>("searchText"); 0098 QTest::addColumn<QString>("replaceText"); 0099 QTest::addColumn<TextCustomEditor::TextEditFindBarBase::FindFlags>("flags"); 0100 QTest::addColumn<int>("nbElement"); 0101 QTest::addColumn<QString>("resultStr"); 0102 0103 QTest::newRow("empty") << QString() << QStringLiteral("blabla") << QStringLiteral("replace") << TextCustomEditor::TextEditFindBarBase::FindFlags() << 0 0104 << QString(); 0105 0106 { 0107 TextCustomEditor::TextEditFindBarBase::FindFlags flags; 0108 flags |= TextCustomEditor::TextEditFindBarBase::FindWholeWords; 0109 QTest::newRow("wholewords") << QStringLiteral("bla bla") << QStringLiteral("bla") << QStringLiteral("replace") << flags << 2 0110 << QStringLiteral("replace replace"); 0111 } 0112 { 0113 TextCustomEditor::TextEditFindBarBase::FindFlags flags; 0114 flags |= TextCustomEditor::TextEditFindBarBase::FindWholeWords; 0115 flags |= TextCustomEditor::TextEditFindBarBase::FindRespectDiacritics; 0116 QTest::newRow("wholewords-diacritics") << QStringLiteral("réunion reunion réunion") << QStringLiteral("réunion") << QStringLiteral("replace") << flags 0117 << 2 << QStringLiteral("replace reunion replace"); 0118 } 0119 { 0120 TextCustomEditor::TextEditFindBarBase::FindFlags flags; 0121 flags |= TextCustomEditor::TextEditFindBarBase::FindWholeWords; 0122 QTest::newRow("wholewords-no-diacritics") << QStringLiteral("réunion reunion réunion") << QStringLiteral("réunion") << QStringLiteral("replace") 0123 << flags << 3 << QStringLiteral("replace replace replace"); 0124 } 0125 { 0126 TextCustomEditor::TextEditFindBarBase::FindFlags flags; 0127 flags |= TextCustomEditor::TextEditFindBarBase::FindWholeWords; 0128 QTest::newRow("wholewords-no-diacritics-2") << QStringLiteral("réunion réunion réunion") << QStringLiteral("réunion") << QStringLiteral("replace") 0129 << flags << 3 << QStringLiteral("replace replace replace"); 0130 } 0131 { 0132 TextCustomEditor::TextEditFindBarBase::FindFlags flags; 0133 flags |= TextCustomEditor::TextEditFindBarBase::FindWholeWords; 0134 QTest::newRow("wholewords-no-diacritics-2") << QStringLiteral("voiture voiture voiture") << QStringLiteral("réunion") << QStringLiteral("replace") 0135 << flags << 0 << QStringLiteral("voiture voiture voiture"); 0136 } 0137 } 0138 0139 void RichTextEditFindBarTest::shouldReplaceAllText() 0140 { 0141 QFETCH(QString, text); 0142 QFETCH(QString, searchText); 0143 QFETCH(QString, replaceText); 0144 QFETCH(TextCustomEditor::TextEditFindBarBase::FindFlags, flags); 0145 QFETCH(int, nbElement); 0146 QFETCH(QString, resultStr); 0147 0148 QTextEdit edit; 0149 edit.setPlainText(text); 0150 RichTextEditFindBarExample w(&edit); 0151 const int nbElementResult = TextCustomEditor::FindUtils::replaceAll(&edit, searchText, replaceText, flags); 0152 QCOMPARE(edit.toPlainText(), resultStr); 0153 QCOMPARE(nbElementResult, nbElement); 0154 } 0155 0156 #include "moc_richtexteditfindbartest.cpp"