cpplocatorfilter_test.cpp 14.9 KB
Newer Older
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5 6 7 8 9 10 11
**
** 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
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 24 25 26 27 28 29 30 31 32
**
****************************************************************************/

#include "cpptoolsplugin.h"

#include "cppclassesfilter.h"
#include "cppcurrentdocumentfilter.h"
#include "cppfunctionsfilter.h"
#include "cpplocatorfilter.h"
#include "cppmodelmanager.h"
33
#include "cpptoolstestcase.h"
34 35

#include <coreplugin/editormanager/editormanager.h>
36
#include <coreplugin/testdatadir.h>
37
#include <extensionsystem/pluginmanager.h>
38
#include <coreplugin/locator/locatorfiltertest.h>
39 40 41 42 43 44 45
#include <utils/fileutils.h>

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

using namespace Core;
46
using namespace Core::Tests;
47 48 49 50 51 52
using namespace CppTools::Internal;
using namespace ExtensionSystem;
using namespace Utils;

namespace {

53 54
enum { debug = 0 };

55 56 57
QTC_DECLARE_MYTESTDATADIR("../../../tests/cpplocators/")

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

59 60 61
class CppLocatorFilterTestCase
    : public BasicLocatorFilterTest
    , public CppTools::Tests::TestCase
62 63
{
public:
64 65 66 67
    CppLocatorFilterTestCase(ILocatorFilter *filter,
                             const QString &fileName,
                             const QString &searchText,
                             const ResultDataList &expectedResults)
68 69 70
        : BasicLocatorFilterTest(filter)
        , m_fileName(fileName)
    {
71
        QVERIFY(succeededSoFar());
72
        QVERIFY(!m_fileName.isEmpty());
73
        QVERIFY(garbageCollectGlobalSnapshot());
74 75

        ResultDataList results = ResultData::fromFilterEntryList(matchesFor(searchText));
76 77 78 79
        if (debug) {
            ResultData::printFilterEntries(expectedResults, QLatin1String("Expected:"));
            ResultData::printFilterEntries(results, QLatin1String("Results:"));
        }
80 81
        QVERIFY(!results.isEmpty());
        QCOMPARE(results, expectedResults);
82 83 84
    }

private:
85 86
    void doBeforeLocatorRun() { QVERIFY(parseFiles(m_fileName)); }
    void doAfterLocatorRun() { QVERIFY(garbageCollectGlobalSnapshot()); }
87

88
private:
89 90 91
    const QString m_fileName;
};

92
class CppCurrentDocumentFilterTestCase
93 94
    : public BasicLocatorFilterTest
    , public CppTools::Tests::TestCase
95 96
{
public:
97 98
    CppCurrentDocumentFilterTestCase(const QString &fileName,
                                     const ResultDataList &expectedResults)
99 100 101 102
        : BasicLocatorFilterTest(PluginManager::getObject<CppCurrentDocumentFilter>())
        , m_editor(0)
        , m_fileName(fileName)
    {
103
        QVERIFY(succeededSoFar());
104
        QVERIFY(!m_fileName.isEmpty());
105 106

        ResultDataList results = ResultData::fromFilterEntryList(matchesFor());
107 108 109 110
        if (debug) {
            ResultData::printFilterEntries(expectedResults, QLatin1String("Expected:"));
            ResultData::printFilterEntries(results, QLatin1String("Results:"));
        }
111 112
        QVERIFY(!results.isEmpty());
        QCOMPARE(results, expectedResults);
113 114 115
    }

private:
116
    void doBeforeLocatorRun()
117
    {
hjk's avatar
hjk committed
118
        QVERIFY(DocumentModel::openedDocuments().isEmpty());
119
        QVERIFY(garbageCollectGlobalSnapshot());
120 121 122

        m_editor = EditorManager::openEditor(m_fileName);
        QVERIFY(m_editor);
123

124
        QVERIFY(waitForFileInGlobalSnapshot(m_fileName));
125 126
    }

127
    void doAfterLocatorRun()
128
    {
129
        QVERIFY(closeEditorWithoutGarbageCollectorInvocation(m_editor));
130
        QCoreApplication::processEvents();
hjk's avatar
hjk committed
131
        QVERIFY(DocumentModel::openedDocuments().isEmpty());
132
        QVERIFY(garbageCollectGlobalSnapshot());
133 134
    }

135
private:
136 137 138 139
    IEditor *m_editor;
    const QString m_fileName;
};

140 141
} // anonymous namespace

142 143 144 145 146 147 148
void CppToolsPlugin::test_cpplocatorfilters_CppLocatorFilter()
{
    QFETCH(QString, testFile);
    QFETCH(ILocatorFilter *, filter);
    QFETCH(QString, searchText);
    QFETCH(ResultDataList, expectedResults);

149 150
    Tests::VerifyCleanCppModelManager verify;

151
    CppLocatorFilterTestCase(filter, testFile, searchText, expectedResults);
152 153 154 155 156 157 158 159 160 161 162 163 164
}

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>();

165
    MyTestDataDir testDirectory(QLatin1String("testdata_basic"));
166
    const QString testFile = testDirectory.file(QLatin1String("file1.cpp"));
167
    const QString objTestFile = testDirectory.file(QLatin1String("file1.mm"));
168
    const QString testFileShort = FileUtils::shortNativePath(FileName::fromString(testFile));
169
    const QString objTestFileShort = FileUtils::shortNativePath(FileName::fromString(objTestFile));
170 171 172 173

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

193 194 195 196 197 198
    QTest::newRow("CppFunctionsFilter-Sorting")
        << testFile
        << cppFunctionsFilter
        << _("pos")
        << (QList<ResultData>()
            << ResultData(_("positiveNumber()"), testFileShort)
199
            << ResultData(_("matchArgument(Pos)"), testFileShort)
200 201 202
            << ResultData(_("pointOfService()"), testFileShort)
           );

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    QTest::newRow("CppFunctionsFilter-arguments")
            << testFile
            << cppFunctionsFilter
            << _("function*bool")
            << (QList<ResultData>()
                << ResultData(_("functionDefinedInClass(bool, int)"),
                              _("MyClass (file1.cpp)"))
                << ResultData(_("functionDefinedInClass(bool, int)"),
                              _("MyNamespace::MyClass (file1.cpp)"))
                << ResultData(_("functionDefinedInClass(bool, int)"),
                              _("<anonymous namespace>::MyClass (file1.cpp)"))
                << ResultData(_("myFunction(bool, int)"), testFileShort)
                << ResultData(_("myFunction(bool, int)"), _("MyNamespace (file1.cpp)"))
                << ResultData(_("myFunction(bool, int)"), _("<anonymous namespace> (file1.cpp)"))
           );

219 220 221 222 223
    QTest::newRow("CppFunctionsFilter-WithNamespacePrefix")
        << testFile
        << cppFunctionsFilter
        << _("mynamespace::")
        << (QList<ResultData>()
224 225 226 227 228
            << ResultData(_("MyClass()"), _("MyNamespace::MyClass (file1.cpp)"))
            << ResultData(_("functionDefinedInClass(bool, int)"),
                          _("MyNamespace::MyClass (file1.cpp)"))
            << ResultData(_("functionDefinedOutSideClass(char)"),
                          _("MyNamespace::MyClass (file1.cpp)"))
229
            << ResultData(_("functionDefinedOutSideClassAndNamespace(float)"),
230 231
                          _("MyNamespace::MyClass (file1.cpp)"))
            << ResultData(_("myFunction(bool, int)"), _("MyNamespace (file1.cpp)"))
232 233
           );

234 235 236 237 238 239 240 241 242 243
    QTest::newRow("CppClassesFilter")
        << testFile
        << cppClassesFilter
        << _("myclass")
        << (QList<ResultData>()
            << ResultData(_("MyClass"), testFileShort)
            << ResultData(_("MyClass"), _("MyNamespace"))
            << ResultData(_("MyClass"), _("<anonymous namespace>"))
           );

244 245 246 247 248 249 250 251
    QTest::newRow("CppClassesFilter-WithNamespacePrefix")
        << testFile
        << cppClassesFilter
        << _("mynamespace::")
        << (QList<ResultData>()
            << ResultData(_("MyClass"), _("MyNamespace"))
           );

252 253
    // all symbols in the left column are expected to be fully qualified.
    QTest::newRow("CppLocatorFilter-filtered")
254 255
        << testFile
        << cppLocatorFilter
256
        << _("my")
