2016-09-07 10:42:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://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 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.
|
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-09-15 17:41:41 +02:00
|
|
|
#include "googletest.h"
|
|
|
|
|
2016-09-09 14:25:59 +02:00
|
|
|
#include <clangexceptions.h>
|
2016-09-07 10:42:12 +02:00
|
|
|
#include <clangdocument.h>
|
|
|
|
#include <clangdocuments.h>
|
|
|
|
#include <unsavedfiles.h>
|
|
|
|
#include <utf8string.h>
|
|
|
|
|
|
|
|
#include <clang-c/Index.h>
|
|
|
|
|
|
|
|
using ClangBackEnd::Document;
|
|
|
|
using ClangBackEnd::UnsavedFiles;
|
|
|
|
|
|
|
|
using testing::IsNull;
|
|
|
|
using testing::NotNull;
|
|
|
|
using testing::Gt;
|
2016-09-15 14:38:42 +02:00
|
|
|
using testing::Eq;
|
2016-09-07 10:42:12 +02:00
|
|
|
using testing::Not;
|
|
|
|
using testing::Contains;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using ::testing::PrintToString;
|
|
|
|
|
2018-09-25 09:41:32 +02:00
|
|
|
MATCHER_P2(IsDocument, filePath, documentRevision,
|
2016-09-07 10:42:12 +02:00
|
|
|
std::string(negation ? "isn't" : "is")
|
|
|
|
+ " document with file path "+ PrintToString(filePath)
|
|
|
|
+ " and document revision " + PrintToString(documentRevision)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return arg.filePath() == filePath
|
|
|
|
&& arg.documentRevision() == documentRevision;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Documents : public ::testing::Test
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
void SetUp() override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
ClangBackEnd::UnsavedFiles unsavedFiles;
|
2018-09-25 09:41:32 +02:00
|
|
|
ClangBackEnd::Documents documents{unsavedFiles};
|
2016-09-07 10:42:12 +02:00
|
|
|
const Utf8String filePath = Utf8StringLiteral(TESTDATA_DIR"/translationunits.cpp");
|
2016-11-22 16:41:01 +01:00
|
|
|
const Utf8String otherFilePath = Utf8StringLiteral(TESTDATA_DIR"/translationunits.h");
|
2016-09-07 10:42:12 +02:00
|
|
|
const Utf8String headerPath = Utf8StringLiteral(TESTDATA_DIR"/translationunits.h");
|
|
|
|
const Utf8String nonExistingFilePath = Utf8StringLiteral("foo.cpp");
|
2018-09-25 09:41:32 +02:00
|
|
|
const ClangBackEnd::FileContainer fileContainer{filePath};
|
|
|
|
const ClangBackEnd::FileContainer headerContainer{headerPath};
|
2016-09-07 10:42:12 +02:00
|
|
|
};
|
|
|
|
|
2017-01-04 11:39:17 +01:00
|
|
|
using DocumentsSlowTest = Documents;
|
|
|
|
|
2016-09-07 10:42:12 +02:00
|
|
|
TEST_F(Documents, ThrowForGettingWithWrongFilePath)
|
|
|
|
{
|
2018-09-25 09:41:32 +02:00
|
|
|
ASSERT_THROW(documents.document(nonExistingFilePath),
|
2016-09-09 14:42:35 +02:00
|
|
|
ClangBackEnd::DocumentDoesNotExistException);
|
2016-09-07 10:42:12 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, ThrowForAddingNonExistingFile)
|
|
|
|
{
|
2018-09-25 09:41:32 +02:00
|
|
|
ClangBackEnd::FileContainer fileContainer(nonExistingFilePath);
|
2016-09-07 10:42:12 +02:00
|
|
|
|
|
|
|
ASSERT_THROW(documents.create({fileContainer}),
|
2016-09-09 14:42:35 +02:00
|
|
|
ClangBackEnd::DocumentFileDoesNotExistException);
|
2016-09-07 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, DoNotThrowForAddingNonExistingFileWithUnsavedContent)
|
|
|
|
{
|
2018-09-25 09:41:32 +02:00
|
|
|
ClangBackEnd::FileContainer fileContainer(nonExistingFilePath, Utf8String(), true);
|
2016-09-07 10:42:12 +02:00
|
|
|
|
|
|
|
ASSERT_NO_THROW(documents.create({fileContainer}));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, Add)
|
|
|
|
{
|
2018-10-19 10:20:27 +02:00
|
|
|
ClangBackEnd::FileContainer fileContainer(filePath, {}, {}, 74u);
|
2016-09-07 10:42:12 +02:00
|
|
|
|
|
|
|
documents.create({fileContainer});
|
|
|
|
|
2018-09-25 09:41:32 +02:00
|
|
|
ASSERT_THAT(documents.document(filePath),
|
|
|
|
IsDocument(filePath, 74u));
|
2016-09-07 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 16:41:01 +01:00
|
|
|
TEST_F(Documents, CreateWithUnsavedContentSetsDependenciesDirty)
|
|
|
|
{
|
2018-10-19 10:20:27 +02:00
|
|
|
ClangBackEnd::FileContainer fileContainer(filePath, {}, {}, 74u);
|
|
|
|
ClangBackEnd::FileContainer fileContainerWithUnsavedContent(otherFilePath, {}, {}, Utf8String(), true, 2u);
|
2016-11-22 16:41:01 +01:00
|
|
|
auto dependentDocument = documents.create({fileContainer}).at(0);
|
|
|
|
dependentDocument.setDependedFilePaths(QSet<Utf8String>() << filePath << otherFilePath);
|
|
|
|
|
|
|
|
documents.create({fileContainerWithUnsavedContent});
|
|
|
|
|
2017-05-03 10:10:50 +02:00
|
|
|
ASSERT_TRUE(dependentDocument.isDirty());
|
2016-11-22 16:41:01 +01:00
|
|
|
}
|
|
|
|
|
2016-09-07 10:42:12 +02:00
|
|
|
TEST_F(Documents, AddAndTestCreatedTranslationUnit)
|
|
|
|
{
|
2018-10-19 10:20:27 +02:00
|
|
|
ClangBackEnd::FileContainer fileContainer(filePath, {}, {}, 74u);
|
2016-09-07 10:42:12 +02:00
|
|
|
|
|
|
|
auto createdDocuments = documents.create({fileContainer});
|
|
|
|
|
2018-09-25 09:41:32 +02:00
|
|
|
ASSERT_THAT(createdDocuments.front(), IsDocument(filePath, 74u));
|
2016-09-07 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, ThrowForCreatingAnExistingDocument)
|
|
|
|
{
|
2018-10-19 10:20:27 +02:00
|
|
|
ClangBackEnd::FileContainer fileContainer(filePath, {}, {}, 74u);
|
2016-09-07 10:42:12 +02:00
|
|
|
documents.create({fileContainer});
|
|
|
|
|
2018-09-25 09:41:32 +02:00
|
|
|
ASSERT_THROW(documents.create({fileContainer}), ClangBackEnd::DocumentAlreadyExistsException);
|
2016-09-07 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, ThrowForUpdatingANonExistingDocument)
|
|
|
|
{
|
2018-10-19 10:20:27 +02:00
|
|
|
ClangBackEnd::FileContainer fileContainer(filePath, {}, {}, 74u);
|
2016-09-07 10:42:12 +02:00
|
|
|
ASSERT_THROW(documents.update({fileContainer}),
|
2016-09-09 14:42:35 +02:00
|
|
|
ClangBackEnd::DocumentDoesNotExistException);
|
2016-09-07 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, UpdateSingle)
|
|
|
|
{
|
2018-10-19 10:20:27 +02:00
|
|
|
ClangBackEnd::FileContainer createFileContainer(filePath, {}, {}, 74u);
|
|
|
|
ClangBackEnd::FileContainer updateFileContainer(filePath, {}, {}, 75u);
|
2016-09-07 10:42:12 +02:00
|
|
|
documents.create({createFileContainer});
|
|
|
|
|
|
|
|
documents.update({updateFileContainer});
|
|
|
|
|
2018-09-25 09:41:32 +02:00
|
|
|
ASSERT_THAT(documents.document(filePath), IsDocument(filePath, 75u));
|
2016-09-07 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
2016-09-15 14:38:42 +02:00
|
|
|
TEST_F(Documents, UpdateReturnsUpdatedDocument)
|
|
|
|
{
|
2018-10-19 10:20:27 +02:00
|
|
|
ClangBackEnd::FileContainer createFileContainer(filePath, {}, {}, 74u);
|
|
|
|
ClangBackEnd::FileContainer updateFileContainer(filePath, {}, {}, 75u);
|
2016-09-15 14:38:42 +02:00
|
|
|
documents.create({createFileContainer});
|
|
|
|
|
|
|
|
const std::vector<Document> updatedDocuments = documents.update({updateFileContainer});
|
|
|
|
|
|
|
|
ASSERT_THAT(updatedDocuments.size(), Eq(1u));
|
|
|
|
ASSERT_THAT(updatedDocuments.front().documentRevision(), Eq(75u));
|
|
|
|
}
|
|
|
|
|
2018-09-25 09:41:32 +02:00
|
|
|
// TODO: Does this test still makes sense?
|
2016-09-07 10:42:12 +02:00
|
|
|
TEST_F(Documents, UpdateMultiple)
|
|
|
|
{
|
2018-10-19 10:20:27 +02:00
|
|
|
ClangBackEnd::FileContainer fileContainer(filePath, {}, {}, 74u);
|
|
|
|
ClangBackEnd::FileContainer fileContainerWithOtherProject(filePath, {}, {}, 74u);
|
|
|
|
ClangBackEnd::FileContainer updatedFileContainer(filePath, {}, {}, 75u);
|
2016-09-07 10:42:12 +02:00
|
|
|
documents.create({fileContainer, fileContainerWithOtherProject});
|
|
|
|
|
|
|
|
documents.update({updatedFileContainer});
|
|
|
|
|
2018-09-25 09:41:32 +02:00
|
|
|
ASSERT_THAT(documents.document(filePath), IsDocument(filePath, 75u));
|
2016-09-07 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 11:39:17 +01:00
|
|
|
TEST_F(DocumentsSlowTest, UpdateUnsavedFileAndCheckForReparse)
|
2016-09-07 10:42:12 +02:00
|
|
|
{
|
2018-10-19 10:20:27 +02:00
|
|
|
ClangBackEnd::FileContainer fileContainer(filePath, {}, {}, 74u);
|
|
|
|
ClangBackEnd::FileContainer headerContainer(headerPath, {}, {}, 74u);
|
2018-09-25 09:41:32 +02:00
|
|
|
ClangBackEnd::FileContainer headerContainerWithUnsavedContent(headerPath, Utf8String(), true, 75u);
|
2016-09-07 10:42:12 +02:00
|
|
|
documents.create({fileContainer, headerContainer});
|
2018-09-25 09:41:32 +02:00
|
|
|
Document document = documents.document(filePath);
|
2016-09-07 10:42:12 +02:00
|
|
|
document.parse();
|
|
|
|
|
|
|
|
documents.update({headerContainerWithUnsavedContent});
|
|
|
|
|
2018-09-25 09:41:32 +02:00
|
|
|
ASSERT_TRUE(documents.document(filePath).isDirty());
|
2016-09-07 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 11:39:17 +01:00
|
|
|
TEST_F(DocumentsSlowTest, RemoveFileAndCheckForReparse)
|
2016-09-07 10:42:12 +02:00
|
|
|
{
|
2018-10-19 10:20:27 +02:00
|
|
|
ClangBackEnd::FileContainer fileContainer(filePath, {}, {}, 74u);
|
|
|
|
ClangBackEnd::FileContainer headerContainer(headerPath, {}, {}, 74u);
|
2018-09-25 09:41:32 +02:00
|
|
|
ClangBackEnd::FileContainer headerContainerWithUnsavedContent(headerPath, Utf8String(), true, 75u);
|
2016-09-07 10:42:12 +02:00
|
|
|
documents.create({fileContainer, headerContainer});
|
2018-09-25 09:41:32 +02:00
|
|
|
Document document = documents.document(filePath);
|
2016-09-07 10:42:12 +02:00
|
|
|
document.parse();
|
|
|
|
|
|
|
|
documents.remove({headerContainerWithUnsavedContent});
|
|
|
|
|
2018-09-25 09:41:32 +02:00
|
|
|
ASSERT_TRUE(documents.document(filePath).isDirty());
|
2016-09-07 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, DontGetNewerFileContainerIfRevisionIsTheSame)
|
|
|
|
{
|
2018-10-19 10:20:27 +02:00
|
|
|
ClangBackEnd::FileContainer fileContainer(filePath, {}, {}, 74u);
|
2016-09-07 10:42:12 +02:00
|
|
|
documents.create({fileContainer});
|
|
|
|
|
|
|
|
auto newerFileContainers = documents.newerFileContainers({fileContainer});
|
|
|
|
|
|
|
|
ASSERT_THAT(newerFileContainers.size(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, GetNewerFileContainerIfRevisionIsDifferent)
|
|
|
|
{
|
2018-10-19 10:20:27 +02:00
|
|
|
ClangBackEnd::FileContainer fileContainer(filePath, {}, {}, 74u);
|
|
|
|
ClangBackEnd::FileContainer newerContainer(filePath, {}, {}, 75u);
|
2016-09-07 10:42:12 +02:00
|
|
|
documents.create({fileContainer});
|
|
|
|
|
|
|
|
auto newerFileContainers = documents.newerFileContainers({newerContainer});
|
|
|
|
|
|
|
|
ASSERT_THAT(newerFileContainers.size(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, ThrowForRemovingWithWrongFilePath)
|
|
|
|
{
|
2018-09-25 09:41:32 +02:00
|
|
|
ClangBackEnd::FileContainer fileContainer(nonExistingFilePath);
|
2016-09-07 10:42:12 +02:00
|
|
|
|
|
|
|
ASSERT_THROW(documents.remove({fileContainer}),
|
2016-09-09 14:42:35 +02:00
|
|
|
ClangBackEnd::DocumentDoesNotExistException);
|
2016-09-07 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, Remove)
|
|
|
|
{
|
2018-09-25 09:41:32 +02:00
|
|
|
ClangBackEnd::FileContainer fileContainer(filePath);
|
2016-09-07 10:42:12 +02:00
|
|
|
documents.create({fileContainer});
|
|
|
|
|
|
|
|
documents.remove({fileContainer});
|
|
|
|
|
2018-09-25 09:41:32 +02:00
|
|
|
ASSERT_THROW(documents.document(filePath),
|
2016-09-09 14:42:35 +02:00
|
|
|
ClangBackEnd::DocumentDoesNotExistException);
|
2016-09-07 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, RemoveAllValidIfExceptionIsThrown)
|
|
|
|
{
|
2018-09-25 09:41:32 +02:00
|
|
|
ClangBackEnd::FileContainer fileContainer(filePath);
|
2016-09-07 10:42:12 +02:00
|
|
|
documents.create({fileContainer});
|
|
|
|
|
2018-09-25 09:41:32 +02:00
|
|
|
ASSERT_THROW(documents.remove({ClangBackEnd::FileContainer(Utf8StringLiteral("dontextist.pro")), fileContainer}),
|
2016-09-09 14:42:35 +02:00
|
|
|
ClangBackEnd::DocumentDoesNotExistException);
|
2016-09-07 10:42:12 +02:00
|
|
|
|
|
|
|
ASSERT_THAT(documents.documents(),
|
2018-10-19 10:20:27 +02:00
|
|
|
Not(Contains(Document(filePath, {}, {}, documents))));
|
2016-09-07 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, HasDocument)
|
|
|
|
{
|
2018-09-25 09:41:32 +02:00
|
|
|
documents.create({{filePath}});
|
2016-09-07 10:42:12 +02:00
|
|
|
|
2018-09-25 09:41:32 +02:00
|
|
|
ASSERT_TRUE(documents.hasDocument(filePath));
|
2016-09-07 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, HasNotDocument)
|
|
|
|
{
|
2018-09-25 09:41:32 +02:00
|
|
|
ASSERT_FALSE(documents.hasDocument(filePath));
|
2016-09-07 10:42:12 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 13:05:44 +02:00
|
|
|
TEST_F(Documents, FilteredPositive)
|
|
|
|
{
|
2018-09-25 09:41:32 +02:00
|
|
|
documents.create({{filePath}});
|
2016-10-14 13:05:44 +02:00
|
|
|
const auto isMatchingFilePath = [this](const Document &document) {
|
|
|
|
return document.filePath() == filePath;
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool hasMatches = !documents.filtered(isMatchingFilePath).empty();
|
|
|
|
|
|
|
|
ASSERT_TRUE(hasMatches);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, FilteredNegative)
|
|
|
|
{
|
2018-09-25 09:41:32 +02:00
|
|
|
documents.create({{filePath}});
|
2016-10-14 13:05:44 +02:00
|
|
|
const auto isMatchingNothing = [](const Document &) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool hasMatches = !documents.filtered(isMatchingNothing).empty();
|
|
|
|
|
|
|
|
ASSERT_FALSE(hasMatches);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, DirtyAndVisibleButNotCurrentDocuments)
|
|
|
|
{
|
2018-09-25 09:41:32 +02:00
|
|
|
documents.create({{filePath}});
|
2016-10-14 13:05:44 +02:00
|
|
|
documents.updateDocumentsWithChangedDependency(filePath);
|
|
|
|
documents.setVisibleInEditors({filePath});
|
|
|
|
documents.setUsedByCurrentEditor(Utf8String());
|
|
|
|
|
|
|
|
const bool hasMatches = !documents.dirtyAndVisibleButNotCurrentDocuments().empty();
|
|
|
|
|
|
|
|
ASSERT_TRUE(hasMatches);
|
|
|
|
}
|
|
|
|
|
2016-09-07 10:42:12 +02:00
|
|
|
TEST_F(Documents, isUsedByCurrentEditor)
|
|
|
|
{
|
|
|
|
documents.create({fileContainer});
|
|
|
|
auto document = documents.document(fileContainer);
|
|
|
|
|
|
|
|
documents.setUsedByCurrentEditor(filePath);
|
|
|
|
|
|
|
|
ASSERT_TRUE(document.isUsedByCurrentEditor());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, IsNotCurrentEditor)
|
|
|
|
{
|
|
|
|
documents.create({fileContainer});
|
|
|
|
auto document = documents.document(fileContainer);
|
|
|
|
|
|
|
|
documents.setUsedByCurrentEditor(headerPath);
|
|
|
|
|
|
|
|
ASSERT_FALSE(document.isUsedByCurrentEditor());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, IsNotCurrentEditorAfterBeingCurrent)
|
|
|
|
{
|
|
|
|
documents.create({fileContainer});
|
|
|
|
auto document = documents.document(fileContainer);
|
|
|
|
documents.setUsedByCurrentEditor(filePath);
|
|
|
|
|
|
|
|
documents.setUsedByCurrentEditor(headerPath);
|
|
|
|
|
|
|
|
ASSERT_FALSE(document.isUsedByCurrentEditor());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, IsVisibleEditor)
|
|
|
|
{
|
|
|
|
documents.create({fileContainer});
|
|
|
|
auto document = documents.document(fileContainer);
|
|
|
|
|
|
|
|
documents.setVisibleInEditors({filePath});
|
|
|
|
|
|
|
|
ASSERT_TRUE(document.isVisibleInEditor());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, IsNotVisibleEditor)
|
|
|
|
{
|
|
|
|
documents.create({fileContainer});
|
|
|
|
auto document = documents.document(fileContainer);
|
|
|
|
|
|
|
|
documents.setVisibleInEditors({headerPath});
|
|
|
|
|
|
|
|
ASSERT_FALSE(document.isVisibleInEditor());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(Documents, IsNotVisibleEditorAfterBeingVisible)
|
|
|
|
{
|
|
|
|
documents.create({fileContainer});
|
|
|
|
auto document = documents.document(fileContainer);
|
|
|
|
documents.setVisibleInEditors({filePath});
|
|
|
|
|
|
|
|
documents.setVisibleInEditors({headerPath});
|
|
|
|
|
|
|
|
ASSERT_FALSE(document.isVisibleInEditor());
|
|
|
|
}
|
|
|
|
|
2018-09-25 09:41:32 +02:00
|
|
|
// TODO: Remove?
|
2016-09-07 10:42:12 +02:00
|
|
|
void Documents::SetUp()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|