| 
									
										
										
										
											2016-07-15 12:30:25 +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-09-15 11:56:25 +02:00
										 |  |  | #include <clangclock.h>
 | 
					
						
							| 
									
										
										
										
											2016-07-15 12:30:25 +02:00
										 |  |  | #include <clangtranslationunitupdater.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <clang-c/Index.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-15 11:56:25 +02:00
										 |  |  | using ClangBackEnd::Clock; | 
					
						
							|  |  |  | using ClangBackEnd::TimePoint; | 
					
						
							| 
									
										
										
										
											2016-07-15 12:30:25 +02:00
										 |  |  | using ClangBackEnd::TranslationUnitUpdater; | 
					
						
							|  |  |  | using ClangBackEnd::TranslationUnitUpdateInput; | 
					
						
							|  |  |  | using ClangBackEnd::TranslationUnitUpdateResult; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-13 10:41:22 +02:00
										 |  |  | using testing::Eq; | 
					
						
							| 
									
										
										
										
											2016-07-15 12:30:25 +02:00
										 |  |  | using testing::Gt; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TranslationUnitUpdater : public ::testing::Test | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |     void TearDown() override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-13 10:41:22 +02:00
										 |  |  |     ::TranslationUnitUpdater createUpdater(const TranslationUnitUpdateInput &input, | 
					
						
							|  |  |  |                                            const Utf8String &translationUnitId = Utf8String()); | 
					
						
							| 
									
										
										
										
											2016-07-15 12:30:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     enum ReparseMode { SetReparseNeeded, DoNotSetReparseNeeded }; | 
					
						
							|  |  |  |     TranslationUnitUpdateInput createInput(ReparseMode reparseMode = DoNotSetReparseNeeded); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |     CXIndex cxIndex = nullptr; | 
					
						
							|  |  |  |     CXTranslationUnit cxTranslationUnit = nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Utf8String filePath = Utf8StringLiteral(TESTDATA_DIR"/translationunits.cpp"); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | using TranslationUnitUpdaterSlowTest = TranslationUnitUpdater; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(TranslationUnitUpdaterSlowTest, ParsesIfNeeded) | 
					
						
							| 
									
										
										
										
											2016-07-15 12:30:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     ::TranslationUnitUpdater updater = createUpdater(createInput()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TranslationUnitUpdateResult result = updater.update(::TranslationUnitUpdater::UpdateMode::AsNeeded); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_TRUE(cxTranslationUnit); | 
					
						
							| 
									
										
										
										
											2016-09-15 11:08:13 +02:00
										 |  |  |     ASSERT_FALSE(result.hasReparsed()); | 
					
						
							| 
									
										
										
										
											2016-07-15 12:30:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(TranslationUnitUpdaterSlowTest, ReparsesIfNeeded) | 
					
						
							| 
									
										
										
										
											2016-07-15 12:30:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     ::TranslationUnitUpdater updater = createUpdater(createInput(SetReparseNeeded)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TranslationUnitUpdateResult result = updater.update(::TranslationUnitUpdater::UpdateMode::AsNeeded); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-15 11:08:13 +02:00
										 |  |  |     ASSERT_TRUE(result.hasReparsed()); | 
					
						
							| 
									
										
										
										
											2016-07-15 12:30:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(TranslationUnitUpdaterSlowTest, PropagatesTranslationUnitId) | 
					
						
							| 
									
										
										
										
											2016-09-13 10:41:22 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     const Utf8String translationUnitId = Utf8StringLiteral("myId"); | 
					
						
							|  |  |  |     ::TranslationUnitUpdater updater = createUpdater(createInput(SetReparseNeeded), translationUnitId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TranslationUnitUpdateResult result = updater.update(::TranslationUnitUpdater::UpdateMode::AsNeeded); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(result.translationUnitId, Eq(translationUnitId)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(TranslationUnitUpdaterSlowTest, UpdatesParseTimePoint) | 
					
						
							| 
									
										
										
										
											2016-07-15 12:30:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     ::TranslationUnitUpdater updater = createUpdater(createInput()); | 
					
						
							| 
									
										
										
										
											2016-09-15 11:56:25 +02:00
										 |  |  |     const TimePoint now = Clock::now(); | 
					
						
							| 
									
										
										
										
											2016-07-15 12:30:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     TranslationUnitUpdateResult result = updater.update(::TranslationUnitUpdater::UpdateMode::AsNeeded); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-15 11:08:13 +02:00
										 |  |  |     ASSERT_TRUE(result.hasParsed()); | 
					
						
							| 
									
										
										
										
											2016-07-15 12:30:25 +02:00
										 |  |  |     ASSERT_THAT(result.parseTimePoint, Gt(now)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(TranslationUnitUpdaterSlowTest, NotUpdatingParseTimePointForReparseOnly) | 
					
						
							| 
									
										
										
										
											2016-07-15 12:30:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     ::TranslationUnitUpdater updater = createUpdater(createInput()); | 
					
						
							|  |  |  |     TranslationUnitUpdateResult result = updater.update(::TranslationUnitUpdater::UpdateMode::AsNeeded); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ::TranslationUnitUpdater reparseUpdater = createUpdater(createInput(SetReparseNeeded)); | 
					
						
							|  |  |  |     result = reparseUpdater.update(::TranslationUnitUpdater::UpdateMode::AsNeeded); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-15 11:08:13 +02:00
										 |  |  |     ASSERT_TRUE(result.hasReparsed()); | 
					
						
							|  |  |  |     ASSERT_FALSE(result.hasParsed()); | 
					
						
							| 
									
										
										
										
											2016-07-15 12:30:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 11:39:17 +01:00
										 |  |  | TEST_F(TranslationUnitUpdaterSlowTest, UpdatesDependendOnFilesOnParse) | 
					
						
							| 
									
										
										
										
											2016-07-15 12:30:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     ::TranslationUnitUpdater updater = createUpdater(createInput()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     TranslationUnitUpdateResult result = updater.update(::TranslationUnitUpdater::UpdateMode::AsNeeded); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_FALSE(result.dependedOnFilePaths.isEmpty()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TranslationUnitUpdater::TearDown() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     clang_disposeIndex(cxIndex); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ::TranslationUnitUpdater | 
					
						
							| 
									
										
										
										
											2016-09-13 10:41:22 +02:00
										 |  |  | TranslationUnitUpdater::createUpdater(const TranslationUnitUpdateInput &input, | 
					
						
							|  |  |  |                                       const Utf8String &translationUnitId) | 
					
						
							| 
									
										
										
										
											2016-07-15 12:30:25 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-13 10:41:22 +02:00
										 |  |  |     return ::TranslationUnitUpdater(translationUnitId, cxIndex, cxTranslationUnit, input); | 
					
						
							| 
									
										
										
										
											2016-07-15 12:30:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TranslationUnitUpdateInput | 
					
						
							|  |  |  | TranslationUnitUpdater::createInput(ReparseMode reparseMode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     TranslationUnitUpdateInput updateInput; | 
					
						
							|  |  |  |     updateInput.filePath = filePath; | 
					
						
							|  |  |  |     updateInput.reparseNeeded = reparseMode == SetReparseNeeded; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return updateInput; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // anonymous
 |