257
        << (QList<ResultData>()
258 259
            << ResultData(_("<anonymous namespace>::MyClass"), testFileShort)
            << ResultData(_("<anonymous namespace>::MyClass::MyClass"), _("()"))
260 261
            << ResultData(_("<anonymous namespace>::MyClass::functionDefinedOutSideClass"),
                          _("(char)"))
262 263
            << ResultData(_("<anonymous namespace>::MyEnum"), testFileShort)
            << ResultData(_("<anonymous namespace>::myFunction"), _("(bool, int)"))
264
            << ResultData(_("MyClass"), testFileShort)
265 266
            << ResultData(_("MyClass::MyClass"), _("()"))
            << ResultData(_("MyClass::functionDefinedOutSideClass"), _("(char)"))
267
            << ResultData(_("MyEnum"), testFileShort)
268 269
            << ResultData(_("MyNamespace::MyClass"), testFileShort)
            << ResultData(_("MyNamespace::MyClass::MyClass"), _("()"))
270 271 272 273
            << ResultData(_("MyNamespace::MyClass::functionDefinedOutSideClass"),
                          _("(char)"))
            << ResultData(_("MyNamespace::MyClass::functionDefinedOutSideClassAndNamespace"),
                          _("(float)"))
274 275
            << ResultData(_("MyNamespace::MyEnum"), testFileShort)
            << ResultData(_("MyNamespace::myFunction"), _("(bool, int)"))
276
            << ResultData(_("myFunction"), _("(bool, int)"))
277
            );
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

    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>()
295 296 297
            << ResultData(_("anotherMethod"), _("MyClass (file1.mm)"))
            << ResultData(_("anotherMethod:"), _("MyClass (file1.mm)"))
            << ResultData(_("someMethod"), _("MyClass (file1.mm)"))
298
            );
299 300 301 302
}

void CppToolsPlugin::test_cpplocatorfilters_CppCurrentDocumentFilter()
{
303
    MyTestDataDir testDirectory(QLatin1String("testdata_basic"));
304 305 306 307 308
    const QString testFile = testDirectory.file(QLatin1String("file1.cpp"));

    QList<ResultData> expectedResults = QList<ResultData>()
        << ResultData(_("int myVariable"), _(""))
        << ResultData(_("myFunction(bool, int)"), _(""))
309
        << ResultData(_("Pos"), _(""))
310
        << ResultData(_("pointOfService()"), _(""))
311
        << ResultData(_("matchArgument(Pos)"), _(""))
312
        << ResultData(_("positiveNumber()"), _(""))
313 314 315 316
        << ResultData(_("MyEnum"), _(""))
        << ResultData(_("int V1"), _("MyEnum"))
        << ResultData(_("int V2"), _("MyEnum"))
        << ResultData(_("MyClass"), _(""))
317 318 319 320 321
        << ResultData(_("MyClass()"), _("MyClass"))
        << ResultData(_("functionDeclaredOnly()"), _("MyClass"))
        << ResultData(_("functionDefinedInClass(bool, int)"), _("MyClass"))
        << ResultData(_("functionDefinedOutSideClass(char)"), _("MyClass"))
        << ResultData(_("functionDefinedOutSideClass(char)"), _("MyClass"))
322
        << ResultData(_("int myVariable"), _("MyNamespace"))
323
        << ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
324 325 326 327
        << ResultData(_("MyEnum"), _("MyNamespace"))
        << ResultData(_("int V1"), _("MyNamespace::MyEnum"))
        << ResultData(_("int V2"), _("MyNamespace::MyEnum"))
        << ResultData(_("MyClass"), _("MyNamespace"))
328 329 330 331
        << ResultData(_("MyClass()"), _("MyNamespace::MyClass"))
        << ResultData(_("functionDeclaredOnly()"), _("MyNamespace::MyClass"))
        << ResultData(_("functionDefinedInClass(bool, int)"), _("MyNamespace::MyClass"))
        << ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
332 333
        << ResultData(_("functionDefinedOutSideClassAndNamespace(float)"),
                      _("MyNamespace::MyClass"))
334
        << ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
335 336
        << ResultData(_("functionDefinedOutSideClassAndNamespace(float)"),
                      _("MyNamespace::MyClass"))
337
        << ResultData(_("int myVariable"), _("<anonymous namespace>"))
338
        << ResultData(_("myFunction(bool, int)"), _("<anonymous namespace>"))
339 340 341 342
        << ResultData(_("MyEnum"), _("<anonymous namespace>"))
        << ResultData(_("int V1"), _("<anonymous namespace>::MyEnum"))
        << ResultData(_("int V2"), _("<anonymous namespace>::MyEnum"))
        << ResultData(_("MyClass"), _("<anonymous namespace>"))
343 344 345 346 347
        << 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"))
348
        << ResultData(_("main()"), _(""))
349 350
        ;

351
    CppCurrentDocumentFilterTestCase(testFile, expectedResults);
352
}