File: kreplacetest.cpp

package info (click to toggle)
ktextwidgets 5.116.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,272 kB
  • sloc: cpp: 5,786; sh: 21; makefile: 7
file content (389 lines) | stat: -rw-r--r-- 17,720 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
    This file is part of the KDE project
    SPDX-FileCopyrightText: 2002 David Faure <david@mandrakesoft.com>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#include "kreplacetest.h"

#include <assert.h>
#include <stdlib.h>

#include <QApplication>
#include <QDebug>
#include <QEventLoop>
#include <QPushButton>

#include <kreplace.h>
#include <kreplacedialog.h>

void KReplaceTest::enterLoop()
{
    QEventLoop eventLoop;
    connect(this, &KReplaceTest::exitLoop, &eventLoop, &QEventLoop::quit);
    eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
}

KReplaceTest::KReplaceTest(const QStringList &text, const QString &buttonName)
    : QObject(nullptr)
    , m_text(text)
    , m_replace(nullptr)
    , m_buttonName(buttonName)
{
}

KReplaceTest::~KReplaceTest()
{
}

void KReplaceTest::replace(const QString &pattern, const QString &replacement, long options)
{
    m_needEventLoop = false;
    // This creates a replace-next-prompt dialog if needed.
    m_replace.reset(new KReplace(pattern, replacement, options));

    // Connect highlight (or textFound) signal to code which handles highlighting of found text.
#if KTEXTWIDGETS_BUILD_DEPRECATED_SINCE(5, 81)
    connect(m_replace.get(), qOverload<const QString &, int, int>(&KFind::highlight), this, &KReplaceTest::slotHighlight);
#else
    connect(m_replace.get(), &KFind::textFound, this, &KReplaceTest::slotHighlight);
#endif

    // Connect findNext signal - called when pressing the button in the dialog
    connect(m_replace.get(), &KFind::findNext, this, &KReplaceTest::slotReplaceNext);

    // Connect replace signal - called when doing a replacement
#if KTEXTWIDGETS_ENABLE_DEPRECATED_SINCE(5, 83)
    connect(m_replace.get(), qOverload<const QString &, int, int, int>(&KReplace::replace), this, &KReplaceTest::slotReplace);
#else
    connect(m_replace.get(), &KReplace::textReplaced, this, &KReplaceTest::slotReplace);
#endif

    // Go to initial position
    if ((options & KFind::FromCursor) == 0) {
        if (m_text.isEmpty()) {
            return;
        }
        if (m_replace->options() & KFind::FindBackwards) {
            m_currentPos = --m_text.end();
        } else {
            m_currentPos = m_text.begin();
        }
    }

    // Launch first replacement
    slotReplaceNext();

    if (m_needEventLoop) {
        enterLoop();
    }
}

void KReplaceTest::slotHighlight(const QString &str, int matchingIndex, int matchedLength)
{
    qDebug() << "slotHighlight Index:" << matchingIndex << " Length:" << matchedLength << " Substr:" << str.mid(matchingIndex, matchedLength);
    // Emulate the user saying yes
    // We need Qt::QueuedConnection (and the enterloop/exitloop)
    // otherwise we get an infinite loop (Match never returned,
    // so slotReplaceNext never returns)
    if (m_replace->options() & KReplaceDialog::PromptOnReplace) {
        QDialog *dlg = m_replace->replaceNextDialog(false);
        disconnect(dlg, &QDialog::finished, m_replace.get(), nullptr); // hack to avoid slotDialogClosed being called
        dlg->hide();

        QPushButton *button = dlg->findChild<QPushButton *>(m_buttonName);
        auto clickFunc = [button]() {
            button->click();
        };
        QMetaObject::invokeMethod(button, clickFunc, Qt::QueuedConnection);

        m_needEventLoop = true;
    }
}

