cppprojectfilecategorizer-test.cpp 6.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/****************************************************************************
**
** 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 "gtest-qt-printing.h"
28
#include "mimedatabase-utilities.h"
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

#include <cpptools/cppprojectfilecategorizer.h>
#include <utils/mimetypes/mimedatabase.h>

#include <QDebug>
#include <QFileInfo>

using CppTools::ProjectFile;
using CppTools::ProjectFiles;
using CppTools::ProjectFileCategorizer;

using testing::IsNull;
using testing::NotNull;
using testing::Eq;
using testing::Gt;
using testing::Contains;
using testing::EndsWith;
using testing::AllOf;

namespace CppTools {

void PrintTo(const ProjectFile &projectFile, std::ostream *os)
{
    *os << "ProjectFile(";
    QString output;
    QDebug(&output) << projectFile;
    *os << qPrintable(output);
    *os << ")";
}

} // namespace CppTools

namespace {

class ProjectFileCategorizer : public ::testing::Test
{
protected:
    void SetUp() override;

    static ProjectFiles singleFile(const QString &filePath, ProjectFile::Kind kind);

protected:
    const QString dummyProjectPartName;
};

74 75 76
using ProjectFileCategorizerVerySlowTest = ProjectFileCategorizer;

TEST_F(ProjectFileCategorizerVerySlowTest, C)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
{
    const QStringList inputFilePaths = QStringList() << "foo.c" << "foo.h";
    const ProjectFiles expected {
        ProjectFile("foo.c", ProjectFile::CSource),
        ProjectFile("foo.h", ProjectFile::CHeader),
    };

    ::ProjectFileCategorizer categorizer{dummyProjectPartName, inputFilePaths};

    ASSERT_THAT(categorizer.cSources(), Eq(expected));
    ASSERT_TRUE(categorizer.cxxSources().isEmpty());
    ASSERT_TRUE(categorizer.objcSources().isEmpty());
    ASSERT_TRUE(categorizer.objcxxSources().isEmpty());
}

92
TEST_F(ProjectFileCategorizerVerySlowTest, CxxWithUnambiguousHeaderSuffix)
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
{
    const QStringList inputFilePaths = QStringList() << "foo.cpp" << "foo.hpp";
    const ProjectFiles expected {
        ProjectFile("foo.cpp", ProjectFile::CXXSource),
        ProjectFile("foo.hpp", ProjectFile::CXXHeader),
    };

    ::ProjectFileCategorizer categorizer{dummyProjectPartName, inputFilePaths};

    ASSERT_THAT(categorizer.cxxSources(), Eq(expected));
    ASSERT_TRUE(categorizer.cSources().isEmpty());
    ASSERT_TRUE(categorizer.objcSources().isEmpty());
    ASSERT_TRUE(categorizer.objcxxSources().isEmpty());
}

108
TEST_F(ProjectFileCategorizerVerySlowTest, CxxWithAmbiguousHeaderSuffix)
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
{
    const QStringList inputFilePaths = QStringList() << "foo.cpp" << "foo.h";
    const ProjectFiles expected {
        ProjectFile("foo.cpp", ProjectFile::CXXSource),
        ProjectFile("foo.h", ProjectFile::CXXHeader),
    };

    ::ProjectFileCategorizer categorizer{dummyProjectPartName, inputFilePaths};

    ASSERT_THAT(categorizer.cxxSources(), Eq(expected));
    ASSERT_TRUE(categorizer.cSources().isEmpty());
    ASSERT_TRUE(categorizer.objcSources().isEmpty());
    ASSERT_TRUE(categorizer.objcxxSources().isEmpty());
}

124
TEST_F(ProjectFileCategorizerVerySlowTest, ObjectiveC)
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
{
    const QStringList inputFilePaths = QStringList() << "foo.m" << "foo.h";
    const ProjectFiles expected {
        ProjectFile("foo.m", ProjectFile::ObjCSource),
        ProjectFile("foo.h", ProjectFile::ObjCHeader),
    };

    ::ProjectFileCategorizer categorizer{dummyProjectPartName, inputFilePaths};

    ASSERT_THAT(categorizer.objcSources(), Eq(expected));
    ASSERT_TRUE(categorizer.cxxSources().isEmpty());
    ASSERT_TRUE(categorizer.cSources().isEmpty());
    ASSERT_TRUE(categorizer.objcxxSources().isEmpty());
}

140
TEST_F(ProjectFileCategorizerVerySlowTest, ObjectiveCxx)
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
{
    const QStringList inputFilePaths = QStringList() << "foo.mm" << "foo.h";
    const ProjectFiles expected {
        ProjectFile("foo.mm", ProjectFile::ObjCXXSource),
        ProjectFile("foo.h", ProjectFile::ObjCXXHeader),
    };

    ::ProjectFileCategorizer categorizer{dummyProjectPartName, inputFilePaths};

    ASSERT_THAT(categorizer.objcxxSources(), Eq(expected));
    ASSERT_TRUE(categorizer.objcSources().isEmpty());
    ASSERT_TRUE(categorizer.cSources().isEmpty());
    ASSERT_TRUE(categorizer.cxxSources().isEmpty());
}

156
TEST_F(ProjectFileCategorizerVerySlowTest, MixedCAndCxx)
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
{
    const QStringList inputFilePaths = QStringList() << "foo.cpp" << "foo.h"
                                                     << "bar.c" << "bar.h";
    const ProjectFiles expectedCxxSources {
        ProjectFile("foo.cpp", ProjectFile::CXXSource),
        ProjectFile("foo.h", ProjectFile::CXXHeader),
        ProjectFile("bar.h", ProjectFile::CXXHeader),
    };
    const ProjectFiles expectedCSources {
        ProjectFile("bar.c", ProjectFile::CSource),
        ProjectFile("foo.h", ProjectFile::CHeader),
        ProjectFile("bar.h", ProjectFile::CHeader),
    };

    ::ProjectFileCategorizer categorizer{dummyProjectPartName, inputFilePaths};

    ASSERT_THAT(categorizer.cxxSources(), Eq(expectedCxxSources));
    ASSERT_THAT(categorizer.cSources(), Eq(expectedCSources));
    ASSERT_TRUE(categorizer.objcSources().isEmpty());
    ASSERT_TRUE(categorizer.objcxxSources().isEmpty());
}

179
TEST_F(ProjectFileCategorizerVerySlowTest, AmbiguousHeaderOnly)
180 181 182 183 184 185 186 187 188 189 190
{
    ::ProjectFileCategorizer categorizer{dummyProjectPartName, QStringList() << "foo.h"};

    ASSERT_THAT(categorizer.cSources(), Eq(singleFile("foo.h", ProjectFile::CHeader)));
    ASSERT_THAT(categorizer.cxxSources(), Eq(singleFile("foo.h", ProjectFile::CXXHeader)));
    ASSERT_THAT(categorizer.objcSources(), Eq(singleFile("foo.h", ProjectFile::ObjCHeader)));
    ASSERT_THAT(categorizer.objcxxSources(), Eq(singleFile("foo.h", ProjectFile::ObjCXXHeader)));
}

void ProjectFileCategorizer::SetUp()
{
191
    ASSERT_TRUE(MimeDataBaseUtilities::addCppToolsMimeTypes());
192 193 194 195 196 197 198 199 200
}

QVector<CppTools::ProjectFile>ProjectFileCategorizer::singleFile(const QString &filePath,
                                                                 ProjectFile::Kind kind)
{
    return { ProjectFile(filePath, kind) };
}

} // anonymous namespace