cpplocatorfilter_test.cpp 13.4 KB
Newer Older
1 2
/****************************************************************************
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "cpptoolsplugin.h"

#include "cppclassesfilter.h"
#include "cppcurrentdocumentfilter.h"
#include "cppfunctionsfilter.h"
#include "cpplocatorfilter.h"
#include "cppmodelmanager.h"
37
#include "cpptoolstestcase.h"
38 39

#include <coreplugin/editormanager/editormanager.h>
40
#include <coreplugin/testdatadir.h>
41
#include <extensionsystem/pluginmanager.h>
42
#include <coreplugin/locator/locatorfiltertest.h>
43 44 45 46 47 48 49
#include <utils/fileutils.h>

#include <QDebug>
#include <QFileInfo>
#include <QtTest>

using namespace Core;
50
using namespace Core::Tests;
51 52 53 54 55 56 57 58
using namespace CppTools::Internal;
using namespace ExtensionSystem;
using namespace Utils;

Q_DECLARE_METATYPE(ILocatorFilter *)

namespace {

59 60
enum { debug = 0 };

61 62 63
QTC_DECLARE_MYTESTDATADIR("../../../tests/cpplocators/")

inline QString _(const QByteArray &ba) { return QString::fromLatin1(ba, ba.size()); }
64

65 66 67
class CppLocatorFilterTestCase
    : public BasicLocatorFilterTest
    , public CppTools::Tests::TestCase
68 69
{
public:
70 71 72 73
    CppLocatorFilterTestCase(ILocatorFilter *filter,
                             const QString &fileName,
                             const QString &searchText,
                             const ResultDataList &expectedResults)
74 75 76
        : BasicLocatorFilterTest(filter)
        , m_fileName(fileName)
    {
77
        QVERIFY(succeededSoFar());
78
        QVERIFY(!m_fileName.isEmpty());
79
        QVERIFY(garbageCollectGlobalSnapshot());
80 81

        ResultDataList results = ResultData::fromFilterEntryList(matchesFor(searchText));
82 83 84 85
        if (debug) {
            ResultData::printFilterEntries(expectedResults, QLatin1String("Expected:"));
            ResultData::printFilterEntries(results, QLatin1String("Results:"));
        }
86 87
        QVERIFY(!results.isEmpty());
        QCOMPARE(results, expectedResults);
88 89 90
    }

private:
91 92
    void doBeforeLocatorRun() { QVERIFY(parseFiles(m_fileName)); }
    void doAfterLocatorRun() { QVERIFY(garbageCollectGlobalSnapshot()); }
93

94
private:
95 96 97
    const QString m_fileName;
};

98
class CppCurrentDocumentFilterTestCase
99 100
    : public BasicLocatorFilterTest
    , public CppTools::Tests::TestCase
101 102
{
public:
103 104
    CppCurrentDocumentFilterTestCase(const QString &fileName,
                                     const ResultDataList &expectedResults)
105 106 107 108
        : BasicLocatorFilterTest(PluginManager::getObject<CppCurrentDocumentFilter>())
        , m_editor(0)
        , m_fileName(fileName)
    {
109
        QVERIFY(succeededSoFar());
110
        QVERIFY(!m_fileName.isEmpty());
111 112

        ResultDataList results = ResultData::fromFilterEntryList(matchesFor());
113 114 115 116
        if (debug) {
            ResultData::printFilterEntries(expectedResults, QLatin1String("Expected:"));
            ResultData::printFilterEntries(results, QLatin1String("Results:"));
        }
117 118
        QVERIFY(!results.isEmpty());
        QCOMPARE(results, expectedResults);
119 120 121
    }

private:
122
    void doBeforeLocatorRun()
123
    {
hjk's avatar
hjk committed
124
        QVERIFY(DocumentModel::openedDocuments().isEmpty());
125
        QVERIFY(garbageCollectGlobalSnapshot());
126 127 128

        m_editor = EditorManager::openEditor(m_fileName);
        QVERIFY(m_editor);
129 130

        waitForFileInGlobalSnapshot(m_fileName);
131 132
    }

133
    void doAfterLocatorRun()
134
    {
135
        QVERIFY(closeEditorWithoutGarbageCollectorInvocation(m_editor));
136
        QCoreApplication::processEvents();
hjk's avatar
hjk committed
137
        QVERIFY(DocumentModel::openedDocuments().isEmpty());
138
        QVERIFY(garbageCollectGlobalSnapshot());
139 140
    }

141
private:
142 143 144 145
    IEditor *m_editor;
    const QString m_fileName;
};

146 147
} // anonymous namespace

148 149 150 151 152 153 154
void CppToolsPlugin::test_cpplocatorfilters_CppLocatorFilter()
{
    QFETCH(QString, testFile);
    QFETCH(ILocatorFilter *, filter);
    QFETCH(QString, searchText);
    QFETCH(ResultDataList, expectedResults);

155
    CppLocatorFilterTestCase(filter, testFile, searchText, expectedResults);
156 157 158 159 160 161 162 163 164 165 166 167 168
}

void CppToolsPlugin::test_cpplocatorfilters_CppLocatorFilter_data()
{
    QTest::addColumn<QString>("testFile");
    QTest::addColumn<ILocatorFilter *>("filter");
    QTest::addColumn<QString>("searchText");
    QTest::addColumn<ResultDataList>("expectedResults");

    ILocatorFilter *cppFunctionsFilter = PluginManager::getObject<CppFunctionsFilter>();
    ILocatorFilter *cppClassesFilter = PluginManager::getObject<CppClassesFilter>();
    ILocatorFilter *cppLocatorFilter = PluginManager::getObject<CppLocatorFilter>();

169
    MyTestDataDir testDirectory(QLatin1String("testdata_basic"));
170
    const QString testFile = testDirectory.file(QLatin1String("file1.cpp"));
171
    const QString objTestFile = testDirectory.file(QLatin1String("file1.mm"));
172
    const QString testFileShort = FileUtils::shortNativePath(FileName::fromString(testFile));
173
    const QString objTestFileShort = FileUtils::shortNativePath(FileName::fromString(objTestFile));
174 175 176 177

    QTest::newRow("CppFunctionsFilter")
        << testFile
        << cppFunctionsFilter
178
        << _("function")
179
        << (QList<ResultData>()
180 181
            << ResultData(_("functionDefinedInClass(bool, int)"), _("MyClass"))
            << ResultData(_("functionDefinedInClass(bool, int)"), _("MyNamespace::MyClass"))
182 183
            << ResultData(_("functionDefinedInClass(bool, int)"),
                          _("<anonymous namespace>::MyClass"))
184 185
            << ResultData(_("functionDefinedOutSideClass(char)"), _("MyClass"))
            << ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
186 187 188 189
            << ResultData(_("functionDefinedOutSideClass(char)"),
                          _("<anonymous namespace>::MyClass"))
            << ResultData(_("functionDefinedOutSideClassAndNamespace(float)"),
                          _("MyNamespace::MyClass"))
190 191 192 193 194
            << ResultData(_("myFunction(bool, int)"), testFileShort)
            << ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
            << ResultData(_("myFunction(bool, int)"), _("<anonymous namespace>"))
           );

195 196 197 198 199 200
    QTest::newRow("CppFunctionsFilter-WithNamespacePrefix")
        << testFile
        << cppFunctionsFilter
        << _("mynamespace::")
        << (QList<ResultData>()
            << ResultData(_("MyClass()"), _("MyNamespace::MyClass"))
201
            << ResultData(_("functionDefinedInClass(bool, int)"), _("MyNamespace::MyClass"))
202
            << ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
203 204
            << ResultData(_("functionDefinedOutSideClassAndNamespace(float)"),
                          _("MyNamespace::MyClass"))
205 206 207
            << ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
           );

208 209 210 211 212 213 214 215 216 217
    QTest::newRow("CppClassesFilter")
        << testFile
        << cppClassesFilter
        << _("myclass")
        << (QList<ResultData>()
            << ResultData(_("MyClass"), testFileShort)
            << ResultData(_("MyClass"), _("MyNamespace"))
            << ResultData(_("MyClass"), _("<anonymous namespace>"))
           );

218 219 220 221 222 223 224 225
    QTest::newRow("CppClassesFilter-WithNamespacePrefix")
        << testFile
        << cppClassesFilter
        << _("mynamespace::")
        << (QList<ResultData>()
            << ResultData(_("MyClass"), _("MyNamespace"))
           );

226 227
    // all symbols in the left column are expected to be fully qualified.
    QTest::newRow("CppLocatorFilter-filtered")
228 229
        << testFile
        << cppLocatorFilter
230
        << _("my")
231
        << (QList<ResultData>()
232 233
            << ResultData(_("<anonymous namespace>::MyClass"), testFileShort)
            << ResultData(_("<anonymous namespace>::MyClass::MyClass"), _("()"))
234 235
            << ResultData(_("<anonymous namespace>::MyClass::functionDefinedOutSideClass"),
                          _("(char)"))
236 237
            << ResultData(_("<anonymous namespace>::MyEnum"), testFileShort)
            << ResultData(_("<anonymous namespace>::myFunction"), _("(bool, int)"))
238
            << ResultData(_("MyClass"), testFileShort)
239 240
            << ResultData(_("MyClass::MyClass"), _("()"))
            << ResultData(_("MyClass::functionDefinedOutSideClass"), _("(char)"))
241
            << ResultData(_("MyEnum"), testFileShort)
242 243
            << ResultData(_("MyNamespace::MyClass"), testFileShort)
            << ResultData(_("MyNamespace::MyClass::MyClass"), _("()"))
244 245 246 247
            << ResultData(_("MyNamespace::MyClass::functionDefinedOutSideClass"),
                          _("(char)"))
            << ResultData(_("MyNamespace::MyClass::functionDefinedOutSideClassAndNamespace"),
                          _("(float)"))
248 249
            << ResultData(_("MyNamespace::MyEnum"), testFileShort)
            << ResultData(_("MyNamespace::myFunction"), _("(bool, int)"))
250
            << ResultData(_("myFunction"), _("(bool, int)"))
251
            );
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

    QTest::newRow("CppClassesFilter-ObjC")
            << objTestFile
            << cppClassesFilter
            << _("M")
            << (QList<ResultData>()
                << ResultData(_("MyClass"), objTestFileShort)
                << ResultData(_("MyClass"), objTestFileShort)
                << ResultData(_("MyClass"), objTestFileShort)
                << ResultData(_("MyProtocol"), objTestFileShort)
                );

    QTest::newRow("CppFunctionsFilter-ObjC")
        << objTestFile
        << cppFunctionsFilter
        << _("M")
        << (QList<ResultData>()
            << ResultData(_("anotherMethod"), _("MyClass"))
            << ResultData(_("anotherMethod:"), _("MyClass"))
            << ResultData(_("someMethod"), _("MyClass"))
            );
273 274 275 276
}

void CppToolsPlugin::test_cpplocatorfilters_CppCurrentDocumentFilter()
{
277
    MyTestDataDir testDirectory(QLatin1String("testdata_basic"));
278 279 280 281 282 283 284 285 286
    const QString testFile = testDirectory.file(QLatin1String("file1.cpp"));

    QList<ResultData> expectedResults = QList<ResultData>()
        << ResultData(_("int myVariable"), _(""))
        << ResultData(_("myFunction(bool, int)"), _(""))
        << ResultData(_("MyEnum"), _(""))
        << ResultData(_("int V1"), _("MyEnum"))
        << ResultData(_("int V2"), _("MyEnum"))
        << ResultData(_("MyClass"), _(""))
287 288 289 290 291
        << ResultData(_("MyClass()"), _("MyClass"))
        << ResultData(_("functionDeclaredOnly()"), _("MyClass"))
        << ResultData(_("functionDefinedInClass(bool, int)"), _("MyClass"))
        << ResultData(_("functionDefinedOutSideClass(char)"), _("MyClass"))
        << ResultData(_("functionDefinedOutSideClass(char)"), _("MyClass"))
292
        << ResultData(_("int myVariable"), _("MyNamespace"))
293
        << ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
294 295 296 297
        << ResultData(_("MyEnum"), _("MyNamespace"))
        << ResultData(_("int V1"), _("MyNamespace::MyEnum"))
        << ResultData(_("int V2"), _("MyNamespace::MyEnum"))
        << ResultData(_("MyClass"), _("MyNamespace"))
298 299 300 301
        << ResultData(_("MyClass()"), _("MyNamespace::MyClass"))
        << ResultData(_("functionDeclaredOnly()"), _("MyNamespace::MyClass"))
        << ResultData(_("functionDefinedInClass(bool, int)"), _("MyNamespace::MyClass"))
        << ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
302 303
        << ResultData(_("functionDefinedOutSideClassAndNamespace(float)"),
                      _("MyNamespace::MyClass"))
304
        << ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
305 306
        << ResultData(_("functionDefinedOutSideClassAndNamespace(float)"),
                      _("MyNamespace::MyClass"))
307
        << ResultData(_("int myVariable"), _("<anonymous namespace>"))
308
        << ResultData(_("myFunction(bool, int)"), _("<anonymous namespace>"))
309 310 311 312
        << ResultData(_("MyEnum"), _("<anonymous namespace>"))
        << ResultData(_("int V1"), _("<anonymous namespace>::MyEnum"))
        << ResultData(_("int V2"), _("<anonymous namespace>::MyEnum"))
        << ResultData(_("MyClass"), _("<anonymous namespace>"))
313 314 315 316 317
        << ResultData(_("MyClass()"), _("<anonymous namespace>::MyClass"))
        << ResultData(_("functionDeclaredOnly()"), _("<anonymous namespace>::MyClass"))
        << ResultData(_("functionDefinedInClass(bool, int)"), _("<anonymous namespace>::MyClass"))
        << ResultData(_("functionDefinedOutSideClass(char)"), _("<anonymous namespace>::MyClass"))
        << ResultData(_("functionDefinedOutSideClass(char)"), _("<anonymous namespace>::MyClass"))
318
        << ResultData(_("main()"), _(""))
319 320
        ;

321
    CppCurrentDocumentFilterTestCase(testFile, expectedResults);
322
}