void KReplaceTest::slotReplace(const QString &text, int replacementIndex, int replacedLength, int matchedLength)
{
    Q_UNUSED(replacementIndex);
    Q_UNUSED(replacedLength);
    Q_UNUSED(matchedLength);
    // qDebug() << "index=" << replacementIndex << " replacedLength=" << replacedLength << " matchedLength=" << matchedLength << " text=" << text.left( 50 );
    *m_currentPos = text; // KReplace hacked the replacement into 'text' in already.
}

void KReplaceTest::slotReplaceNext()
{
    // qDebug();
    KFind::Result res = KFind::NoMatch;
    int backwards = m_replace->options() & KFind::FindBackwards;
    while (res == KFind::NoMatch) {
        if (m_replace->needData()) {
            m_replace->setData(*m_currentPos);
        }

        // Let KReplace inspect the text fragment, and display a dialog if a match is found
        res = m_replace->replace();

        if (res == KFind::NoMatch) {
            QStringList::iterator lastItem = backwards ? m_text.begin() : --m_text.end();
            if (m_currentPos == lastItem) {
                break;
            }
            if (m_replace->options() & KFind::FindBackwards) {
                m_currentPos--;
            } else {
                m_currentPos++;
            }
        }
    }

#if 0 // commented out so that this test doesn't require interaction
    if (res == KFind::NoMatch)   // i.e. at end
        if (m_replace->shouldRestart()) {
            if (m_replace->options() & KFind::FindBackwards) {
                m_currentPos = m_text.fromLast();
            } else {
                m_currentPos = m_text.begin();
            }
            slotReplaceNext();
        }
#endif
    if (res == KFind::NoMatch && m_needEventLoop) {
        Q_EMIT exitLoop();
    }
}

void KReplaceTest::print()
{
    QStringList::Iterator it = m_text.begin();
    for (; it != m_text.end(); ++it) {
        qDebug() << *it;
    }
}

/* button is the button that we emulate pressing, when options includes PromptOnReplace.
   Valid possibilities are User1 (replace all) and User3 (replace) */
static void testReplaceSimple(int options, const QString &buttonName = QString())
{
    qDebug() << "testReplaceSimple: " << options;
    KReplaceTest test(QStringList() << QStringLiteral("hellohello"), buttonName);
    test.replace(QStringLiteral("hello"), QStringLiteral("HELLO"), options);
    QStringList textLines = test.textLines();
    assert(textLines.count() == 1);
    if (textLines[0] != QLatin1String("HELLOHELLO")) {
        qCritical() << "ASSERT FAILED: replaced text is '" << textLines[0] << "' instead of 'HELLOHELLO'";
        exit(1);
    }
}

// Replacing "a" with "".
// input="aaaaaa", expected output=""
static void testReplaceBlank(int options, const QString &buttonName = QString())
{
    qDebug() << "testReplaceBlank: " << options;
    KReplaceTest test(QStringList() << QStringLiteral("aaaaaa"), buttonName);
    test.replace(QStringLiteral("a"), QString(), options);
    QStringList textLines = test.textLines();
    assert(textLines.count() == 1);
    if (!textLines[0].isEmpty()) {
        qCritical() << "ASSERT FAILED: replaced text is '" << textLines[0] << "' instead of ''";
        exit(1);
    }
}

// Replacing "" with "foo"
// input="bbbb", expected output="foobfoobfoobfoobfoo"
static void testReplaceBlankSearch(int options, const QString &buttonName = QString())
{
    qDebug() << "testReplaceBlankSearch: " << options;
    KReplaceTest test(QStringList() << QStringLiteral("bbbb"), buttonName);
    test.replace(QString(), QStringLiteral("foo"), options);
    QStringList textLines = test.textLines();
    assert(textLines.count() == 1);
    if (textLines[0] != QLatin1String("foobfoobfoobfoobfoo")) {
        qCritical() << "ASSERT FAILED: replaced text is '" << textLines[0] << "' instead of 'foobfoobfoobfoobfoo'";
        exit(1);
    }
}

