summaryrefslogtreecommitdiffstats
path: root/kutils/tests
diff options
context:
space:
mode:
authortoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
committertoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
commitce4a32fe52ef09d8f5ff1dd22c001110902b60a2 (patch)
tree5ac38a06f3dde268dc7927dc155896926aaf7012 /kutils/tests
downloadtdelibs-ce4a32fe52ef09d8f5ff1dd22c001110902b60a2.tar.gz
tdelibs-ce4a32fe52ef09d8f5ff1dd22c001110902b60a2.zip
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdelibs@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'kutils/tests')
-rw-r--r--kutils/tests/Makefile.am16
-rw-r--r--kutils/tests/kfindtest.cpp252
-rw-r--r--kutils/tests/kfindtest.h59
-rw-r--r--kutils/tests/kreplacetest.cpp342
-rw-r--r--kutils/tests/kreplacetest.h52
5 files changed, 721 insertions, 0 deletions
diff --git a/kutils/tests/Makefile.am b/kutils/tests/Makefile.am
new file mode 100644
index 000000000..3bde7a9a2
--- /dev/null
+++ b/kutils/tests/Makefile.am
@@ -0,0 +1,16 @@
+INCLUDES = $(all_includes)
+
+check_PROGRAMS = kfindtest kreplacetest
+
+TESTS = kfindtest kreplacetest
+
+kfindtest_SOURCES = kfindtest.cpp
+kfindtest_LDADD = ../libkutils.la
+kfindtest_LDFLAGS = $(all_libraries)
+
+kreplacetest_SOURCES = kreplacetest.cpp
+kreplacetest_LDADD = ../libkutils.la
+kreplacetest_LDFLAGS = $(all_libraries)
+
+METASOURCES = AUTO
+
diff --git a/kutils/tests/kfindtest.cpp b/kutils/tests/kfindtest.cpp
new file mode 100644
index 000000000..17e8da7ee
--- /dev/null
+++ b/kutils/tests/kfindtest.cpp
@@ -0,0 +1,252 @@
+/*
+ Copyright (C) 2004, Arend van Beelen jr. <arend@auton.nl>
+ This file is part of the KDE project
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License version 2, as published by the Free Software Foundation.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+*/
+
+#include "../kfind.h"
+#include "../kfinddialog.h"
+#include "kfindtest.h"
+
+#include <kapplication.h>
+#include <kcmdlineargs.h>
+#include <kdebug.h>
+
+#include <stdlib.h>
+#include <assert.h>
+
+static bool check(QString txt, QString a, QString b) // from kurltest
+{
+ if (a.isEmpty())
+ a = QString::null;
+ if (b.isEmpty())
+ b = QString::null;
+ if (a == b) {
+ kdDebug() << txt << " : checking '" << a << "' against expected value '" << b << "'... " << "ok" << endl;
+ }
+ else {
+ kdDebug() << txt << " : checking '" << a << "' against expected value '" << b << "'... " << "KO !" << endl;
+ exit(1);
+ }
+ return true;
+}
+
+void KFindTest::changeText(uint line, const QString &text)
+{
+ Q_ASSERT(line < m_text.count());
+ Q_ASSERT(m_find != 0);
+
+ m_line = line;
+ m_text[line] = text;
+ m_find->setData(line, text);
+}
+
+void KFindTest::find(const QString &pattern, long options)
+{
+ delete m_find;
+ m_find = new KFind(pattern, options, 0);
+
+ connect(m_find, SIGNAL(highlight(const QString &, int, int)),
+ SLOT(slotHighlight(const QString &, int, int)));
+ connect(m_find, SIGNAL(highlight(int, int, int)),
+ SLOT(slotHighlight(int, int, int)));
+
+ m_line = 0;
+ KFind::Result result = KFind::NoMatch;
+ do
+ {
+ if(options & KFindDialog::FindIncremental)
+ m_find->setData(m_line, m_text[m_line]);
+ else
+ m_find->setData(m_text[m_line]);
+
+ m_line++;
+
+ result = m_find->find();
+ } while(result == KFind::NoMatch && m_line < m_text.count());
+}
+
+void KFindTest::findNext(const QString &pattern)
+{
+ Q_ASSERT(m_find != 0);
+
+ if(!pattern.isNull())
+ {
+ m_find->setPattern(pattern);
+ }
+
+ KFind::Result result = KFind::NoMatch;
+ do
+ {
+ //kdDebug() << "m_line: " << m_line << endl;
+
+ result = m_find->find();
+
+ if(result == KFind::NoMatch && m_line < m_text.count())
+ {
+ //kdDebug() << "incrementing m_line..." << endl;
+ if(m_find->options() & KFindDialog::FindIncremental)
+ m_find->setData(m_line, m_text[m_line]);
+ else
+ m_find->setData(m_text[m_line]);
+
+ m_line++;
+ }
+ } while(result == KFind::NoMatch && m_line < m_text.count());
+ //kdDebug() << "find next completed" << m_line << endl;
+}
+
+void KFindTest::slotHighlight(const QString &text, int index, int matchedLength)
+{
+ m_hits.append("line: \"" + text + "\", index: " + QString::number(index) +
+ ", length: " + QString::number(matchedLength) + "\n");
+}
+
+void KFindTest::slotHighlight(int id, int index, int matchedLength)
+{
+ m_hits.append("line: \"" + m_text[id] + "\", index: " + QString::number(index) +
+ ", length: " + QString::number(matchedLength) + "\n");
+}
+
+int main(int argc, char **argv)
+{
+ KCmdLineArgs::init(argc, argv, "kfindtest", "KFindTest", 0, 0, false);
+ KApplication app;
+
+ QString text = "This file is part of the KDE project.\n"
+ "This library is free software; you can redistribute it and/or\n"
+ "modify it under the terms of the GNU Library General Public\n"
+ "License version 2, as published by the Free Software Foundation.\n"
+ "\n"
+ " This library is distributed in the hope that it will be useful,\n"
+ " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
+ " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
+ " Library General Public License for more details.\n"
+ "\n"
+ " You should have received a copy of the GNU Library General Public License\n"
+ " along with this library; see the file COPYING.LIB. If not, write to\n"
+ " the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,\n"
+ " Boston, MA 02110-1301, USA.\n";
+
+ QString output1 = "line: \"This file is part of the KDE project.\", index: 0, length: 4\n"
+ "line: \"This library is free software; you can redistribute it and/or\", index: 0, length: 4\n"
+ "line: \" This library is distributed in the hope that it will be useful,\", index: 4, length: 4\n"
+ "line: \" along with this library; see the file COPYING.LIB. If not, write to\", index: 15, length: 4\n";
+
+ QString output2 = "line: \"This file is part of the KDE project.\", index: 0, length: 0\n"
+ "line: \"This file is part of the KDE project.\", index: 2, length: 1\n"
+ "line: \"This file is part of the KDE project.\", index: 2, length: 2\n"
+ "line: \"This library is free software; you can redistribute it and/or\", index: 42, length: 3\n"
+ "line: \" This library is distributed in the hope that it will be useful,\", index: 21, length: 3\n"
+ "line: \" This library is distributed in the hope that it will be useful,\", index: 21, length: 5\n"
+ "line: \" This library is distributed in the hope that it will be useful,\", index: 21, length: 4\n"
+ "line: \" This library is distributed in the hope that it will be useful,\", index: 21, length: 3\n"
+ "line: \"This file is part of the KDE project.\", index: 2, length: 2\n"
+ "line: \"This library is free software; you can redistribute it and/or\", index: 25, length: 1\n"
+ "line: \"This library is free software; you can redistribute it and/or\", index: 25, length: 2\n"
+ "line: \" but WITHOUT ANY WARRANTY; without even the implied warranty of\", index: 20, length: 8\n"
+ "line: \"This library is free software; you can redistribute it and/or\", index: 16, length: 4\n"
+ "line: \"License version 2, as published by the Free Software Foundation.\", index: 44, length: 19\n";
+
+ QString output3 = "line: \"This file is part of the KDE project.\", index: 0, length: 0\n"
+ "line: \"This file is part of the KDE project.\", index: 2, length: 1\n"
+ "line: \"This file is part of the KDE project.\", index: 2, length: 2\n"
+ "line: \"This library is free software; you can redistribute it and/or\", index: 42, length: 3\n"
+ "line: \"This library is free software; you can redistribute it and/or\", index: 42, length: 4\n"
+ "line: \" This library is distributed in the hope that it will be useful,\", index: 21, length: 4\n"
+ "line: \" This library is distributed in the hope that it will be useful,\", index: 21, length: 5\n"
+ "line: \" This library is distributed in the hope that it will be useful,\", index: 21, length: 4\n"
+ "line: \" This library is distributed in the hope that it will be useful,\", index: 21, length: 3\n"
+ "line: \"This file is part of the KDE project.\", index: 2, length: 2\n"
+ "line: \"This file is part of the KDE project.\", index: 2, length: 1\n"
+ "line: \"The second line now looks a whole lot different.\", index: 18, length: 1\n"
+ "line: \"License version 2, as published by the Free Software Foundation.\", index: 48, length: 2\n"
+ "line: \" but WITHOUT ANY WARRANTY; without even the implied warranty of\", index: 20, length: 8\n"
+ "line: \" but WITHOUT ANY xxxx; without even the implied warranty of\", index: 51, length: 6\n"
+ "line: \"License version 2, as published by the Free Software Foundation.\", index: 39, length: 4\n"
+ "line: \"License version 2, as published by the Free Software Foundation.\", index: 44, length: 19\n";
+
+ KFindTest *test = new KFindTest(QStringList::split('\n', text, true));
+
+ kdDebug() << "Plain static search..." << endl;
+
+ // first we do a simple text searching the text and doing a few find nexts
+ test->find("This", 0);
+ test->findNext();
+ test->findNext();
+ test->findNext();
+ test->findNext();
+ test->findNext();
+
+ check("result", test->hits().join(""), output1);
+ test->clearHits();
+ kdDebug() << "PASSED" << endl;
+
+ kdDebug() << "FindIncremental with static contents..." << endl;
+
+ // now we'll do some searches using FindIncremental
+ test->find("", KFindDialog::FindIncremental);
+ test->findNext("i");
+ test->findNext("is");
+ test->findNext("ist");
+ test->findNext();
+ test->findNext("istri");
+ test->findNext("istr");
+ test->findNext("ist");
+ test->findNext("is");
+ test->findNext("W");
+ test->findNext("WA");
+ test->findNext("WARRANTY");
+ test->findNext("Free");
+ test->findNext("Software Foundation");
+
+ check("result", test->hits().join(""), output2);
+ test->clearHits();
+ kdDebug() << "PASSED" << endl;
+
+ kdDebug() << "FindIncremental with dynamic contents..." << endl;
+
+ // now do that again but with pages that change between searches
+ test->find("", KFindDialog::FindIncremental);
+ test->findNext("i");
+ test->findNext("is");
+ test->findNext("ist");
+ test->findNext("istr");
+ test->findNext();
+ test->changeText(1, "The second line now looks a whole lot different.");
+ test->findNext("istri");
+ test->findNext("istr");
+ test->findNext("ist");
+ test->findNext("is");
+ test->findNext("i");
+ test->findNext("W");
+ test->findNext("WA");
+ test->findNext("WARRANTY");
+ test->changeText(6, " but WITHOUT ANY xxxx; without even the implied warranty of");
+ test->findNext("WARRAN");
+ test->findNext("Free");
+ test->findNext("Software Foundation");
+
+ check("result", test->hits().join(""), output3);
+ test->clearHits();
+ kdDebug() << "PASSED" << endl;
+
+ //return app.exec();
+ delete test;
+ return 0;
+}
+
+#include "kfindtest.moc"
diff --git a/kutils/tests/kfindtest.h b/kutils/tests/kfindtest.h
new file mode 100644
index 000000000..2cb60ed76
--- /dev/null
+++ b/kutils/tests/kfindtest.h
@@ -0,0 +1,59 @@
+/*
+ Copyright (C) 2004, Arend van Beelen jr. <arend@auton.nl>
+ This file is part of the KDE project
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License version 2, as published by the Free Software Foundation.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+*/
+
+#ifndef KFINDTEST_H
+#define KFINDTEST_H
+
+#include <qobject.h>
+#include <qstringlist.h>
+
+class KFind;
+
+class KFindTest : public QObject
+{
+ Q_OBJECT
+
+ public:
+ KFindTest(const QStringList &text) :
+ QObject(0),
+ m_find(0),
+ m_text(text),
+ m_line(0)
+ {}
+
+ void find(const QString &pattern, long options = 0);
+ void findNext(const QString &pattern = QString::null);
+
+ void changeText(uint line, const QString &text);
+
+ const QStringList &hits() const { return m_hits; }
+ void clearHits() { m_hits.clear(); }
+
+ public slots:
+ void slotHighlight(const QString &text, int index, int matchedLength);
+ void slotHighlight(int id, int index, int matchedLengthlength);
+
+ private:
+ KFind *m_find;
+ QStringList m_text;
+ uint m_line;
+ QStringList m_hits;
+};
+
+#endif
diff --git a/kutils/tests/kreplacetest.cpp b/kutils/tests/kreplacetest.cpp
new file mode 100644
index 000000000..9c6f67b3a
--- /dev/null
+++ b/kutils/tests/kreplacetest.cpp
@@ -0,0 +1,342 @@
+/*
+ Copyright (C) 2002, David Faure <david@mandrakesoft.com>
+ This file is part of the KDE project
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License version 2, as published by the Free Software Foundation.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+*/
+
+#include <assert.h>
+
+#include <kcmdlineargs.h>
+#include <kapplication.h>
+#include <qeventloop.h>
+#include <kpushbutton.h>
+#include "../kreplace.h"
+#include "../kreplacedialog.h"
+
+#include "kreplacetest.h"
+#include <kdebug.h>
+#include <stdlib.h>
+
+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 = new KReplace(pattern, replacement, options);
+
+ // Connect highlight signal to code which handles highlighting
+ // of found text.
+ connect(m_replace, SIGNAL( highlight( const QString &, int, int ) ),
+ this, SLOT( slotHighlight( const QString &, int, int ) ) );
+ // Connect findNext signal - called when pressing the button in the dialog
+ connect(m_replace, SIGNAL( findNext() ),
+ this, SLOT( slotReplaceNext() ) );
+ // Connect replace signal - called when doing a replacement
+ connect(m_replace, SIGNAL( replace(const QString &, int, int, int) ),
+ this, SLOT( slotReplace(const QString &, int, int, int) ) );
+
+ // Go to initial position
+ if ( (options & KReplaceDialog::FromCursor) == 0 )
+ {
+ if ( m_replace->options() & KFindDialog::FindBackwards )
+ m_currentPos = m_text.fromLast();
+ else
+ m_currentPos = m_text.begin();
+ }
+
+ // Launch first replacement
+ slotReplaceNext();
+
+ if ( m_needEventLoop )
+ qApp->eventLoop()->enterLoop();
+}
+
+void KReplaceTest::slotHighlight( const QString &str, int matchingIndex, int matchedLength )
+{
+ kdDebug() << "slotHighlight Index:" << matchingIndex << " Length:" << matchedLength
+ << " Substr:" << str.mid(matchingIndex, matchedLength)
+ << endl;
+ // Emulate the user saying yes
+ // animateClick triggers a timer, hence the enterloop/exitloop
+ // Calling slotReplace directly would lead to infinite loop anyway (Match never returned,
+ // so slotReplaceNext never returns)
+ if ( m_replace->options() & KReplaceDialog::PromptOnReplace ) {
+ m_replace->replaceNextDialog( false )->actionButton( (KDialogBase::ButtonCode)m_button )->animateClick();
+ m_needEventLoop = true;
+ }
+}
+
+
+void KReplaceTest::slotReplace(const QString &text, int replacementIndex, int replacedLength, int matchedLength)
+{
+ kdDebug() << "slotReplace index=" << replacementIndex << " replacedLength=" << replacedLength << " matchedLength=" << matchedLength << " text=" << text.left( 50 ) << endl;
+ *m_currentPos = text; // KReplace hacked the replacement into 'text' in already.
+}
+
+void KReplaceTest::slotReplaceNext()
+{
+ //kdDebug() << k_funcinfo << endl;
+ KFind::Result res = KFind::NoMatch;
+ while ( res == KFind::NoMatch && m_currentPos != m_text.end() ) {
+ 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 ) {
+ if ( m_replace->options() & KFindDialog::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() & KFindDialog::FindBackwards )
+ m_currentPos = m_text.fromLast();
+ else
+ m_currentPos = m_text.begin();
+ slotReplaceNext();
+ }
+#endif
+ if ( res == KFind::NoMatch && m_needEventLoop )
+ qApp->eventLoop()->exitLoop();
+}
+
+void KReplaceTest::print()
+{
+ QStringList::Iterator it = m_text.begin();
+ for ( ; it != m_text.end() ; ++it )
+ kdDebug() << *it << endl;
+}
+
+/* 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, int button = 0 )
+{
+ kdDebug() << "testReplaceSimple: " << options << endl;
+ KReplaceTest test( QString( "hellohello" ), button );
+ test.replace( "hello", "HELLO", options );
+ QStringList textLines = test.textLines();
+ assert( textLines.count() == 1 );
+ if ( textLines[ 0 ] != "HELLOHELLO" ) {
+ kdError() << "ASSERT FAILED: replaced text is '" << textLines[ 0 ] << "' instead of 'HELLOHELLO'" << endl;
+ exit(1);
+ }
+}
+
+// Replacing "a" with "".
+// input="aaaaaa", expected output=""
+static void testReplaceBlank( int options, int button = 0 )
+{
+ kdDebug() << "testReplaceBlank: " << options << endl;
+ KReplaceTest test( QString( "aaaaaa" ), button );
+ test.replace( "a", "", options );
+ QStringList textLines = test.textLines();
+ assert( textLines.count() == 1 );
+ if ( !textLines[ 0 ].isEmpty() ) {
+ kdError() << "ASSERT FAILED: replaced text is '" << textLines[ 0 ] << "' instead of ''" << endl;
+ exit(1);
+ }
+}
+
+// Replacing "" with "foo"
+// input="bbbb", expected output="foobfoobfoobfoobfoo"
+static void testReplaceBlankSearch( int options, int button = 0 )
+{
+ kdDebug() << "testReplaceBlankSearch: " << options << endl;
+ KReplaceTest test( QString( "bbbb" ), button );
+ test.replace( "", "foo", options );
+ QStringList textLines = test.textLines();
+ assert( textLines.count() == 1 );
+ if ( textLines[ 0 ] != "foobfoobfoobfoobfoo" ) {
+ kdError() << "ASSERT FAILED: replaced text is '" << textLines[ 0 ] << "' instead of 'foobfoobfoobfoobfoo'" << endl;
+ exit(1);
+ }
+}
+
+static void testReplaceLonger( int options, int button = 0 )
+{
+ kdDebug() << "testReplaceLonger: " << options << endl;
+ // Standard test of a replacement string longer than the matched string
+ KReplaceTest test( QString( "aaaa" ), button );
+ test.replace( "a", "bb", options );
+ QStringList textLines = test.textLines();
+ assert( textLines.count() == 1 );
+ if ( textLines[ 0 ] != "bbbbbbbb" ) {
+ kdError() << "ASSERT FAILED: replaced text is '" << textLines[ 0 ] << "' instead of 'bbbbbbbb'" << endl;
+ exit(1);
+ }
+}
+
+static void testReplaceLongerInclude( int options, int button = 0 )
+{
+ kdDebug() << "testReplaceLongerInclude: " << options << endl;
+ // Similar test, where the replacement string includes the search string
+ KReplaceTest test( QString( "a foo b" ), button );
+ test.replace( "foo", "foobar", options );
+ QStringList textLines = test.textLines();
+ assert( textLines.count() == 1 );
+ if ( textLines[ 0 ] != "a foobar b" ) {
+ kdError() << "ASSERT FAILED: replaced text is '" << textLines[ 0 ] << "' instead of 'a foobar b'" << endl;
+ exit(1);
+ }
+}
+
+static void testReplaceLongerInclude2( int options, int button = 0 )
+{
+ kdDebug() << "testReplaceLongerInclude2: " << options << endl;
+ // Similar test, but with more chances of matches inside the replacement string
+ KReplaceTest test( QString( "aaaa" ), button );
+ test.replace( "a", "aa", options );
+ QStringList textLines = test.textLines();
+ assert( textLines.count() == 1 );
+ if ( textLines[ 0 ] != "aaaaaaaa" ) {
+ kdError() << "ASSERT FAILED: replaced text is '" << textLines[ 0 ] << "' instead of 'aaaaaaaa'" << endl;
+ exit(1);
+ }
+}
+
+// Test for the \0 backref
+static void testReplaceBackRef( int options, int button = 0 )
+{
+ kdDebug() << "testReplaceBackRef: " << options << endl;
+ KReplaceTest test( QString( "abc def" ), button );
+ test.replace( "abc", "(\\0)", options );
+ QStringList textLines = test.textLines();
+ assert( textLines.count() == 1 );
+ QString expected = options & KReplaceDialog::BackReference ? "(abc) def" : "(\\0) def";
+ if ( textLines[ 0 ] != expected ) {
+ kdError() << "ASSERT FAILED: replaced text is '" << textLines[ 0 ] << "' instead of '"<< expected << "'" << endl;
+ exit(1);
+ }
+}
+
+static void testReplacementHistory( const QStringList& findHistory, const QStringList& replaceHistory )
+{
+ KReplaceDialog dlg( 0, 0, 0, findHistory, replaceHistory );
+ dlg.show();
+ kdDebug() << "testReplacementHistory:" << dlg.replacementHistory() << endl;
+ assert( dlg.replacementHistory() == replaceHistory );
+}
+
+static void testReplacementHistory()
+{
+ QStringList findHistory;
+ QStringList replaceHistory;
+ findHistory << "foo" << "bar";
+ replaceHistory << "FOO" << "BAR";
+ testReplacementHistory( findHistory, replaceHistory );
+
+ findHistory.clear();
+ replaceHistory.clear();
+ findHistory << "foo" << "bar";
+ replaceHistory << QString::null << "baz"; // #130831
+ testReplacementHistory( findHistory, replaceHistory );
+}
+
+int main( int argc, char **argv )
+{
+ KCmdLineArgs::init(argc, argv, "kreplacetest", 0, 0);
+ KApplication app;
+
+ testReplacementHistory(); // #130831
+
+ testReplaceBlank( 0 );
+ testReplaceBlank( KReplaceDialog::PromptOnReplace, KDialogBase::User3 ); // replace
+ testReplaceBlank( KReplaceDialog::PromptOnReplace, KDialogBase::User1 ); // replace all
+ testReplaceBlank( KReplaceDialog::FindBackwards, 0 );
+ testReplaceBlank( KReplaceDialog::FindBackwards | KReplaceDialog::PromptOnReplace, KDialogBase::User3 ); // replace
+ testReplaceBlank( KReplaceDialog::FindBackwards | KReplaceDialog::PromptOnReplace, KDialogBase::User1 ); // replace all
+
+ testReplaceBlankSearch( 0 );
+ testReplaceBlankSearch( KReplaceDialog::PromptOnReplace, KDialogBase::User3 ); // replace
+ testReplaceBlankSearch( KReplaceDialog::PromptOnReplace, KDialogBase::User1 ); // replace all
+ testReplaceBlankSearch( KReplaceDialog::FindBackwards, 0 );
+ testReplaceBlankSearch( KReplaceDialog::FindBackwards | KReplaceDialog::PromptOnReplace, KDialogBase::User3 ); // replace
+ testReplaceBlankSearch( KReplaceDialog::FindBackwards | KReplaceDialog::PromptOnReplace, KDialogBase::User1 ); // replace all
+
+ testReplaceSimple( 0 );
+ testReplaceSimple( KReplaceDialog::PromptOnReplace, KDialogBase::User3 ); // replace
+ testReplaceSimple( KReplaceDialog::PromptOnReplace, KDialogBase::User1 ); // replace all
+ testReplaceSimple( KReplaceDialog::FindBackwards, 0 );
+ testReplaceSimple( KReplaceDialog::FindBackwards | KReplaceDialog::PromptOnReplace, KDialogBase::User3 ); // replace
+ testReplaceSimple( KReplaceDialog::FindBackwards | KReplaceDialog::PromptOnReplace, KDialogBase::User1 ); // replace all
+
+ testReplaceLonger( 0 );
+ testReplaceLonger( KReplaceDialog::PromptOnReplace, KDialogBase::User3 ); // replace
+ testReplaceLonger( KReplaceDialog::PromptOnReplace, KDialogBase::User1 ); // replace all
+ testReplaceLonger( KReplaceDialog::FindBackwards, 0 );
+ testReplaceLonger( KReplaceDialog::FindBackwards | KReplaceDialog::PromptOnReplace, KDialogBase::User3 ); // replace
+ testReplaceLonger( KReplaceDialog::FindBackwards | KReplaceDialog::PromptOnReplace, KDialogBase::User1 ); // replace all
+
+ testReplaceLongerInclude( 0 );
+ testReplaceLongerInclude( KReplaceDialog::PromptOnReplace, KDialogBase::User3 ); // replace
+ testReplaceLongerInclude( KReplaceDialog::PromptOnReplace, KDialogBase::User1 ); // replace all
+ testReplaceLongerInclude( KReplaceDialog::FindBackwards, 0 );
+ testReplaceLongerInclude( KReplaceDialog::FindBackwards | KReplaceDialog::PromptOnReplace, KDialogBase::User3 ); // replace
+ testReplaceLongerInclude( KReplaceDialog::FindBackwards | KReplaceDialog::PromptOnReplace, KDialogBase::User1 ); // replace all
+
+ testReplaceLongerInclude2( 0 );
+ testReplaceLongerInclude2( KReplaceDialog::PromptOnReplace, KDialogBase::User3 ); // replace
+ testReplaceLongerInclude2( KReplaceDialog::PromptOnReplace, KDialogBase::User1 ); // replace all
+ testReplaceLongerInclude2( KReplaceDialog::FindBackwards, 0 );
+ testReplaceLongerInclude2( KReplaceDialog::FindBackwards | KReplaceDialog::PromptOnReplace, KDialogBase::User3 ); // replace
+ testReplaceLongerInclude2( KReplaceDialog::FindBackwards | KReplaceDialog::PromptOnReplace, KDialogBase::User1 ); // replace all
+
+ testReplaceBackRef( 0 );
+ testReplaceBackRef( KReplaceDialog::PromptOnReplace, KDialogBase::User3 ); // replace
+ testReplaceBackRef( KReplaceDialog::PromptOnReplace, KDialogBase::User1 ); // replace all
+ testReplaceBackRef( KReplaceDialog::FindBackwards, 0 );
+ testReplaceBackRef( KReplaceDialog::FindBackwards | KReplaceDialog::PromptOnReplace, KDialogBase::User3 ); // replace
+ testReplaceBackRef( KReplaceDialog::FindBackwards | KReplaceDialog::PromptOnReplace, KDialogBase::User1 ); // replace all
+ testReplaceBackRef( KReplaceDialog::BackReference | KReplaceDialog::PromptOnReplace, KDialogBase::User3 ); // replace
+ testReplaceBackRef( KReplaceDialog::BackReference | KReplaceDialog::PromptOnReplace, KDialogBase::User1 ); // replace all
+ testReplaceBackRef( KReplaceDialog::BackReference | KReplaceDialog::FindBackwards, 0 );
+ testReplaceBackRef( KReplaceDialog::BackReference | KReplaceDialog::FindBackwards | KReplaceDialog::PromptOnReplace, KDialogBase::User3 ); // replace
+ testReplaceBackRef( KReplaceDialog::BackReference | KReplaceDialog::FindBackwards | KReplaceDialog::PromptOnReplace, KDialogBase::User1 ); // replace all
+
+ QString text = "This file is part of the KDE project.\n"
+ "This library is free software; you can redistribute it and/or\n"
+ "modify it under the terms of the GNU Library General Public\n"
+ "License version 2, as published by the Free Software Foundation.\n"
+ "\n"
+ " This library is distributed in the hope that it will be useful,\n"
+ " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
+ " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
+ " Library General Public License for more details.\n"
+ "\n"
+ " You should have received a copy of the GNU Library General Public License\n"
+ " along with this library; see the file COPYING.LIB. If not, write to\n"
+ " the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,\n"
+ " Boston, MA 02110-1301, USA.\n"
+ "More tests:\n"
+ "ThisThis This, This. This\n"
+ "aGNU\n"
+ "free";
+ KReplaceTest test( QStringList::split( '\n', text, true ), 0 );
+
+ test.replace( "GNU", "KDE", 0 );
+ test.replace( "free", "*free*", 0 );
+ test.replace( "This", "THIS*", KFindDialog::FindBackwards );
+
+ test.print();
+ //return app.exec();
+ return 0;
+}
+#include "kreplacetest.moc"
diff --git a/kutils/tests/kreplacetest.h b/kutils/tests/kreplacetest.h
new file mode 100644
index 000000000..a60563e77
--- /dev/null
+++ b/kutils/tests/kreplacetest.h
@@ -0,0 +1,52 @@
+/*
+ Copyright (C) 2002, David Faure <david@mandrakesoft.com>
+ This file is part of the KDE project
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License version 2, as published by the Free Software Foundation.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+*/
+
+#ifndef KREPLACETEST_H
+#define KREPLACETEST_H
+
+#include <qobject.h>
+#include <qstringlist.h>
+
+class KReplace;
+
+class KReplaceTest : public QObject
+{
+ Q_OBJECT
+public:
+ KReplaceTest( const QStringList& text, int button )
+ : QObject( 0L ), m_text( text ), m_replace( 0 ), m_button( button ) {}
+
+ void replace( const QString &pattern, const QString &replacement, long options );
+ void print();
+ const QStringList& textLines() const { return m_text; }
+
+public slots:
+ void slotHighlight( const QString &, int, int );
+ void slotReplaceNext();
+ void slotReplace(const QString &text, int replacementIndex, int replacedLength, int matchedLength);
+
+private:
+ QStringList::Iterator m_currentPos;
+ QStringList m_text;
+ KReplace* m_replace;
+ bool m_needEventLoop;
+ int m_button;
+};
+
+#endif