Files
qt-creator/tests/unit/unittest/symbolstorage-test.cpp
Marco Bubke 3adb71d45e Clang: Add Symbol Indexing
It is a first step and now a database is generated if you start QtCreator.
Some code is now shared with the PchManager which can be improved in the
future.

Change-Id: Ic267fe7960f6c455d91832859a673ce98f269aa2
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
2017-09-14 13:39:55 +00:00

204 lines
7.6 KiB
C++

/****************************************************************************
**
** 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 "mocksqlitereadstatement.h"
#include "mocksqlitewritestatement.h"
#include <storagesqlitestatementfactory.h>
#include <symbolstorage.h>
#include <sqlitedatabase.h>
#include <storagesqlitestatementfactory.h>
namespace {
using Utils::PathString;
using ClangBackEnd::FilePathCache;
using ClangBackEnd::SymbolEntries;
using ClangBackEnd::SymbolEntry;
using ClangBackEnd::SourceLocationEntries;
using ClangBackEnd::SourceLocationEntry;
using ClangBackEnd::StorageSqliteStatementFactory;
using ClangBackEnd::SymbolType;
using Sqlite::SqliteDatabase;
using Sqlite::SqliteTable;
using StatementFactory = StorageSqliteStatementFactory<MockSqliteDatabase,
MockSqliteReadStatement,
MockSqliteWriteStatement>;
using Storage = ClangBackEnd::SymbolStorage<StatementFactory>;
class SymbolStorage : public testing::Test
{
protected:
void SetUp();
protected:
FilePathCache<std::mutex> filePathCache;
NiceMock<MockSqliteDatabase> mockDatabase;
StatementFactory statementFactory{mockDatabase};
MockSqliteWriteStatement &insertSymbolsToNewSymbolsStatement = statementFactory.insertSymbolsToNewSymbolsStatement;
MockSqliteWriteStatement &insertLocationsToNewLocationsStatement = statementFactory.insertLocationsToNewLocationsStatement;
MockSqliteWriteStatement &insertSourcesStatement = statementFactory.insertSourcesStatement;
MockSqliteReadStatement &selectNewSourceIdsStatement = statementFactory.selectNewSourceIdsStatement;
MockSqliteWriteStatement &addNewSymbolsToSymbolsStatement = statementFactory.addNewSymbolsToSymbolsStatement;
MockSqliteWriteStatement &syncNewSymbolsFromSymbolsStatement = statementFactory.syncNewSymbolsFromSymbolsStatement;
MockSqliteWriteStatement &syncSymbolsIntoNewLocationsStatement = statementFactory.syncSymbolsIntoNewLocationsStatement;
MockSqliteWriteStatement &deleteAllLocationsFromUpdatedFilesStatement = statementFactory.deleteAllLocationsFromUpdatedFilesStatement;
MockSqliteWriteStatement &insertNewLocationsInLocationsStatement = statementFactory.insertNewLocationsInLocationsStatement;
MockSqliteWriteStatement &deleteNewSymbolsTableStatement = statementFactory.deleteNewSymbolsTableStatement;
MockSqliteWriteStatement &deleteNewLocationsTableStatement = statementFactory.deleteNewLocationsTableStatement;
SymbolEntries symbolEntries{{1, {"functionUSR", "function"}},
{2, {"function2USR", "function2"}}};
SourceLocationEntries sourceLocations{{1, 3, {42, 23}, SymbolType::Declaration},
{2, 4, {7, 11}, SymbolType::Declaration}};
Storage storage{statementFactory, filePathCache};
};
TEST_F(SymbolStorage, CreateAndFillTemporaryLocationsTable)
{
InSequence sequence;
EXPECT_CALL(insertLocationsToNewLocationsStatement, write(1, 42, 23, 3));
EXPECT_CALL(insertLocationsToNewLocationsStatement, write(2, 7, 11, 4));
storage.fillTemporaryLocationsTable(sourceLocations);
}
TEST_F(SymbolStorage, AddNewSymbolsToSymbols)
{
EXPECT_CALL(addNewSymbolsToSymbolsStatement, execute());
storage.addNewSymbolsToSymbols();
}
TEST_F(SymbolStorage, SyncNewSymbolsFromSymbols)
{
EXPECT_CALL(syncNewSymbolsFromSymbolsStatement, execute());
storage.syncNewSymbolsFromSymbols();
}
TEST_F(SymbolStorage, SyncSymbolsIntoNewLocations)
{
EXPECT_CALL(syncSymbolsIntoNewLocationsStatement, execute());
storage.syncSymbolsIntoNewLocations();
}
TEST_F(SymbolStorage, DeleteAllLocationsFromUpdatedFiles)
{
EXPECT_CALL(deleteAllLocationsFromUpdatedFilesStatement, execute());
storage.deleteAllLocationsFromUpdatedFiles();
}
TEST_F(SymbolStorage, InsertNewLocationsInLocations)
{
EXPECT_CALL(insertNewLocationsInLocationsStatement, execute());
storage.insertNewLocationsInLocations();
}
TEST_F(SymbolStorage, SelectNewSourceIdsCalls)
{
EXPECT_CALL(selectNewSourceIdsStatement, valuesReturnStdVectorInt(_));
storage.selectNewSourceIds();
}
TEST_F(SymbolStorage, SelectNewSourceIds)
{
EXPECT_CALL(selectNewSourceIdsStatement, valuesReturnStdVectorInt(_));
auto sourceIds = storage.selectNewSourceIds();
ASSERT_THAT(sourceIds, ElementsAre(0, 1, 2));
}
TEST_F(SymbolStorage, InserNewSources)
{
InSequence sequence;
EXPECT_CALL(selectNewSourceIdsStatement, valuesReturnStdVectorInt(_));
EXPECT_CALL(insertSourcesStatement, write(0, Eq("/path/to/source1")));
EXPECT_CALL(insertSourcesStatement, write(1, Eq("/path/to/source2")));
EXPECT_CALL(insertSourcesStatement, write(2, Eq("/path/to/source3")));
storage.insertNewSources();
}
TEST_F(SymbolStorage, DropNewSymbolsTable)
{
EXPECT_CALL(deleteNewSymbolsTableStatement, execute());
storage.deleteNewSymbolsTable();
}
TEST_F(SymbolStorage, DropNewLocationsTable)
{
EXPECT_CALL(deleteNewLocationsTableStatement, execute());
storage.deleteNewLocationsTable();
}
TEST_F(SymbolStorage, AddSymbolsAndSourceLocationsCallsWrite)
{
InSequence sequence;
EXPECT_CALL(mockDatabase, execute(Eq("BEGIN IMMEDIATE")));
EXPECT_CALL(insertSymbolsToNewSymbolsStatement, write(_, _, _)).Times(2);
EXPECT_CALL(insertLocationsToNewLocationsStatement, write(1, 42, 23, 3));
EXPECT_CALL(insertLocationsToNewLocationsStatement, write(2, 7, 11, 4));
EXPECT_CALL(addNewSymbolsToSymbolsStatement, execute());
EXPECT_CALL(syncNewSymbolsFromSymbolsStatement, execute());
EXPECT_CALL(syncSymbolsIntoNewLocationsStatement, execute());
EXPECT_CALL(selectNewSourceIdsStatement, valuesReturnStdVectorInt(_));
EXPECT_CALL(insertSourcesStatement, write(0, Eq("/path/to/source1")));
EXPECT_CALL(insertSourcesStatement, write(1, Eq("/path/to/source2")));
EXPECT_CALL(insertSourcesStatement, write(2, Eq("/path/to/source3")));
EXPECT_CALL(deleteAllLocationsFromUpdatedFilesStatement, execute());
EXPECT_CALL(insertNewLocationsInLocationsStatement, execute());
EXPECT_CALL(deleteNewSymbolsTableStatement, execute());
EXPECT_CALL(deleteNewLocationsTableStatement, execute());
EXPECT_CALL(mockDatabase, execute(Eq("COMMIT")));
storage.addSymbolsAndSourceLocations(symbolEntries, sourceLocations);
}
void SymbolStorage::SetUp()
{
ON_CALL(selectNewSourceIdsStatement, valuesReturnStdVectorInt(_))
.WillByDefault(Return(std::vector<FilePathIndex>{0, 1, 2}));
filePathCache.stringIds({"/path/to/source1", "/path/to/source2", "/path/to/source3"});
}
}