cpplocatorfilter_test.cpp 11.8 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::Internal::Tests;
51 52 53 54 55 56 57 58 59 60 61
using namespace CppTools::Internal;
using namespace ExtensionSystem;
using namespace Locator;
using namespace Locator::Internal;
using namespace Locator::Internal::Tests;
using namespace Utils;

Q_DECLARE_METATYPE(ILocatorFilter *)

namespace {

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

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

66 67 68
} // anonymous namespace

class CppLocatorFilterTest : public BasicLocatorFilterTest, public CppTools::Tests::TestCase
69 70 71 72 73 74 75
{
public:
    CppLocatorFilterTest(ILocatorFilter *filter, const QString &fileName)
        : BasicLocatorFilterTest(filter)
        , m_fileName(fileName)
    {
        QVERIFY(!m_fileName.isEmpty());
76
        QVERIFY(garbageCollectGlobalSnapshot());
77 78 79
    }

private:
80 81
    virtual void doBeforeLocatorRun() { QVERIFY(parseFiles(m_fileName)); }
    virtual void doAfterLocatorRun() { QVERIFY(garbageCollectGlobalSnapshot()); }
82 83 84 85

    const QString m_fileName;
};

86 87 88
class CppCurrentDocumentFilterTest
    : public BasicLocatorFilterTest
    , public CppTools::Tests::TestCase
89 90 91 92 93 94 95 96 97 98 99 100 101 102
{
public:
    CppCurrentDocumentFilterTest(const QString &fileName)
        : BasicLocatorFilterTest(PluginManager::getObject<CppCurrentDocumentFilter>())
        , m_editor(0)
        , m_fileName(fileName)
    {
        QVERIFY(!m_fileName.isEmpty());
    }

private:
    virtual void doBeforeLocatorRun()
    {
        QVERIFY(EditorManager::documentModel()->openedDocuments().isEmpty());
103
        QVERIFY(garbageCollectGlobalSnapshot());
104 105 106

        m_editor = EditorManager::openEditor(m_fileName);
        QVERIFY(m_editor);
107 108

        waitForFileInGlobalSnapshot(m_fileName);
109 110 111 112
    }

    virtual void doAfterLocatorRun()
    {
113 114
        EditorManager::closeEditor(m_editor, /*askAboutModifiedEditors=*/ false);
        QCoreApplication::processEvents();
115
        QVERIFY(EditorManager::documentModel()->openedDocuments().isEmpty());
116
        QVERIFY(garbageCollectGlobalSnapshot());
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    }

    IEditor *m_editor;
    const QString m_fileName;
};

void CppToolsPlugin::test_cpplocatorfilters_CppLocatorFilter()
{
    QFETCH(QString, testFile);
    QFETCH(ILocatorFilter *, filter);
    QFETCH(QString, searchText);
    QFETCH(ResultDataList, expectedResults);

    CppLocatorFilterTest test(filter, testFile);
    ResultDataList results = ResultData::fromFilterEntryList(test.matchesFor(searchText));
//    ResultData::printFilterEntries(results);
    QVERIFY(!results.isEmpty());
134
    QCOMPARE(results, expectedResults);
135 136 137 138 139 140 141 142 143 144 145 146 147
}

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

148
    MyTestDataDir testDirectory(QLatin1String("testdata_basic"));
149 150 151 152 153 154
    const QString testFile = testDirectory.file(QLatin1String("file1.cpp"));
    const QString testFileShort = FileUtils::shortNativePath(FileName::fromString(testFile));

    QTest::newRow("CppFunctionsFilter")
        << testFile
        << cppFunctionsFilter
155
        << QString::fromLatin1("function")
156
        << (QList<ResultData>()
157 158 159
            << ResultData(_("functionDefinedInClass(bool, int)"), _("MyClass"))
            << ResultData(_("functionDefinedInClass(bool, int)"), _("MyNamespace::MyClass"))
            << ResultData(_("functionDefinedInClass(bool, int)"), _("<anonymous namespace>::MyClass"))
160 161 162 163
            << ResultData(_("functionDefinedOutSideClass(char)"), _("MyClass"))
            << ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
            << ResultData(_("functionDefinedOutSideClass(char)"), _("<anonymous namespace>::MyClass"))
            << ResultData(_("functionDefinedOutSideClassAndNamespace(float)"), _("MyNamespace::MyClass"))
164 165 166 167 168
            << ResultData(_("myFunction(bool, int)"), testFileShort)
            << ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
            << ResultData(_("myFunction(bool, int)"), _("<anonymous namespace>"))
           );

169 170 171 172 173 174
    QTest::newRow("CppFunctionsFilter-WithNamespacePrefix")
        << testFile
        << cppFunctionsFilter
        << _("mynamespace::")
        << (QList<ResultData>()
            << ResultData(_("MyClass()"), _("MyNamespace::MyClass"))
175
            << ResultData(_("functionDefinedInClass(bool, int)"), _("MyNamespace::MyClass"))
176 177
            << ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
            << ResultData(_("functionDefinedOutSideClassAndNamespace(float)"), _("MyNamespace::MyClass"))
178 179 180
            << ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
           );

181 182 183 184 185 186 187 188 189 190
    QTest::newRow("CppClassesFilter")
        << testFile
        << cppClassesFilter
        << _("myclass")
        << (QList<ResultData>()
            << ResultData(_("MyClass"), testFileShort)
            << ResultData(_("MyClass"), _("MyNamespace"))
            << ResultData(_("MyClass"), _("<anonymous namespace>"))
           );

191 192 193 194 195 196 197 198
    QTest::newRow("CppClassesFilter-WithNamespacePrefix")
        << testFile
        << cppClassesFilter
        << _("mynamespace::")
        << (QList<ResultData>()
            << ResultData(_("MyClass"), _("MyNamespace"))
           );

199 200
    // all symbols in the left column are expected to be fully qualified.
    QTest::newRow("CppLocatorFilter-filtered")
201 202
        << testFile
        << cppLocatorFilter
203
        << _("my")
204
        << (QList<ResultData>()
205 206 207 208 209
            << ResultData(_("<anonymous namespace>::MyClass"), testFileShort)
            << ResultData(_("<anonymous namespace>::MyClass::MyClass"), _("()"))
            << ResultData(_("<anonymous namespace>::MyClass::functionDefinedOutSideClass"), _("(char)"))
            << ResultData(_("<anonymous namespace>::MyEnum"), testFileShort)
            << ResultData(_("<anonymous namespace>::myFunction"), _("(bool, int)"))
210
            << ResultData(_("MyClass"), testFileShort)
211 212
            << ResultData(_("MyClass::MyClass"), _("()"))
            << ResultData(_("MyClass::functionDefinedOutSideClass"), _("(char)"))
213
            << ResultData(_("MyEnum"), testFileShort)
214 215 216 217 218 219
            << ResultData(_("MyNamespace::MyClass"), testFileShort)
            << ResultData(_("MyNamespace::MyClass::MyClass"), _("()"))
            << ResultData(_("MyNamespace::MyClass::functionDefinedOutSideClass"), _("(char)"))
            << ResultData(_("MyNamespace::MyClass::functionDefinedOutSideClassAndNamespace"), _("(float)"))
            << ResultData(_("MyNamespace::MyEnum"), testFileShort)
            << ResultData(_("MyNamespace::myFunction"), _("(bool, int)"))
220
            << ResultData(_("myFunction"), _("(bool, int)"))
221 222 223 224 225
            );
}

void CppToolsPlugin::test_cpplocatorfilters_CppCurrentDocumentFilter()
{
226
    MyTestDataDir testDirectory(QLatin1String("testdata_basic"));
227 228 229 230 231 232 233 234 235
    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"), _(""))
236 237 238 239 240
        << ResultData(_("MyClass()"), _("MyClass"))
        << ResultData(_("functionDeclaredOnly()"), _("MyClass"))
        << ResultData(_("functionDefinedInClass(bool, int)"), _("MyClass"))
        << ResultData(_("functionDefinedOutSideClass(char)"), _("MyClass"))
        << ResultData(_("functionDefinedOutSideClass(char)"), _("MyClass"))
241
        << ResultData(_("int myVariable"), _("MyNamespace"))
242
        << ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
243 244 245 246
        << ResultData(_("MyEnum"), _("MyNamespace"))
        << ResultData(_("int V1"), _("MyNamespace::MyEnum"))
        << ResultData(_("int V2"), _("MyNamespace::MyEnum"))
        << ResultData(_("MyClass"), _("MyNamespace"))
247 248 249 250 251 252 253
        << ResultData(_("MyClass()"), _("MyNamespace::MyClass"))
        << ResultData(_("functionDeclaredOnly()"), _("MyNamespace::MyClass"))
        << ResultData(_("functionDefinedInClass(bool, int)"), _("MyNamespace::MyClass"))
        << ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
        << ResultData(_("functionDefinedOutSideClassAndNamespace(float)"), _("MyNamespace::MyClass"))
        << ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
        << ResultData(_("functionDefinedOutSideClassAndNamespace(float)"), _("MyNamespace::MyClass"))
254
        << ResultData(_("int myVariable"), _("<anonymous namespace>"))
255
        << ResultData(_("myFunction(bool, int)"), _("<anonymous namespace>"))
256 257 258 259
        << ResultData(_("MyEnum"), _("<anonymous namespace>"))
        << ResultData(_("int V1"), _("<anonymous namespace>::MyEnum"))
        << ResultData(_("int V2"), _("<anonymous namespace>::MyEnum"))
        << ResultData(_("MyClass"), _("<anonymous namespace>"))
260 261 262 263 264
        << 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"))
265
        << ResultData(_("main()"), _(""))
266 267 268 269 270 271 272 273
        ;

    CppCurrentDocumentFilterTest test(testFile);
    ResultDataList results = ResultData::fromFilterEntryList(test.matchesFor());
//    ResultData::printFilterEntries(results);
    QVERIFY(!results.isEmpty());
    QCOMPARE(expectedResults, results);
}