2013-08-16 16:37:16 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2013-08-16 16:37:16 +02:00
|
|
|
** 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 "builtinindexingsupport.h"
|
|
|
|
|
#include "cppmodelmanager.h"
|
2013-12-16 16:02:45 +01:00
|
|
|
#include "cpptoolstestcase.h"
|
2013-08-16 16:37:16 +02:00
|
|
|
#include "searchsymbols.h"
|
|
|
|
|
|
2013-10-08 13:02:52 +03:00
|
|
|
#include <coreplugin/testdatadir.h>
|
2013-08-16 16:37:16 +02:00
|
|
|
#include <utils/runextensions.h>
|
|
|
|
|
|
|
|
|
|
#include <QtTest>
|
|
|
|
|
|
|
|
|
|
using namespace CppTools;
|
|
|
|
|
using namespace CppTools::Internal;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2013-12-16 16:02:45 +01:00
|
|
|
QTC_DECLARE_MYTESTDATADIR("../../../tests/cppsymbolsearcher/")
|
2013-08-16 16:37:16 +02:00
|
|
|
|
2013-12-19 01:30:49 +01:00
|
|
|
inline QString _(const QByteArray &ba) { return QString::fromLatin1(ba, ba.size()); }
|
|
|
|
|
|
2013-08-16 16:37:16 +02:00
|
|
|
class ResultData
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef QList<ResultData> ResultDataList;
|
|
|
|
|
|
|
|
|
|
ResultData() {}
|
|
|
|
|
ResultData(const QString &symbolName, const QString &scope)
|
|
|
|
|
: m_symbolName(symbolName), m_scope(scope) {}
|
|
|
|
|
|
|
|
|
|
bool operator==(const ResultData &other) const
|
|
|
|
|
{
|
|
|
|
|
return m_symbolName == other.m_symbolName && m_scope == other.m_scope;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-13 16:17:34 +01:00
|
|
|
static ResultDataList fromSearchResultList(const QList<Core::SearchResultItem> &entries)
|
2013-08-16 16:37:16 +02:00
|
|
|
{
|
|
|
|
|
ResultDataList result;
|
2014-01-13 16:17:34 +01:00
|
|
|
foreach (const Core::SearchResultItem &entry, entries)
|
2013-08-16 16:37:16 +02:00
|
|
|
result << ResultData(entry.text, entry.path.join(QLatin1String("::")));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// For debugging and creating reference data
|
|
|
|
|
static void printFilterEntries(const ResultDataList &entries)
|
|
|
|
|
{
|
|
|
|
|
QTextStream out(stdout);
|
|
|
|
|
foreach (const ResultData entry, entries) {
|
|
|
|
|
out << "<< ResultData(_(\"" << entry.m_symbolName << "\"), _(\""
|
|
|
|
|
<< entry.m_scope << "\"))" << endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-19 01:30:49 +01:00
|
|
|
public:
|
2013-08-16 16:37:16 +02:00
|
|
|
QString m_symbolName;
|
|
|
|
|
QString m_scope;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef ResultData::ResultDataList ResultDataList;
|
|
|
|
|
|
2013-12-19 01:30:49 +01:00
|
|
|
class SymbolSearcherTestCase : public CppTools::Tests::TestCase
|
2013-08-16 16:37:16 +02:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/// Takes no ownership of indexingSupportToUse
|
2013-12-30 19:44:42 +01:00
|
|
|
SymbolSearcherTestCase(const QString &testFile,
|
|
|
|
|
CppIndexingSupport *indexingSupportToUse,
|
|
|
|
|
const SymbolSearcher::Parameters &searchParameters,
|
|
|
|
|
const ResultDataList &expectedResults)
|
|
|
|
|
: m_indexingSupportToRestore(0)
|
|
|
|
|
, m_indexingSupportToUse(indexingSupportToUse)
|
2013-08-16 16:37:16 +02:00
|
|
|
{
|
2013-12-30 19:44:42 +01:00
|
|
|
QVERIFY(succeededSoFar());
|
|
|
|
|
|
2013-08-16 16:37:16 +02:00
|
|
|
QVERIFY(m_indexingSupportToUse);
|
2013-12-30 19:44:42 +01:00
|
|
|
QVERIFY(parseFiles(testFile));
|
2013-08-16 16:37:16 +02:00
|
|
|
m_indexingSupportToRestore = m_modelManager->indexingSupport();
|
|
|
|
|
m_modelManager->setIndexingSupport(m_indexingSupportToUse);
|
|
|
|
|
|
|
|
|
|
CppIndexingSupport *indexingSupport = m_modelManager->indexingSupport();
|
2013-12-19 01:30:49 +01:00
|
|
|
SymbolSearcher *symbolSearcher = indexingSupport->createSymbolSearcher(searchParameters,
|
2013-12-30 19:44:42 +01:00
|
|
|
QSet<QString>() << testFile);
|
2014-01-13 16:17:34 +01:00
|
|
|
QFuture<Core::SearchResultItem> search
|
2013-08-16 16:37:16 +02:00
|
|
|
= QtConcurrent::run(&SymbolSearcher::runSearch, symbolSearcher);
|
|
|
|
|
search.waitForFinished();
|
|
|
|
|
ResultDataList results = ResultData::fromSearchResultList(search.results());
|
2013-12-30 19:44:42 +01:00
|
|
|
QCOMPARE(results, expectedResults);
|
2013-08-16 16:37:16 +02:00
|
|
|
}
|
|
|
|
|
|
2013-12-19 01:30:49 +01:00
|
|
|
~SymbolSearcherTestCase()
|
2013-08-16 16:37:16 +02:00
|
|
|
{
|
2013-12-30 19:44:42 +01:00
|
|
|
if (m_indexingSupportToRestore)
|
|
|
|
|
m_modelManager->setIndexingSupport(m_indexingSupportToRestore);
|
2013-08-16 16:37:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
CppIndexingSupport *m_indexingSupportToRestore;
|
|
|
|
|
CppIndexingSupport *m_indexingSupportToUse;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(ResultData)
|
|
|
|
|
Q_DECLARE_METATYPE(ResultDataList)
|
|
|
|
|
|
2013-08-20 17:16:25 +02:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
namespace QTest {
|
|
|
|
|
|
|
|
|
|
template<> char *toString(const ResultData &data)
|
|
|
|
|
{
|
|
|
|
|
QByteArray ba = "\"" + data.m_symbolName.toUtf8() + "\", \"" + data.m_scope.toUtf8() + "\"";
|
|
|
|
|
return qstrdup(ba.data());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace QTest
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
2013-08-16 16:37:16 +02:00
|
|
|
void CppToolsPlugin::test_builtinsymbolsearcher()
|
|
|
|
|
{
|
|
|
|
|
QFETCH(QString, testFile);
|
|
|
|
|
QFETCH(SymbolSearcher::Parameters, searchParameters);
|
|
|
|
|
QFETCH(ResultDataList, expectedResults);
|
|
|
|
|
|
|
|
|
|
QScopedPointer<CppIndexingSupport> builtinIndexingSupport(new BuiltinIndexingSupport);
|
2013-12-30 19:44:42 +01:00
|
|
|
SymbolSearcherTestCase(testFile,
|
|
|
|
|
builtinIndexingSupport.data(),
|
|
|
|
|
searchParameters,
|
|
|
|
|
expectedResults);
|
2013-08-16 16:37:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CppToolsPlugin::test_builtinsymbolsearcher_data()
|
|
|
|
|
{
|
|
|
|
|
QTest::addColumn<QString>("testFile");
|
|
|
|
|
QTest::addColumn<SymbolSearcher::Parameters>("searchParameters");
|
|
|
|
|
QTest::addColumn<ResultDataList>("expectedResults");
|
|
|
|
|
|
2013-08-19 11:49:13 +02:00
|
|
|
MyTestDataDir testDirectory(QLatin1String("testdata_basic"));
|
2013-08-16 16:37:16 +02:00
|
|
|
const QString testFile = testDirectory.file(QLatin1String("file1.cpp"));
|
|
|
|
|
|
|
|
|
|
QScopedPointer<CppIndexingSupport> builtinIndexingSupport(new BuiltinIndexingSupport);
|
|
|
|
|
|
|
|
|
|
SymbolSearcher::Parameters searchParameters;
|
|
|
|
|
|
|
|
|
|
// Check All Symbol Types
|
|
|
|
|
searchParameters = SymbolSearcher::Parameters();
|
|
|
|
|
searchParameters.text = _("");
|
|
|
|
|
searchParameters.flags = 0;
|
|
|
|
|
searchParameters.types = SearchSymbols::AllTypes;
|
|
|
|
|
searchParameters.scope = SymbolSearcher::SearchGlobal;
|
|
|
|
|
QTest::newRow("BuiltinSymbolSearcher::AllTypes")
|
|
|
|
|
<< testFile
|
|
|
|
|
<< searchParameters
|
|
|
|
|
<< (ResultDataList()
|
|
|
|
|
<< ResultData(_("int myVariable"), _(""))
|
|
|
|
|
<< ResultData(_("myFunction(bool, int)"), _(""))
|
|
|
|
|
<< ResultData(_("MyEnum"), _(""))
|
2013-08-20 17:16:25 +02:00
|
|
|
<< ResultData(_("int V1"), _("MyEnum"))
|
|
|
|
|
<< ResultData(_("int V2"), _("MyEnum"))
|
2013-08-16 16:37:16 +02:00
|
|
|
<< ResultData(_("MyClass"), _(""))
|
|
|
|
|
<< ResultData(_("MyClass()"), _("MyClass"))
|
2013-09-12 15:26:55 +02:00
|
|
|
<< ResultData(_("functionDeclaredOnly()"), _("MyClass"))
|
|
|
|
|
<< ResultData(_("functionDefinedInClass(bool, int)"), _("MyClass"))
|
|
|
|
|
<< ResultData(_("functionDefinedOutSideClass(char)"), _("MyClass"))
|
|
|
|
|
<< ResultData(_("functionDefinedOutSideClass(char)"), _("MyClass"))
|
2013-08-16 16:37:16 +02:00
|
|
|
<< ResultData(_("int myVariable"), _("MyNamespace"))
|
|
|
|
|
<< ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
|
|
|
|
|
<< ResultData(_("MyEnum"), _("MyNamespace"))
|
2013-08-20 17:16:25 +02:00
|
|
|
<< ResultData(_("int V1"), _("MyNamespace::MyEnum"))
|
|
|
|
|
<< ResultData(_("int V2"), _("MyNamespace::MyEnum"))
|
2013-08-16 16:37:16 +02:00
|
|
|
<< ResultData(_("MyClass"), _("MyNamespace"))
|
|
|
|
|
<< ResultData(_("MyClass()"), _("MyNamespace::MyClass"))
|
2013-09-12 15:26:55 +02:00
|
|
|
<< ResultData(_("functionDeclaredOnly()"), _("MyNamespace::MyClass"))
|
|
|
|
|
<< ResultData(_("functionDefinedInClass(bool, int)"), _("MyNamespace::MyClass"))
|
|
|
|
|
<< ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
|
2013-12-19 01:30:49 +01:00
|
|
|
<< ResultData(_("functionDefinedOutSideClassAndNamespace(float)"),
|
|
|
|
|
_("MyNamespace::MyClass"))
|
2013-09-12 15:26:55 +02:00
|
|
|
<< ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
|
2013-12-19 01:30:49 +01:00
|
|
|
<< ResultData(_("functionDefinedOutSideClassAndNamespace(float)"),
|
|
|
|
|
_("MyNamespace::MyClass"))
|
2013-08-20 17:16:25 +02:00
|
|
|
<< 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"))
|
2013-09-12 15:26:55 +02:00
|
|
|
<< ResultData(_("functionDeclaredOnly()"), _("<anonymous namespace>::MyClass"))
|
2013-12-19 01:30:49 +01:00
|
|
|
<< ResultData(_("functionDefinedInClass(bool, int)"),
|
|
|
|
|
_("<anonymous namespace>::MyClass"))
|
|
|
|
|
<< ResultData(_("functionDefinedOutSideClass(char)"),
|
|
|
|
|
_("<anonymous namespace>::MyClass"))
|
|
|
|
|
<< ResultData(_("functionDefinedOutSideClass(char)"),
|
|
|
|
|
_("<anonymous namespace>::MyClass"))
|
2013-09-12 15:26:55 +02:00
|
|
|
<< ResultData(_("main()"), _(""))
|
|
|
|
|
|
2013-08-16 16:37:16 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Check Classes
|
|
|
|
|
searchParameters = SymbolSearcher::Parameters();
|
|
|
|
|
searchParameters.text = _("myclass");
|
|
|
|
|
searchParameters.flags = 0;
|
|
|
|
|
searchParameters.types = SymbolSearcher::Classes;
|
|
|
|
|
searchParameters.scope = SymbolSearcher::SearchGlobal;
|
|
|
|
|
QTest::newRow("BuiltinSymbolSearcher::Classes")
|
|
|
|
|
<< testFile
|
|
|
|
|
<< searchParameters
|
|
|
|
|
<< (ResultDataList()
|
|
|
|
|
<< ResultData(_("MyClass"), _(""))
|
|
|
|
|
<< ResultData(_("MyClass"), _("MyNamespace"))
|
2013-08-20 17:16:25 +02:00
|
|
|
<< ResultData(_("MyClass"), _("<anonymous namespace>"))
|
2013-08-16 16:37:16 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Check Functions
|
|
|
|
|
searchParameters = SymbolSearcher::Parameters();
|
|
|
|
|
searchParameters.text = _("fun");
|
|
|
|
|
searchParameters.flags = 0;
|
|
|
|
|
searchParameters.types = SymbolSearcher::Functions;
|
|
|
|
|
searchParameters.scope = SymbolSearcher::SearchGlobal;
|
|
|
|
|
QTest::newRow("BuiltinSymbolSearcher::Functions")
|
|
|
|
|
<< testFile
|
|
|
|
|
<< searchParameters
|
|
|
|
|
<< (ResultDataList()
|
|
|
|
|
<< ResultData(_("myFunction(bool, int)"), _(""))
|
2013-09-12 15:26:55 +02:00
|
|
|
<< ResultData(_("functionDefinedInClass(bool, int)"), _("MyClass"))
|
|
|
|
|
<< ResultData(_("functionDefinedOutSideClass(char)"), _("MyClass"))
|
2013-08-16 16:37:16 +02:00
|
|
|
<< ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
|
2013-09-12 15:26:55 +02:00
|
|
|
<< ResultData(_("functionDefinedInClass(bool, int)"), _("MyNamespace::MyClass"))
|
|
|
|
|
<< ResultData(_("functionDefinedOutSideClass(char)"), _("MyNamespace::MyClass"))
|
2013-12-19 01:30:49 +01:00
|
|
|
<< ResultData(_("functionDefinedOutSideClassAndNamespace(float)"),
|
|
|
|
|
_("MyNamespace::MyClass"))
|
2013-08-20 17:16:25 +02:00
|
|
|
<< ResultData(_("myFunction(bool, int)"), _("<anonymous namespace>"))
|
2013-12-19 01:30:49 +01:00
|
|
|
<< ResultData(_("functionDefinedInClass(bool, int)"),
|
|
|
|
|
_("<anonymous namespace>::MyClass"))
|
|
|
|
|
<< ResultData(_("functionDefinedOutSideClass(char)"),
|
|
|
|
|
_("<anonymous namespace>::MyClass"))
|
2013-08-16 16:37:16 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Check Enums
|
|
|
|
|
searchParameters = SymbolSearcher::Parameters();
|
|
|
|
|
searchParameters.text = _("enum");
|
|
|
|
|
searchParameters.flags = 0;
|
|
|
|
|
searchParameters.types = SymbolSearcher::Enums;
|
|
|
|
|
searchParameters.scope = SymbolSearcher::SearchGlobal;
|
|
|
|
|
QTest::newRow("BuiltinSymbolSearcher::Enums")
|
|
|
|
|
<< testFile
|
|
|
|
|
<< searchParameters
|
|
|
|
|
<< (ResultDataList()
|
|
|
|
|
<< ResultData(_("MyEnum"), _(""))
|
|
|
|
|
<< ResultData(_("MyEnum"), _("MyNamespace"))
|
2013-08-20 17:16:25 +02:00
|
|
|
<< ResultData(_("MyEnum"), _("<anonymous namespace>"))
|
2013-08-16 16:37:16 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Check Declarations
|
|
|
|
|
searchParameters = SymbolSearcher::Parameters();
|
|
|
|
|
searchParameters.text = _("myvar");
|
|
|
|
|
searchParameters.flags = 0;
|
|
|
|
|
searchParameters.types = SymbolSearcher::Declarations;
|
|
|
|
|
searchParameters.scope = SymbolSearcher::SearchGlobal;
|
|
|
|
|
QTest::newRow("BuiltinSymbolSearcher::Declarations")
|
|
|
|
|
<< testFile
|
|
|
|
|
<< searchParameters
|
|
|
|
|
<< (ResultDataList()
|
|
|
|
|
<< ResultData(_("int myVariable"), _(""))
|
|
|
|
|
<< ResultData(_("int myVariable"), _("MyNamespace"))
|
2013-08-20 17:16:25 +02:00
|
|
|
<< ResultData(_("int myVariable"), _("<anonymous namespace>"))
|
2013-08-16 16:37:16 +02:00
|
|
|
);
|
|
|
|
|
}
|