CppTools: Move ProjectPartChooser tests into the plugin

Change-Id: Ic2ef95d4d6a5bd65cbc611b66a0b297c7468bbcb
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2021-07-02 16:52:13 +02:00
parent 85797068bb
commit 32f46e55ed
10 changed files with 347 additions and 361 deletions

View File

@@ -122,6 +122,7 @@ extend_qtc_plugin(CppTools
cppsourceprocessor_test.cpp cppsourceprocessor_test.cpp
cpptoolstestcase.cpp cpptoolstestcase.h cpptoolstestcase.cpp cpptoolstestcase.h
modelmanagertesthelper.cpp modelmanagertesthelper.h modelmanagertesthelper.cpp modelmanagertesthelper.h
projectinfo_test.cpp
symbolsearcher_test.cpp symbolsearcher_test.cpp
typehierarchybuilder_test.cpp typehierarchybuilder_test.cpp
) )

View File

@@ -216,6 +216,7 @@ equals(TEST, 1) {
cpptoolstestcase.cpp \ cpptoolstestcase.cpp \
compileroptionsbuilder_test.cpp \ compileroptionsbuilder_test.cpp \
modelmanagertesthelper.cpp \ modelmanagertesthelper.cpp \
projectinfo_test.cpp \
symbolsearcher_test.cpp \ symbolsearcher_test.cpp \
typehierarchybuilder_test.cpp typehierarchybuilder_test.cpp

View File

@@ -242,6 +242,7 @@ Project {
"cppsourceprocessor_test.cpp", "cppsourceprocessor_test.cpp",
"modelmanagertesthelper.cpp", "modelmanagertesthelper.cpp",
"modelmanagertesthelper.h", "modelmanagertesthelper.h",
"projectinfo_test.cpp",
"symbolsearcher_test.cpp", "symbolsearcher_test.cpp",
"typehierarchybuilder_test.cpp", "typehierarchybuilder_test.cpp",
] ]

View File

@@ -210,6 +210,28 @@ private slots:
void test_optionsBuilder_buildAllOptions(); void test_optionsBuilder_buildAllOptions();
void test_optionsBuilder_buildAllOptionsMsvc(); void test_optionsBuilder_buildAllOptionsMsvc();
void test_optionsBuilder_buildAllOptionsMsvcWithExceptions(); void test_optionsBuilder_buildAllOptionsMsvcWithExceptions();
void test_projectPartChooser_chooseManuallySet();
void test_projectPartChooser_indicateManuallySet();
void test_projectPartChooser_indicateManuallySetForFallbackToProjectPartFromDependencies();
void test_projectPartChooser_doNotIndicateNotManuallySet();
void test_projectPartChooser_forMultipleChooseFromActiveProject();
void test_projectPartChooser_forMultiplePreferSelectedForBuilding();
void test_projectPartChooser_forMultipleFromDependenciesChooseFromActiveProject();
void test_projectPartChooser_forMultipleCheckIfActiveProjectChanged();
void test_projectPartChooser_forMultipleAndAmbigiousHeaderPreferCProjectPart();
void test_projectPartChooser_forMultipleAndAmbigiousHeaderPreferCxxProjectPart();
void test_projectPartChooser_indicateMultiple();
void test_projectPartChooser_indicateMultipleForFallbackToProjectPartFromDependencies();
void test_projectPartChooser_forMultipleChooseNewIfPreviousIsGone();
void test_projectPartChooser_fallbackToProjectPartFromDependencies();
void test_projectPartChooser_fallbackToProjectPartFromModelManager();
void test_projectPartChooser_continueUsingFallbackFromModelManagerIfProjectDoesNotChange();
void test_projectPartChooser_stopUsingFallbackFromModelManagerIfProjectChanges1();
void test_projectPartChooser_stopUsingFallbackFromModelManagerIfProjectChanges2();
void test_projectPartChooser_indicateFallbacktoProjectPartFromModelManager();
void test_projectPartChooser_indicateFromDependencies();
void test_projectPartChooser_doNotIndicateFromDependencies();
#endif #endif
private: private:

View File

