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 #include "TestGlobal.h"
20 #include "autotype/WildcardMatcher.h"
21 
22 QTEST_GUILESS_MAIN(TestWildcardMatcher)
23 
24 const QString TestWildcardMatcher::DefaultText = QString("some text");
25 const QString TestWildcardMatcher::AlternativeText = QString("some other text");
26 
testMatcher_data()27 void TestWildcardMatcher::testMatcher_data()
28 {
29     QTest::addColumn<QString>("text");
30     QTest::addColumn<QString>("pattern");
31     QTest::addColumn<bool>("match");
32 
33     QTest::newRow("MatchWithoutWildcard") << DefaultText << DefaultText << true;
34     QTest::newRow("NoMatchWithoutWildcard") << DefaultText << QString("no match text") << false;
35     QTest::newRow("MatchWithOneWildcardInTheMiddle") << AlternativeText << QString("some*text") << true;
36     QTest::newRow("NoMatchWithOneWildcardInTheMiddle") << AlternativeText << QString("differen*text") << false;
37     QTest::newRow("MatchWithOneWildcardAtBegin") << DefaultText << QString("*text") << true;
38     QTest::newRow("NoMatchWithOneWildcardAtBegin") << DefaultText << QString("*text something else") << false;
39     QTest::newRow("NatchWithOneWildcardAtEnd") << DefaultText << QString("some*") << true;
40     QTest::newRow("NoMatchWithOneWildcardAtEnd") << DefaultText << QString("some other*") << false;
41     QTest::newRow("MatchWithMultipleWildcards") << AlternativeText << QString("some*th*text") << true;
42     QTest::newRow("NoMatchWithMultipleWildcards") << AlternativeText << QString("some*abc*text") << false;
43     QTest::newRow("MatchJustWildcard") << DefaultText << QString("*") << true;
44     QTest::newRow("MatchFollowingWildcards") << DefaultText << QString("some t**t") << true;
45     QTest::newRow("CaseSensitivity") << DefaultText.toUpper() << QString("some t**t") << true;
46 }
47 
testMatcher()48 void TestWildcardMatcher::testMatcher()
49 {
50     QFETCH(QString, text);
51     QFETCH(QString, pattern);
52     QFETCH(bool, match);
53 
54     initMatcher(text);
55     verifyMatchResult(pattern, match);
56     cleanupMatcher();
57 }
58 
initMatcher(QString text)59 void TestWildcardMatcher::initMatcher(QString text)
60 {
61     m_matcher = new WildcardMatcher(text);
62 }
63 
cleanupMatcher()64 void TestWildcardMatcher::cleanupMatcher()
65 {
66     delete m_matcher;
67 }
68 
verifyMatchResult(QString pattern,bool expected)69 void TestWildcardMatcher::verifyMatchResult(QString pattern, bool expected)
70 {
71     if (expected) {
72         verifyMatch(pattern);
73     } else {
74         verifyNoMatch(pattern);
75     }
76 }
77 
verifyMatch(QString pattern)78 void TestWildcardMatcher::verifyMatch(QString pattern)
79 {
80     bool matchResult = m_matcher->match(pattern);
81     QVERIFY(matchResult);
82 }
83 
verifyNoMatch(QString pattern)84 void TestWildcardMatcher::verifyNoMatch(QString pattern)
85 {
86     bool matchResult = m_matcher->match(pattern);
87     QVERIFY(!matchResult);
88 }
89