2015-06-01 18:51:55 +02:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2015 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
** 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 <codecompleter.h>
|
|
|
|
#include <filecontainer.h>
|
2015-08-04 15:04:41 +02:00
|
|
|
#include <projectpart.h>
|
2015-08-31 12:40:14 +02:00
|
|
|
#include <projects.h>
|
2015-06-01 18:51:55 +02:00
|
|
|
#include <translationunit.h>
|
2015-08-31 12:40:14 +02:00
|
|
|
#include <translationunits.h>
|
2015-06-01 18:51:55 +02:00
|
|
|
#include <unsavedfiles.h>
|
|
|
|
#include <utf8stringvector.h>
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
#include <QCoreApplication>
|
2015-06-01 18:51:55 +02:00
|
|
|
#include <QFile>
|
2015-08-04 15:04:41 +02:00
|
|
|
#include <QTemporaryDir>
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2015-08-04 15:04:41 +02:00
|
|
|
#include <gmock/gmock.h>
|
|
|
|
#include <gmock/gmock-matchers.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "gtest-qt-printing.h"
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
using ::testing::ElementsAreArray;
|
|
|
|
using ::testing::Contains;
|
|
|
|
using ::testing::AllOf;
|
|
|
|
using ::testing::Not;
|
2015-08-04 15:04:41 +02:00
|
|
|
using ::testing::PrintToString;
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2015-06-16 11:56:00 +02:00
|
|
|
using ClangBackEnd::CodeCompletion;
|
|
|
|
using ClangBackEnd::CodeCompleter;
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-08-04 15:04:41 +02:00
|
|
|
MATCHER_P2(IsCodeCompletion, text, completionKind,
|
|
|
|
std::string(negation ? "isn't" : "is") + " code completion with text "
|
|
|
|
+ PrintToString(text) + " and kind " + PrintToString(completionKind)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (arg.text() != text) {
|
|
|
|
*result_listener << "text is " + PrintToString(arg.text()) + " and not " + PrintToString(text);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg.completionKind() != completionKind) {
|
|
|
|
*result_listener << "kind is " + PrintToString(arg.completionKind()) + " and not " + PrintToString(completionKind);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
class CodeCompleter : public ::testing::Test
|
|
|
|
{
|
2015-08-04 15:04:41 +02:00
|
|
|
protected:
|
|
|
|
void SetUp();
|
|
|
|
void copyTargetHeaderToTemporaryIncludeDirecory();
|
|
|
|
void copyChangedTargetHeaderToTemporaryIncludeDirecory();
|
|
|
|
static Utf8String readFileContent(const QString &fileName);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
protected:
|
2015-08-04 15:04:41 +02:00
|
|
|
QTemporaryDir includeDirectory;
|
2015-08-31 12:40:14 +02:00
|
|
|
Utf8String includePath{QStringLiteral("-I") + includeDirectory.path()};
|
2015-08-04 15:04:41 +02:00
|
|
|
QString targetHeaderPath{includeDirectory.path() + QStringLiteral("/complete_target_header.h")};
|
2015-08-31 12:40:14 +02:00
|
|
|
ClangBackEnd::ProjectPartContainer projectPart{Utf8StringLiteral("projectPartId"), {includePath}};
|
|
|
|
ClangBackEnd::FileContainer mainFileContainer{Utf8StringLiteral(TESTDATA_DIR"/complete_completer_main.cpp"),
|
|
|
|
projectPart.projectPartId()};
|
|
|
|
ClangBackEnd::ProjectParts projects;
|
2015-08-04 15:04:41 +02:00
|
|
|
ClangBackEnd::UnsavedFiles unsavedFiles;
|
2015-08-31 12:40:14 +02:00
|
|
|
ClangBackEnd::TranslationUnits translationUnits{projects, unsavedFiles};
|
|
|
|
ClangBackEnd::TranslationUnit translationUnit;
|
|
|
|
ClangBackEnd::CodeCompleter completer;
|
|
|
|
ClangBackEnd::FileContainer unsavedMainFileContainer{mainFileContainer.filePath(),
|
2015-08-04 15:04:41 +02:00
|
|
|
projectPart.projectPartId(),
|
|
|
|
readFileContent(QStringLiteral("/complete_completer_main_unsaved.cpp")),
|
|
|
|
true};
|
|
|
|
ClangBackEnd::FileContainer unsavedTargetHeaderFileContainer{targetHeaderPath,
|
|
|
|
projectPart.projectPartId(),
|
|
|
|
readFileContent(QStringLiteral("/complete_target_header_unsaved.h")),
|
|
|
|
true};
|
2015-06-01 18:51:55 +02:00
|
|
|
};
|
|
|
|
|
2015-08-04 15:04:41 +02:00
|
|
|
Utf8String CodeCompleter::readFileContent(const QString &fileName)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2015-08-04 15:04:41 +02:00
|
|
|
QFile readFileContentFile(QStringLiteral(TESTDATA_DIR) + fileName);
|
|
|
|
bool hasOpened = readFileContentFile.open(QIODevice::ReadOnly | QIODevice::Text);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2015-08-04 15:04:41 +02:00
|
|
|
EXPECT_TRUE(hasOpened);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2015-08-04 15:04:41 +02:00
|
|
|
return Utf8String::fromByteArray(readFileContentFile.readAll());
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
2015-08-04 15:04:41 +02:00
|
|
|
void CodeCompleter::copyTargetHeaderToTemporaryIncludeDirecory()
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2015-08-04 15:04:41 +02:00
|
|
|
QFile::remove(targetHeaderPath);
|
|
|
|
bool hasCopied = QFile::copy(QStringLiteral(TESTDATA_DIR "/complete_target_header.h"),
|
|
|
|
targetHeaderPath);
|
|
|
|
EXPECT_TRUE(hasCopied);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
2015-08-04 15:04:41 +02:00
|
|
|
void CodeCompleter::copyChangedTargetHeaderToTemporaryIncludeDirecory()
|
|
|
|
{
|
|
|
|
QFile::remove(targetHeaderPath);
|
|
|
|
bool hasCopied = QFile::copy(QStringLiteral(TESTDATA_DIR "/complete_target_header_changed.h"),
|
|
|
|
targetHeaderPath);
|
|
|
|
EXPECT_TRUE(hasCopied);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeCompleter::SetUp()
|
|
|
|
{
|
|
|
|
EXPECT_TRUE(includeDirectory.isValid());
|
2015-08-31 12:40:14 +02:00
|
|
|
projects.createOrUpdate({projectPart});
|
|
|
|
translationUnits.createOrUpdate({mainFileContainer});
|
|
|
|
translationUnit = translationUnits.translationUnit(mainFileContainer);
|
|
|
|
completer = ClangBackEnd::CodeCompleter(translationUnit);
|
2015-08-04 15:04:41 +02:00
|
|
|
|
|
|
|
copyTargetHeaderToTemporaryIncludeDirecory();
|
|
|
|
|
|
|
|
translationUnit.cxTranslationUnit(); // initialize translation unit so we check changes
|
|
|
|
}
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
TEST_F(CodeCompleter, FunctionInUnsavedFile)
|
|
|
|
{
|
2015-08-04 15:04:41 +02:00
|
|
|
unsavedFiles.createOrUpdate({unsavedMainFileContainer});
|
2015-08-31 12:40:14 +02:00
|
|
|
translationUnits.createOrUpdate({unsavedMainFileContainer});
|
2015-08-04 15:04:41 +02:00
|
|
|
|
|
|
|
ASSERT_THAT(completer.complete(27, 1),
|
|
|
|
AllOf(Contains(IsCodeCompletion(Utf8StringLiteral("FunctionWithArguments"),
|
|
|
|
CodeCompletion::FunctionCompletionKind)),
|
|
|
|
Contains(IsCodeCompletion(Utf8StringLiteral("Function"),
|
|
|
|
CodeCompletion::FunctionCompletionKind)),
|
|
|
|
Contains(IsCodeCompletion(Utf8StringLiteral("UnsavedFunction"),
|
|
|
|
CodeCompletion::FunctionCompletionKind)),
|
|
|
|
Contains(IsCodeCompletion(Utf8StringLiteral("f"),
|
|
|
|
CodeCompletion::FunctionCompletionKind)),
|
|
|
|
Not(Contains(IsCodeCompletion(Utf8StringLiteral("SavedFunction"),
|
|
|
|
CodeCompletion::FunctionCompletionKind)))));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
2015-08-04 15:04:41 +02:00
|
|
|
TEST_F(CodeCompleter, VariableInUnsavedFile)
|
|
|
|
{
|
|
|
|
unsavedFiles.createOrUpdate({unsavedMainFileContainer});
|
2015-08-31 12:40:14 +02:00
|
|
|
translationUnits.createOrUpdate({unsavedMainFileContainer});
|
2015-08-04 15:04:41 +02:00
|
|
|
|
|
|
|
ASSERT_THAT(completer.complete(27, 1),
|
|
|
|
Contains(IsCodeCompletion(Utf8StringLiteral("VariableInUnsavedFile"),
|
|
|
|
CodeCompletion::VariableCompletionKind)));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CodeCompleter, GlobalVariableInUnsavedFile)
|
|
|
|
{
|
|
|
|
unsavedFiles.createOrUpdate({unsavedMainFileContainer});
|
2015-08-31 12:40:14 +02:00
|
|
|
translationUnits.createOrUpdate({unsavedMainFileContainer});
|
2015-08-04 15:04:41 +02:00
|
|
|
|
|
|
|
ASSERT_THAT(completer.complete(27, 1),
|
|
|
|
Contains(IsCodeCompletion(Utf8StringLiteral("GlobalVariableInUnsavedFile"),
|
|
|
|
CodeCompletion::VariableCompletionKind)));
|
|
|
|
}
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
TEST_F(CodeCompleter, Macro)
|
|
|
|
{
|
2015-08-04 15:04:41 +02:00
|
|
|
unsavedFiles.createOrUpdate({unsavedMainFileContainer});
|
2015-08-31 12:40:14 +02:00
|
|
|
translationUnits.createOrUpdate({unsavedMainFileContainer});
|
2015-08-04 15:04:41 +02:00
|
|
|
|
|
|
|
ASSERT_THAT(completer.complete(27, 1),
|
|
|
|
Contains(IsCodeCompletion(Utf8StringLiteral("Macro"),
|
|
|
|
CodeCompletion::PreProcessorCompletionKind)));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CodeCompleter, Keyword)
|
|
|
|
{
|
2015-08-04 15:04:41 +02:00
|
|
|
ASSERT_THAT(completer.complete(27, 1),
|
|
|
|
Contains(IsCodeCompletion(Utf8StringLiteral("switch"),
|
|
|
|
CodeCompletion::KeywordCompletionKind)));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CodeCompleter, FunctionInIncludedHeader)
|
|
|
|
{
|
|
|
|
ASSERT_THAT(completer.complete(27, 1),
|
|
|
|
Contains(IsCodeCompletion(Utf8StringLiteral("FunctionInIncludedHeader"),
|
|
|
|
CodeCompletion::FunctionCompletionKind)));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
2015-08-04 15:04:41 +02:00
|
|
|
TEST_F(CodeCompleter, FunctionInUnsavedIncludedHeader)
|
|
|
|
{
|
|
|
|
unsavedFiles.createOrUpdate({unsavedTargetHeaderFileContainer});
|
2015-08-31 12:40:14 +02:00
|
|
|
translationUnits.createOrUpdate({unsavedTargetHeaderFileContainer});
|
2015-08-04 15:04:41 +02:00
|
|
|
|
|
|
|
ASSERT_THAT(completer.complete(27, 1),
|
|
|
|
Contains(IsCodeCompletion(Utf8StringLiteral("FunctionInIncludedHeaderUnsaved"),
|
|
|
|
CodeCompletion::FunctionCompletionKind)));
|
|
|
|
}
|
|
|
|
|
2015-08-31 12:40:14 +02:00
|
|
|
TEST_F(CodeCompleter, DISABLED_FunctionInChangedIncludedHeader)
|
2015-08-04 15:04:41 +02:00
|
|
|
{
|
|
|
|
copyChangedTargetHeaderToTemporaryIncludeDirecory();
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2015-08-04 15:04:41 +02:00
|
|
|
ASSERT_THAT(completer.complete(27, 1),
|
|
|
|
Contains(IsCodeCompletion(Utf8StringLiteral("FunctionInIncludedHeaderChanged"),
|
|
|
|
CodeCompletion::FunctionCompletionKind)));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
2015-08-04 15:04:41 +02:00
|
|
|
TEST_F(CodeCompleter, FunctionInChangedIncludedHeaderWithUnsavedContentInMainFile)
|
|
|
|
{
|
|
|
|
unsavedFiles.createOrUpdate({unsavedMainFileContainer});
|
2015-08-31 12:40:14 +02:00
|
|
|
translationUnits.createOrUpdate({unsavedMainFileContainer});
|
2015-08-04 15:04:41 +02:00
|
|
|
|
|
|
|
copyChangedTargetHeaderToTemporaryIncludeDirecory();
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2015-08-04 15:04:41 +02:00
|
|
|
ASSERT_THAT(completer.complete(27, 1),
|
|
|
|
Contains(IsCodeCompletion(Utf8StringLiteral("FunctionInIncludedHeaderChanged"),
|
|
|
|
CodeCompletion::FunctionCompletionKind)));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|