@@ -11,7 +11,6 @@ HEADERS += \
$$PWD/cppprojectfilecategorizer.h \ $$PWD/cppprojectfilecategorizer.h \
$$PWD/projectinfo.h \ $$PWD/projectinfo.h \
$$PWD/cppprojectinfogenerator.cpp \ $$PWD/cppprojectinfogenerator.cpp \
$$PWD/cppprojectpartchooser.h \
$$PWD/headerpathfilter.h $$PWD/headerpathfilter.h
SOURCES += \ SOURCES += \
@@ -21,5 +20,4 @@ SOURCES += \
$$PWD/cppprojectfilecategorizer.cpp \ $$PWD/cppprojectfilecategorizer.cpp \
$$PWD/projectinfo.cpp \ $$PWD/projectinfo.cpp \
$$PWD/cppprojectinfogenerator.cpp \ $$PWD/cppprojectinfogenerator.cpp \
$$PWD/cppprojectpartchooser.cpp \
$$PWD/headerpathfilter.cpp $$PWD/headerpathfilter.cpp

View File

@@ -0,0 +1,322 @@
/****************************************************************************
**
** Copyright (C) 2021 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.
**
****************************************************************************/
#include "cppprojectpartchooser.h"
#include "cpptoolsplugin.h"
#include <QtTest>
namespace CppTools {
namespace Internal {
namespace {
class ProjectPartChooserTest
{
public:
ProjectPartChooserTest()
{
chooser.setFallbackProjectPart([&]() {
return fallbackProjectPart;
});
chooser.setProjectPartsForFile([&](const QString &) {
return projectPartsForFile;
});
chooser.setProjectPartsFromDependenciesForFile([&](const QString &) {
return projectPartsFromDependenciesForFile;
});
}
const ProjectPartInfo choose()
{
return chooser.choose(filePath, currentProjectPartInfo, preferredProjectPartId,
activeProject, languagePreference, projectsChanged);
}
static QList<ProjectPart::Ptr> createProjectPartsWithDifferentProjects()
{
QList<ProjectPart::Ptr> projectParts;
const ProjectPart::Ptr p1{new ProjectPart};
p1->project = reinterpret_cast<ProjectExplorer::Project *>(1 << 0);
projectParts.append(p1);
const ProjectPart::Ptr p2{new ProjectPart};
p2->project = reinterpret_cast<ProjectExplorer::Project *>(1 << 1);
projectParts.append(p2);
return projectParts;
}
static QList<ProjectPart::Ptr> createCAndCxxProjectParts()
{
QList<ProjectPart::Ptr> projectParts;
// Create project part for C
const ProjectPart::Ptr cprojectpart{new ProjectPart};
cprojectpart->languageVersion = Utils::LanguageVersion::C11;
projectParts.append(cprojectpart);
// Create project part for CXX
const ProjectPart::Ptr cxxprojectpart{new ProjectPart};
cxxprojectpart->languageVersion = Utils::LanguageVersion::CXX98;
projectParts.append(cxxprojectpart);
return projectParts;
}
QString filePath;
ProjectPart::Ptr currentProjectPart{new ProjectPart};
ProjectPartInfo currentProjectPartInfo{currentProjectPart,
{currentProjectPart},
ProjectPartInfo::NoHint};
QString preferredProjectPartId;
const ProjectExplorer::Project *activeProject = nullptr;
Language languagePreference = Language::Cxx;
bool projectsChanged = false;
ProjectPartChooser chooser;
QList<ProjectPart::Ptr> projectPartsForFile;
QList<ProjectPart::Ptr> projectPartsFromDependenciesForFile;
ProjectPart::Ptr fallbackProjectPart;
};
}
void CppToolsPlugin::test_projectPartChooser_chooseManuallySet()
{
ProjectPart::Ptr p1(new ProjectPart);
ProjectPart::Ptr p2(new ProjectPart);
ProjectPartChooserTest t;
p2->projectFile = "someId";
t.preferredProjectPartId = p2->projectFile;
t.projectPartsForFile += {p1, p2};
QCOMPARE(t.choose().projectPart, p2);
}
void CppToolsPlugin::test_projectPartChooser_indicateManuallySet()
{
ProjectPart::Ptr p1(new ProjectPart);
ProjectPart::Ptr p2(new ProjectPart);
ProjectPartChooserTest t;
p2->projectFile = "someId";
t.preferredProjectPartId = p2->projectFile;
t.projectPartsForFile += {p1, p2};
QVERIFY(t.choose().hints & ProjectPartInfo::IsPreferredMatch);
}
void CppToolsPlugin::test_projectPartChooser_indicateManuallySetForFallbackToProjectPartFromDependencies()
{
ProjectPart::Ptr p1(new ProjectPart);
ProjectPart::Ptr p2(new ProjectPart);
ProjectPartChooserTest t;
p2->projectFile = "someId";
t.preferredProjectPartId = p2->projectFile;
t.projectPartsFromDependenciesForFile += {p1, p2};
QVERIFY(t.choose().hints & ProjectPartInfo::IsPreferredMatch);
}
void CppToolsPlugin::test_projectPartChooser_doNotIndicateNotManuallySet()
{
QVERIFY(!(ProjectPartChooserTest().choose().hints & ProjectPartInfo::IsPreferredMatch));
}
void CppToolsPlugin::test_projectPartChooser_forMultipleChooseFromActiveProject()
{
ProjectPartChooserTest t;
const QList<ProjectPart::Ptr> projectParts = t.createProjectPartsWithDifferentProjects();
const ProjectPart::Ptr secondProjectPart = projectParts.at(1);
t.projectPartsForFile += projectParts;
t.activeProject = secondProjectPart->project;
QCOMPARE(t.choose().projectPart, secondProjectPart);
}
void CppToolsPlugin::test_projectPartChooser_forMultiplePreferSelectedForBuilding()
{
const ProjectPart::Ptr firstProjectPart{new ProjectPart};
const ProjectPart::Ptr secondProjectPart{new ProjectPart};
firstProjectPart->selectedForBuilding = false;
secondProjectPart->selectedForBuilding = true;
ProjectPartChooserTest t;
t.projectPartsForFile += firstProjectPart;
t.projectPartsForFile += secondProjectPart;
QCOMPARE(t.choose().projectPart, secondProjectPart);
}
void CppToolsPlugin::test_projectPartChooser_forMultipleFromDependenciesChooseFromActiveProject()
{
ProjectPartChooserTest t;
const QList<ProjectPart::Ptr> projectParts = t.createProjectPartsWithDifferentProjects();
const ProjectPart::Ptr secondProjectPart = projectParts.at(1);
t.projectPartsFromDependenciesForFile += projectParts;
t.activeProject = secondProjectPart->project;
QCOMPARE(t.choose().projectPart, secondProjectPart);
}
void CppToolsPlugin::test_projectPartChooser_forMultipleCheckIfActiveProjectChanged()
{
ProjectPartChooserTest t;
const QList<ProjectPart::Ptr> projectParts = t.createProjectPartsWithDifferentProjects();
const ProjectPart::Ptr firstProjectPart = projectParts.at(0);
const ProjectPart::Ptr secondProjectPart = projectParts.at(1);
t.projectPartsForFile += projectParts;
t.currentProjectPartInfo.projectPart = firstProjectPart;
t.activeProject = secondProjectPart->project;
QCOMPARE(t.choose().projectPart, secondProjectPart);
}
void CppToolsPlugin::test_projectPartChooser_forMultipleAndAmbigiousHeaderPreferCProjectPart()
{
ProjectPartChooserTest t;
t.languagePreference = Language::C;
t.projectPartsForFile = t.createCAndCxxProjectParts();
const ProjectPart::Ptr cProjectPart = t.projectPartsForFile.at(0);
QCOMPARE(t.choose().projectPart, cProjectPart);
}
void CppToolsPlugin::test_projectPartChooser_forMultipleAndAmbigiousHeaderPreferCxxProjectPart()
{
ProjectPartChooserTest t;
t.languagePreference = Language::Cxx;
t.projectPartsForFile = t.createCAndCxxProjectParts();
const ProjectPart::Ptr cxxProjectPart = t.projectPartsForFile.at(1);
QCOMPARE(t.choose().projectPart, cxxProjectPart);
}
void CppToolsPlugin::test_projectPartChooser_indicateMultiple()
{
const ProjectPart::Ptr p1{new ProjectPart};
const ProjectPart::Ptr p2{new ProjectPart};
ProjectPartChooserTest t;
t.projectPartsForFile += { p1, p2 };
QVERIFY(t.choose().hints & ProjectPartInfo::IsAmbiguousMatch);
}
void CppToolsPlugin::test_projectPartChooser_indicateMultipleForFallbackToProjectPartFromDependencies()
{
const ProjectPart::Ptr p1{new ProjectPart};
const ProjectPart::Ptr p2{new ProjectPart};
ProjectPartChooserTest t;
t.projectPartsFromDependenciesForFile += { p1, p2 };
QVERIFY(t.choose().hints & ProjectPartInfo::IsAmbiguousMatch);
}
void CppToolsPlugin::test_projectPartChooser_forMultipleChooseNewIfPreviousIsGone()
{
const ProjectPart::Ptr newProjectPart{new ProjectPart};
ProjectPartChooserTest t;
t.projectPartsForFile += newProjectPart;
QCOMPARE(t.choose().projectPart, newProjectPart);
}
void CppToolsPlugin::test_projectPartChooser_fallbackToProjectPartFromDependencies()
{
const ProjectPart::Ptr fromDependencies{new ProjectPart};
ProjectPartChooserTest t;
t.projectPartsFromDependenciesForFile += fromDependencies;
QCOMPARE(t.choose().projectPart, fromDependencies);
}
void CppToolsPlugin::test_projectPartChooser_fallbackToProjectPartFromModelManager()
{
ProjectPartChooserTest t;
t.fallbackProjectPart.reset(new ProjectPart);
QCOMPARE(t.choose().projectPart, t.fallbackProjectPart);
}
void CppToolsPlugin::test_projectPartChooser_continueUsingFallbackFromModelManagerIfProjectDoesNotChange()
{
// ...without re-calculating the dependency table.
ProjectPartChooserTest t;
t.fallbackProjectPart.reset(new ProjectPart);
t.currentProjectPartInfo.projectPart = t.fallbackProjectPart;
t.currentProjectPartInfo.hints |= ProjectPartInfo::IsFallbackMatch;
t.projectPartsFromDependenciesForFile += ProjectPart::Ptr(new ProjectPart);
QCOMPARE(t.choose().projectPart, t.fallbackProjectPart);
}
void CppToolsPlugin::test_projectPartChooser_stopUsingFallbackFromModelManagerIfProjectChanges1()
{
ProjectPartChooserTest t;
t.fallbackProjectPart.reset(new ProjectPart);
t.currentProjectPartInfo.projectPart = t.fallbackProjectPart;
t.currentProjectPartInfo.hints |= ProjectPartInfo::IsFallbackMatch;
const ProjectPart::Ptr addedProject(new ProjectPart);
t.projectPartsForFile += addedProject;
QCOMPARE(t.choose().projectPart, addedProject);
}
void CppToolsPlugin::test_projectPartChooser_stopUsingFallbackFromModelManagerIfProjectChanges2()
{
ProjectPartChooserTest t;
t.fallbackProjectPart.reset(new ProjectPart);
t.currentProjectPartInfo.projectPart = t.fallbackProjectPart;
t.currentProjectPartInfo.hints |= ProjectPartInfo::IsFallbackMatch;
const ProjectPart::Ptr addedProject(new ProjectPart);
t.projectPartsFromDependenciesForFile += addedProject;
t.projectsChanged = true;
QCOMPARE(t.choose().projectPart, addedProject);
}
void CppToolsPlugin::test_projectPartChooser_indicateFallbacktoProjectPartFromModelManager()
{
ProjectPartChooserTest t;
t.fallbackProjectPart.reset(new ProjectPart);
QVERIFY(t.choose().hints & ProjectPartInfo::IsFallbackMatch);
}
void CppToolsPlugin::test_projectPartChooser_indicateFromDependencies()
{
ProjectPartChooserTest t;
t.projectPartsFromDependenciesForFile += ProjectPart::Ptr(new ProjectPart);
QVERIFY(t.choose().hints & ProjectPartInfo::IsFromDependenciesMatch);
}
void CppToolsPlugin::test_projectPartChooser_doNotIndicateFromDependencies()
{
ProjectPartChooserTest t;
t.projectPartsForFile += ProjectPart::Ptr(new ProjectPart);
QVERIFY(!(t.choose().hints & ProjectPartInfo::IsFromDependenciesMatch));
}
} // namespace Internal
} // namespace CppTools

