translationunitupdater-test.cpp 4.97 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
/****************************************************************************
**
** 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.
**
****************************************************************************/

Marco Bubke's avatar
Marco Bubke committed
26 27
#include "googletest.h"

28
#include <clangclock.h>
29 30 31 32
#include <clangtranslationunitupdater.h>

#include <clang-c/Index.h>

33 34
using ClangBackEnd::Clock;
using ClangBackEnd::TimePoint;
35 36 37 38
using ClangBackEnd::TranslationUnitUpdater;
using ClangBackEnd::TranslationUnitUpdateInput;
using ClangBackEnd::TranslationUnitUpdateResult;

39
using testing::Eq;
40 41 42 43 44 45 46 47 48
using testing::Gt;

namespace {

class TranslationUnitUpdater : public ::testing::Test
{
protected:
    void TearDown() override;

49 50
    ::TranslationUnitUpdater createUpdater(const TranslationUnitUpdateInput &input,
                                           const Utf8String &translationUnitId = Utf8String());
51 52 53 54 55 56 57 58 59 60 61

    enum ReparseMode { SetReparseNeeded, DoNotSetReparseNeeded };
    TranslationUnitUpdateInput createInput(ReparseMode reparseMode = DoNotSetReparseNeeded);

protected:
    CXIndex cxIndex = nullptr;
    CXTranslationUnit cxTranslationUnit = nullptr;

    Utf8String filePath = Utf8StringLiteral(TESTDATA_DIR"/translationunits.cpp");
};

62 63 64
using TranslationUnitUpdaterSlowTest = TranslationUnitUpdater;

TEST_F(TranslationUnitUpdaterSlowTest, ParsesIfNeeded)
65 66 67 68 69 70
{
    ::TranslationUnitUpdater updater = createUpdater(createInput());

    TranslationUnitUpdateResult result = updater.update(::TranslationUnitUpdater::UpdateMode::AsNeeded);

    ASSERT_TRUE(cxTranslationUnit);
71
    ASSERT_FALSE(result.hasReparsed());
72 73
}

74
TEST_F(TranslationUnitUpdaterSlowTest, ReparsesIfNeeded)
75 76 77 78 79
{
    ::TranslationUnitUpdater updater = createUpdater(createInput(SetReparseNeeded));

    TranslationUnitUpdateResult result = updater.update(::TranslationUnitUpdater::UpdateMode::AsNeeded);

80
    ASSERT_TRUE(result.hasReparsed());
81 82
}

83
TEST_F(TranslationUnitUpdaterSlowTest, PropagatesTranslationUnitId)
84 85 86 87 88 89 90 91 92
{
    const Utf8String translationUnitId = Utf8StringLiteral("myId");
    ::TranslationUnitUpdater updater = createUpdater(createInput(SetReparseNeeded), translationUnitId);

    TranslationUnitUpdateResult result = updater.update(::TranslationUnitUpdater::UpdateMode::AsNeeded);

    ASSERT_THAT(result.translationUnitId, Eq(translationUnitId));
}

93
TEST_F(TranslationUnitUpdaterSlowTest, UpdatesParseTimePoint)
94 95
{
    ::TranslationUnitUpdater updater = createUpdater(createInput());
96
    const TimePoint now = Clock::now();
97 98 99

    TranslationUnitUpdateResult result = updater.update(::TranslationUnitUpdater::UpdateMode::AsNeeded);

100
    ASSERT_TRUE(result.hasParsed());
101 102 103
    ASSERT_THAT(result.parseTimePoint, Gt(now));
}

104
TEST_F(TranslationUnitUpdaterSlowTest, NotUpdatingParseTimePointForReparseOnly)
105 106 107 108 109 110 111
{
    ::TranslationUnitUpdater updater = createUpdater(createInput());
    TranslationUnitUpdateResult result = updater.update(::TranslationUnitUpdater::UpdateMode::AsNeeded);

    ::TranslationUnitUpdater reparseUpdater = createUpdater(createInput(SetReparseNeeded));
    result = reparseUpdater.update(::TranslationUnitUpdater::UpdateMode::AsNeeded);

112 113
    ASSERT_TRUE(result.hasReparsed());
    ASSERT_FALSE(result.hasParsed());
114 115
}

116
TEST_F(TranslationUnitUpdaterSlowTest, UpdatesDependendOnFilesOnParse)
117 118 119 120 121 122 123 124 125 126 127 128 129 130
{
    ::TranslationUnitUpdater updater = createUpdater(createInput());

    TranslationUnitUpdateResult result = updater.update(::TranslationUnitUpdater::UpdateMode::AsNeeded);

    ASSERT_FALSE(result.dependedOnFilePaths.isEmpty());
}

void TranslationUnitUpdater::TearDown()
{
    clang_disposeIndex(cxIndex);
}

::TranslationUnitUpdater
131 132
TranslationUnitUpdater::createUpdater(const TranslationUnitUpdateInput &input,
                                      const Utf8String &translationUnitId)
133
{
134
    return ::TranslationUnitUpdater(translationUnitId, cxIndex, cxTranslationUnit, input);
135 136 137 138 139 140 141 142 143 144 145 146 147
}

TranslationUnitUpdateInput
TranslationUnitUpdater::createInput(ReparseMode reparseMode)
{
    TranslationUnitUpdateInput updateInput;
    updateInput.filePath = filePath;
    updateInput.reparseNeeded = reparseMode == SetReparseNeeded;

    return updateInput;
}

} // anonymous