static void testReplaceLonger(int options, const QString &buttonName = QString())
{
    qDebug() << "testReplaceLonger: " << options;
    // Standard test of a replacement string longer than the matched string
    KReplaceTest test(QStringList() << QStringLiteral("aaaa"), buttonName);
    test.replace(QStringLiteral("a"), QStringLiteral("bb"), options);
    QStringList textLines = test.textLines();
    assert(textLines.count() == 1);
    if (textLines[0] != QLatin1String("bbbbbbbb")) {
        qCritical() << "ASSERT FAILED: replaced text is '" << textLines[0] << "' instead of 'bbbbbbbb'";
        exit(1);
    }
}

static void testReplaceLongerInclude(int options, const QString &buttonName = QString())
{
    qDebug() << "testReplaceLongerInclude: " << options;
    // Similar test, where the replacement string includes the search string
    KReplaceTest test(QStringList() << QStringLiteral("a foo b"), buttonName);
    test.replace(QStringLiteral("foo"), QStringLiteral("foobar"), options);
    QStringList textLines = test.textLines();
    assert(textLines.count() == 1);
    if (textLines[0] != QLatin1String("a foobar b")) {
        qCritical() << "ASSERT FAILED: replaced text is '" << textLines[0] << "' instead of 'a foobar b'";
        exit(1);
    }
}

static void testReplaceLongerInclude2(int options, const QString &buttonName = QString())
{
    qDebug() << "testReplaceLongerInclude2: " << options;
    // Similar test, but with more chances of matches inside the replacement string
    KReplaceTest test(QStringList() << QStringLiteral("aaaa"), buttonName);
    test.replace(QStringLiteral("a"), QStringLiteral("aa"), options);
    QStringList textLines = test.textLines();
    assert(textLines.count() == 1);
    if (textLines[0] != QLatin1String("aaaaaaaa")) {
        qCritical() << "ASSERT FAILED: replaced text is '" << textLines[0] << "' instead of 'aaaaaaaa'";
        exit(1);
    }
}

// Test for the \0 backref
static void testReplaceBackRef(int options, const QString &buttonName = QString())
{
    KReplaceTest test(QStringList() << QStringLiteral("abc def"), buttonName);
    test.replace(QStringLiteral("abc"), QStringLiteral("(\\0)"), options);
    QStringList textLines = test.textLines();
    assert(textLines.count() == 1);
    QString expected = options & KReplaceDialog::BackReference ? QStringLiteral("(abc) def") : QStringLiteral("(\\0) def");
    if (textLines[0] != expected) {
        qCritical() << "ASSERT FAILED: replaced text is '" << textLines[0] << "' instead of '" << expected << "'";
        exit(1);
    }
}

// Test for other backrefs
static void testReplaceBackRef1(int options, const QString &buttonName = QString())
{
    KReplaceTest test(QStringList() << QStringLiteral("a1 b2 a3"), buttonName);
    test.replace(QStringLiteral("([ab])([\\d])"), QStringLiteral("\\1 and \\2 in (\\0)"), options);
    QStringList textLines = test.textLines();
    assert(textLines.count() == 1);
    QString expected = QStringLiteral("a and 1 in (a1) b and 2 in (b2) a and 3 in (a3)");
    if (textLines[0] != expected) {
        qCritical() << "ASSERT FAILED: replaced text is '" << textLines[0] << "' instead of '" << expected << "'";
        exit(1);
    }
}

static void testReplacementHistory(const QStringList &findHistory, const QStringList &replaceHistory)
{
    KReplaceDialog dlg(nullptr, 0, findHistory, replaceHistory);
    dlg.show();
    qDebug() << "testReplacementHistory:" << dlg.replacementHistory();
    assert(dlg.replacementHistory() == replaceHistory);
}

