cpplocatorfilter_test.cpp 12.3 KB
Newer Older
1 2 3 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
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** 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 42 43 44 45 46 47 48 49
#include <extensionsystem/pluginmanager.h>
#include <locator/locatorfiltertest.h>
#include <utils/fileutils.h>

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

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

Q_DECLARE_METATYPE(ILocatorFilter *)

namespace {

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 82 83 84

        ResultDataList results = ResultData::fromFilterEntryList(matchesFor(searchText));
//        ResultData::printFilterEntries(results);
        QVERIFY(!results.isEmpty());
        QCOMPARE(results, expectedResults);
85 86 87
    }

private:
88 89
    void doBeforeLocatorRun() { QVERIFY(parseFiles(m_fileName)); }
    void doAfterLocatorRun() { QVERIFY(garbageCollectGlobalSnapshot()); }
90

91
private:
92 93 94
    const QString m_fileName;
};

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

        ResultDataList results = ResultData::fromFilterEntryList(matchesFor());
//        ResultData::printFilterEntries(results);
        QVERIFY(!results.isEmpty());
        QCOMPARE(results, expectedResults);
113 114 115
    }

private:
116
    void doBeforeLocatorRun()
117 118
    {
        QVERIFY(EditorManager::documentModel()->openedDocuments().isEmpty());
119
        QVERIFY(garbageCollectGlobalSnapshot());
120 121 122

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

        waitForFileInGlobalSnapshot(m_fileName);
125 126
    }

127
    void doAfterLocatorRun()
128
    {
129
        QVERIFY(closeEditorWithoutGarbageCollectorInvocation(m_editor));
130
        QCoreApplication::processEvents();
131
        QVERIFY(EditorManager::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
    CppLocatorFilterTestCase(filter, testFile, searchText, expectedResults);
150 151 152 153 154 155 156 157 158 159 160 161 162
}

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

163
    MyTestDataDir testDirectory(QLatin1String("testdata_basic"));
164 165 166 167 168 169
    const QString testFile = testDirectory.file(QLatin1String("file1.cpp"));
    const QString testFileShort = FileUtils::shortNativePath(FileName::fromString(testFile));

    QTest::newRow("CppFunctionsFilter")
        << testFile
        << cppFunctionsFilter
170
        << _("function")
171
        << (QList<ResultData>()
172 173
            << ResultData(_("functionDefinedInClass(bool, int)"), _("MyClass"))
            << ResultData(_("functionDefinedInClass(bool, int)"), _("MyNamespace::MyClass"))
174 175
            << ResultData(_("functionDefinedInClass(bool, int)"),
                          _("<anonymous namespace>::MyClass"))
176 177
            << ResultData(_("functionDefinedOutSideClass(char)"), _("MyClass"))
            << ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
178 179 180 181
            << ResultData(_("functionDefinedOutSideClass(char)"),
                          _("<anonymous namespace>::MyClass"))
            << ResultData(_("functionDefinedOutSideClassAndNamespace(float)"),
                          _("MyNamespace::MyClass"))
182 183 184 185 186
            << ResultData(_("myFunction(bool, int)"), testFileShort)
            << ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
            << ResultData(_("myFunction(bool, int)"), _("<anonymous namespace>"))
           );

187 188 189 190 191 192
    QTest::newRow("CppFunctionsFilter-WithNamespacePrefix")
        << testFile
        << cppFunctionsFilter
        << _("mynamespace::")
        << (QList<ResultData>()
            << ResultData(_("MyClass()"), _("MyNamespace::MyClass"))
193
            << ResultData(_("functionDefinedInClass(bool, int)"), _("MyNamespace::MyClass"))
194
            << ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
195 196
            << ResultData(_("functionDefinedOutSideClassAndNamespace(float)"),
                          _("MyNamespace::MyClass"))
197 198 199
            << ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
           );

200 201 202 203 204 205 206 207 208 209
    QTest::newRow("CppClassesFilter")
        << testFile
        << cppClassesFilter
        << _("myclass")
        << (QList<ResultData>()
            << ResultData(_("MyClass"), testFileShort)
            << ResultData(_("MyClass"), _("MyNamespace"))
            << ResultData(_("MyClass"), _("<anonymous namespace>"))
           );

210 211 212 213 214 215 216 217
    QTest::newRow("CppClassesFilter-WithNamespacePrefix")
        << testFile
        << cppClassesFilter
        << _("mynamespace::")
        << (QList<ResultData>()
            << ResultData(_("MyClass"), _("MyNamespace"))
           );

218 219
    // all symbols in the left column are expected to be fully qualified.
    QTest::newRow("CppLocatorFilter-filtered")
220 221
        << testFile
        << cppLocatorFilter
222
        << _("my")
223
        << (QList<ResultData>()
224 225
            << ResultData(_("<anonymous namespace>::MyClass"), testFileShort)
            << ResultData(_("<anonymous namespace>::MyClass::MyClass"), _("()"))
226 227
            << ResultData(_("<anonymous namespace>::MyClass::functionDefinedOutSideClass"),
                          _("(char)"))
228 229
            << ResultData(_("<anonymous namespace>::MyEnum"), testFileShort)
            << ResultData(_("<anonymous namespace>::myFunction"), _("(bool, int)"))
230
            << ResultData(_("MyClass"), testFileShort)
231 232
            << ResultData(_("MyClass::MyClass"), _("()"))
            << ResultData(_("MyClass::functionDefinedOutSideClass"), _("(char)"))
233
            << ResultData(_("MyEnum"), testFileShort)
234 235
            << ResultData(_("MyNamespace::MyClass"), testFileShort)
            << ResultData(_("MyNamespace::MyClass::MyClass"), _("()"))
236 237 238 239
            << ResultData(_("MyNamespace::MyClass::functionDefinedOutSideClass"),
                          _("(char)"))
            << ResultData(_("MyNamespace::MyClass::functionDefinedOutSideClassAndNamespace"),
                          _("(float)"))
240 241
            << ResultData(_("MyNamespace::MyEnum"), testFileShort)
            << ResultData(_("MyNamespace::myFunction"), _("(bool, int)"))
242
            << ResultData(_("myFunction"), _("(bool, int)"))
243 244 245 246 247
            );
}

void CppToolsPlugin::test_cpplocatorfilters_CppCurrentDocumentFilter()
{
248
    MyTestDataDir testDirectory(QLatin1String("testdata_basic"));
249 250 251 252 253 254 255 256 257
    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"), _(""))
258 259 260 261 262
        << ResultData(_("MyClass()"), _("MyClass"))
        << ResultData(_("functionDeclaredOnly()"), _("MyClass"))
        << ResultData(_("functionDefinedInClass(bool, int)"), _("MyClass"))
        << ResultData(_("functionDefinedOutSideClass(char)"), _("MyClass"))
        << ResultData(_("functionDefinedOutSideClass(char)"), _("MyClass"))
263
        << ResultData(_("int myVariable"), _("MyNamespace"))
264
        << ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
265 266 267 268
        << ResultData(_("MyEnum"), _("MyNamespace"))
        << ResultData(_("int V1"), _("MyNamespace::MyEnum"))
        << ResultData(_("int V2"), _("MyNamespace::MyEnum"))
        << ResultData(_("MyClass"), _("MyNamespace"))
269 270 271 272
        << ResultData(_("MyClass()"), _("MyNamespace::MyClass"))
        << ResultData(_("functionDeclaredOnly()"), _("MyNamespace::MyClass"))
        << ResultData(_("functionDefinedInClass(bool, int)"), _("MyNamespace::MyClass"))
        << ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
273 274
        << ResultData(_("functionDefinedOutSideClassAndNamespace(float)"),
                      _("MyNamespace::MyClass"))
275
        << ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
276 277
        << ResultData(_("functionDefinedOutSideClassAndNamespace(float)"),
                      _("MyNamespace::MyClass"))
278
        << ResultData(_("int myVariable"), _("<anonymous namespace>"))
279
        << ResultData(_("myFunction(bool, int)"), _("<anonymous namespace>"))
280 281 282 283
        << ResultData(_("MyEnum"), _("<anonymous namespace>"))
        << ResultData(_("int V1"), _("<anonymous namespace>::MyEnum"))
        << ResultData(_("int V2"), _("<anonymous namespace>::MyEnum"))
        << ResultData(_("MyClass"), _("<anonymous namespace>"))
284 285 286 287 288
        << 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"))
289
        << ResultData(_("main()"), _(""))
290 291
        ;

292
    CppCurrentDocumentFilterTestCase(testFile, expectedResults);
293
}