From a1f599c7c4e1d3d83c153d21d85eff6ca3a4b233 Mon Sep 17 00:00:00 2001 From: Aetf Date: Fri, 24 Jan 2020 12:42:00 -0500 Subject: Add an option to EntrySearcher to skip protected attributes --- tests/TestEntrySearcher.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++ tests/TestEntrySearcher.h | 1 + 2 files changed, 52 insertions(+) (limited to 'tests') diff --git a/tests/TestEntrySearcher.cpp b/tests/TestEntrySearcher.cpp index 3a0ac6836..1b8b4f5b8 100644 --- a/tests/TestEntrySearcher.cpp +++ b/tests/TestEntrySearcher.cpp @@ -23,6 +23,7 @@ QTEST_GUILESS_MAIN(TestEntrySearcher) void TestEntrySearcher::init() { m_rootGroup = new Group(); + m_entrySearcher = EntrySearcher(); } void TestEntrySearcher::cleanup() @@ -259,6 +260,7 @@ void TestEntrySearcher::testCustomAttributesAreSearched() QCOMPARE(m_searchResult.count(), 2); // protected attributes are ignored + m_entrySearcher = EntrySearcher(false, true); m_searchResult = m_entrySearcher.search("_testAttribute:test _testProtected:testP2", m_rootGroup); QCOMPARE(m_searchResult.count(), 2); } @@ -319,3 +321,52 @@ void TestEntrySearcher::testGroup() m_searchResult = m_entrySearcher.search("g:/group1 search", m_rootGroup); QCOMPARE(m_searchResult.count(), 1); } + +void TestEntrySearcher::testSkipProtected() +{ + QScopedPointer e1(new Entry()); + e1->setGroup(m_rootGroup); + + e1->attributes()->set("testAttribute", "testE1"); + e1->attributes()->set("testProtected", "apple", true); + + QScopedPointer e2(new Entry()); + e2->setGroup(m_rootGroup); + e2->attributes()->set("testAttribute", "testE2"); + e2->attributes()->set("testProtected", "banana", true); + + const QList expectE1{e1.data()}; + const QList expectE2{e2.data()}; + const QList expectBoth{e1.data(), e2.data()}; + + // when not skipping protected, empty term matches everything + m_searchResult = m_entrySearcher.search("", m_rootGroup); + QCOMPARE(m_searchResult, expectBoth); + + // now test the searcher with skipProtected = true + m_entrySearcher = EntrySearcher(false, true); + + // when skipping protected, empty term matches nothing + m_searchResult = m_entrySearcher.search("", m_rootGroup); + QCOMPARE(m_searchResult, {}); + + // having a protected entry in terms should not affect the results in anyways + m_searchResult = m_entrySearcher.search("_testProtected:apple", m_rootGroup); + QCOMPARE(m_searchResult, {}); + m_searchResult = m_entrySearcher.search("_testProtected:apple _testAttribute:testE2", m_rootGroup); + QCOMPARE(m_searchResult, expectE2); + m_searchResult = m_entrySearcher.search("_testProtected:apple _testAttribute:testE1", m_rootGroup); + QCOMPARE(m_searchResult, expectE1); + m_searchResult = + m_entrySearcher.search("_testProtected:apple _testAttribute:testE1 _testAttribute:testE2", m_rootGroup); + QCOMPARE(m_searchResult, {}); + + // also move the protected term around to execurise the short-circut logic + m_searchResult = m_entrySearcher.search("_testAttribute:testE2 _testProtected:apple", m_rootGroup); + QCOMPARE(m_searchResult, expectE2); + m_searchResult = m_entrySearcher.search("_testAttribute:testE1 _testProtected:apple", m_rootGroup); + QCOMPARE(m_searchResult, expectE1); + m_searchResult = + m_entrySearcher.search("_testAttribute:testE1 _testProtected:apple _testAttribute:testE2", m_rootGroup); + QCOMPARE(m_searchResult, {}); +} diff --git a/tests/TestEntrySearcher.h b/tests/TestEntrySearcher.h index 498a00742..4e3e99a43 100644 --- a/tests/TestEntrySearcher.h +++ b/tests/TestEntrySearcher.h @@ -37,6 +37,7 @@ private slots: void testSearchTermParser(); void testCustomAttributesAreSearched(); void testGroup(); + void testSkipProtected(); private: Group* m_rootGroup; -- cgit v1.2.3