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
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
#include "gmock/gmock-matchers.h"
|
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
|
#include "gtest-qt-printing.h"
|
|
|
|
|
|
|
|
|
|
#include <unsavedfiles.h>
|
|
|
|
|
#include <filecontainer.h>
|
|
|
|
|
|
|
|
|
|
#include <QVector>
|
|
|
|
|
|
2015-06-16 11:56:00 +02:00
|
|
|
using ClangBackEnd::UnsavedFiles;
|
|
|
|
|
using ClangBackEnd::FileContainer;
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
using ::testing::IsNull;
|
|
|
|
|
using ::testing::NotNull;
|
|
|
|
|
using ::testing::Gt;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2015-06-16 11:56:00 +02:00
|
|
|
bool operator==(const ClangBackEnd::FileContainer &fileContainer, const CXUnsavedFile &cxUnsavedFile)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
return fileContainer.filePath() == Utf8String::fromUtf8(cxUnsavedFile.Filename)
|
|
|
|
|
&& fileContainer.unsavedFileContent() == Utf8String(cxUnsavedFile.Contents, cxUnsavedFile.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool fileContainersContainsItemMatchingToCxUnsavedFile(const QVector<FileContainer> &fileContainers, const CXUnsavedFile &cxUnsavedFile)
|
|
|
|
|
{
|
|
|
|
|
for (const FileContainer &fileContainer : fileContainers)
|
|
|
|
|
if (fileContainer == cxUnsavedFile)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const CXUnsavedFile &cxUnsavedFile : unsavedFiles.cxUnsavedFileVector()) {
|
|
|
|
|
if (!fileContainersContainsItemMatchingToCxUnsavedFile(fileContainers, cxUnsavedFile))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class UnsavedFiles : public ::testing::Test
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
void TearDown() override;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
::UnsavedFiles unsavedFiles;
|
2015-08-31 12:40:14 +02:00
|
|
|
Utf8String filePath{Utf8StringLiteral("file.cpp")};
|
|
|
|
|
Utf8String projectPartId{Utf8StringLiteral("projectPartId")};
|
|
|
|
|
|
|
|
|
|
Utf8String unsavedContent1{Utf8StringLiteral("foo")};
|
|
|
|
|
Utf8String unsavedContent2{Utf8StringLiteral("bar")};
|
2015-06-01 18:51:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void UnsavedFiles::TearDown()
|
|
|
|
|
{
|
|
|
|
|
unsavedFiles.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(UnsavedFiles, DoNothingForUpdateIfFileHasNoUnsavedContent)
|
|
|
|
|
{
|
2015-08-31 12:40:14 +02:00
|
|
|
QVector<FileContainer> fileContainers({FileContainer(filePath, projectPartId)});
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
unsavedFiles.createOrUpdate(fileContainers);
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(unsavedFiles, HasUnsavedFiles(QVector<FileContainer>()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(UnsavedFiles, AddUnsavedFileForUpdateWithUnsavedContent)
|
|
|
|
|
{
|
2015-08-31 12:40:14 +02:00
|
|
|
QVector<FileContainer> fileContainers({FileContainer(filePath, projectPartId),
|
|
|
|
|
FileContainer(filePath, projectPartId, unsavedContent1, true)});
|
2015-06-01 18:51:55 +02:00
|
|
|
unsavedFiles.createOrUpdate(fileContainers);
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
ASSERT_THAT(unsavedFiles, HasUnsavedFiles(QVector<FileContainer>({FileContainer(filePath, projectPartId, unsavedContent1, true)})));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(UnsavedFiles, RemoveUnsavedFileForUpdateWithUnsavedContent)
|
|
|
|
|
{
|
2015-08-31 12:40:14 +02:00
|
|
|
QVector<FileContainer> fileContainers({FileContainer(filePath, projectPartId, unsavedContent1, true),
|
|
|
|
|
FileContainer(filePath, projectPartId)});
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
unsavedFiles.createOrUpdate(fileContainers);
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(unsavedFiles, HasUnsavedFiles(QVector<FileContainer>()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(UnsavedFiles, ExchangeUnsavedFileForUpdateWithUnsavedContent)
|
|
|
|
|
{
|
2015-08-31 12:40:14 +02:00
|
|
|
QVector<FileContainer> fileContainers({FileContainer(filePath, projectPartId, unsavedContent1, true),
|
|
|
|
|
FileContainer(filePath, projectPartId, unsavedContent2, true)});
|
2015-06-01 18:51:55 +02:00
|
|
|
unsavedFiles.createOrUpdate(fileContainers);
|
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
ASSERT_THAT(unsavedFiles, HasUnsavedFiles(QVector<FileContainer>({FileContainer(filePath, projectPartId, unsavedContent2, true)})));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(UnsavedFiles, TimeStampIsUpdatedAsUnsavedFilesChanged)
|
|
|
|
|
{
|
2015-08-31 12:40:14 +02:00
|
|
|
QVector<FileContainer> fileContainers({FileContainer(filePath, projectPartId, unsavedContent1, true),
|
|
|
|
|
FileContainer(filePath, projectPartId, 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)
|
|
|
|
|
{
|
2015-08-31 12:40:14 +02:00
|
|
|
QVector<FileContainer> fileContainers({FileContainer(filePath, projectPartId, unsavedContent1, true)});
|
2015-06-01 18:51:55 +02:00
|
|
|
unsavedFiles.createOrUpdate(fileContainers);
|
|
|
|
|
|
|
|
|
|
unsavedFiles.remove(fileContainers);
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(unsavedFiles, HasUnsavedFiles(QVector<FileContainer>()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|