typehierarchybuilder_test.cpp 6.07 KB
Newer Older
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5 6 7 8 9 10 11
**
** 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
12 13 14
** 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.
15
**
16 17 18 19 20 21 22
** 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.
23 24 25 26 27
**
****************************************************************************/

#include "cpptoolsplugin.h"

28
#include "cppmodelmanager.h"
29
#include "cpptoolstestcase.h"
30 31 32
#include "typehierarchybuilder.h"

#include <cplusplus/Overview.h>
33
#include <cplusplus/SymbolVisitor.h>
34
#include <utils/algorithm.h>
35 36 37 38 39 40 41 42 43
#include <utils/fileutils.h>

#include <QDir>
#include <QtTest>

using namespace CPlusPlus;
using namespace CppTools;
using namespace CppTools::Internal;

44 45
Q_DECLARE_METATYPE(QList<Tests::TestDocument>)

46 47 48 49 50 51 52 53 54
namespace {

QString toString(const TypeHierarchy &hierarchy, int indent = 0)
{
    Symbol *symbol = hierarchy.symbol();
    QString result = QString(indent, QLatin1Char(' '))
        + Overview().prettyName(symbol->name()) + QLatin1Char('\n');

    QList<TypeHierarchy> sortedHierarchy = hierarchy.hierarchy();
55 56 57 58
    Overview oo;
    Utils::sort(sortedHierarchy, [&oo](const TypeHierarchy &h1, const TypeHierarchy &h2) -> bool {
        return oo.prettyName(h1.symbol()->name()) < oo.prettyName(h2.symbol()->name());
    });
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    foreach (TypeHierarchy childHierarchy, sortedHierarchy)
        result += toString(childHierarchy, indent + 2);
    return result;
}

class FindFirstClassInDocument: private SymbolVisitor
{
public:
    FindFirstClassInDocument() : m_clazz(0) {}

    Class *operator()(const Document::Ptr &document)
    {
        accept(document->globalNamespace());
        return m_clazz;
    }

private:
    bool preVisit(Symbol *symbol)
    {
        if (m_clazz)
            return false;

        if (Class *c = symbol->asClass()) {
            m_clazz = c;
            return false;
        }

        return true;
    }

89
private:
90 91 92
    Class *m_clazz;
};

93
class TypeHierarchyBuilderTestCase : public Tests::TestCase
94 95
{
public:
96 97
    TypeHierarchyBuilderTestCase(const QList<Tests::TestDocument> &documents,
                                 const QString &expectedHierarchy)
98
    {
99 100
        QVERIFY(succeededSoFar());

101 102 103 104 105
        Tests::TemporaryDir temporaryDir;
        QVERIFY(temporaryDir.isValid());

        QList<Tests::TestDocument> documents_ = documents;

106
        // Write files
107
        QSet<QString> filePaths;
108 109 110 111
        for (int i = 0, size = documents_.size(); i < size; ++i) {
            documents_[i].setBaseDirectory(temporaryDir.path());
            QVERIFY(documents_[i].writeToDisk());
            filePaths << documents_[i].filePath();
112 113 114
        }

        // Parse files
115 116
        QVERIFY(parseFiles(filePaths));
        const Snapshot snapshot = globalSnapshot();
117 118

        // Get class for which to generate the hierarchy
119
        const Document::Ptr firstDocument = snapshot.document(documents_.first().filePath());
120 121
        QVERIFY(firstDocument);
        QVERIFY(firstDocument->diagnosticMessages().isEmpty());
122 123 124 125 126 127 128 129 130 131
        Class *clazz = FindFirstClassInDocument()(firstDocument);
        QVERIFY(clazz);

        // Generate and compare hierarchies
        TypeHierarchyBuilder typeHierarchyBuilder(clazz, snapshot);
        const TypeHierarchy hierarchy = typeHierarchyBuilder.buildDerivedTypeHierarchy();

        const QString actualHierarchy = toString(hierarchy);
//        Uncomment for updating/generating reference data:
//        qDebug() << actualHierarchy;
132
        QCOMPARE(actualHierarchy, expectedHierarchy);
133 134 135 136 137 138 139
    }
};

} // anonymous namespace

void CppToolsPlugin::test_typehierarchy_data()
{
140
    QTest::addColumn<QList<Tests::TestDocument> >("documents");
141 142
    QTest::addColumn<QString>("expectedHierarchy");

143
    typedef Tests::TestDocument TestDocument;
144 145 146

    QTest::newRow("basic-single-document")
        << (QList<TestDocument>()
147 148 149 150 151 152
            << TestDocument("a.h",
                            "class A {};\n"
                            "class B : public A {};\n"
                            "class C1 : public B {};\n"
                            "class C2 : public B {};\n"
                            "class D : public C1 {};\n"))
153 154 155 156 157 158 159 160 161
        << QString::fromLatin1(
            "A\n"
            "  B\n"
            "    C1\n"
            "      D\n"
            "    C2\n" );

    QTest::newRow("basic-multiple-documents")
        << (QList<TestDocument>()
162 163 164 165 166 167 168 169 170 171 172 173 174 175
            << TestDocument("a.h",
                            "class A {};")
            << TestDocument("b.h",
                            "#include \"a.h\"\n"
                            "class B : public A {};")
            << TestDocument("c1.h",
                            "#include \"b.h\"\n"
                            "class C1 : public B {};")
            << TestDocument("c2.h",
                            "#include \"b.h\"\n"
                            "class C2 : public B {};")
            << TestDocument("d.h",
                            "#include \"c1.h\"\n"
                            "class D : public C1 {};"))
176 177 178 179 180 181 182 183 184 185 186
        << QString::fromLatin1(
            "A\n"
            "  B\n"
            "    C1\n"
            "      D\n"
            "    C2\n"
            );
}

void CppToolsPlugin::test_typehierarchy()
{
187
    QFETCH(QList<Tests::TestDocument>, documents);
188 189
    QFETCH(QString, expectedHierarchy);

190
    TypeHierarchyBuilderTestCase(documents, expectedHierarchy);
191
}