1 /* 2 * Copyright (C) 2012 Felix Geyer <debfx@fobos.de> 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 or (at your option) 7 * version 3 of the License. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #include "TestWildcardMatcher.h" 19 20 #include <QTest> 21 22 #include "autotype/WildcardMatcher.h" 23 24 QTEST_GUILESS_MAIN(TestWildcardMatcher) 25 26 const QString TestWildcardMatcher::DefaultText = QString("some text"); 27 const QString TestWildcardMatcher::AlternativeText = QString("some other text"); 28 testMatcher_data()29void TestWildcardMatcher::testMatcher_data() 30 { 31 QTest::addColumn<QString>("text"); 32 QTest::addColumn<QString>("pattern"); 33 QTest::addColumn<bool>("match"); 34 35 QTest::newRow("MatchWithoutWildcard") << DefaultText << DefaultText << true; 36 QTest::newRow("NoMatchWithoutWildcard") << DefaultText << QString("no match text") << false; 37 QTest::newRow("MatchWithOneWildcardInTheMiddle") << AlternativeText << QString("some*text") << true; 38 QTest::newRow("NoMatchWithOneWildcardInTheMiddle") << AlternativeText << QString("differen*text") << false; 39 QTest::newRow("MatchWithOneWildcardAtBegin") << DefaultText << QString("*text") << true; 40 QTest::newRow("NoMatchWithOneWildcardAtBegin") << DefaultText << QString("*text something else") << false; 41 QTest::newRow("NatchWithOneWildcardAtEnd") << DefaultText << QString("some*") << true; 42 QTest::newRow("NoMatchWithOneWildcardAtEnd") << DefaultText << QString("some other*") << false; 43 QTest::newRow("MatchWithMultipleWildcards") << AlternativeText << QString("some*th*text") << true; 44 QTest::newRow("NoMatchWithMultipleWildcards") << AlternativeText << QString("some*abc*text") << false; 45 QTest::newRow("MatchJustWildcard") << DefaultText << QString("*") << true; 46 QTest::newRow("MatchFollowingWildcards") << DefaultText << QString("some t**t") << true; 47 QTest::newRow("CaseSensitivity") << DefaultText.toUpper() << QString("some t**t") << true; 48 } 49 testMatcher()50void TestWildcardMatcher::testMatcher() 51 { 52 QFETCH(QString, text); 53 QFETCH(QString, pattern); 54 QFETCH(bool, match); 55 56 initMatcher(text); 57 verifyMatchResult(pattern, match); 58 } 59 initMatcher(QString text)60void TestWildcardMatcher::initMatcher(QString text) 61 { 62 m_matcher = new WildcardMatcher(text); 63 } 64 verifyMatchResult(QString pattern,bool expected)65void TestWildcardMatcher::verifyMatchResult(QString pattern, bool expected) 66 { 67 if (expected) { 68 verifyMatch(pattern); 69 } 70 else { 71 verifyNoMatch(pattern); 72 } 73 } 74 verifyMatch(QString pattern)75void TestWildcardMatcher::verifyMatch(QString pattern) 76 { 77 bool matchResult = m_matcher->match(pattern); 78 QVERIFY(matchResult); 79 } 80 verifyNoMatch(QString pattern)81void TestWildcardMatcher::verifyNoMatch(QString pattern) 82 { 83 bool matchResult = m_matcher->match(pattern); 84 QVERIFY(!matchResult); 85 } 86