| 
									
										
										
										
											2017-01-30 11:24:46 +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"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <includecollector.h>
 | 
					
						
							|  |  |  | #include <stringcache.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using testing::AllOf; | 
					
						
							|  |  |  | using testing::Contains; | 
					
						
							|  |  |  | using testing::Not; | 
					
						
							|  |  |  | using testing::ElementsAre; | 
					
						
							|  |  |  | using testing::UnorderedElementsAre; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class IncludeCollector : public ::testing::Test | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |     void SetUp(); | 
					
						
							|  |  |  |     uint id(const Utils::SmallString &path); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2017-08-21 12:00:27 +02:00
										 |  |  |     ClangBackEnd::FilePathCache<> filePathCache; | 
					
						
							| 
									
										
										
										
											2017-01-30 11:24:46 +01:00
										 |  |  |     ClangBackEnd::IncludeCollector collector{filePathCache}; | 
					
						
							| 
									
										
										
										
											2017-02-16 18:25:19 +01:00
										 |  |  |     ClangBackEnd::IncludeCollector emptyCollector{filePathCache}; | 
					
						
							|  |  |  |     Utils::PathStringVector excludePaths = {TESTDATA_DIR "/includecollector_main.h", | 
					
						
							|  |  |  |                                             TESTDATA_DIR "/includecollector_main2.h", | 
					
						
							|  |  |  |                                             TESTDATA_DIR "/includecollector_header1.h", | 
					
						
							|  |  |  |                                             TESTDATA_DIR "/includecollector_header2.h", | 
					
						
							|  |  |  |                                             TESTDATA_DIR "/includecollector_generated_file.h"}; | 
					
						
							| 
									
										
										
										
											2017-01-30 11:24:46 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(IncludeCollector, IncludesExternalHeader) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     collector.collectIncludes(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(collector.takeIncludeIds(), | 
					
						
							| 
									
										
										
										
											2017-02-02 11:46:19 +01:00
										 |  |  |                 AllOf(Contains(id(TESTDATA_DIR "/includecollector_external1.h")), | 
					
						
							| 
									
										
										
										
											2017-02-16 18:25:19 +01:00
										 |  |  |                       Contains(id(TESTDATA_DIR "/includecollector_external2.h")))); | 
					
						
							| 
									
										
										
										
											2017-01-30 11:24:46 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(IncludeCollector, DoesNotIncludesInternalHeader) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     collector.collectIncludes(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-02 11:46:19 +01:00
										 |  |  |     ASSERT_THAT(collector.takeIncludeIds(), Not(Contains(id(TESTDATA_DIR "/includecollector_header1.h")))); | 
					
						
							| 
									
										
										
										
											2017-01-30 11:24:46 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(IncludeCollector, NoDuplicate) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     collector.collectIncludes(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(collector.takeIncludeIds(), | 
					
						
							| 
									
										
										
										
											2017-02-02 11:46:19 +01:00
										 |  |  |                 UnorderedElementsAre(id(TESTDATA_DIR "/includecollector_external1.h"), | 
					
						
							| 
									
										
										
										
											2017-02-16 18:25:19 +01:00
										 |  |  |                                      id(TESTDATA_DIR "/includecollector_external2.h"), | 
					
						
							|  |  |  |                                      id(TESTDATA_DIR "/includecollector_external3.h"))) << filePathCache.string(3); | 
					
						
							| 
									
										
										
										
											2017-01-30 11:24:46 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(IncludeCollector, IncludesAreSorted) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     collector.collectIncludes(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(collector.takeIncludeIds(), | 
					
						
							| 
									
										
										
										
											2017-02-16 18:25:19 +01:00
										 |  |  |                 ElementsAre(0, 1, 2)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-13 13:30:29 +01:00
										 |  |  | TEST_F(IncludeCollector, If) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     emptyCollector.addFile(TESTDATA_DIR, "includecollector_if.cpp", "", {"cc", "includecollector_if.cpp"}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     emptyCollector.collectIncludes(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(emptyCollector.takeIncludeIds(), | 
					
						
							|  |  |  |                 ElementsAre(id(TESTDATA_DIR "/includecollector_true.h"))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-16 18:25:19 +01:00
										 |  |  | TEST_F(IncludeCollector, LocalPath) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     emptyCollector.addFile(TESTDATA_DIR, "includecollector_main.cpp", "", {"cc", "includecollector_main.cpp"}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     emptyCollector.collectIncludes(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(emptyCollector.takeIncludeIds(), | 
					
						
							|  |  |  |                 UnorderedElementsAre(id(TESTDATA_DIR "/includecollector_external1.h"), | 
					
						
							|  |  |  |                                      id(TESTDATA_DIR "/includecollector_external2.h"), | 
					
						
							|  |  |  |                                      id(TESTDATA_DIR "/includecollector_external3.h"))); | 
					
						
							| 
									
										
										
										
											2017-01-30 11:24:46 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-20 17:14:17 +01:00
										 |  |  | TEST_F(IncludeCollector, IgnoreMissingFile) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     emptyCollector.addFile(TESTDATA_DIR, "includecollector_missingfile.cpp", "", {"cc", "includecollector_missingfile.cpp"}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     emptyCollector.collectIncludes(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(emptyCollector.takeIncludeIds(), | 
					
						
							|  |  |  |                 UnorderedElementsAre(id(TESTDATA_DIR "/includecollector_external1.h"))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-30 11:24:46 +01:00
										 |  |  | void IncludeCollector::SetUp() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     collector.addFile(TESTDATA_DIR, "includecollector_main.cpp", "", {"cc", "includecollector_main.cpp"}); | 
					
						
							|  |  |  |     collector.addFile(TESTDATA_DIR, "includecollector_main2.cpp", "", {"cc", "includecollector_main2.cpp"}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-01 13:43:28 +01:00
										 |  |  |     collector.addUnsavedFiles({{{TESTDATA_DIR, "includecollector_generated_file.h"}, "#pragma once", {}}}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-16 18:25:19 +01:00
										 |  |  |     collector.setExcludedIncludes(excludePaths.clone()); | 
					
						
							|  |  |  |     emptyCollector.setExcludedIncludes(excludePaths.clone()); | 
					
						
							| 
									
										
										
										
											2017-01-30 11:24:46 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | uint IncludeCollector::id(const Utils::SmallString &path) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return filePathCache.stringId(path); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |