| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | /****************************************************************************
 | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** Copyright (C) 2015 The Qt Company Ltd. | 
					
						
							|  |  |  | ** 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 <cmbalivemessage.h>
 | 
					
						
							|  |  |  | #include <cmbcodecompletedmessage.h>
 | 
					
						
							|  |  |  | #include <cmbcompletecodemessage.h>
 | 
					
						
							|  |  |  | #include <cmbendmessage.h>
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | #include <cmbmessages.h>
 | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | #include <cmbregistertranslationunitsforcodecompletionmessage.h>
 | 
					
						
							|  |  |  | #include <cmbunregistertranslationunitsforcodecompletionmessage.h>
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | #include <diagnosticcontainer.h>
 | 
					
						
							|  |  |  | #include <diagnosticschangedmessage.h>
 | 
					
						
							|  |  |  | #include <requestdiagnosticsmessage.h>
 | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | #include <readmessageblock.h>
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | #include <sourcelocation.h>
 | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | #include <writemessageblock.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QBuffer>
 | 
					
						
							|  |  |  | #include <QString>
 | 
					
						
							|  |  |  | #include <QVariant>
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | #include <vector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gmock/gmock.h>
 | 
					
						
							|  |  |  | #include <gmock/gmock-matchers.h>
 | 
					
						
							|  |  |  | #include <gtest/gtest.h>
 | 
					
						
							|  |  |  | #include "gtest-qt-printing.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace testing; | 
					
						
							|  |  |  | namespace CodeModelBackeEndTest { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | class ReadAndWriteMessageBlock : public ::testing::Test | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  |     ReadAndWriteMessageBlock(); | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual void SetUp() override; | 
					
						
							|  |  |  |     virtual void TearDown() override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     template<class Type> | 
					
						
							|  |  |  |     void CompareMessage(const Type &message); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     QVariant writeCodeCompletedMessage(); | 
					
						
							|  |  |  |     void popLastCharacterFromBuffer(); | 
					
						
							|  |  |  |     void pushLastCharacterToBuffer(); | 
					
						
							|  |  |  |     void readPartialMessage(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |     QBuffer buffer; | 
					
						
							|  |  |  |     ClangBackEnd::WriteMessageBlock writeMessageBlock; | 
					
						
							|  |  |  |     ClangBackEnd::ReadMessageBlock readMessageBlock; | 
					
						
							|  |  |  |     char lastCharacter = 0; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | ReadAndWriteMessageBlock::ReadAndWriteMessageBlock() | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  |     :  writeMessageBlock(&buffer), | 
					
						
							|  |  |  |        readMessageBlock(&buffer) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | void ReadAndWriteMessageBlock::SetUp() | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     buffer.open(QIODevice::ReadWrite); | 
					
						
							|  |  |  |     writeMessageBlock = ClangBackEnd::WriteMessageBlock(&buffer); | 
					
						
							|  |  |  |     readMessageBlock = ClangBackEnd::ReadMessageBlock(&buffer); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | void ReadAndWriteMessageBlock::TearDown() | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     buffer.close(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | TEST_F(ReadAndWriteMessageBlock, WriteMessageAndTestSize) | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     writeMessageBlock.write(QVariant::fromValue(ClangBackEnd::EndMessage())); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_EQ(46, buffer.size()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | TEST_F(ReadAndWriteMessageBlock, WriteSecondMessageAndTestSize) | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     writeMessageBlock.write(QVariant::fromValue(ClangBackEnd::EndMessage())); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_EQ(46, buffer.size()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | TEST_F(ReadAndWriteMessageBlock, WriteTwoMessagesAndTestCount) | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     writeMessageBlock.write(QVariant::fromValue(ClangBackEnd::EndMessage())); | 
					
						
							|  |  |  |     writeMessageBlock.write(QVariant::fromValue(ClangBackEnd::EndMessage())); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_EQ(2, writeMessageBlock.counter()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | TEST_F(ReadAndWriteMessageBlock, ReadThreeMessagesAndTestCount) | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     writeMessageBlock.write(QVariant::fromValue(ClangBackEnd::EndMessage())); | 
					
						
							|  |  |  |     writeMessageBlock.write(QVariant::fromValue(ClangBackEnd::EndMessage())); | 
					
						
							|  |  |  |     writeMessageBlock.write(QVariant::fromValue(ClangBackEnd::EndMessage())); | 
					
						
							|  |  |  |     buffer.seek(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_EQ(3, readMessageBlock.readAll().count()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | TEST_F(ReadAndWriteMessageBlock, CompareEndMessage) | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     CompareMessage(ClangBackEnd::EndMessage()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | TEST_F(ReadAndWriteMessageBlock, CompareAliveMessage) | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     CompareMessage(ClangBackEnd::AliveMessage()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | TEST_F(ReadAndWriteMessageBlock, CompareRegisterTranslationUnitForCodeCompletionMessage) | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     ClangBackEnd::FileContainer fileContainer(Utf8StringLiteral("foo.cpp"), Utf8StringLiteral("pathToProject.pro")); | 
					
						
							|  |  |  |     QVector<ClangBackEnd::FileContainer> fileContainers({fileContainer}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     CompareMessage(ClangBackEnd::RegisterTranslationUnitForCodeCompletionMessage(fileContainers)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | TEST_F(ReadAndWriteMessageBlock, CompareUnregisterFileForCodeCompletionMessage) | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     ClangBackEnd::FileContainer fileContainer(Utf8StringLiteral("foo.cpp"), Utf8StringLiteral("pathToProject.pro")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     CompareMessage(ClangBackEnd::UnregisterTranslationUnitsForCodeCompletionMessage({fileContainer})); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | TEST_F(ReadAndWriteMessageBlock, CompareCompleteCodeMessage) | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     CompareMessage(ClangBackEnd::CompleteCodeMessage(Utf8StringLiteral("foo.cpp"), 24, 33, Utf8StringLiteral("do what I want"))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | TEST_F(ReadAndWriteMessageBlock, CompareCodeCompletedMessage) | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     ClangBackEnd::CodeCompletions codeCompletions({Utf8StringLiteral("newFunction()")}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     CompareMessage(ClangBackEnd::CodeCompletedMessage(codeCompletions, 1)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | TEST_F(ReadAndWriteMessageBlock, CompareDiagnosticsChangedMessage) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ClangBackEnd::FileContainer fileContainer(Utf8StringLiteral("foo.cpp"), | 
					
						
							|  |  |  |                                               Utf8StringLiteral("projectId")); | 
					
						
							|  |  |  |     ClangBackEnd::DiagnosticContainer container(Utf8StringLiteral("don't do that"), | 
					
						
							|  |  |  |                                                 Utf8StringLiteral("warning"), | 
					
						
							|  |  |  |                                                 {Utf8StringLiteral("-Wpadded"), Utf8StringLiteral("-Wno-padded")}, | 
					
						
							|  |  |  |                                                 ClangBackEnd::DiagnosticSeverity::Warning, | 
					
						
							|  |  |  |                                                 {Utf8StringLiteral("foo.cpp"), 20u, 103u}, | 
					
						
							|  |  |  |                                                 {{{Utf8StringLiteral("foo.cpp"), 20u, 103u}, {Utf8StringLiteral("foo.cpp"), 20u, 110u}}}, | 
					
						
							|  |  |  |                                                 {}, | 
					
						
							|  |  |  |                                                 {}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     CompareMessage(ClangBackEnd::DiagnosticsChangedMessage(fileContainer, | 
					
						
							| 
									
										
										
										
											2015-08-31 12:40:14 +02:00
										 |  |  |                                                            {container})); | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(ReadAndWriteMessageBlock, RequestDiagnosticsMessage) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ClangBackEnd::FileContainer fileContainer(Utf8StringLiteral("foo.cpp"), Utf8StringLiteral("pathToProject.pro")); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 12:40:14 +02:00
										 |  |  |     CompareMessage(ClangBackEnd::RequestDiagnosticsMessage(fileContainer)); | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TEST_F(ReadAndWriteMessageBlock, GetInvalidMessageForAPartialBuffer) | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     writeCodeCompletedMessage(); | 
					
						
							|  |  |  |     popLastCharacterFromBuffer(); | 
					
						
							|  |  |  |     buffer.seek(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     readPartialMessage(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | TEST_F(ReadAndWriteMessageBlock, ReadMessageAfterInterruption) | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     const QVariant writeMessage = writeCodeCompletedMessage(); | 
					
						
							|  |  |  |     popLastCharacterFromBuffer(); | 
					
						
							|  |  |  |     buffer.seek(0); | 
					
						
							|  |  |  |     readPartialMessage(); | 
					
						
							|  |  |  |     pushLastCharacterToBuffer(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_EQ(readMessageBlock.read(), writeMessage); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | QVariant ReadAndWriteMessageBlock::writeCodeCompletedMessage() | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     ClangBackEnd::CodeCompletedMessage message(ClangBackEnd::CodeCompletions({Utf8StringLiteral("newFunction()")}), 1); | 
					
						
							|  |  |  |     const QVariant writeMessage = QVariant::fromValue(message); | 
					
						
							|  |  |  |     writeMessageBlock.write(writeMessage); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return writeMessage; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | void ReadAndWriteMessageBlock::popLastCharacterFromBuffer() | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     auto &internalBuffer = buffer.buffer(); | 
					
						
							|  |  |  |     lastCharacter = internalBuffer.at(internalBuffer.size() - 1); | 
					
						
							|  |  |  |     internalBuffer.chop(1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | void ReadAndWriteMessageBlock::pushLastCharacterToBuffer() | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     buffer.buffer().push_back(lastCharacter); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | void ReadAndWriteMessageBlock::readPartialMessage() | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     QVariant readMessage = readMessageBlock.read(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_FALSE(readMessage.isValid()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<class Type> | 
					
						
							| 
									
										
										
										
											2015-08-31 16:28:26 +02:00
										 |  |  | void ReadAndWriteMessageBlock::CompareMessage(const Type &message) | 
					
						
							| 
									
										
										
										
											2015-08-19 12:36:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     const QVariant writeMessage = QVariant::fromValue(message); | 
					
						
							|  |  |  |     writeMessageBlock.write(writeMessage); | 
					
						
							|  |  |  |     buffer.seek(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const QVariant readMessage = readMessageBlock.read(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_EQ(writeMessage, readMessage); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |