| 
									
										
										
										
											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"
 | 
					
						
							|  |  |  | #include "mockrefactoringclientcallback.h"
 | 
					
						
							| 
									
										
										
										
											2016-11-15 15:38:12 +01:00
										 |  |  | #include "mocksearchhandle.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-14 14:56:17 +01:00
										 |  |  | #include <cpptools/clangcompileroptionsbuilder.h>
 | 
					
						
							| 
									
										
										
										
											2016-08-04 15:26:53 +02:00
										 |  |  | #include <cpptools/projectpart.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <utils/smallstringvector.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QTextCursor>
 | 
					
						
							|  |  |  | #include <QTextDocument>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-14 14:56:17 +01:00
										 |  |  | using CppTools::ClangCompilerOptionsBuilder; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-04 15:26:53 +02:00
										 |  |  | using ClangRefactoring::RefactoringEngine; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-04 15:26:53 +02:00
										 |  |  | using testing::_; | 
					
						
							| 
									
										
										
										
											2016-11-15 15:38:12 +01:00
										 |  |  | using testing::Pair; | 
					
						
							|  |  |  | using testing::Contains; | 
					
						
							|  |  |  | using testing::NiceMock; | 
					
						
							| 
									
										
										
										
											2016-08-04 15:26:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | using Utils::SmallString; | 
					
						
							|  |  |  | using Utils::SmallStringVector; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class RefactoringClient : public ::testing::Test | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     void SetUp(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2016-11-15 15:38:12 +01:00
										 |  |  |     NiceMock<MockSearchHandle> mockSearchHandle; | 
					
						
							| 
									
										
										
										
											2016-09-15 17:41:41 +02:00
										 |  |  |     MockRefactoringClientCallBack callbackMock; | 
					
						
							| 
									
										
										
										
											2016-08-04 15:26:53 +02:00
										 |  |  |     ClangRefactoring::RefactoringClient client; | 
					
						
							|  |  |  |     ClangBackEnd::RefactoringConnectionClient connectionClient{&client}; | 
					
						
							|  |  |  |     RefactoringEngine engine{connectionClient.serverProxy(), client}; | 
					
						
							|  |  |  |     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", | 
					
						
							|  |  |  |                                                     {{{42u, clangBackEndFilePath.clone()}}, | 
					
						
							|  |  |  |                                                      {{42u, 1, 1, 0}, {42u, 2, 5, 10}}}, | 
					
						
							|  |  |  |                                                     1}; | 
					
						
							| 
									
										
										
										
											2017-07-03 12:35:58 +02:00
										 |  |  |     SourceRangesForQueryMessage queryResultMessage{{{{42u, clangBackEndFilePath.clone()}}, | 
					
						
							|  |  |  |                                                     {{42u, 1, 1, 0, 1, 5, 4, ""}, | 
					
						
							|  |  |  |                                                      {42u, 2, 1, 5, 2, 5, 10, ""}}}}; | 
					
						
							|  |  |  |     SourceRangesForQueryMessage emptyQueryResultMessage{{{},{}}}; | 
					
						
							| 
									
										
										
										
											2016-08-04 15:26:53 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(RefactoringClient, SourceLocationsForRenaming) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     client.setLocalRenamingCallback([&] (const QString &symbolName, | 
					
						
							|  |  |  |                                          const ClangBackEnd::SourceLocationsContainer &sourceLocations, | 
					
						
							|  |  |  |                                          int textDocumentRevision) { | 
					
						
							|  |  |  |         callbackMock.localRenaming(symbolName, | 
					
						
							|  |  |  |                                    sourceLocations, | 
					
						
							|  |  |  |                                    textDocumentRevision); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-15 15:38:12 +01:00
										 |  |  |     EXPECT_CALL(callbackMock, localRenaming(renameMessage.symbolName().toQString(), | 
					
						
							|  |  |  |                                             renameMessage.sourceLocations(), | 
					
						
							|  |  |  |                                             renameMessage.textDocumentRevision())) | 
					
						
							| 
									
										
										
										
											2016-08-04 15:26:53 +02:00
										 |  |  |         .Times(1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     client.setLocalRenamingCallback([&] (const QString &symbolName, | 
					
						
							|  |  |  |                                          const ClangBackEnd::SourceLocationsContainer &sourceLocations, | 
					
						
							|  |  |  |                                          int textDocumentRevision) { | 
					
						
							|  |  |  |         callbackMock.localRenaming(symbolName, | 
					
						
							|  |  |  |                                    sourceLocations, | 
					
						
							|  |  |  |                                    textDocumentRevision); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     EXPECT_CALL(callbackMock, localRenaming(_,_,_)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-15 15:38:12 +01:00
										 |  |  |     client.sourceLocationsForRenamingMessage(std::move(renameMessage)); | 
					
						
							| 
									
										
										
										
											2016-08-04 15:26:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_TRUE(engine.isUsable()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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-15 15:38:12 +01:00
										 |  |  | TEST_F(RefactoringClient, ConvertFilePaths) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     std::unordered_map<uint, ClangBackEnd::FilePath> filePaths{{42u, clangBackEndFilePath.clone()}}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto qstringFilePaths = ClangRefactoring::RefactoringClient::convertFilePaths(filePaths); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(qstringFilePaths, Contains(Pair(42u, qStringFilePath))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-23 13:31:47 +01:00
										 |  |  | TEST_F(RefactoringClient, XXX) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     const Core::Search::TextRange textRange{{1,0,1},{1,0,1}}; | 
					
						
							|  |  |  |     const ClangBackEnd::SourceRangeWithTextContainer sourceRange{1, 1, 1, 1, 1, 1, 1, "function"}; | 
					
						
							|  |  |  |     std::unordered_map<uint, QString> filePaths = {{1, "/path/to/file"}}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     EXPECT_CALL(mockSearchHandle, addResult(QString("/path/to/file"), QString("function"), textRange)) | 
					
						
							|  |  |  |         .Times(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     client.addSearchResult(sourceRange, filePaths); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2016-08-04 15:26:53 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |