2016-08-04 15:26:53 +02:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-09-15 17:41:41 +02:00
|
|
|
#include "googletest.h"
|
2016-11-15 15:38:12 +01:00
|
|
|
#include "mocksearchhandle.h"
|
2017-09-21 11:43:59 +02:00
|
|
|
#include "mockfilepathcaching.h"
|
2017-09-19 15:38:20 +02:00
|
|
|
#include "mocksymbolquery.h"
|
2016-08-04 15:26:53 +02:00
|
|
|
|
2017-02-20 15:31:18 +01:00
|
|
|
#include <clangqueryprojectsfindfilter.h>
|
2016-08-04 15:26:53 +02:00
|
|
|
#include <refactoringclient.h>
|
|
|
|
#include <refactoringengine.h>
|
|
|
|
#include <refactoringconnectionclient.h>
|
|
|
|
|
2017-07-03 12:35:58 +02:00
|
|
|
#include <clangrefactoringclientmessages.h>
|
2016-08-04 15:26:53 +02:00
|
|
|
|
2017-09-27 10:32:52 +02:00
|
|
|
#include <cpptools/compileroptionsbuilder.h>
|
2016-08-04 15:26:53 +02:00
|
|
|
#include <cpptools/projectpart.h>
|
|
|
|
|
|
|
|
#include <utils/smallstringvector.h>
|
|
|
|
|
2016-08-17 15:29:37 +02:00
|
|
|
#include <QBuffer>
|
2016-08-04 15:26:53 +02:00
|
|
|
#include <QTextCursor>
|
|
|
|
#include <QTextDocument>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-09-27 10:32:52 +02:00
|
|
|
using CppTools::CompilerOptionsBuilder;
|
2016-12-14 14:56:17 +01:00
|
|
|
|
2016-08-04 15:26:53 +02:00
|
|
|
using ClangRefactoring::RefactoringEngine;
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
using ClangBackEnd::FilePath;
|
|
|
|
using ClangBackEnd::FilePathId;
|
2016-11-15 15:38:12 +01:00
|
|
|
using ClangBackEnd::SourceLocationsForRenamingMessage;
|
|
|
|
using ClangBackEnd::SourceRangesAndDiagnosticsForQueryMessage;
|
2017-07-03 12:35:58 +02:00
|
|
|
using ClangBackEnd::SourceRangesForQueryMessage;
|
2016-11-15 15:38:12 +01:00
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
using Utils::PathString;
|
2016-08-04 15:26:53 +02:00
|
|
|
using Utils::SmallString;
|
|
|
|
using Utils::SmallStringVector;
|
|
|
|
|
|
|
|
class RefactoringClient : public ::testing::Test
|
|
|
|
{
|
|
|
|
void SetUp();
|
|
|
|
|
|
|
|
protected:
|
2017-09-21 11:43:59 +02:00
|
|
|
NiceMock<MockFilePathCaching> mockFilePathCaching;
|
2016-11-15 15:38:12 +01:00
|
|
|
NiceMock<MockSearchHandle> mockSearchHandle;
|
2017-09-19 15:38:20 +02:00
|
|
|
NiceMock<MockSymbolQuery> mockSymbolQuery;
|
2016-08-17 15:29:37 +02:00
|
|
|
QBuffer ioDevice;
|
2017-12-28 20:21:04 +01:00
|
|
|
MockFunction<void(const QString &,
|
|
|
|
const ClangBackEnd::SourceLocationsContainer &,
|
|
|
|
int)> mockLocalRenaming;
|
2016-08-04 15:26:53 +02:00
|
|
|
ClangRefactoring::RefactoringClient client;
|
2016-08-17 15:29:37 +02:00
|
|
|
ClangBackEnd::RefactoringServerProxy serverProxy{&client, &ioDevice};
|
|
|
|
RefactoringEngine engine{serverProxy, client, mockFilePathCaching, mockSymbolQuery};
|
2016-08-04 15:26:53 +02:00
|
|
|
QString fileContent{QStringLiteral("int x;\nint y;")};
|
|
|
|
QTextDocument textDocument{fileContent};
|
|
|
|
QTextCursor cursor{&textDocument};
|
|
|
|
QString qStringFilePath{QStringLiteral("/home/user/file.cpp")};
|
|
|
|
Utils::FileName filePath{Utils::FileName::fromString(qStringFilePath)};
|
2016-11-15 15:38:12 +01:00
|
|
|
ClangBackEnd::FilePath clangBackEndFilePath{qStringFilePath};
|
2016-08-04 15:26:53 +02:00
|
|
|
SmallStringVector commandLine;
|
|
|
|
CppTools::ProjectPart::Ptr projectPart;
|
|
|
|
CppTools::ProjectFile projectFile{qStringFilePath, CppTools::ProjectFile::CXXSource};
|
2016-11-15 15:38:12 +01:00
|
|
|
SourceLocationsForRenamingMessage renameMessage{"symbol",
|
2017-09-21 11:43:59 +02:00
|
|
|
{{{{1, 42}, 1, 1, 0}, {{1, 42}, 2, 5, 10}}},
|
2016-11-15 15:38:12 +01:00
|
|
|
1};
|
2017-09-21 11:43:59 +02:00
|
|
|
SourceRangesForQueryMessage queryResultMessage{{{{{1, 42}, 1, 1, 0, 1, 5, 4, ""},
|
|
|
|
{{1, 42}, 2, 1, 5, 2, 5, 10, ""}}}};
|
|
|
|
SourceRangesForQueryMessage emptyQueryResultMessage;
|
2016-08-04 15:26:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(RefactoringClient, SourceLocationsForRenaming)
|
|
|
|
{
|
2017-12-28 20:21:04 +01:00
|
|
|
client.setLocalRenamingCallback(mockLocalRenaming.AsStdFunction());
|
|
|
|
|
|
|
|
EXPECT_CALL(mockLocalRenaming, Call(renameMessage.symbolName().toQString(),
|
|
|
|
renameMessage.sourceLocations(),
|
|
|
|
renameMessage.textDocumentRevision()));
|
2016-08-04 15:26:53 +02:00
|
|
|
|
2016-11-15 15:38:12 +01:00
|
|
|
client.sourceLocationsForRenamingMessage(std::move(renameMessage));
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(RefactoringClient, AfterSourceLocationsForRenamingEngineIsUsableAgain)
|
|
|
|
{
|
2017-12-28 20:21:04 +01:00
|
|
|
client.setLocalRenamingCallback(mockLocalRenaming.AsStdFunction());
|
|
|
|
EXPECT_CALL(mockLocalRenaming, Call(_,_,_));
|
2016-08-04 15:26:53 +02:00
|
|
|
|
2016-11-15 15:38:12 +01:00
|
|
|
client.sourceLocationsForRenamingMessage(std::move(renameMessage));
|
2016-08-04 15:26:53 +02:00
|
|
|
|
2017-09-25 16:41:17 +02:00
|
|
|
ASSERT_TRUE(engine.isRefactoringEngineAvailable());
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(RefactoringClient, AfterStartLocalRenameHasValidCallback)
|
|
|
|
{
|
2017-08-03 14:13:42 +02:00
|
|
|
engine.startLocalRenaming(CppTools::CursorInEditor{cursor, filePath},
|
2016-11-15 15:38:12 +01:00
|
|
|
projectPart.data(),
|
|
|
|
[&] (const QString &,
|
|
|
|
const ClangBackEnd::SourceLocationsContainer &,
|
|
|
|
int) {});
|
2016-08-04 15:26:53 +02:00
|
|
|
|
|
|
|
ASSERT_TRUE(client.hasValidLocalRenamingCallback());
|
|
|
|
}
|
|
|
|
|
2016-11-15 15:38:12 +01:00
|
|
|
TEST_F(RefactoringClient, CallAddResultsForEmptyQueryMessage)
|
|
|
|
{
|
2016-11-23 13:13:38 +01:00
|
|
|
EXPECT_CALL(mockSearchHandle, addResult(_ ,_ ,_))
|
2016-11-15 15:38:12 +01:00
|
|
|
.Times(0);
|
|
|
|
|
2017-07-03 12:35:58 +02:00
|
|
|
client.sourceRangesForQueryMessage(std::move(emptyQueryResultMessage));
|
2016-11-15 15:38:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(RefactoringClient, CallAddResultsForQueryMessage)
|
|
|
|
{
|
2016-11-23 13:13:38 +01:00
|
|
|
EXPECT_CALL(mockSearchHandle, addResult(_ ,_ ,_))
|
2016-11-15 15:38:12 +01:00
|
|
|
.Times(2);
|
|
|
|
|
2017-07-03 12:35:58 +02:00
|
|
|
client.sourceRangesForQueryMessage(std::move(queryResultMessage));
|
2016-11-15 15:38:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(RefactoringClient, CallFinishSearchForEmptyQueryMessage)
|
|
|
|
{
|
|
|
|
EXPECT_CALL(mockSearchHandle, finishSearch())
|
|
|
|
.Times(1);
|
|
|
|
|
2017-07-03 12:35:58 +02:00
|
|
|
client.sourceRangesForQueryMessage(std::move(emptyQueryResultMessage));
|
2016-11-15 15:38:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(RefactoringClient, CallFinishSearchQueryMessage)
|
|
|
|
{
|
|
|
|
EXPECT_CALL(mockSearchHandle, finishSearch())
|
|
|
|
.Times(1);
|
|
|
|
|
2017-07-03 12:35:58 +02:00
|
|
|
client.sourceRangesForQueryMessage(std::move(queryResultMessage));
|
2016-11-15 15:38:12 +01:00
|
|
|
}
|
|
|
|
|
2016-11-23 13:13:38 +01:00
|
|
|
TEST_F(RefactoringClient, CallFinishSearchForTwoQueryMessages)
|
|
|
|
{
|
|
|
|
client.setExpectedResultCount(2);
|
|
|
|
|
|
|
|
EXPECT_CALL(mockSearchHandle, finishSearch())
|
|
|
|
.Times(1);
|
|
|
|
|
2017-07-03 12:35:58 +02:00
|
|
|
client.sourceRangesForQueryMessage(std::move(queryResultMessage));
|
|
|
|
client.sourceRangesForQueryMessage(std::move(queryResultMessage));
|
2016-11-23 13:13:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(RefactoringClient, CallSetExpectedResultCountInSearchHandle)
|
|
|
|
{
|
|
|
|
EXPECT_CALL(mockSearchHandle, setExpectedResultCount(3))
|
|
|
|
.Times(1);
|
|
|
|
|
|
|
|
client.setExpectedResultCount(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(RefactoringClient, ResultCounterIsOneAfterQueryMessage)
|
|
|
|
{
|
2017-07-03 12:35:58 +02:00
|
|
|
client.sourceRangesForQueryMessage(std::move(queryResultMessage));
|
2016-11-23 13:13:38 +01:00
|
|
|
|
|
|
|
ASSERT_THAT(client.resultCounter(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(RefactoringClient, ResultCounterIsSetInSearchHandleToOne)
|
|
|
|
{
|
|
|
|
EXPECT_CALL(mockSearchHandle, setResultCounter(1))
|
|
|
|
.Times(1);
|
|
|
|
|
2017-07-03 12:35:58 +02:00
|
|
|
client.sourceRangesForQueryMessage(std::move(queryResultMessage));
|
2016-11-23 13:13:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(RefactoringClient, ResultCounterIsSetInSearchHandleToTwo)
|
|
|
|
{
|
2017-07-03 12:35:58 +02:00
|
|
|
client.sourceRangesForQueryMessage(std::move(queryResultMessage));
|
2016-11-23 13:13:38 +01:00
|
|
|
|
|
|
|
EXPECT_CALL(mockSearchHandle, setResultCounter(2))
|
|
|
|
.Times(1);
|
|
|
|
|
2017-07-03 12:35:58 +02:00
|
|
|
client.sourceRangesForQueryMessage(std::move(queryResultMessage));
|
2016-11-23 13:13:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(RefactoringClient, ResultCounterIsZeroAfterSettingExpectedResultCount)
|
|
|
|
{
|
2017-07-03 12:35:58 +02:00
|
|
|
client.sourceRangesForQueryMessage(std::move(queryResultMessage));
|
2016-11-23 13:13:38 +01:00
|
|
|
|
|
|
|
client.setExpectedResultCount(3);
|
|
|
|
|
|
|
|
ASSERT_THAT(client.resultCounter(), 0);
|
|
|
|
}
|
|
|
|
|
2016-11-23 13:31:47 +01:00
|
|
|
TEST_F(RefactoringClient, XXX)
|
|
|
|
{
|
|
|
|
const Core::Search::TextRange textRange{{1,0,1},{1,0,1}};
|
2017-09-21 11:43:59 +02:00
|
|
|
const ClangBackEnd::SourceRangeWithTextContainer sourceRange{{1, 1}, 1, 1, 1, 1, 1, 1, "function"};
|
2016-11-23 13:31:47 +01:00
|
|
|
|
|
|
|
EXPECT_CALL(mockSearchHandle, addResult(QString("/path/to/file"), QString("function"), textRange))
|
|
|
|
.Times(1);
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
client.addSearchResult(sourceRange);
|
2016-11-23 13:31:47 +01:00
|
|
|
}
|
|
|
|
|
2016-08-04 15:26:53 +02:00
|
|
|
void RefactoringClient::SetUp()
|
|
|
|
{
|
2017-02-20 15:31:18 +01:00
|
|
|
using Filter = ClangRefactoring::ClangQueryProjectsFindFilter;
|
|
|
|
|
2016-08-04 15:26:53 +02:00
|
|
|
client.setRefactoringEngine(&engine);
|
|
|
|
|
|
|
|
projectPart = CppTools::ProjectPart::Ptr(new CppTools::ProjectPart);
|
|
|
|
projectPart->files.push_back(projectFile);
|
|
|
|
|
2017-02-20 15:31:18 +01:00
|
|
|
commandLine = Filter::compilerArguments(projectPart.data(), projectFile.kind);
|
2016-11-15 15:38:12 +01:00
|
|
|
|
|
|
|
client.setSearchHandle(&mockSearchHandle);
|
2016-11-23 13:13:38 +01:00
|
|
|
client.setExpectedResultCount(1);
|
2017-09-21 11:43:59 +02:00
|
|
|
|
|
|
|
ON_CALL(mockFilePathCaching, filePath(Eq(FilePathId{1, 1})))
|
|
|
|
.WillByDefault(Return(FilePath(PathString("/path/to/file"))));
|
|
|
|
ON_CALL(mockFilePathCaching, filePath(Eq(FilePathId{1, 42})))
|
|
|
|
.WillByDefault(Return(clangBackEndFilePath));
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|