symbolquery-test.cpp 4.7 KB
Newer Older
Marco Bubke's avatar
Marco Bubke committed
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
/****************************************************************************
**
** 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>
32 33
#include <refactoringdatabaseinitializer.h>
#include <symbolquery.h>
Marco Bubke's avatar
Marco Bubke committed
34 35

#include <sqlitedatabase.h>
36 37
#include <sqlitereadstatement.h>
#include <sqlitewritestatement.h>
Marco Bubke's avatar
Marco Bubke committed
38 39 40 41

namespace {

using ClangRefactoring::QuerySqliteStatementFactory;
42
using Sqlite::Database;
Marco Bubke's avatar
Marco Bubke committed
43 44 45 46 47

using StatementFactory = QuerySqliteStatementFactory<MockSqliteDatabase,
                                                     MockSqliteReadStatement>;
using Query = ClangRefactoring::SymbolQuery<StatementFactory>;

48 49 50 51 52 53 54 55 56 57
struct Data
{
    Sqlite::Database database{":memory:", Sqlite::JournalMode::Memory};
    ClangBackEnd::RefactoringDatabaseInitializer<Sqlite::Database> initializer{database};
    using StatementFactory = QuerySqliteStatementFactory<Sqlite::Database,
                                                         Sqlite::ReadStatement>;
    using Query = ClangRefactoring::SymbolQuery<StatementFactory>;
    StatementFactory statementFactory{database};
    Query query{statementFactory};
};
Marco Bubke's avatar
Marco Bubke committed
58 59 60 61

class SymbolQuery : public testing::Test
{
protected:
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    static void SetUpTestCase()
    {
        data = std::make_unique<Data>();
        insertDataInDatabase();
    }

    static void TearDownTestCase()
    {
        data.reset();
    }

    static void insertDataInDatabase()
    {
        auto &database = data->database;
        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\")");
        database.execute("INSERT INTO locations VALUES (1, 2, 3, 1)");
        database.execute("INSERT INTO locations VALUES (1, 4, 6, 2)");
        database.execute("INSERT INTO symbols VALUES (1, \"functionusr\", \"function\")");
    }

protected:
    static std::unique_ptr<Data> data;
Marco Bubke's avatar
Marco Bubke committed
86 87 88
    NiceMock<MockSqliteDatabase> mockDatabase;
    StatementFactory statementFactory{mockDatabase};
    MockSqliteReadStatement &selectLocationsForSymbolLocation = statementFactory.selectLocationsForSymbolLocation;
89
    MockSqliteReadStatement &selectSourceUsagesForSymbolLocation = statementFactory.selectSourceUsagesForSymbolLocation;
Marco Bubke's avatar
Marco Bubke committed
90 91 92 93 94 95
    SourceLocations locations{{{1, 1}, 1, 1},
                              {{1, 1}, 2, 3},
                              {{1, 2}, 1, 1},
                              {{1, 2}, 3, 1},
                              {{1, 4}, 1, 1},
                              {{1, 4}, 1, 3}};
Marco Bubke's avatar
Marco Bubke committed
96
    Query query{statementFactory};
97
    Data::Query &realQuery = data->query;
Marco Bubke's avatar
Marco Bubke committed
98 99
};

100 101 102
std::unique_ptr<Data> SymbolQuery::data;

TEST_F(SymbolQuery, LocationsAtCallsValues)
Marco Bubke's avatar
Marco Bubke committed
103
{
104
    EXPECT_CALL(selectLocationsForSymbolLocation, valuesReturnSourceLocations(_, 42, 14, 7));
Marco Bubke's avatar
Marco Bubke committed
105

Marco Bubke's avatar
Marco Bubke committed
106
    query.locationsAt({1, 42}, 14, 7);
Marco Bubke's avatar
Marco Bubke committed
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
TEST_F(SymbolQuery, LocationsAt)
{
    auto locations = realQuery.locationsAt({1, 2}, 4, 6);

    ASSERT_THAT(locations,
                UnorderedElementsAre(SourceLocation({1, 1}, 2, 3),
                                     SourceLocation({1, 2}, 4, 6)));
}

TEST_F(SymbolQuery, SourceUsagesAtCallsValues)
{
    EXPECT_CALL(selectSourceUsagesForSymbolLocation, valuesReturnSourceUsages(_, 42, 14, 7));

    query.sourceUsagesAt({1, 42}, 14, 7);
}

TEST_F(SymbolQuery, SourceUsagesAt)
{
    auto usages = realQuery.sourceUsagesAt({1, 2}, 4, 6);

    ASSERT_THAT(usages,
                UnorderedElementsAre(CppTools::Usage("/path/to/filename.h", 2, 3),
                                     CppTools::Usage("/path/to/filename.cpp", 4, 6)));
}

Marco Bubke's avatar
Marco Bubke committed
134
}