| 
									
										
										
										
											2015-08-24 18:26:09 +02:00
										 |  |  | /****************************************************************************
 | 
					
						
							|  |  |  | ** | 
					
						
							| 
									
										
										
										
											2016-01-15 14:55:33 +01:00
										 |  |  | ** Copyright (C) 2016 The Qt Company Ltd. | 
					
						
							|  |  |  | ** Contact: https://www.qt.io/licensing/
 | 
					
						
							| 
									
										
										
										
											2015-08-24 18:26:09 +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-08-24 18:26:09 +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-08-24 18:26:09 +02:00
										 |  |  | ** | 
					
						
							|  |  |  | ****************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-15 17:41:41 +02:00
										 |  |  | #include "googletest.h"
 | 
					
						
							| 
									
										
										
										
											2015-08-24 18:26:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <clangfixitoperation.h>
 | 
					
						
							|  |  |  | #include <fixitcontainer.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <utils/changeset.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QFile>
 | 
					
						
							|  |  |  | #include <QVector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using ClangBackEnd::FixItContainer; | 
					
						
							|  |  |  | using ClangCodeModel::ClangFixItOperation; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using ::testing::PrintToString; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QString unsavedFileContent(const QString &unsavedFilePath) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QFile unsavedFileContentFile(unsavedFilePath); | 
					
						
							|  |  |  |     const bool isOpen = unsavedFileContentFile.open(QFile::ReadOnly | QFile::Text); | 
					
						
							|  |  |  |     if (!isOpen) | 
					
						
							|  |  |  |         ADD_FAILURE() << "File with the unsaved content cannot be opened!"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return QString::fromUtf8(unsavedFileContentFile.readAll()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-02 20:07:29 +01:00
										 |  |  | MATCHER_P(MatchText, expectedText, | 
					
						
							|  |  |  |           std::string(negation ? "hasn't" : "has") | 
					
						
							|  |  |  |           + " expected text:\n" + PrintToString(expectedText)) | 
					
						
							| 
									
										
										
										
											2015-08-24 18:26:09 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-12-02 20:07:29 +01:00
										 |  |  |     const ::ClangFixItOperation &operation = arg; | 
					
						
							| 
									
										
										
										
											2016-06-27 10:45:51 +02:00
										 |  |  |     QString resultText = operation.firstRefactoringFileContent_forTestOnly(); | 
					
						
							| 
									
										
										
										
											2015-08-24 18:26:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (resultText != expectedText) { | 
					
						
							|  |  |  |         *result_listener << "\n" << resultText.toUtf8().constData(); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ClangFixItOperation : public ::testing::Test | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2015-12-02 20:07:29 +01:00
										 |  |  |     Utf8String semicolonFilePath{TESTDATA_DIR"/diagnostic_semicolon_fixit.cpp", -1}; | 
					
						
							|  |  |  |     Utf8String compareFilePath{TESTDATA_DIR"/diagnostic_comparison_fixit.cpp", -1}; | 
					
						
							| 
									
										
										
										
											2015-08-24 18:26:09 +02:00
										 |  |  |     Utf8String diagnosticText{Utf8StringLiteral("expected ';' at end of declaration")}; | 
					
						
							|  |  |  |     FixItContainer semicolonFixItContainer{Utf8StringLiteral(";"), | 
					
						
							| 
									
										
										
										
											2015-12-02 20:07:29 +01:00
										 |  |  |                                   {{semicolonFilePath, 3u, 13u}, | 
					
						
							|  |  |  |                                    {semicolonFilePath, 3u, 13u}}}; | 
					
						
							|  |  |  |     QString semicolonErrorFile{semicolonFilePath.toString()}; | 
					
						
							| 
									
										
										
										
											2015-11-24 11:07:51 +01:00
										 |  |  |     QString semicolonExpectedFile{QString::fromUtf8(TESTDATA_DIR"/diagnostic_semicolon_fixit_expected.cpp")}; | 
					
						
							| 
									
										
										
										
											2015-12-02 20:07:29 +01:00
										 |  |  |     QString compareWarningFile{compareFilePath.toString()}; | 
					
						
							| 
									
										
										
										
											2015-11-24 11:07:51 +01:00
										 |  |  |     QString compareExpected1File{QString::fromUtf8(TESTDATA_DIR"/diagnostic_comparison_fixit_expected1.cpp")}; | 
					
						
							|  |  |  |     QString compareExpected2File{QString::fromUtf8(TESTDATA_DIR"/diagnostic_comparison_fixit_expected2.cpp")}; | 
					
						
							| 
									
										
										
										
											2015-08-24 18:26:09 +02:00
										 |  |  |     FixItContainer compareFixItContainer{Utf8StringLiteral("=="), | 
					
						
							| 
									
										
										
										
											2015-12-02 20:07:29 +01:00
										 |  |  |                                   {{compareFilePath, 4u, 11u}, | 
					
						
							|  |  |  |                                    {compareFilePath, 4u, 12u}}}; | 
					
						
							| 
									
										
										
										
											2015-08-24 18:26:09 +02:00
										 |  |  |     FixItContainer assignmentFixItContainerParenLeft{Utf8StringLiteral("("), | 
					
						
							| 
									
										
										
										
											2015-12-02 20:07:29 +01:00
										 |  |  |                                   {{compareFilePath, 4u, 9u}, | 
					
						
							|  |  |  |                                    {compareFilePath, 4u, 9u}}}; | 
					
						
							| 
									
										
										
										
											2015-08-24 18:26:09 +02:00
										 |  |  |     FixItContainer assignmentFixItContainerParenRight{Utf8StringLiteral(")"), | 
					
						
							| 
									
										
										
										
											2015-12-02 20:07:29 +01:00
										 |  |  |                                   {{compareFilePath, 4u, 14u}, | 
					
						
							|  |  |  |                                    {compareFilePath, 4u, 14u}}}; | 
					
						
							| 
									
										
										
										
											2015-08-24 18:26:09 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(ClangFixItOperation, Description) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-27 10:45:51 +02:00
										 |  |  |     ::ClangFixItOperation operation(diagnosticText, {semicolonFixItContainer}); | 
					
						
							| 
									
										
										
										
											2015-08-24 18:26:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(operation.description(), | 
					
						
							|  |  |  |                 QStringLiteral("Apply Fix: expected ';' at end of declaration")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(ClangFixItOperation, AppendSemicolon) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-27 10:45:51 +02:00
										 |  |  |     ::ClangFixItOperation operation(diagnosticText, {semicolonFixItContainer}); | 
					
						
							| 
									
										
										
										
											2015-08-24 18:26:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-02 20:07:29 +01:00
										 |  |  |     operation.perform(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(operation, MatchText(unsavedFileContent(semicolonExpectedFile))); | 
					
						
							| 
									
										
										
										
											2015-08-24 18:26:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(ClangFixItOperation, ComparisonVersusAssignmentChooseComparison) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-27 10:45:51 +02:00
										 |  |  |     ::ClangFixItOperation operation(diagnosticText, {compareFixItContainer}); | 
					
						
							| 
									
										
										
										
											2015-12-02 20:07:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     operation.perform(); | 
					
						
							| 
									
										
										
										
											2015-08-24 18:26:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-02 20:07:29 +01:00
										 |  |  |     ASSERT_THAT(operation, MatchText(unsavedFileContent(compareExpected1File))); | 
					
						
							| 
									
										
										
										
											2015-08-24 18:26:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(ClangFixItOperation, ComparisonVersusAssignmentChooseParentheses) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-27 10:45:51 +02:00
										 |  |  |     ::ClangFixItOperation operation(diagnosticText, | 
					
						
							| 
									
										
										
										
											2015-08-24 18:26:09 +02:00
										 |  |  |                                     {assignmentFixItContainerParenLeft, | 
					
						
							|  |  |  |                                      assignmentFixItContainerParenRight}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-02 20:07:29 +01:00
										 |  |  |     operation.perform(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(operation, MatchText(unsavedFileContent(compareExpected2File))); | 
					
						
							| 
									
										
										
										
											2015-08-24 18:26:09 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |