clangfollowsymbol-test.cpp 12.1 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 28
/****************************************************************************
**
** Copyright (C) 2017 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 "testenvironment.h"

29
#include <clangsupport_global.h>
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 74 75 76
#include <clangfollowsymboljob.h>
#include <clangdocument.h>
#include <clangdocuments.h>
#include <clangtranslationunit.h>
#include <fixitcontainer.h>
#include <projectpart.h>
#include <projects.h>
#include <sourcelocationcontainer.h>
#include <sourcerangecontainer.h>
#include <unsavedfiles.h>
#include <commandlinearguments.h>

#include <utils/qtcassert.h>

#include <clang-c/Index.h>

using ::testing::Contains;
using ::testing::Not;
using ::testing::ContainerEq;
using ::testing::Eq;
using ::testing::PrintToString;

using ::ClangBackEnd::ProjectPart;
using ::ClangBackEnd::SourceLocationContainer;
using ::ClangBackEnd::Document;
using ::ClangBackEnd::UnsavedFiles;
using ::ClangBackEnd::ReferencesResult;
using ::ClangBackEnd::SourceRangeContainer;

namespace {
const Utf8String sourceFilePath = Utf8StringLiteral(TESTDATA_DIR"/followsymbol_main.cpp");
const Utf8String headerFilePath = Utf8StringLiteral(TESTDATA_DIR"/followsymbol_header.h");
const Utf8String cursorPath = Utf8StringLiteral(TESTDATA_DIR"/cursor.cpp");

MATCHER_P3(MatchesHeaderSourceRange, line, column, length,
           std::string(negation ? "isn't " : "is ")
           + PrintToString(SourceRangeContainer {
                               SourceLocationContainer(headerFilePath, line, column),
                               SourceLocationContainer(headerFilePath, line, column + length)
                           })
           )
{
    const SourceRangeContainer expected = {
        SourceLocationContainer(headerFilePath, line, column),
        SourceLocationContainer(headerFilePath, line, column + length)
    };

77
    return arg == expected;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
}

MATCHER_P3(MatchesSourceRange, line, column, length,
           std::string(negation ? "isn't " : "is ")
           + PrintToString(SourceRangeContainer {
                               SourceLocationContainer(sourceFilePath, line, column),
                               SourceLocationContainer(sourceFilePath, line, column + length)
                           })
           )
{
    const SourceRangeContainer expected = {
        SourceLocationContainer(sourceFilePath, line, column),
        SourceLocationContainer(sourceFilePath, line, column + length)
    };

93
    return arg == expected;
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
}

MATCHER_P4(MatchesFileSourceRange, filename, line, column, length,
           std::string(negation ? "isn't " : "is ")
           + PrintToString(SourceRangeContainer {
                               SourceLocationContainer(filename, line, column),
                               SourceLocationContainer(filename, line, column + length)
                           })
           )
{
    const SourceRangeContainer expected = {
        SourceLocationContainer(filename, line, column),
        SourceLocationContainer(filename, line, column + length)
    };

109
    return arg == expected;
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
}

class Data {
public:
    Data()
    {
        m_document.parse();
        m_headerDocument.parse();
    }

    const Document &document() const { return m_document; }
    const Document &headerDocument() const { return m_headerDocument; }
    const QVector<Utf8String> &deps() const { return m_deps; }
private:
    ProjectPart m_projectPart{
        Utf8StringLiteral("projectPartId"),
                TestEnvironment::addPlatformArguments({Utf8StringLiteral("-std=c++14")})};
    ClangBackEnd::ProjectParts m_projects;
    ClangBackEnd::UnsavedFiles m_unsavedFiles;
    ClangBackEnd::Documents m_documents{m_projects, m_unsavedFiles};
    Document m_document = {sourceFilePath,
                           m_projectPart,
                           Utf8StringVector(),
                           m_documents};
    Document m_headerDocument = {headerFilePath,
                                 m_projectPart,
                                 Utf8StringVector(),
                                 m_documents};
    QVector<Utf8String> m_deps {sourceFilePath, cursorPath};
};

class FollowSymbol : public ::testing::Test
{
protected:
144
    SourceRangeContainer followSymbol(uint line, uint column)
145 146 147 148 149 150 151 152 153 154 155
    {
        ClangBackEnd::TranslationUnitUpdateInput updateInput = d->document().createUpdateInput();
        const ClangBackEnd::CommandLineArguments currentArgs(updateInput.filePath.constData(),
                                                             updateInput.projectArguments,
                                                             updateInput.fileArguments,
                                                             false);
        return d->document().translationUnit().followSymbol(line, column,
                                                            d->deps(),
                                                            currentArgs);
    }

156
    SourceRangeContainer followHeaderSymbol(uint line, uint column)
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    {
        ClangBackEnd::TranslationUnitUpdateInput updateInput
                = d->headerDocument().createUpdateInput();
        const ClangBackEnd::CommandLineArguments currentArgs(updateInput.filePath.constData(),
                                                             updateInput.projectArguments,
                                                             updateInput.fileArguments,
                                                             false);
        return d->headerDocument().translationUnit().followSymbol(line, column,
                                                                  d->deps(),
                                                                  currentArgs);
    }

    static void SetUpTestCase();
    static void TearDownTestCase();

private:
    static std::unique_ptr<Data> d;
};

TEST_F(FollowSymbol, CursorOnNamespace)
{
    const auto namespaceDefinition = followSymbol(27, 1);

    ASSERT_THAT(namespaceDefinition, MatchesHeaderSourceRange(28, 11, 6));
}

TEST_F(FollowSymbol, CursorOnClassReference)
{
    const auto classDefinition = followSymbol(27, 9);

    ASSERT_THAT(classDefinition, MatchesHeaderSourceRange(34, 7, 3));
}

TEST_F(FollowSymbol, CursorOnClassForwardDeclarationFollowToHeader)
{
    const auto classDefinition = followHeaderSymbol(32, 7);

    ASSERT_THAT(classDefinition, MatchesHeaderSourceRange(34, 7, 3));
}

TEST_F(FollowSymbol, CursorOnClassForwardDeclarationFollowToCpp)
{
    const auto classDefinition = followHeaderSymbol(48, 9);

    ASSERT_THAT(classDefinition, MatchesSourceRange(54, 7, 8));
}

TEST_F(FollowSymbol, CursorOnClassDefinition)
{
    const auto classForwardDeclaration = followHeaderSymbol(34, 7);

    ASSERT_THAT(classForwardDeclaration, MatchesHeaderSourceRange(32, 7, 3));
}

TEST_F(FollowSymbol, CursorOnClassDefinitionInCpp)
{
    const auto classForwardDeclaration = followSymbol(54, 7);

    ASSERT_THAT(classForwardDeclaration, MatchesHeaderSourceRange(48, 7, 8));
}

TEST_F(FollowSymbol, CursorOnConstructorDeclaration)
{
    const auto constructorDefinition = followHeaderSymbol(36, 5);

    ASSERT_THAT(constructorDefinition, MatchesSourceRange(27, 14, 3));
}

TEST_F(FollowSymbol, CursorOnConstructorDefinition)
{
    const auto constructorDeclaration = followSymbol(27, 14);

    ASSERT_THAT(constructorDeclaration, MatchesHeaderSourceRange(36, 5, 3));
}

TEST_F(FollowSymbol, CursorOnMemberReference)
{
    const auto memberDeclaration = followSymbol(39, 10);

    ASSERT_THAT(memberDeclaration, MatchesHeaderSourceRange(38, 18, 6));
}

TEST_F(FollowSymbol, CursorOnMemberDeclaration)
{
    const auto sameMemberDeclaration = followHeaderSymbol(38, 18);

    ASSERT_THAT(sameMemberDeclaration, MatchesHeaderSourceRange(38, 18, 6));
}

TEST_F(FollowSymbol, CursorOnFunctionReference)
{
    const auto functionDefinition = followSymbol(66, 12);

    ASSERT_THAT(functionDefinition, MatchesSourceRange(35, 5, 3));
}

TEST_F(FollowSymbol, CursorOnMemberFunctionReference)
{
    const auto memberFunctionDefinition = followSymbol(42, 12);

    ASSERT_THAT(memberFunctionDefinition, MatchesSourceRange(49, 21, 3));
}

TEST_F(FollowSymbol, CursorOnFunctionWithoutDefinitionReference)
{
    const auto functionDeclaration = followSymbol(43, 5);

    ASSERT_THAT(functionDeclaration, MatchesHeaderSourceRange(59, 5, 3));
}

TEST_F(FollowSymbol, CursorOnFunctionDefinition)
{
    const auto functionDeclaration = followSymbol(35, 5);

    ASSERT_THAT(functionDeclaration, MatchesHeaderSourceRange(52, 5, 3));
}

TEST_F(FollowSymbol, CursorOnMemberFunctionDefinition)
{
    const auto memberFunctionDeclaration = followSymbol(49, 21);

    ASSERT_THAT(memberFunctionDeclaration, MatchesHeaderSourceRange(43, 9, 3));
}

TEST_F(FollowSymbol, CursorOnMemberFunctionDeclaration)
{
    const auto memberFunctionDefinition = followHeaderSymbol(43, 9);

    ASSERT_THAT(memberFunctionDefinition, MatchesSourceRange(49, 21, 3));
}

TEST_F(FollowSymbol, CursorOnInclude)
{
    const auto startOfIncludeFile = followSymbol(25, 13);

    ASSERT_THAT(startOfIncludeFile, MatchesHeaderSourceRange(1, 1, 0));
}

TEST_F(FollowSymbol, CursorOnLocalVariable)
{
    const auto variableDeclaration = followSymbol(39, 6);

    ASSERT_THAT(variableDeclaration, MatchesSourceRange(36, 9, 3));
}

TEST_F(FollowSymbol, CursorOnClassAlias)
{
    const auto aliasDefinition = followSymbol(36, 5);

    ASSERT_THAT(aliasDefinition, MatchesSourceRange(33, 7, 3));
}

TEST_F(FollowSymbol, CursorOnStaticVariable)
{
    const auto staticVariableDeclaration = followSymbol(40, 27);

    ASSERT_THAT(staticVariableDeclaration, MatchesHeaderSourceRange(30, 7, 7));
}

TEST_F(FollowSymbol, CursorOnFunctionFromOtherClass)
{
    const auto functionDefinition = followSymbol(62, 39);

    ASSERT_THAT(functionDefinition, MatchesFileSourceRange(cursorPath, 104, 22, 8));
}

TEST_F(FollowSymbol, CursorOnDefineReference)
{
    const auto functionDefinition = followSymbol(66, 43);

    ASSERT_THAT(functionDefinition, MatchesHeaderSourceRange(27, 9, 11));
}


TEST_F(FollowSymbol, CursorInTheMiddleOfNamespace)
{
    const auto namespaceDefinition = followSymbol(27, 3);

    ASSERT_THAT(namespaceDefinition, MatchesHeaderSourceRange(28, 11, 6));
}

TEST_F(FollowSymbol, CursorAfterNamespace)
{
    const auto namespaceDefinition = followSymbol(27, 7);

    ASSERT_THAT(namespaceDefinition, MatchesFileSourceRange(QString(""), 0, 0, 0));
}

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
TEST_F(FollowSymbol, CursorOnOneSymbolOperatorDefinition)
{
    const auto namespaceDefinition = followSymbol(76, 13);

    ASSERT_THAT(namespaceDefinition, MatchesSourceRange(72, 9, 9));
}

TEST_F(FollowSymbol, CursorOnTwoSymbolOperatorDefinition)
{
    const auto namespaceDefinition = followSymbol(80, 15);

    ASSERT_THAT(namespaceDefinition, MatchesSourceRange(73, 10, 10));
}

TEST_F(FollowSymbol, CursorOnOneSymbolOperatorDeclaration)
{
    const auto namespaceDefinition = followSymbol(72, 12);

    ASSERT_THAT(namespaceDefinition, MatchesSourceRange(76, 10, 9));
}

TEST_F(FollowSymbol, CursorOnTwoSymbolOperatorDeclaration)
{
    const auto namespaceDefinition = followSymbol(73, 12);

    ASSERT_THAT(namespaceDefinition, MatchesSourceRange(80, 11, 10));
}

373 374 375 376 377 378 379 380 381 382 383 384 385
std::unique_ptr<Data> FollowSymbol::d;

void FollowSymbol::SetUpTestCase()
{
    d.reset(new Data);
}

void FollowSymbol::TearDownTestCase()
{
    d.reset();
}

} // anonymous namespace