2017-08-15 17:33:31 +02:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2017 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
**
|
|
|
|
** 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 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.
|
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "googletest.h"
|
|
|
|
|
|
|
|
#include "mocksqlitedatabase.h"
|
|
|
|
#include "mocksqlitereadstatement.h"
|
|
|
|
|
|
|
|
#include <querysqlitestatementfactory.h>
|
2017-10-18 13:38:58 +02:00
|
|
|
#include <refactoringdatabaseinitializer.h>
|
|
|
|
#include <symbolquery.h>
|
2017-08-15 17:33:31 +02:00
|
|
|
|
|
|
|
#include <sqlitedatabase.h>
|
2017-10-18 13:38:58 +02:00
|
|
|
#include <sqlitereadstatement.h>
|
|
|
|
#include <sqlitewritestatement.h>
|
2017-08-15 17:33:31 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using ClangRefactoring::QuerySqliteStatementFactory;
|
2017-09-18 10:21:45 +02:00
|
|
|
using Sqlite::Database;
|
2018-04-09 13:30:30 +02:00
|
|
|
using ClangBackEnd::SourceLocationKind;
|
|
|
|
using ClangBackEnd::SymbolKind;
|
2017-11-29 18:18:42 +01:00
|
|
|
using MockStatementFactory = QuerySqliteStatementFactory<MockSqliteDatabase,
|
|
|
|
MockSqliteReadStatement>;
|
|
|
|
using MockQuery = ClangRefactoring::SymbolQuery<MockStatementFactory>;
|
2017-08-15 17:33:31 +02:00
|
|
|
|
2017-11-29 18:18:42 +01:00
|
|
|
using RealStatementFactory = QuerySqliteStatementFactory<Sqlite::Database,
|
2017-10-18 13:38:58 +02:00
|
|
|
Sqlite::ReadStatement>;
|
2017-11-29 18:18:42 +01:00
|
|
|
using RealQuery = ClangRefactoring::SymbolQuery<RealStatementFactory>;
|
2017-08-15 17:33:31 +02:00
|
|
|
|
|
|
|
class SymbolQuery : public testing::Test
|
|
|
|
{
|
2018-04-09 13:30:30 +02:00
|
|
|
protected:
|
|
|
|
NiceMock<MockSqliteDatabase> mockDatabase;
|
|
|
|
MockStatementFactory mockStatementFactory{mockDatabase};
|
|
|
|
MockSqliteReadStatement &selectLocationsForSymbolLocation = mockStatementFactory.selectLocationsForSymbolLocation;
|
|
|
|
MockSqliteReadStatement &selectSourceUsagesForSymbolLocation = mockStatementFactory.selectSourceUsagesForSymbolLocation;
|
|
|
|
MockSqliteReadStatement &selectSymbolsForKindAndStartsWith = mockStatementFactory.selectSymbolsForKindAndStartsWith;
|
|
|
|
MockSqliteReadStatement &selectSymbolsForKindAndStartsWith2 = mockStatementFactory.selectSymbolsForKindAndStartsWith2;
|
|
|
|
MockSqliteReadStatement &selectSymbolsForKindAndStartsWith3 = mockStatementFactory.selectSymbolsForKindAndStartsWith3;
|
|
|
|
MockSqliteReadStatement &selectLocationOfSymbol = mockStatementFactory.selectLocationOfSymbol;
|
2018-11-06 19:02:00 +01:00
|
|
|
SourceLocations locations{{1, 1, 1},
|
|
|
|
{1, 2, 3},
|
|
|
|
{2, 1, 1},
|
|
|
|
{2, 3, 1},
|
|
|
|
{4, 1, 1},
|
|
|
|
{4, 1, 3}};
|
2018-04-09 13:30:30 +02:00
|
|
|
MockQuery query{mockStatementFactory};
|
|
|
|
};
|
|
|
|
|
|
|
|
class SymbolQuerySlowTest : public testing::Test
|
|
|
|
{
|
2017-08-15 17:33:31 +02:00
|
|
|
protected:
|
2017-11-29 18:18:42 +01:00
|
|
|
void SetUp() override
|
2017-10-18 13:38:58 +02:00
|
|
|
{
|
|
|
|
database.execute("INSERT INTO sources VALUES (1, 1, \"filename.h\", 1)");
|
|
|
|
database.execute("INSERT INTO sources VALUES (2, 1, \"filename.cpp\", 1)");
|
|
|
|
database.execute("INSERT INTO directories VALUES (1, \"/path/to\")");
|
2018-04-09 13:30:30 +02:00
|
|
|
database.execute("INSERT INTO locations VALUES (1, 2, 3, 1, 1)");
|
|
|
|
database.execute("INSERT INTO locations VALUES (1, 4, 6, 2, 3)");
|
|
|
|
database.execute("INSERT INTO symbols VALUES (1, \"functionusr\", \"Function\", 3, \"void function(int)\")");
|
|
|
|
database.execute("INSERT INTO symbols VALUES (2, \"classusr\", \"Class\", 2, \"class Class final\")");
|
|
|
|
database.execute("INSERT INTO symbols VALUES (3, \"enumusr\", \"Enum\", 1, \"enum Enum : char\")");
|
2017-10-18 13:38:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2017-11-29 18:18:42 +01:00
|
|
|
Sqlite::Database database{":memory:", Sqlite::JournalMode::Memory};
|
|
|
|
ClangBackEnd::RefactoringDatabaseInitializer<Sqlite::Database> initializer{database};
|
|
|
|
RealStatementFactory realStatementFactory{database};
|
2018-04-09 13:30:30 +02:00
|
|
|
RealQuery query{realStatementFactory};
|
2017-08-15 17:33:31 +02:00
|
|
|
};
|
|
|
|
|
2017-10-18 13:38:58 +02:00
|
|
|
TEST_F(SymbolQuery, LocationsAtCallsValues)
|
2017-08-15 17:33:31 +02:00
|
|
|
{
|
2017-10-18 13:38:58 +02:00
|
|
|
EXPECT_CALL(selectLocationsForSymbolLocation, valuesReturnSourceLocations(_, 42, 14, 7));
|
2017-08-15 17:33:31 +02:00
|
|
|
|
2018-11-06 19:02:00 +01:00
|
|
|
query.locationsAt(42, 14, 7);
|
2017-08-15 17:33:31 +02:00
|
|
|
}
|
|
|
|
|
2018-04-09 13:30:30 +02:00
|
|
|
TEST_F(SymbolQuerySlowTest, LocationsAt)
|
2017-10-18 13:38:58 +02:00
|
|
|
{
|
2018-11-06 19:02:00 +01:00
|
|
|
auto locations = query.locationsAt(2, 4, 6);
|
2017-10-18 13:38:58 +02:00
|
|
|
|
|
|
|
ASSERT_THAT(locations,
|
2018-11-06 19:02:00 +01:00
|
|
|
UnorderedElementsAre(SourceLocation(1, 2, 3),
|
|
|
|
SourceLocation(2, 4, 6)));
|
2017-10-18 13:38:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SymbolQuery, SourceUsagesAtCallsValues)
|
|
|
|
{
|
|
|
|
EXPECT_CALL(selectSourceUsagesForSymbolLocation, valuesReturnSourceUsages(_, 42, 14, 7));
|
|
|
|
|
2018-11-06 19:02:00 +01:00
|
|
|
query.sourceUsagesAt(42, 14, 7);
|
2017-10-18 13:38:58 +02:00
|
|
|
}
|
|
|
|
|
2018-04-09 13:30:30 +02:00
|
|
|
TEST_F(SymbolQuerySlowTest, SourceUsagesAt)
|
2017-10-18 13:38:58 +02:00
|
|
|
{
|
2018-11-06 19:02:00 +01:00
|
|
|
auto usages = query.sourceUsagesAt(2, 4, 6);
|
2017-10-18 13:38:58 +02:00
|
|
|
|
|
|
|
ASSERT_THAT(usages,
|
|
|
|
UnorderedElementsAre(CppTools::Usage("/path/to/filename.h", 2, 3),
|
|
|
|
CppTools::Usage("/path/to/filename.cpp", 4, 6)));
|
|
|
|
}
|
|
|
|
|
2018-04-09 13:30:30 +02:00
|
|
|
TEST_F(SymbolQuery, SymbolsCallsValuesWithOneKindParameter)
|
|
|
|
{
|
|
|
|
EXPECT_CALL(selectSymbolsForKindAndStartsWith, valuesReturnSymbols(100, int(SymbolKind::Record), Eq("foo")));
|
|
|
|
|
|
|
|
query.symbols({SymbolKind::Record}, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SymbolQuerySlowTest, SymbolsWithOneKindParameters)
|
|
|
|
{
|
|
|
|
auto symbols = query.symbols({SymbolKind::Record}, "Cla%");
|
|
|
|
|
|
|
|
ASSERT_THAT(symbols,
|
|
|
|
UnorderedElementsAre(Symbol{2, "Class", "class Class final"}));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SymbolQuerySlowTest, SymbolsWithEmptyKinds)
|
|
|
|
{
|
|
|
|
auto symbols = query.symbols({}, "%");
|
|
|
|
|
|
|
|
ASSERT_THAT(symbols, IsEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SymbolQuery, SymbolsCallsValuesWithTwoKindParameters)
|
|
|
|
{
|
|
|
|
EXPECT_CALL(selectSymbolsForKindAndStartsWith2, valuesReturnSymbols(100, int(SymbolKind::Record), int(SymbolKind::Function), Eq("foo")));
|
|
|
|
|
|
|
|
query.symbols({SymbolKind::Record, SymbolKind::Function}, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SymbolQuerySlowTest, SymbolsWithTwoKindParameters)
|
|
|
|
{
|
|
|
|
auto symbols = query.symbols({SymbolKind::Record, SymbolKind::Function}, "%c%");
|
|
|
|
|
|
|
|
ASSERT_THAT(symbols,
|
|
|
|
UnorderedElementsAre(Symbol{2, "Class", "class Class final"},
|
|
|
|
Symbol{1, "Function", "void function(int)"}));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SymbolQuery, SymbolsCallsValuesWithThreeKindParameters)
|
|
|
|
{
|
|
|
|
EXPECT_CALL(selectSymbolsForKindAndStartsWith3, valuesReturnSymbols(100, int(SymbolKind::Record), int(SymbolKind::Function), int(SymbolKind::Enumeration), Eq("foo")));
|
|
|
|
|
|
|
|
query.symbols({SymbolKind::Record, SymbolKind::Function, SymbolKind::Enumeration}, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SymbolQuerySlowTest, SymbolsWithThreeKindParameters)
|
|
|
|
{
|
|
|
|
auto symbols = query.symbols({SymbolKind::Record, SymbolKind::Function, SymbolKind::Enumeration}, "%");
|
|
|
|
|
|
|
|
ASSERT_THAT(symbols,
|
|
|
|
UnorderedElementsAre(Symbol{2, "Class", "class Class final"},
|
|
|
|
Symbol{1, "Function", "void function(int)"},
|
|
|
|
Symbol{3, "Enum", "enum Enum : char"}));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SymbolQuery, LocationForSymbolIdCallsValueReturningSourceLocation)
|
|
|
|
{
|
|
|
|
EXPECT_CALL(selectLocationOfSymbol, valueReturnSourceLocation(1, int(SourceLocationKind::Definition)));
|
|
|
|
|
|
|
|
query.locationForSymbolId(1, SourceLocationKind::Definition);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SymbolQuerySlowTest, LocationForSymbolId)
|
|
|
|
{
|
|
|
|
auto location = query.locationForSymbolId(1, SourceLocationKind::Definition);
|
|
|
|
|
2018-11-06 19:02:00 +01:00
|
|
|
ASSERT_THAT(location.value(), Eq(SourceLocation(2, {4, 6})));
|
2018-04-09 13:30:30 +02:00
|
|
|
}
|
|
|
|
|
2017-08-15 17:33:31 +02:00
|
|
|
}
|