static void testReplacementHistory()
{
    QStringList findHistory;
    QStringList replaceHistory;
    findHistory << QStringLiteral("foo") << QStringLiteral("bar");
    replaceHistory << QStringLiteral("FOO") << QStringLiteral("BAR");
    testReplacementHistory(findHistory, replaceHistory);

    findHistory.clear();
    replaceHistory.clear();
    findHistory << QStringLiteral("foo") << QStringLiteral("bar");
    replaceHistory << QString() << QStringLiteral("baz"); // #130831
    testReplacementHistory(findHistory, replaceHistory);
}

int main(int argc, char **argv)
{
    QApplication::setApplicationName(QStringLiteral("kreplacetest"));
    QApplication app(argc, argv);

    testReplacementHistory(); // #130831

    testReplaceBlank(0);
    testReplaceBlank(KReplaceDialog::PromptOnReplace, QStringLiteral("replaceButton")); // replace
    testReplaceBlank(KReplaceDialog::PromptOnReplace, QStringLiteral("allButton")); // replace all
    testReplaceBlank(KFind::FindBackwards);
    testReplaceBlank(KFind::FindBackwards | KReplaceDialog::PromptOnReplace, QStringLiteral("replaceButton")); // replace
    testReplaceBlank(KFind::FindBackwards | KReplaceDialog::PromptOnReplace, QStringLiteral("allButton")); // replace all

    testReplaceBlankSearch(0);
    testReplaceBlankSearch(KReplaceDialog::PromptOnReplace, QStringLiteral("replaceButton")); // replace
    testReplaceBlankSearch(KReplaceDialog::PromptOnReplace, QStringLiteral("allButton")); // replace all
    testReplaceBlankSearch(KFind::FindBackwards);
    testReplaceBlankSearch(KFind::FindBackwards | KReplaceDialog::PromptOnReplace, QStringLiteral("replaceButton")); // replace
    testReplaceBlankSearch(KFind::FindBackwards | KReplaceDialog::PromptOnReplace, QStringLiteral("allButton")); // replace all

    testReplaceSimple(0);
    testReplaceSimple(KReplaceDialog::PromptOnReplace, QStringLiteral("replaceButton")); // replace
    testReplaceSimple(KReplaceDialog::PromptOnReplace, QStringLiteral("allButton")); // replace all
    testReplaceSimple(KFind::FindBackwards);
    testReplaceSimple(KFind::FindBackwards | KReplaceDialog::PromptOnReplace, QStringLiteral("replaceButton")); // replace
    testReplaceSimple(KFind::FindBackwards | KReplaceDialog::PromptOnReplace, QStringLiteral("allButton")); // replace all

    testReplaceLonger(0);
    testReplaceLonger(KReplaceDialog::PromptOnReplace, QStringLiteral("replaceButton")); // replace
    testReplaceLonger(KReplaceDialog::PromptOnReplace, QStringLiteral("allButton")); // replace all
    testReplaceLonger(KFind::FindBackwards);
    testReplaceLonger(KFind::FindBackwards | KReplaceDialog::PromptOnReplace, QStringLiteral("replaceButton")); // replace
    testReplaceLonger(KFind::FindBackwards | KReplaceDialog::PromptOnReplace, QStringLiteral("allButton")); // replace all

    testReplaceLongerInclude(0);
    testReplaceLongerInclude(KReplaceDialog::PromptOnReplace, QStringLiteral("replaceButton")); // replace
    testReplaceLongerInclude(KReplaceDialog::PromptOnReplace, QStringLiteral("allButton")); // replace all
    testReplaceLongerInclude(KFind::FindBackwards);
    testReplaceLongerInclude(KFind::FindBackwards | KReplaceDialog::PromptOnReplace, QStringLiteral("replaceButton")); // replace
    testReplaceLongerInclude(KFind::FindBackwards | KReplaceDialog::PromptOnReplace, QStringLiteral("allButton")); // replace all

    testReplaceLongerInclude2(0);
    testReplaceLongerInclude2(KReplaceDialog::PromptOnReplace, QStringLiteral("replaceButton")); // replace
    testReplaceLongerInclude2(KReplaceDialog::PromptOnReplace, QStringLiteral("allButton")); // replace all
    testReplaceLongerInclude2(KFind::FindBackwards);
    testReplaceLongerInclude2(KFind::FindBackwards | KReplaceDialog::PromptOnReplace, QStringLiteral("replaceButton")); // replace
    testReplaceLongerInclude2(KFind::FindBackwards | KReplaceDialog::PromptOnReplace, QStringLiteral("allButton")); // replace all

    testReplaceBackRef(0);
    testReplaceBackRef(KReplaceDialog::PromptOnReplace, QStringLiteral("replaceButton")); // replace
    testReplaceBackRef(KReplaceDialog::PromptOnReplace, QStringLiteral("allButton")); // replace all

    testReplaceBackRef(KFind::FindBackwards);
    testReplaceBackRef(KFind::FindBackwards | KReplaceDialog::PromptOnReplace, QStringLiteral("replaceButton")); // replace
    testReplaceBackRef(KFind::FindBackwards | KReplaceDialog::PromptOnReplace, QStringLiteral("allButton")); // replace all
    testReplaceBackRef(KReplaceDialog::BackReference | KReplaceDialog::PromptOnReplace, QStringLiteral("replaceButton")); // replace
    testReplaceBackRef(KReplaceDialog::BackReference | KReplaceDialog::PromptOnReplace, QStringLiteral("allButton")); // replace all
    testReplaceBackRef(KReplaceDialog::BackReference | KFind::FindBackwards);
    testReplaceBackRef(KReplaceDialog::BackReference | KFind::FindBackwards | KReplaceDialog::PromptOnReplace, QStringLiteral("replaceButton")); // replace
    testReplaceBackRef(KReplaceDialog::BackReference | KFind::FindBackwards | KReplaceDialog::PromptOnReplace, QStringLiteral("allButton")); // replace all

    testReplaceBackRef1(KReplaceDialog::BackReference | KFind::RegularExpression, QStringLiteral("replaceButton")); // replace
    testReplaceBackRef1(KReplaceDialog::BackReference | KFind::RegularExpression, QStringLiteral("allButton")); // replace all

    QString text = QLatin1String("This file is part of the KDE project.\n") + QLatin1String("This library is free software; you can redistribute it and/or\n")
        + QLatin1String("modify it under the terms of the GNU Library General Public\n")
        + QLatin1String("License version 2, as published by the Free Software Foundation.\n") + QLatin1Char('\n')
        + QLatin1String("    This library is distributed in the hope that it will be useful,\n")
        + QLatin1String("    but WITHOUT ANY WARRANTY; without even the implied warranty of\n")
        + QLatin1String("    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n")
        + QLatin1String("    Library General Public License for more details.\n") + QLatin1Char('\n')
        + QLatin1String("    You should have received a copy of the GNU Library General Public License\n")
        + QLatin1String("    along with this library; see the file COPYING.LIB.  If not, write to\n")
        + QLatin1String("    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,\n") + QLatin1String("    Boston, MA 02110-1301, USA.\n")
        + QLatin1String("More tests:\n") + QLatin1String("ThisThis This, This. This\n") + QLatin1String("aGNU\n") + QLatin1String("free");
    KReplaceTest test(text.split(QLatin1Char('\n')), QStringLiteral("0"));

    test.replace(QStringLiteral("GNU"), QStringLiteral("KDE"), 0);
    test.replace(QStringLiteral("free"), QStringLiteral("*free*"), 0);
    test.replace(QStringLiteral("This"), QStringLiteral("THIS*"), KFind::FindBackwards);

    test.print();
    // return app.exec();
    return 0;
}

#include "moc_kreplacetest.cpp"