/* Copyright (C) 2004, Arend van Beelen jr. Copyright (C) 2010, David Faure 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 "kfindtest.h" #include "qtest_kde.h" #include "kfind.h" #include #include void KFindRecorder::changeText(int 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 KFindRecorder::find(const QString &pattern, long options) { delete m_find; m_find = new KFind(pattern, options, 0); // Prevent dialogs from popping up m_find->closeFindNextDialog(); connect(m_find, SIGNAL(highlight(QString,int,int)), SLOT(slotHighlight(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 & KFind::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()); } bool KFindRecorder::findNext(const QString &pattern) { Q_ASSERT(m_find != 0); if(!pattern.isNull()) { m_find->setPattern(pattern); } KFind::Result result = KFind::NoMatch; do { //kDebug() << "m_line: " << m_line; result = m_find->find(); if(result == KFind::NoMatch && m_line < m_text.count()) { //kDebug() << "incrementing m_line..."; if(m_find->options() & KFind::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()); //kDebug() << "find next completed" << m_line; return result != KFind::NoMatch; } void KFindRecorder::slotHighlight(const QString &text, int index, int matchedLength) { m_hits.append("line: \"" + text + "\", index: " + QString::number(index) + ", length: " + QString::number(matchedLength) + "\n"); } void KFindRecorder::slotHighlight(int id, int index, int matchedLength) { m_hits.append("line: \"" + m_text[id] + "\", index: " + QString::number(index) + ", length: " + QString::number(matchedLength) + "\n"); } //// TestKFind::TestKFind() : QObject() { m_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"; } void TestKFind::testStaticFindString_data() { // Tests for the core method "static KFind::find" QTest::addColumn("text"); QTest::addColumn("pattern"); QTest::addColumn("startIndex"); QTest::addColumn("options"); QTest::addColumn("expectedResult"); QTest::addColumn("expectedMatchedLength"); QTest::newRow("simple (0)") << "abc" << "a" << 0 << 0 << 0 << 1; QTest::newRow("simple (1)") << "abc" << "b" << 0 << 0 << 1 << 1; QTest::newRow("not found") << "abca" << "ba" << 0 << 0 << -1 << 0; QTest::newRow("from index") << "abc bc" << "b" << 3 << 0 << 4 << 1; QTest::newRow("from exact index") << "abc bc" << "b" << 4 << 0 << 4 << 1; QTest::newRow("past index (not found)") << "abc bc" << "b" << 5 << 0 << -1 << 0; QTest::newRow("dot") << "ab." << "b." << 0 << 0 << 1 << 2; QTest::newRow("^should fail") << "text" << "^tex" << 0 << 0 << -1 << 0; QTest::newRow("multiline with \\n") << "foo\nbar" << "o\nb" << 0 << 0 << 2 << 3; QTest::newRow("whole words ok") << "abc bcbc bc bmore be" << "bc" << 0 << int(KFind::WholeWordsOnly) << 9 << 2; QTest::newRow("whole words not found") << "abab abx" << "ab" << 0 << int(KFind::WholeWordsOnly) << -1 << 0; QTest::newRow("whole words not found (_)") << "abab ab_" << "ab" << 0 << int(KFind::WholeWordsOnly) << -1 << 0; QTest::newRow("whole words ok (.)") << "ab." << "ab" << 0 << int(KFind::WholeWordsOnly) << 0 << 2; QTest::newRow("backwards") << "abc bcbc8bc" << "bc" << 10 << int(KFind::FindBackwards) << 9 << 2; QTest::newRow("backwards again") << "abc bcbc8bc" << "bc" << 8 << int(KFind::FindBackwards) << 6 << 2; QTest::newRow("backwards 2") << "abc bcbc8bc" << "bc" << 5 << int(KFind::FindBackwards) << 4 << 2; QTest::newRow("backwards 3") << "abc bcbc8bc" << "bc" << 3 << int(KFind::FindBackwards) << 1 << 2; QTest::newRow("empty (0)") << "a" << "" << 0 << int(0) << 0 << 0; QTest::newRow("empty (1)") << "a" << "" << 1 << int(0) << 1 << 0; // kreplacetest testReplaceBlankSearch relies on this QTest::newRow("at end, not found") << "a" << "b" << 1 << int(0) << -1 << 0; // just for catching the while(index("text"); QTest::addColumn("pattern"); QTest::addColumn("startIndex"); QTest::addColumn("options"); QTest::addColumn("expectedResult"); QTest::addColumn("expectedMatchedLength"); QTest::newRow("simple (0)") << "abc" << "a" << 0 << 0 << 0 << 1; QTest::newRow("simple (1)") << "abc" << "b" << 0 << 0 << 1 << 1; QTest::newRow("not found") << "abca" << "ba" << 0 << 0 << -1 << 0; QTest::newRow("from index") << "abc bc" << "b" << 3 << 0 << 4 << 1; QTest::newRow("from exact index") << "abc bc" << "b" << 4 << 0 << 4 << 1; QTest::newRow("past index (not found)") << "abc bc" << "b" << 5 << 0 << -1 << 0; QTest::newRow("dot") << "abc" << "b." << 0 << 0 << 1 << 2; QTest::newRow("^simple") << "text" << "^tex" << 0 << 0 << 0 << 3; QTest::newRow("^multiline first") << "foo\nbar" << "^f" << 0 << 0 << 0 << 1; QTest::newRow("^multiline last") << "foo\nbar" << "^bar" << 0 << 0 << 4 << 3; QTest::newRow("^multiline with index") << "boo\nbar" << "^b" << 1 << 0 << 4 << 1; QTest::newRow("simple$") << "text" << "xt$" << 0 << 0 << 2 << 2; QTest::newRow("$ backwards") << "text" << "xt$" << 4 << int(KFind::FindBackwards) << 2 << 2; QTest::newRow("multiline$") << "foo\nbar" << "oo$" << 0 << 0 << 1 << 2; QTest::newRow("multiline$ intermediary line") << "foo\nbar\nagain bar" << "r$" << 0 << 0 << 6 << 1; QTest::newRow("multiline$ with index, last line") << "foo\nbar\nagain bar" << "r$" << 7 << 0 << 16 << 1; QTest::newRow("multiline$ backwards") << "foo\nbar" << "oo$" << 7 << int(KFind::FindBackwards) << 1 << 2; QTest::newRow("multiline with \\n") << "foo\nbar" << "o\nb" << 0 << 0 << 2 << 3; QTest::newRow("whole words ok") << "abc bcbc bc bmore be" << "b." << 0 << int(KFind::WholeWordsOnly) << 9 << 2; QTest::newRow("whole words not found") << "abab abx" << "ab" << 0 << int(KFind::WholeWordsOnly) << -1 << 0; QTest::newRow("whole words not found (_)") << "abab ab_" << "ab" << 0 << int(KFind::WholeWordsOnly) << -1 << 0; QTest::newRow("whole words ok (.)") << "ab." << "ab" << 0 << int(KFind::WholeWordsOnly) << 0 << 2; QTest::newRow("backwards") << "abc bcbc bc" << "b." << 10 << int(KFind::FindBackwards) << 9 << 2; QTest::newRow("empty (0)") << "a" << "" << 0 << int(0) << 0 << 0; QTest::newRow("empty (1)") << "a" << "" << 1 << int(0) << 1 << 0; // kreplacetest testReplaceBlankSearch relies on this QTest::newRow("at end, not found") << "a" << "b" << 1 << int(0) << -1 << 0; // just for catching the while(index