| 
									
										
										
										
											2016-11-23 13:13:38 +01: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.
 | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ****************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "googletest.h"
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:59 +02:00
										 |  |  | #include "sourcerangecontainer-matcher.h"
 | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | #include "testclangtool.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:59 +02:00
										 |  |  | #include <refactoringdatabaseinitializer.h>
 | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | #include <sourcerangeextractor.h>
 | 
					
						
							|  |  |  | #include <sourcerangescontainer.h>
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:59 +02:00
										 |  |  | #include <filepathcaching.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <sqlitedatabase.h>
 | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <clang/Basic/SourceManager.h>
 | 
					
						
							|  |  |  | #include <clang/Lex/Lexer.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:59 +02:00
										 |  |  | #include <QDir>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-03 11:12:00 +02:00
										 |  |  | #include <mutex>
 | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | using testing::Contains; | 
					
						
							|  |  |  | using ::testing::Eq; | 
					
						
							|  |  |  | using ::testing::StrEq; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using ClangBackEnd::SourceRangeWithTextContainer; | 
					
						
							|  |  |  | using ClangBackEnd::SourceRangeExtractor; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SourceRangeExtractor : public ::testing::Test | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |     void SetUp() override; | 
					
						
							|  |  |  |     void TearDown() override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |     TestClangTool clangTool{TESTDATA_DIR, "sourcerangeextractor_location.cpp", "",  {"cc", "sourcerangeextractor_location.cpp"}}; | 
					
						
							|  |  |  |     ClangBackEnd::SourceRangesContainer sourceRangesContainer; | 
					
						
							|  |  |  |     const clang::SourceManager &sourceManager{clangTool.sourceManager()}; | 
					
						
							| 
									
										
										
										
											2017-10-18 13:34:05 +02:00
										 |  |  |     Sqlite::Database database{":memory:", Sqlite::JournalMode::Memory}; | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:59 +02:00
										 |  |  |     ClangBackEnd::RefactoringDatabaseInitializer<Sqlite::Database> databaseInitializer{database}; | 
					
						
							|  |  |  |     ClangBackEnd::FilePathCaching filePathCache{database}; | 
					
						
							| 
									
										
										
										
											2017-07-03 11:12:00 +02:00
										 |  |  |     ClangBackEnd::SourceRangeExtractor extractor{sourceManager, clangTool.languageOptions(), filePathCache, sourceRangesContainer}; | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  |     clang::SourceLocation startLocation = sourceManager.getLocForStartOfFile(sourceManager.getMainFileID()); | 
					
						
							|  |  |  |     clang::SourceLocation endLocation = sourceManager.getLocForStartOfFile(sourceManager.getMainFileID()).getLocWithOffset(4); | 
					
						
							|  |  |  |     clang::SourceRange sourceRange{startLocation, endLocation}; | 
					
						
							|  |  |  |     clang::SourceRange extendedSourceRange{startLocation, endLocation.getLocWithOffset(5)}; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | using SourceRangeExtractorSlowTest = SourceRangeExtractor; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(SourceRangeExtractorSlowTest, ExtractSourceRangeContainer) | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-06 19:02:00 +01:00
										 |  |  |     SourceRangeWithTextContainer sourceRangeContainer{1, 1, 1, 0, 1, 10, 9, Utils::SmallString("int value;")}; | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     extractor.addSourceRange(sourceRange); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-21 11:43:59 +02:00
										 |  |  |     ASSERT_THAT(extractor.sourceRangeWithTextContainers(), | 
					
						
							|  |  |  |                 Contains(IsSourceRangeWithText(1, 1, 1, 10, "int value;"))); | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(SourceRangeExtractorSlowTest, ExtendedSourceRange) | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto range = extractor.extendSourceRangeToLastTokenEnd(sourceRange); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(range, extendedSourceRange); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(SourceRangeExtractorSlowTest, FindStartOfLineInEmptyBuffer) | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     clang::StringRef text = ""; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto found = ::SourceRangeExtractor::findStartOfLineInBuffer(text, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 18:55:08 +02:00
										 |  |  |     ASSERT_THAT(found, StrEq("")); | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(SourceRangeExtractorSlowTest, FindStartOfLineInBufferInFirstLine) | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     clang::StringRef text = "first line"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto found = ::SourceRangeExtractor::findStartOfLineInBuffer(text, 5); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 18:55:08 +02:00
										 |  |  |     ASSERT_THAT(found, StrEq("first line")); | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(SourceRangeExtractorSlowTest, FindStartOfNewLineInBufferInSecondLine) | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     clang::StringRef text = "first line\nsecond line"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto found = ::SourceRangeExtractor::findStartOfLineInBuffer(text, 15); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(found, StrEq("second line")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(SourceRangeExtractorSlowTest, FindStartOfCarriageReturnInBufferInSecondLine) | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     clang::StringRef text = "first line\rsecond line"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto found = ::SourceRangeExtractor::findStartOfLineInBuffer(text, 15); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(found, StrEq("second line")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(SourceRangeExtractorSlowTest, FindStartOfNewLineCarriageReturnInBufferInSecondLine) | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     clang::StringRef text = "first line\n\rsecond line"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto found = ::SourceRangeExtractor::findStartOfLineInBuffer(text, 15); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(found, StrEq("second line")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(SourceRangeExtractorSlowTest, FindEndOfLineInEmptyBuffer) | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     clang::StringRef text = ""; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto found = ::SourceRangeExtractor::findEndOfLineInBuffer(text, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(found, StrEq("")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(SourceRangeExtractorSlowTest, FindEndOfLineInBuffer) | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     clang::StringRef text = "first line"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto found = ::SourceRangeExtractor::findEndOfLineInBuffer(text, 5); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(found, StrEq("")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(SourceRangeExtractorSlowTest, FindEndOfLineInBufferInFirstLineWithNewLine) | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     clang::StringRef text = "first line\nsecond line\nthird line"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto found = ::SourceRangeExtractor::findEndOfLineInBuffer(text, 15); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(found, StrEq("\nthird line")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(SourceRangeExtractorSlowTest, FindEndOfLineInBufferInFirstLineWithCarriageReturn) | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     clang::StringRef text = "first line\rsecond line\rthird line"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto found = ::SourceRangeExtractor::findEndOfLineInBuffer(text, 15); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(found, StrEq("\rthird line")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(SourceRangeExtractorSlowTest, EpandText) | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     clang::StringRef text = "first line\nsecond line\nthird line\nforth line"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto expandedText = ::SourceRangeExtractor::getExpandedText(text, 15, 25); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 12:54:10 +02:00
										 |  |  |     ASSERT_THAT(expandedText, Eq("second line\nthird line")); | 
					
						
							| 
									
										
										
										
											2016-11-23 13:13:38 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SourceRangeExtractor::SetUp() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TestGlobal::setSourceManager(&sourceManager); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SourceRangeExtractor::TearDown() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TestGlobal::setSourceManager(nullptr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |