Files
qt-creator/tests/unit/unittest/cppprojectpartchooser-test.cpp
Nikolai Kosjar bddfe21961 CppTools: Fix using updated project part
1. Open a project consisting of two subprojects referencing the same
   source file.
2. Open the source file.
3. Click '#' in the editor toolbar and select the second project (part).
4. Update the project file, e.g. add a define
   ==> Editor does not reflect the added define

This is due to comparing project part pointers. Fix by using the project
part id that remains stable across project manager updates.

Change-Id: Ifd1a113e55ebe2ecf036cd7caafdbfd6e4cdf415
Reviewed-by: David Schulz <david.schulz@qt.io>
2017-01-26 07:50:11 +00:00

293 lines
9.5 KiB
C++

/****************************************************************************
**
** 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;
ProjectPartInfo currentProjectPartInfo{ProjectPart::Ptr(new ProjectPart),
ProjectPartInfo::NoHint};
QString preferredProjectPartId;
const ProjectExplorer::Project *activeProject = nullptr;
bool projectHasChanged = false;
Language languagePreference = Language::Cxx;
::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, ForMultipleChoosePrevious)
{
const ProjectPart::Ptr otherProjectPart;
projectPartsForFile += otherProjectPart;
projectPartsForFile += currentProjectPartInfo.projectPart;
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(currentProjectPartInfo.projectPart));
}
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;
projectHasChanged = true;
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::Hint hint = choose().hint;
ASSERT_THAT(hint, Eq(ProjectPartInfo::Hint::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.hint = ProjectPartInfo::IsFallbackMatch;
projectPartsFromDependenciesForFile += ProjectPart::Ptr(new ProjectPart);
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(fallbackProjectPart));
}
TEST_F(ProjectPartChooser, StopUsingFallbackFromModelManagerIfProjectChanges)
{
fallbackProjectPart.reset(new ProjectPart);
currentProjectPartInfo.projectPart = fallbackProjectPart;
currentProjectPartInfo.hint = ProjectPartInfo::IsFallbackMatch;
const ProjectPart::Ptr addedProject(new ProjectPart);
projectPartsForFile += addedProject;
const ProjectPart::Ptr chosen = choose().projectPart;
ASSERT_THAT(chosen, Eq(addedProject));
}
TEST_F(ProjectPartChooser, IndicateFallbacktoProjectPartFromModelManager)
{
fallbackProjectPart.reset(new ProjectPart);
const ProjectPartInfo::Hint hint = choose().hint;
ASSERT_THAT(hint, Eq(ProjectPartInfo::Hint::IsFallbackMatch));
}
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,
projectHasChanged);
}
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 = ProjectPart::C11;
projectParts.append(cprojectpart);
// Create project part for CXX
const ProjectPart::Ptr cxxprojectpart{new ProjectPart};
cxxprojectpart->languageVersion = ProjectPart::CXX98;
projectParts.append(cxxprojectpart);
return projectParts;
}
} // anonymous namespace