View File

@@ -45,7 +45,6 @@ add_qtc_test(unittest GTEST
conditionally-disabled-tests.h conditionally-disabled-tests.h
cppprojectfilecategorizer-test.cpp cppprojectfilecategorizer-test.cpp
cppprojectinfogenerator-test.cpp cppprojectinfogenerator-test.cpp
cppprojectpartchooser-test.cpp
dummyclangipcclient.h dummyclangipcclient.h
dynamicastmatcherdiagnosticcontainer-matcher.h dynamicastmatcherdiagnosticcontainer-matcher.h
eventspy.cpp eventspy.h eventspy.cpp eventspy.h

View File

@@ -1,356 +0,0 @@
/****************************************************************************
**
** 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.
**
****************************************************************************/
#include "googletest.h"
#include <cpptools/cppprojectpartchooser.h>
#include <cpptools/cpptools_utils.h>
#include <cpptools/projectpart.h>
using CppTools::Internal::ProjectPartChooser;
using CppTools::ProjectPart;
using CppTools::ProjectPartInfo;
using CppTools::Language;
using testing::Eq;
namespace {
class ProjectPartChooser : public ::testing::Test
{
protected:
void SetUp() override;
const ProjectPartInfo choose() const;
static QList<ProjectPart::Ptr> createProjectPartsWithDifferentProjects();
static QList<ProjectPart::Ptr> createCAndCxxProjectParts();
protected:
QString filePath;
ProjectPart::Ptr currentProjectPart{new ProjectPart};
ProjectPartInfo currentProjectPartInfo{currentProjectPart,
{currentProjectPart},
ProjectPartInfo::NoHint};
QString preferredProjectPartId;
const ProjectExplorer::Project *activeProject = nullptr;
Language languagePreference = Language::Cxx;
bool projectsChanged = false;
::ProjectPartChooser chooser;
QList<ProjectPart::Ptr> projectPartsForFile;
QList<ProjectPart::Ptr> projectPartsFromDependenciesForFile;
ProjectPart::Ptr fallbackProjectPart;
};
TEST_F(ProjectPartChooser, ChooseManuallySet)
{
ProjectPart::Ptr p1(new ProjectPart);
ProjectPart::Ptr p2(new ProjectPart);
p2->projectFile = preferredProjectPartId = "someId";
projectPartsForFile += {p1, p2};
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(p2));
}
TEST_F(ProjectPartChooser, IndicateManuallySet)
{
ProjectPart::Ptr p1(new ProjectPart);
ProjectPart::Ptr p2(new ProjectPart);
p2->projectFile = preferredProjectPartId = "someId";
projectPartsForFile += {p1, p2};
const ProjectPartInfo::Hints hints = choose().hints;
ASSERT_TRUE(hints & ProjectPartInfo::IsPreferredMatch);
}
TEST_F(ProjectPartChooser, IndicateManuallySetForFallbackToProjectPartFromDependencies)
{
ProjectPart::Ptr p1(new ProjectPart);
ProjectPart::Ptr p2(new ProjectPart);
p2->projectFile = preferredProjectPartId = "someId";
projectPartsFromDependenciesForFile += {p1, p2};
const ProjectPartInfo::Hints hints = choose().hints;
ASSERT_TRUE(hints & ProjectPartInfo::IsPreferredMatch);
}
TEST_F(ProjectPartChooser, DoNotIndicateNotManuallySet)
{
const ProjectPartInfo::Hints hints = choose().hints;
ASSERT_FALSE(hints & ProjectPartInfo::IsPreferredMatch);
}
TEST_F(ProjectPartChooser, ForMultipleChooseFromActiveProject)
{
const QList<ProjectPart::Ptr> projectParts = createProjectPartsWithDifferentProjects();
const ProjectPart::Ptr secondProjectPart = projectParts.at(1);
projectPartsForFile += projectParts;
activeProject = secondProjectPart->project;
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(secondProjectPart));
}
TEST_F(ProjectPartChooser, ForMultiplePreferSelectedForBuilding)
{
const ProjectPart::Ptr firstProjectPart{new ProjectPart};
const ProjectPart::Ptr secondProjectPart{new ProjectPart};
firstProjectPart->selectedForBuilding = false;
secondProjectPart->selectedForBuilding = true;
projectPartsForFile += firstProjectPart;
projectPartsForFile += secondProjectPart;
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(secondProjectPart));
}
TEST_F(ProjectPartChooser, ForMultipleFromDependenciesChooseFromActiveProject)
{
const QList<ProjectPart::Ptr> projectParts = createProjectPartsWithDifferentProjects();
const ProjectPart::Ptr secondProjectPart = projectParts.at(1);
projectPartsFromDependenciesForFile += projectParts;
activeProject = secondProjectPart->project;
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(secondProjectPart));
}
TEST_F(ProjectPartChooser, ForMultipleCheckIfActiveProjectChanged)
{
const QList<ProjectPart::Ptr> projectParts = createProjectPartsWithDifferentProjects();
const ProjectPart::Ptr firstProjectPart = projectParts.at(0);
const ProjectPart::Ptr secondProjectPart = projectParts.at(1);
projectPartsForFile += projectParts;
currentProjectPartInfo.projectPart = firstProjectPart;
activeProject = secondProjectPart->project;
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(secondProjectPart));
}
TEST_F(ProjectPartChooser, ForMultipleAndAmbigiousHeaderPreferCProjectPart)
{
languagePreference = Language::C;
projectPartsForFile = createCAndCxxProjectParts();
const ProjectPart::Ptr cProjectPart = projectPartsForFile.at(0);
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(cProjectPart));
}
TEST_F(ProjectPartChooser, ForMultipleAndAmbigiousHeaderPreferCxxProjectPart)
{
languagePreference = Language::Cxx;
projectPartsForFile = createCAndCxxProjectParts();
const ProjectPart::Ptr cxxProjectPart = projectPartsForFile.at(1);
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(cxxProjectPart));
}
TEST_F(ProjectPartChooser, IndicateMultiple)
{
const ProjectPart::Ptr p1{new ProjectPart};
const ProjectPart::Ptr p2{new ProjectPart};
projectPartsForFile += { p1, p2 };
const ProjectPartInfo::Hints hints = choose().hints;
ASSERT_TRUE(hints & ProjectPartInfo::IsAmbiguousMatch);
}
TEST_F(ProjectPartChooser, IndicateMultipleForFallbackToProjectPartFromDependencies)
{
const ProjectPart::Ptr p1{new ProjectPart};
const ProjectPart::Ptr p2{new ProjectPart};
projectPartsFromDependenciesForFile += { p1, p2 };
const ProjectPartInfo::Hints hints = choose().hints;
ASSERT_TRUE(hints & ProjectPartInfo::IsAmbiguousMatch);
}
TEST_F(ProjectPartChooser, ForMultipleChooseNewIfPreviousIsGone)
{
const ProjectPart::Ptr newProjectPart{new ProjectPart};
projectPartsForFile += newProjectPart;
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(newProjectPart));
}
TEST_F(ProjectPartChooser, FallbackToProjectPartFromDependencies)
{
const ProjectPart::Ptr fromDependencies{new ProjectPart};
projectPartsFromDependenciesForFile += fromDependencies;
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(fromDependencies));
}
TEST_F(ProjectPartChooser, FallbackToProjectPartFromModelManager)
{
fallbackProjectPart.reset(new ProjectPart);
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(fallbackProjectPart));
}
TEST_F(ProjectPartChooser, ContinueUsingFallbackFromModelManagerIfProjectDoesNotChange)
{
// ...without re-calculating the dependency table.
fallbackProjectPart.reset(new ProjectPart);
currentProjectPartInfo.projectPart = fallbackProjectPart;
currentProjectPartInfo.hints |= ProjectPartInfo::IsFallbackMatch;
projectPartsFromDependenciesForFile += ProjectPart::Ptr(new ProjectPart);
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(fallbackProjectPart));
}
TEST_F(ProjectPartChooser, StopUsingFallbackFromModelManagerIfProjectChanges1)
{
fallbackProjectPart.reset(new ProjectPart);
currentProjectPartInfo.projectPart = fallbackProjectPart;
currentProjectPartInfo.hints |= ProjectPartInfo::IsFallbackMatch;
const ProjectPart::Ptr addedProject(new ProjectPart);
projectPartsForFile += addedProject;
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(addedProject));
}
TEST_F(ProjectPartChooser, StopUsingFallbackFromModelManagerIfProjectChanges2)
{
fallbackProjectPart.reset(new ProjectPart);
currentProjectPartInfo.projectPart = fallbackProjectPart;
currentProjectPartInfo.hints |= ProjectPartInfo::IsFallbackMatch;
const ProjectPart::Ptr addedProject(new ProjectPart);
projectPartsFromDependenciesForFile += addedProject;
projectsChanged = true;
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(addedProject));
}
TEST_F(ProjectPartChooser, IndicateFallbacktoProjectPartFromModelManager)
{
fallbackProjectPart.reset(new ProjectPart);
const ProjectPartInfo::Hints hints = choose().hints;
ASSERT_TRUE(hints & ProjectPartInfo::IsFallbackMatch);
}
TEST_F(ProjectPartChooser, IndicateFromDependencies)
{
projectPartsFromDependenciesForFile += ProjectPart::Ptr(new ProjectPart);
const ProjectPartInfo::Hints hints = choose().hints;
ASSERT_TRUE(hints & ProjectPartInfo::IsFromDependenciesMatch);
}
TEST_F(ProjectPartChooser, DoNotIndicateFromDependencies)
{
projectPartsForFile += ProjectPart::Ptr(new ProjectPart);
const ProjectPartInfo::Hints hints = choose().hints;
ASSERT_FALSE(hints & ProjectPartInfo::IsFromDependenciesMatch);
}
void ProjectPartChooser::SetUp()
{
chooser.setFallbackProjectPart([&]() {
return fallbackProjectPart;
});
chooser.setProjectPartsForFile([&](const QString &) {
return projectPartsForFile;
});
chooser.setProjectPartsFromDependenciesForFile([&](const QString &) {
return projectPartsFromDependenciesForFile;
});
}
const ProjectPartInfo ProjectPartChooser::choose() const
{
return chooser.choose(filePath,
currentProjectPartInfo,
preferredProjectPartId,
activeProject,
languagePreference,
projectsChanged);
}
QList<ProjectPart::Ptr> ProjectPartChooser::createProjectPartsWithDifferentProjects()
{
QList<ProjectPart::Ptr> projectParts;
const ProjectPart::Ptr p1{new ProjectPart};
p1->project = reinterpret_cast<ProjectExplorer::Project *>(1 << 0);
projectParts.append(p1);
const ProjectPart::Ptr p2{new ProjectPart};
p2->project = reinterpret_cast<ProjectExplorer::Project *>(1 << 1);
projectParts.append(p2);
return projectParts;
}
QList<ProjectPart::Ptr> ProjectPartChooser::createCAndCxxProjectParts()
{
QList<ProjectPart::Ptr> projectParts;
// Create project part for C
const ProjectPart::Ptr cprojectpart{new ProjectPart};
cprojectpart->languageVersion = Utils::LanguageVersion::C11;
projectParts.append(cprojectpart);
// Create project part for CXX
const ProjectPart::Ptr cxxprojectpart{new ProjectPart};
cxxprojectpart->languageVersion = Utils::LanguageVersion::CXX98;
projectParts.append(cxxprojectpart);
return projectParts;
}
} // anonymous namespace

