Files
qt-creator/tests/unit/unittest/symbolfindertest.cpp

204 lines
5.9 KiB
C++
Raw Normal View History

/****************************************************************************
**
** Copyright (C) 2016 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 <clangrefactoringbackend_global.h>
#include <symbolfinder.h>
#include "filesystemutilities.h"
#include <gmock/gmock.h>
#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>
#include "gtest-qt-printing.h"
using ClangBackEnd::SymbolFinder;
using ClangBackEnd::FileContent;
using testing::PrintToString;
using testing::AllOf;
using testing::Contains;
using testing::Not;
namespace {
MATCHER_P2(IsSourceLocation, line, column,
std::string(negation ? "isn't" : "is")
+ ", line " + PrintToString(line)
+ ", column " + PrintToString(column)
)
{
if (arg.line() != uint(line)
|| arg.column() != uint(column))
return false;
return true;
}
MATCHER_P(StrEq, text,
std::string(negation ? "isn't" : "is")
+ " text " + PrintToString(text)
)
{
return std::string(arg.data(), arg.size()) == std::string(text);
}
TEST(SymbolFinder, FileContentFilePath)
{
FileContent fileContent(toNativePath("/tmp"), "data.cpp", "int variable;", {"cc", "data.cpp"});
ASSERT_THAT(fileContent.filePath, toNativePath("/tmp/data.cpp"));
}
TEST(SymbolFinder, FindName)
{
SymbolFinder finder(1, 5);
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "int variable;", {"cc", "renamevariable.cpp"});
finder.findSymbol();
ASSERT_THAT(finder.takeSymbolName(), "variable");
}
TEST(SymbolFinder, FindNameInUnsavedFile)
{
SymbolFinder finder(1, 5);
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "int newVariable;", {"cc", "renamevariable.cpp"});
finder.findSymbol();
ASSERT_THAT(finder.takeSymbolName(), "newVariable");
}
TEST(SymbolFinder, FindUsrs)
{
SymbolFinder finder(1, 5);
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "int variable;", {"cc", "renamevariable.cpp"});
finder.findSymbol();
ASSERT_THAT(finder.unifiedSymbolResolutions().front(), StrEq("c:@variable"));
}
TEST(SymbolFinder, SourceLocations)
{
SymbolFinder finder(1, 5);
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "", {"cc", "renamevariable.cpp"});
finder.findSymbol();
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
AllOf(Contains(IsSourceLocation(1, 5)),
Contains(IsSourceLocation(3, 9))));
}
TEST(SymbolFinder, CursorOverMacroDefintionSymbolName)
{
SymbolFinder finder(1, 9);
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
finder.findSymbol();
ASSERT_THAT(finder.takeSymbolName(), "Macro");
}
TEST(SymbolFinder, CursorOverMacroExpansionSymbolName)
{
SymbolFinder finder(10, 10);
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
finder.findSymbol();
ASSERT_THAT(finder.takeSymbolName(), "Macro");
}
TEST(SymbolFinder, FindMacroDefinition)
{
SymbolFinder finder(1, 9);
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
finder.findSymbol();
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
Contains(IsSourceLocation(1, 9)));
}
TEST(SymbolFinder, FindMacroExpansion)
{
SymbolFinder finder(1, 9);
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
finder.findSymbol();
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
Contains(IsSourceLocation(5, 17)));
}
TEST(SymbolFinder, DoNotFindUndedefinedMacroExpansion)
{
SymbolFinder finder(1, 9);
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
finder.findSymbol();
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
Not(Contains(IsSourceLocation(10, 10))));
}
TEST(SymbolFinder, FindMacroDefinitionFromMacroExpansion)
{
SymbolFinder finder(10, 10);
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
finder.findSymbol();
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
Contains(IsSourceLocation(8, 9)));
}
TEST(SymbolFinder, FindMacroExpansionBeforeMacroExpansionWithCursor)
{
SymbolFinder finder(12, 10);
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
finder.findSymbol();
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
Contains(IsSourceLocation(10, 10)));
}
TEST(SymbolFinder, FindMacroExpansionAfterMacroExpansionWithCursor)
{
SymbolFinder finder(10, 10);
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
finder.findSymbol();
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
Contains(IsSourceLocation(12, 10)));
}
}