OSDN Git Service

6f2589360aff17dbbff61c9787245d362028a2d2
[qt-creator-jp/qt-creator-jp.git] / tests / auto / filesearch / tst_filesearch.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include <filesearch.h>
35
36 #include <QtTest/QtTest>
37
38 bool operator==(const Utils::FileSearchResult &r1, const Utils::FileSearchResult &r2)
39 {
40     return r1.fileName == r2.fileName
41             && r1.lineNumber == r2.lineNumber
42             && r1.matchingLine == r2.matchingLine
43             && r1.matchStart == r2.matchStart
44             && r1.matchLength == r2.matchLength
45             && r1.regexpCapturedTexts == r2.regexpCapturedTexts;
46 }
47
48 class tst_FileSearch : public QObject
49 {
50     Q_OBJECT
51
52 private slots:
53     void multipleResults();
54     void caseSensitive();
55     void caseInSensitive();
56 };
57
58 namespace {
59     const char * const FILENAME = ":/tst_filesearch/testfile.txt";
60
61     void test_helper(const Utils::FileSearchResultList &expectedResults,
62                      const QString &term,
63                      QTextDocument::FindFlags flags)
64     {
65         Utils::FileIterator *it = new Utils::FileIterator(QStringList() << QLatin1String(FILENAME), QList<QTextCodec *>() << QTextCodec::codecForLocale());
66         QFutureWatcher<Utils::FileSearchResultList> watcher;
67         QSignalSpy ready(&watcher, SIGNAL(resultsReadyAt(int,int)));
68         watcher.setFuture(Utils::findInFiles(term, it, flags));
69         watcher.future().waitForFinished();
70         QTest::qWait(100); // process events
71         QCOMPARE(ready.count(), 1);
72         Utils::FileSearchResultList results = watcher.resultAt(0);
73         QCOMPARE(results.count(), expectedResults.count());
74         for (int i = 0; i < expectedResults.size(); ++i) {
75             QCOMPARE(results.at(i), expectedResults.at(i));
76         }
77     }
78 }
79
80 void tst_FileSearch::multipleResults()
81 {
82     Utils::FileSearchResultList expectedResults;
83     expectedResults << Utils::FileSearchResult(QLatin1String(FILENAME), 2, QLatin1String("search to find multiple find results"), 10, 4, QStringList());
84     expectedResults << Utils::FileSearchResult(QLatin1String(FILENAME), 2, QLatin1String("search to find multiple find results"), 24, 4, QStringList());
85     expectedResults << Utils::FileSearchResult(QLatin1String(FILENAME), 4, QLatin1String("here you find another result"), 9, 4, QStringList());
86     test_helper(expectedResults, QLatin1String("find"), QTextDocument::FindFlags(0));
87 }
88
89 void tst_FileSearch::caseSensitive()
90 {
91     Utils::FileSearchResultList expectedResults;
92     expectedResults << Utils::FileSearchResult(QLatin1String(FILENAME), 3, QLatin1String("search CaseSensitively for casesensitive"), 7, 13, QStringList());
93     test_helper(expectedResults, QLatin1String("CaseSensitive"), QTextDocument::FindCaseSensitively);
94 }
95
96 void tst_FileSearch::caseInSensitive()
97 {
98     Utils::FileSearchResultList expectedResults;
99     expectedResults << Utils::FileSearchResult(QLatin1String(FILENAME), 3, QLatin1String("search CaseSensitively for casesensitive"), 7, 13, QStringList());
100     expectedResults << Utils::FileSearchResult(QLatin1String(FILENAME), 3, QLatin1String("search CaseSensitively for casesensitive"), 27, 13, QStringList());
101     test_helper(expectedResults, QLatin1String("CaseSensitive"), QTextDocument::FindFlags(0));
102 }
103
104 QTEST_MAIN(tst_FileSearch)
105
106 #include "tst_filesearch.moc"