View File

@@ -58,7 +58,6 @@ SOURCES += \
clientserveroutsideprocess-test.cpp \ clientserveroutsideprocess-test.cpp \
cppprojectfilecategorizer-test.cpp \ cppprojectfilecategorizer-test.cpp \
cppprojectinfogenerator-test.cpp \ cppprojectinfogenerator-test.cpp \
cppprojectpartchooser-test.cpp \
fakeprocess.cpp \ fakeprocess.cpp \
gtest-creator-printing.cpp \ gtest-creator-printing.cpp \
gtest-qt-printing.cpp \ gtest-qt-printing.cpp \

View File

@@ -176,7 +176,6 @@ Project {
"conditionally-disabled-tests.h", "conditionally-disabled-tests.h",
"cppprojectfilecategorizer-test.cpp", "cppprojectfilecategorizer-test.cpp",
"cppprojectinfogenerator-test.cpp", "cppprojectinfogenerator-test.cpp",
"cppprojectpartchooser-test.cpp",
"createtablesqlstatementbuilder-test.cpp", "createtablesqlstatementbuilder-test.cpp",
"dummyclangipcclient.h", "dummyclangipcclient.h",
"dynamicastmatcherdiagnosticcontainer-matcher.h", "dynamicastmatcherdiagnosticcontainer-matcher.h",