| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | /****************************************************************************
 | 
					
						
							|  |  |  | ** | 
					
						
							| 
									
										
										
										
											2016-01-15 14:55:33 +01:00
										 |  |  | ** Copyright (C) 2016 The Qt Company Ltd. | 
					
						
							|  |  |  | ** Contact: https://www.qt.io/licensing/
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | ** | 
					
						
							|  |  |  | ** 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 | 
					
						
							| 
									
										
										
										
											2016-01-15 14:55:33 +01:00
										 |  |  | ** 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.
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2016-01-15 14:55:33 +01:00
										 |  |  | ** 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.
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | ** | 
					
						
							|  |  |  | ****************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-15 17:41:41 +02:00
										 |  |  | #include "googletest.h"
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <filecontainer.h>
 | 
					
						
							| 
									
										
										
										
											2016-01-28 18:31:05 +01:00
										 |  |  | #include <unsavedfiles.h>
 | 
					
						
							|  |  |  | #include <unsavedfile.h>
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <QVector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 18:31:05 +01:00
										 |  |  | using ClangBackEnd::UnsavedFile; | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  | using ClangBackEnd::UnsavedFiles; | 
					
						
							|  |  |  | using ClangBackEnd::FileContainer; | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-30 10:25:52 +02:00
										 |  |  | using ::testing::Eq; | 
					
						
							| 
									
										
										
										
											2016-01-28 18:31:05 +01:00
										 |  |  | using ::testing::Gt; | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | using ::testing::IsNull; | 
					
						
							| 
									
										
										
										
											2016-05-30 10:25:52 +02:00
										 |  |  | using ::testing::Ne; | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | using ::testing::NotNull; | 
					
						
							| 
									
										
										
										
											2016-01-28 18:31:05 +01:00
										 |  |  | using ::testing::PrintToString; | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-30 10:25:52 +02:00
										 |  |  | bool operator==(const ClangBackEnd::FileContainer &fileContainer, const UnsavedFile &unsavedFile) | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-04-04 18:25:23 +02:00
										 |  |  |     return fileContainer.filePath == unsavedFile.filePath() | 
					
						
							|  |  |  |         && fileContainer.unsavedFileContent == unsavedFile.fileContent(); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-30 10:25:52 +02:00
										 |  |  | bool fileContainersContainsItemMatchingToCxUnsavedFile(const QVector<FileContainer> &fileContainers, const UnsavedFile &unsavedFile) | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-05-30 10:25:52 +02:00
										 |  |  |     for (const FileContainer &fileContainer : fileContainers) { | 
					
						
							|  |  |  |         if (fileContainer == unsavedFile) | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |             return true; | 
					
						
							| 
									
										
										
										
											2016-05-30 10:25:52 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MATCHER_P(HasUnsavedFiles, fileContainers, "") | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-16 11:56:00 +02:00
										 |  |  |     ClangBackEnd::UnsavedFiles unsavedFiles = arg; | 
					
						
							| 
									
										
										
										
											2016-01-01 15:58:43 +02:00
										 |  |  |     if (unsavedFiles.count() != uint(fileContainers.size())) { | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |         *result_listener << "unsaved count is " << unsavedFiles.count() << " and not " << fileContainers.size(); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 18:31:05 +01:00
										 |  |  |     for (uint i = 0, to = unsavedFiles.count(); i < to; ++i) { | 
					
						
							| 
									
										
										
										
											2016-05-30 10:25:52 +02:00
										 |  |  |         UnsavedFile unsavedFile = unsavedFiles.at(i); | 
					
						
							|  |  |  |         if (!fileContainersContainsItemMatchingToCxUnsavedFile(fileContainers, unsavedFile)) | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |             return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 18:31:05 +01:00
										 |  |  | MATCHER_P3(IsUnsavedFile, fileName, contents, contentsLength, | 
					
						
							|  |  |  |           std::string(negation ? "isn't" : "is") | 
					
						
							|  |  |  |           + " file name " + PrintToString(fileName) | 
					
						
							|  |  |  |           + ", contents " + PrintToString(contents) | 
					
						
							|  |  |  |           + ", contents length " + PrintToString(contentsLength)) | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-05-30 10:25:52 +02:00
										 |  |  |     return fileName == arg.filePath() | 
					
						
							|  |  |  |         && contents == arg.fileContent() | 
					
						
							|  |  |  |         && int(contentsLength) == arg.fileContent().byteSize(); | 
					
						
							| 
									
										
										
										
											2016-01-28 18:31:05 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class UnsavedFiles : public ::testing::Test | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | protected: | 
					
						
							|  |  |  |     ::UnsavedFiles unsavedFiles; | 
					
						
							| 
									
										
										
										
											2015-08-31 12:40:14 +02:00
										 |  |  |     Utf8String filePath{Utf8StringLiteral("file.cpp")}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Utf8String unsavedContent1{Utf8StringLiteral("foo")}; | 
					
						
							|  |  |  |     Utf8String unsavedContent2{Utf8StringLiteral("bar")}; | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-30 10:25:52 +02:00
										 |  |  | TEST_F(UnsavedFiles, ModifiedCopyIsDifferent) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     QVector<FileContainer> fileContainers({FileContainer(filePath, unsavedContent1, true)}); | 
					
						
							| 
									
										
										
										
											2016-05-30 10:25:52 +02:00
										 |  |  |     unsavedFiles.createOrUpdate(fileContainers); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ::UnsavedFiles copy = unsavedFiles; | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     QVector<FileContainer> updatedFileContainers({FileContainer(filePath, unsavedContent2, true)}); | 
					
						
							| 
									
										
										
										
											2016-05-30 10:25:52 +02:00
										 |  |  |     copy.createOrUpdate(updatedFileContainers); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(copy.at(0).fileContent(), Ne(unsavedFiles.at(0).fileContent())); | 
					
						
							|  |  |  |     ASSERT_THAT(copy.lastChangeTimePoint(), Gt(unsavedFiles.lastChangeTimePoint())); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | TEST_F(UnsavedFiles, DoNothingForUpdateIfFileHasNoUnsavedContent) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     QVector<FileContainer> fileContainers({FileContainer(filePath)}); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     unsavedFiles.createOrUpdate(fileContainers); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(unsavedFiles, HasUnsavedFiles(QVector<FileContainer>())); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(UnsavedFiles, AddUnsavedFileForUpdateWithUnsavedContent) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     QVector<FileContainer> fileContainers({FileContainer(filePath), | 
					
						
							|  |  |  |                                            FileContainer(filePath, unsavedContent1, true)}); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |     unsavedFiles.createOrUpdate(fileContainers); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     ASSERT_THAT(unsavedFiles, HasUnsavedFiles(QVector<FileContainer>({FileContainer(filePath, unsavedContent1, true)}))); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(UnsavedFiles, RemoveUnsavedFileForUpdateWithUnsavedContent) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     QVector<FileContainer> fileContainers({FileContainer(filePath, unsavedContent1, true), | 
					
						
							|  |  |  |                                            FileContainer(filePath)}); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     unsavedFiles.createOrUpdate(fileContainers); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(unsavedFiles, HasUnsavedFiles(QVector<FileContainer>())); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(UnsavedFiles, ExchangeUnsavedFileForUpdateWithUnsavedContent) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     QVector<FileContainer> fileContainers({FileContainer(filePath, unsavedContent1, true), | 
					
						
							|  |  |  |                                            FileContainer(filePath, unsavedContent2, true)}); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |     unsavedFiles.createOrUpdate(fileContainers); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     ASSERT_THAT(unsavedFiles, HasUnsavedFiles(QVector<FileContainer>({FileContainer(filePath, unsavedContent2, true)}))); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(UnsavedFiles, TimeStampIsUpdatedAsUnsavedFilesChanged) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     QVector<FileContainer> fileContainers({FileContainer(filePath, unsavedContent1, true), | 
					
						
							|  |  |  |                                            FileContainer(filePath, unsavedContent2, true)}); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |     auto lastChangeTimePoint = unsavedFiles.lastChangeTimePoint(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     unsavedFiles.createOrUpdate(fileContainers); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(unsavedFiles.lastChangeTimePoint(), Gt(lastChangeTimePoint)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(UnsavedFiles, RemoveUnsavedFiles) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     QVector<FileContainer> fileContainers({FileContainer(filePath, unsavedContent1, true)}); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  |     unsavedFiles.createOrUpdate(fileContainers); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     unsavedFiles.remove(fileContainers); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(unsavedFiles, HasUnsavedFiles(QVector<FileContainer>())); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-01-28 18:31:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | TEST_F(UnsavedFiles, FindUnsavedFile) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     QVector<FileContainer> fileContainers({FileContainer(filePath, unsavedContent1, true)}); | 
					
						
							| 
									
										
										
										
											2016-01-28 18:31:05 +01:00
										 |  |  |     unsavedFiles.createOrUpdate(fileContainers); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     UnsavedFile &unsavedFile = unsavedFiles.unsavedFile(filePath); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(unsavedFile, IsUnsavedFile(filePath, unsavedContent1, unsavedContent1.byteSize())); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 18:31:05 +01:00
										 |  |  | TEST_F(UnsavedFiles, FindNoUnsavedFile) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-25 09:41:32 +02:00
										 |  |  |     QVector<FileContainer> fileContainers({FileContainer(filePath, unsavedContent1, true)}); | 
					
						
							| 
									
										
										
										
											2016-01-28 18:31:05 +01:00
										 |  |  |     unsavedFiles.createOrUpdate(fileContainers); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     UnsavedFile &unsavedFile = unsavedFiles.unsavedFile(Utf8StringLiteral("nonExistingFilePath.cpp")); | 
					
						
							| 
									
										
										
										
											2015-06-01 18:51:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 18:31:05 +01:00
										 |  |  |     ASSERT_THAT(unsavedFile, IsUnsavedFile(Utf8String(), Utf8String(), 0UL)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |