Refactor TestEntrySearcher.

This commit is contained in:
Florian Geyer 2014-05-16 00:26:09 +02:00
parent 819cfd459a
commit 4f60df029d
2 changed files with 34 additions and 39 deletions

View File

@ -20,19 +20,18 @@
#include <QTest> #include <QTest>
#include "tests.h" #include "tests.h"
#include "core/EntrySearcher.h"
#include "core/Group.h"
QTEST_GUILESS_MAIN(TestEntrySearcher) QTEST_GUILESS_MAIN(TestEntrySearcher)
void TestEntrySearcher::initTestCase() void TestEntrySearcher::initTestCase()
{ {
groupRoot = new Group(); m_groupRoot = new Group();
} }
void TestEntrySearcher::cleanupTestCase() void TestEntrySearcher::cleanupTestCase()
{ {
delete groupRoot; delete m_groupRoot;
} }
void TestEntrySearcher::testSearch() void TestEntrySearcher::testSearch()
@ -41,9 +40,9 @@ void TestEntrySearcher::testSearch()
Group* group2 = new Group(); Group* group2 = new Group();
Group* group3 = new Group(); Group* group3 = new Group();
group1->setParent(groupRoot); group1->setParent(m_groupRoot);
group2->setParent(groupRoot); group2->setParent(m_groupRoot);
group3->setParent(groupRoot); group3->setParent(m_groupRoot);
Group* group11 = new Group(); Group* group11 = new Group();
@ -62,11 +61,11 @@ void TestEntrySearcher::testSearch()
Entry* eRoot = new Entry(); Entry* eRoot = new Entry();
eRoot->setNotes("test search term test"); eRoot->setNotes("test search term test");
eRoot->setGroup(groupRoot); eRoot->setGroup(m_groupRoot);
Entry* eRoot2 = new Entry(); Entry* eRoot2 = new Entry();
eRoot2->setNotes("test term test"); eRoot2->setNotes("test term test");
eRoot2->setGroup(groupRoot); eRoot2->setGroup(m_groupRoot);
Entry* e1 = new Entry(); Entry* e1 = new Entry();
e1->setNotes("test search term test"); e1->setNotes("test search term test");
@ -92,22 +91,18 @@ void TestEntrySearcher::testSearch()
e3b->setNotes("test search test"); e3b->setNotes("test search test");
e3b->setGroup(group3); e3b->setGroup(group3);
QList<Entry*> searchResult; m_searchResult = m_entrySearcher.search("search term", m_groupRoot, Qt::CaseInsensitive);
EntrySearcher entrySearcher; QCOMPARE(m_searchResult.count(), 3);
searchResult = entrySearcher.search("search term", groupRoot, Qt::CaseInsensitive); m_searchResult = m_entrySearcher.search("search term", group211, Qt::CaseInsensitive);
QCOMPARE(m_searchResult.count(), 1);
QCOMPARE(searchResult.count(), 3); m_searchResult = m_entrySearcher.search("search term", group11, Qt::CaseInsensitive);
QCOMPARE(m_searchResult.count(), 1);
searchResult = entrySearcher.search("search term", group211, Qt::CaseInsensitive); m_searchResult = m_entrySearcher.search("search term", group1, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 1); QCOMPARE(m_searchResult.count(), 0);
searchResult = entrySearcher.search("search term", group11, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 1);
searchResult = entrySearcher.search("search term", group1, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 0);
} }
void TestEntrySearcher::testAndConcatenationInSearch() void TestEntrySearcher::testAndConcatenationInSearch()
@ -115,26 +110,23 @@ void TestEntrySearcher::testAndConcatenationInSearch()
Entry* entry = new Entry(); Entry* entry = new Entry();
entry->setNotes("abc def ghi"); entry->setNotes("abc def ghi");
entry->setTitle("jkl"); entry->setTitle("jkl");
entry->setGroup(groupRoot); entry->setGroup(m_groupRoot);
EntrySearcher entrySearcher; m_searchResult = m_entrySearcher.search("", m_groupRoot, Qt::CaseInsensitive);
QList<Entry*> searchResult; QCOMPARE(m_searchResult.count(), 1);
searchResult = entrySearcher.search("", groupRoot, Qt::CaseInsensitive); m_searchResult = m_entrySearcher.search("def", m_groupRoot, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 1); QCOMPARE(m_searchResult.count(), 1);
searchResult = entrySearcher.search("def", groupRoot, Qt::CaseInsensitive); m_searchResult = m_entrySearcher.search(" abc ghi ", m_groupRoot, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 1); QCOMPARE(m_searchResult.count(), 1);
searchResult = entrySearcher.search(" abc ghi ", groupRoot, Qt::CaseInsensitive); m_searchResult = m_entrySearcher.search("ghi ef", m_groupRoot, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 1); QCOMPARE(m_searchResult.count(), 1);
searchResult = entrySearcher.search("ghi ef", groupRoot, Qt::CaseInsensitive); m_searchResult = m_entrySearcher.search("abc ef xyz", m_groupRoot, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 1); QCOMPARE(m_searchResult.count(), 0);
searchResult = entrySearcher.search("abc ef xyz", groupRoot, Qt::CaseInsensitive); m_searchResult = m_entrySearcher.search("abc kl", m_groupRoot, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 0); QCOMPARE(m_searchResult.count(), 1);
searchResult = entrySearcher.search("abc kl", groupRoot, Qt::CaseInsensitive);
QCOMPARE(searchResult.count(), 1);
} }

View File

@ -21,7 +21,8 @@
#include <QObject> #include <QObject>
class Group; #include "core/EntrySearcher.h"
#include "core/Group.h"
class TestEntrySearcher : public QObject class TestEntrySearcher : public QObject
{ {
@ -34,7 +35,9 @@ private Q_SLOTS:
void testAndConcatenationInSearch(); void testAndConcatenationInSearch();
void testSearch(); void testSearch();
private: private:
Group* groupRoot; Group* m_groupRoot;
EntrySearcher m_entrySearcher;
QList<Entry*> m_searchResult;
}; };
#endif // KEEPASSX_TESTENTRYSEARCHER_H #endif // KEEPASSX_TESTENTRYSEARCHER_H