| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  | /****************************************************************************
 | 
					
						
							|  |  |  |  | ** | 
					
						
							|  |  |  |  | ** Copyright (C) 2015 Digia Plc and/or its subsidiary(-ies). | 
					
						
							|  |  |  |  | ** Contact: http://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 Digia.  For licensing terms and | 
					
						
							|  |  |  |  | ** conditions see http://www.qt.io/licensing.  For further information
 | 
					
						
							|  |  |  |  | ** use the contact form at http://www.qt.io/contact-us.
 | 
					
						
							|  |  |  |  | ** | 
					
						
							|  |  |  |  | ** GNU Lesser General Public License Usage | 
					
						
							|  |  |  |  | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
					
						
							|  |  |  |  | ** General Public License version 2.1 or version 3 as published by the Free | 
					
						
							|  |  |  |  | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | 
					
						
							|  |  |  |  | ** LICENSE.LGPLv3 included in the packaging of this file.  Please review the | 
					
						
							|  |  |  |  | ** following information to ensure the GNU Lesser General Public License | 
					
						
							|  |  |  |  | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
 | 
					
						
							|  |  |  |  | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 | 
					
						
							|  |  |  |  | ** | 
					
						
							|  |  |  |  | ** In addition, as a special exception, Digia gives you certain additional | 
					
						
							|  |  |  |  | ** rights.  These rights are described in the Digia Qt LGPL Exception | 
					
						
							|  |  |  |  | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
					
						
							|  |  |  |  | ** | 
					
						
							|  |  |  |  | ****************************************************************************/ | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #include "gtest/gtest.h"
 | 
					
						
							|  |  |  |  | #include "gmock/gmock-matchers.h"
 | 
					
						
							|  |  |  |  | #include "gmock/gmock-generated-matchers.h"
 | 
					
						
							|  |  |  |  | #include "gtest-qt-printing.h"
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #include <clang-c/Index.h>
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #include <translationunit.h>
 | 
					
						
							|  |  |  |  | #include <unsavedfiles.h>
 | 
					
						
							|  |  |  |  | #include <utf8string.h>
 | 
					
						
							|  |  |  |  | #include <projectpart.h>
 | 
					
						
							|  |  |  |  | #include <translationunits.h>
 | 
					
						
							|  |  |  |  | #include <filecontainer.h>
 | 
					
						
							|  |  |  |  | #include <projectpartcontainer.h>
 | 
					
						
							|  |  |  |  | #include <projects.h>
 | 
					
						
							|  |  |  |  | #include <translationunitdoesnotexistexception.h>
 | 
					
						
							|  |  |  |  | #include <translationunitisnullexception.h>
 | 
					
						
							|  |  |  |  | #include <translationunitfilenotexitexception.h>
 | 
					
						
							|  |  |  |  | #include <projectpartsdonotexistexception.h>
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  | using ClangBackEnd::TranslationUnit; | 
					
						
							|  |  |  |  | using ClangBackEnd::UnsavedFiles; | 
					
						
							|  |  |  |  | using ClangBackEnd::ProjectPart; | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | using testing::IsNull; | 
					
						
							|  |  |  |  | using testing::NotNull; | 
					
						
							|  |  |  |  | using testing::Gt; | 
					
						
							|  |  |  |  | using testing::Not; | 
					
						
							|  |  |  |  | using testing::Contains; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace { | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | using ::testing::PrintToString; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | MATCHER_P2(IsTranslationUnit, filePath, projectPartId, | 
					
						
							|  |  |  |  |            std::string(negation ? "isn't" : "is") + " translation unit with file path " | 
					
						
							|  |  |  |  |            + PrintToString(filePath) + " and project " + PrintToString(projectPartId) | 
					
						
							|  |  |  |  |            ) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     if (arg.filePath() != filePath) { | 
					
						
							|  |  |  |  |         *result_listener << "file path is " + PrintToString(arg.filePath()) + " and not " +  PrintToString(filePath); | 
					
						
							|  |  |  |  |         return false; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (arg.projectPartId() != projectPartId) { | 
					
						
							|  |  |  |  |         *result_listener << "file path is " + PrintToString(arg.projectPartId()) + " and not " +  PrintToString(projectPartId); | 
					
						
							|  |  |  |  |         return false; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     return true; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | class TranslationUnits : public ::testing::Test | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | protected: | 
					
						
							|  |  |  |  |     void SetUp() override; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  |     ClangBackEnd::ProjectParts projects; | 
					
						
							|  |  |  |  |     ClangBackEnd::UnsavedFiles unsavedFiles; | 
					
						
							|  |  |  |  |     ClangBackEnd::TranslationUnits translationUnits = ClangBackEnd::TranslationUnits(projects, unsavedFiles); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  |     const Utf8String filePath = Utf8StringLiteral(TESTDATA_DIR"/complete_testfile_1.cpp"); | 
					
						
							|  |  |  |  |     const Utf8String projectPartId = Utf8StringLiteral("/path/to/projectfile"); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void TranslationUnits::SetUp() | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  |     projects.createOrUpdate({ClangBackEnd::ProjectPartContainer(projectPartId)}); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | TEST_F(TranslationUnits, ThrowForGettingWithWrongFilePath) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     ASSERT_THROW(translationUnits.translationUnit(Utf8StringLiteral("foo.cpp"), projectPartId), | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  |                  ClangBackEnd::TranslationUnitDoesNotExistException); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | TEST_F(TranslationUnits, ThrowForGettingWithWrongProjectPartFilePath) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     ASSERT_THROW(translationUnits.translationUnit(filePath, Utf8StringLiteral("foo.pro")), | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  |                  ClangBackEnd::ProjectPartDoNotExistException); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | TEST_F(TranslationUnits, ThrowForAddingNonExistingFile) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  |     ClangBackEnd::FileContainer fileContainer(Utf8StringLiteral("foo.cpp"), projectPartId); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     ASSERT_THROW(translationUnits.createOrUpdate({fileContainer}), | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  |                  ClangBackEnd::TranslationUnitFileNotExitsException); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | TEST_F(TranslationUnits, DoNotThrowForAddingNonExistingFileWithUnsavedContent) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  |     ClangBackEnd::FileContainer fileContainer(Utf8StringLiteral("foo.cpp"), projectPartId, Utf8String(), true); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     ASSERT_NO_THROW(translationUnits.createOrUpdate({fileContainer})); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | TEST_F(TranslationUnits, Add) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  |     ClangBackEnd::FileContainer fileContainer(filePath, projectPartId); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     translationUnits.createOrUpdate({fileContainer}); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     ASSERT_THAT(translationUnits.translationUnit(filePath, projectPartId), | 
					
						
							|  |  |  |  |                 IsTranslationUnit(filePath, projectPartId)); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | TEST_F(TranslationUnits, ThrowForRemovingWithWrongFilePath) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  |     ClangBackEnd::FileContainer fileContainer(Utf8StringLiteral("foo.cpp"), projectPartId); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     ASSERT_THROW(translationUnits.remove({fileContainer}), | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  |                  ClangBackEnd::TranslationUnitDoesNotExistException); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | TEST_F(TranslationUnits, ThrowForRemovingWithWrongProjectPartFilePath) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  |     ClangBackEnd::FileContainer fileContainer(filePath, Utf8StringLiteral("foo.pro")); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     ASSERT_THROW(translationUnits.remove({fileContainer}), | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  |                  ClangBackEnd::ProjectPartDoNotExistException); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | TEST_F(TranslationUnits, Remove) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  |     ClangBackEnd::FileContainer fileContainer(filePath, projectPartId); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  |     translationUnits.createOrUpdate({fileContainer}); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     translationUnits.remove({fileContainer}); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     ASSERT_THROW(translationUnits.translationUnit(filePath, projectPartId), | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  |                  ClangBackEnd::TranslationUnitDoesNotExistException); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | TEST_F(TranslationUnits, RemoveAllValidIfExceptionIsThrown) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  |     ClangBackEnd::FileContainer fileContainer(filePath, projectPartId); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  |     translationUnits.createOrUpdate({fileContainer}); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |  |     ASSERT_THROW(translationUnits.remove({ClangBackEnd::FileContainer(Utf8StringLiteral("dontextist.pro"), projectPartId), fileContainer}), | 
					
						
							|  |  |  |  |                  ClangBackEnd::TranslationUnitDoesNotExistException); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     ASSERT_THAT(translationUnits.translationUnits(), | 
					
						
							|  |  |  |  |                 Not(Contains(TranslationUnit(filePath, unsavedFiles, projects.project(projectPartId))))); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 |