cpplocatorfilter_test.cpp 10.6 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 37 38
/****************************************************************************
**
** 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"

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

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

using namespace Core;
49
using namespace Core::Internal::Tests;
50 51 52 53 54 55 56 57 58 59 60
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 {

61
class MyTestDataDir : public Core::Internal::Tests::TestDataDir
62 63
{
public:
64 65
    MyTestDataDir(const QString &testDataDirectory)
        : TestDataDir(QLatin1String(SRCDIR "/../../../tests/cpplocators/") + testDataDirectory) {}
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
};

class CppLocatorFilterTest : public BasicLocatorFilterTest
{
public:
    CppLocatorFilterTest(ILocatorFilter *filter, const QString &fileName)
        : BasicLocatorFilterTest(filter)
        , m_modelManager(CppModelManager::instance())
        , m_fileName(fileName)
    {
        QVERIFY(!m_fileName.isEmpty());
        m_modelManager->GC();
        QVERIFY(m_modelManager->snapshot().isEmpty());
    }

private:
    virtual void doBeforeLocatorRun()
    {
        m_modelManager->updateSourceFiles(QStringList() << m_fileName).waitForFinished();
        QVERIFY(m_modelManager->snapshot().contains(m_fileName));
        QCoreApplication::processEvents();
    }

    virtual void doAfterLocatorRun()
    {
        m_modelManager->GC();
        QVERIFY(m_modelManager->snapshot().isEmpty());
    }

    CppModelManager *m_modelManager;
    const QString m_fileName;
};

class CppCurrentDocumentFilterTest : public BasicLocatorFilterTest
{
public:
    CppCurrentDocumentFilterTest(const QString &fileName)
        : BasicLocatorFilterTest(PluginManager::getObject<CppCurrentDocumentFilter>())
        , m_modelManager(CppModelManager::instance())
        , m_editor(0)
        , m_fileName(fileName)
    {
        QVERIFY(!m_fileName.isEmpty());
        m_modelManager->GC();
        QVERIFY(m_modelManager->snapshot().isEmpty());
    }

private:
    virtual void doBeforeLocatorRun()
    {
        QVERIFY(EditorManager::documentModel()->openedDocuments().isEmpty());
        m_modelManager->GC();
        QVERIFY(m_modelManager->snapshot().isEmpty());

        m_editor = EditorManager::openEditor(m_fileName);
        QVERIFY(m_editor);
        while (!m_modelManager->snapshot().contains(m_fileName))
            QCoreApplication::processEvents();
    }

    virtual void doAfterLocatorRun()
    {
        EditorManager::instance()->closeEditor(m_editor, /*askAboutModifiedEditors=*/ false);
        QCoreApplication::processEvents();
        QVERIFY(EditorManager::documentModel()->openedDocuments().isEmpty());
        m_modelManager->GC();
        QVERIFY(m_modelManager->snapshot().isEmpty());
    }

    CppModelManager *m_modelManager;
    IEditor *m_editor;
    const QString m_fileName;
};

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

} // anonymous namespace

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());
155
    QCOMPARE(results, 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 171 172 173 174 175 176 177 178 179 180 181 182
    const QString testFile = testDirectory.file(QLatin1String("file1.cpp"));
    const QString testFileShort = FileUtils::shortNativePath(FileName::fromString(testFile));

    QTest::newRow("CppFunctionsFilter")
        << testFile
        << cppFunctionsFilter
        << QString::fromLatin1("myfunction")
        << (QList<ResultData>()
            << ResultData(_("myFunction(bool, int)"), testFileShort)
            << ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
            << ResultData(_("myFunction(bool, int)"), _("<anonymous namespace>"))
           );

183 184 185 186 187 188 189 190 191 192
    QTest::newRow("CppFunctionsFilter-WithNamespacePrefix")
        << testFile
        << cppFunctionsFilter
        << _("mynamespace::")
        << (QList<ResultData>()
            << ResultData(_("MyClass()"), _("MyNamespace::MyClass"))
            << ResultData(_("function2(bool, int)"), _("MyNamespace::MyClass"))
            << ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
           );

193 194 195 196 197 198 199 200 201 202
    QTest::newRow("CppClassesFilter")
        << testFile
        << cppClassesFilter
        << _("myclass")
        << (QList<ResultData>()
            << ResultData(_("MyClass"), testFileShort)
            << ResultData(_("MyClass"), _("MyNamespace"))
            << ResultData(_("MyClass"), _("<anonymous namespace>"))
           );

203 204 205 206 207 208 209 210
    QTest::newRow("CppClassesFilter-WithNamespacePrefix")
        << testFile
        << cppClassesFilter
        << _("mynamespace::")
        << (QList<ResultData>()
            << ResultData(_("MyClass"), _("MyNamespace"))
           );

211 212 213
    QTest::newRow("CppLocatorFilter")
        << testFile
        << cppLocatorFilter
214
        << _("my")
215 216 217 218
        << (QList<ResultData>()
            << ResultData(_("MyClass"), testFileShort)
            << ResultData(_("MyClass::MyClass"), _("()"))
            << ResultData(_("MyClass::function2"), _("(bool, int)"))
219
            << ResultData(_("MyEnum"), testFileShort)
220 221 222
            << ResultData(_("MyNamespace::MyClass"), testFileShort)
            << ResultData(_("MyNamespace::MyClass::MyClass"), _("()"))
            << ResultData(_("MyNamespace::MyClass::function2"), _("(bool, int)"))
223 224 225 226 227 228 229 230
            << ResultData(_("MyNamespace::MyEnum"), testFileShort)
            << ResultData(_("MyNamespace::myFunction"), _("(bool, int)"))
            << ResultData(_("myFunction"), _("(bool, int)"))
            << ResultData(_("<anonymous namespace>::MyClass"), testFileShort)
            << ResultData(_("<anonymous namespace>::MyClass::MyClass"), _("()"))
            << ResultData(_("<anonymous namespace>::MyClass::function2"), _("(bool, int)"))
            << ResultData(_("<anonymous namespace>::MyEnum"), testFileShort)
            << ResultData(_("<anonymous namespace>::myFunction"), _("(bool, int)"))
231 232 233 234 235
            );
}

void CppToolsPlugin::test_cpplocatorfilters_CppCurrentDocumentFilter()
{
236
    MyTestDataDir testDirectory(QLatin1String("testdata_basic"));
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    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"), _(""))
        << ResultData(_("MyClass()"), _("MyClass"))
        << ResultData(_("function1()"), _("MyClass"))
        << ResultData(_("function2(bool, int)"), _("MyClass"))
        << ResultData(_("int myVariable"), _("MyNamespace"))
        << ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
        << ResultData(_("MyEnum"), _("MyNamespace"))
        << ResultData(_("int V1"), _("MyNamespace::MyEnum"))
        << ResultData(_("int V2"), _("MyNamespace::MyEnum"))
        << ResultData(_("MyClass"), _("MyNamespace"))
        << ResultData(_("MyClass()"), _("MyNamespace::MyClass"))
        << ResultData(_("function1()"), _("MyNamespace::MyClass"))
        << ResultData(_("function2(bool, int)"), _("MyNamespace::MyClass"))
        << ResultData(_("int myVariable"), _("<anonymous namespace>"))
        << ResultData(_("myFunction(bool, int)"), _("<anonymous namespace>"))
        << ResultData(_("MyEnum"), _("<anonymous namespace>"))
        << ResultData(_("int V1"), _("<anonymous namespace>::MyEnum"))
        << ResultData(_("int V2"), _("<anonymous namespace>::MyEnum"))
        << ResultData(_("MyClass"), _("<anonymous namespace>"))
        << ResultData(_("MyClass()"), _("<anonymous namespace>::MyClass"))
        << ResultData(_("function1()"), _("<anonymous namespace>::MyClass"))
        << ResultData(_("function2(bool, int)"), _("<anonymous namespace>::MyClass"))
        ;

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