keepassxc/tests/TestWildcardMatcher.cpp

92 lines
3.2 KiB
C++
Raw Normal View History

2012-07-22 08:04:50 -04:00
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TestWildcardMatcher.h"
#include <QTest>
2012-07-22 08:04:50 -04:00
#include "autotype/WildcardMatcher.h"
QTEST_GUILESS_MAIN(TestWildcardMatcher)
2012-07-22 08:04:50 -04:00
const QString TestWildcardMatcher::DefaultText = QString("some text");
const QString TestWildcardMatcher::AlternativeText = QString("some other text");
2012-07-24 14:30:16 -04:00
void TestWildcardMatcher::testMatcher_data()
2012-07-22 08:04:50 -04:00
{
2012-07-24 14:30:16 -04:00
QTest::addColumn<QString>("text");
QTest::addColumn<QString>("pattern");
QTest::addColumn<bool>("match");
2012-07-22 08:04:50 -04:00
2012-07-24 14:30:16 -04:00
QTest::newRow("MatchWithoutWildcard") << DefaultText << DefaultText << true;
QTest::newRow("NoMatchWithoutWildcard") << DefaultText << QString("no match text") << false;
QTest::newRow("MatchWithOneWildcardInTheMiddle") << AlternativeText << QString("some*text") << true;
QTest::newRow("NoMatchWithOneWildcardInTheMiddle") << AlternativeText << QString("differen*text") << false;
QTest::newRow("MatchWithOneWildcardAtBegin") << DefaultText << QString("*text") << true;
QTest::newRow("NoMatchWithOneWildcardAtBegin") << DefaultText << QString("*text something else") << false;
QTest::newRow("NatchWithOneWildcardAtEnd") << DefaultText << QString("some*") << true;
QTest::newRow("NoMatchWithOneWildcardAtEnd") << DefaultText << QString("some other*") << false;
QTest::newRow("MatchWithMultipleWildcards") << AlternativeText << QString("some*th*text") << true;
QTest::newRow("NoMatchWithMultipleWildcards") << AlternativeText << QString("some*abc*text") << false;
QTest::newRow("MatchJustWildcard") << DefaultText << QString("*") << true;
QTest::newRow("MatchFollowingWildcards") << DefaultText << QString("some t**t") << true;
QTest::newRow("CaseSensitivity") << DefaultText.toUpper() << QString("some t**t") << true;
2012-07-22 08:04:50 -04:00
}
2012-07-24 14:30:16 -04:00
void TestWildcardMatcher::testMatcher()
2012-07-22 08:04:50 -04:00
{
2012-07-24 14:30:16 -04:00
QFETCH(QString, text);
QFETCH(QString, pattern);
QFETCH(bool, match);
2012-07-22 08:04:50 -04:00
2012-07-24 14:30:16 -04:00
initMatcher(text);
verifyMatchResult(pattern, match);
2016-10-28 12:49:51 -04:00
cleanupMatcher();
2012-07-22 08:04:50 -04:00
}
2012-07-24 14:30:16 -04:00
void TestWildcardMatcher::initMatcher(QString text)
{
2012-07-24 14:30:16 -04:00
m_matcher = new WildcardMatcher(text);
}
2016-10-28 12:49:51 -04:00
void TestWildcardMatcher::cleanupMatcher()
{
delete m_matcher;
}
2012-07-24 14:30:16 -04:00
void TestWildcardMatcher::verifyMatchResult(QString pattern, bool expected)
2012-07-22 08:04:50 -04:00
{
2012-07-24 14:30:16 -04:00
if (expected) {
verifyMatch(pattern);
}
else {
verifyNoMatch(pattern);
}
2012-07-22 08:04:50 -04:00
}
void TestWildcardMatcher::verifyMatch(QString pattern)
{
bool matchResult = m_matcher->match(pattern);
QVERIFY(matchResult);
}
void TestWildcardMatcher::verifyNoMatch(QString pattern)
{
bool matchResult = m_matcher->match(pattern);
QVERIFY(!matchResult);
}