completionchunkstotextconverter-test.cpp 16 KB
Newer Older
Nikolai Kosjar's avatar
Nikolai Kosjar committed
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
**
****************************************************************************/

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

28
#include <codecompletionchunk.h>
29
#include <clangcompletionchunkstotextconverter.h>
30 31 32

namespace {

33
using ClangBackEnd::CodeCompletionChunk;
34
using ClangBackEnd::CodeCompletionChunks;
35
using Converter = ClangCodeModel::Internal::CompletionChunksToTextConverter;
36 37 38

class CompletionChunksToTextConverter : public ::testing::Test
{
39 40
protected:
    Converter converter;
41
    CodeCompletionChunk integerResultType{CodeCompletionChunk::ResultType, Utf8StringLiteral("int")};
42
    CodeCompletionChunk templateResultType{CodeCompletionChunk::ResultType, Utf8StringLiteral("Foo<int>")};
43 44
    CodeCompletionChunk enumerationResultType{CodeCompletionChunk::ResultType, Utf8StringLiteral("Enumeration")};
    CodeCompletionChunk functionName{CodeCompletionChunk::TypedText, Utf8StringLiteral("Function")};
45
    CodeCompletionChunk namespaceName{CodeCompletionChunk::TypedText, Utf8StringLiteral("Namespace")};
46 47 48 49 50
    CodeCompletionChunk variableName{CodeCompletionChunk::TypedText, Utf8StringLiteral("Variable")};
    CodeCompletionChunk enumeratorName{CodeCompletionChunk::TypedText, Utf8StringLiteral("Enumerator")};
    CodeCompletionChunk enumerationName{CodeCompletionChunk::TypedText, Utf8StringLiteral("Enumeration")};
    CodeCompletionChunk className{CodeCompletionChunk::TypedText, Utf8StringLiteral("Class")};
    CodeCompletionChunk leftParen{CodeCompletionChunk::LeftParen, Utf8StringLiteral("(")};
51
    CodeCompletionChunk rightParen{CodeCompletionChunk::RightParen, Utf8StringLiteral(")")};
52
    CodeCompletionChunk comma{CodeCompletionChunk::Comma, Utf8StringLiteral(", ")};
53
    CodeCompletionChunk semicolon{CodeCompletionChunk::SemiColon, Utf8StringLiteral(";")};
54
    CodeCompletionChunk colonColonText{CodeCompletionChunk::Text, Utf8StringLiteral("::")};
55
    CodeCompletionChunk functionArgumentX{CodeCompletionChunk::Placeholder, Utf8StringLiteral("char x")};
56
    CodeCompletionChunk functionArgumentXAsCurrentParameter{CodeCompletionChunk::CurrentParameter, Utf8StringLiteral("char x")};
57
    CodeCompletionChunk functionArgumentY{CodeCompletionChunk::Placeholder, Utf8StringLiteral("int y")};
58
    CodeCompletionChunk functionArgumentYAsCurrentParamter{CodeCompletionChunk::CurrentParameter, Utf8StringLiteral("int y")};
59
    CodeCompletionChunk functionArgumentZ{CodeCompletionChunk::Placeholder, Utf8StringLiteral("int z")};
60
    CodeCompletionChunk functionArgumentTemplate{CodeCompletionChunk::Placeholder, Utf8StringLiteral("const Foo<int> &foo")};
61 62 63 64 65 66 67 68 69 70 71 72 73 74
    CodeCompletionChunk switchName{CodeCompletionChunk::TypedText, Utf8StringLiteral("switch")};
    CodeCompletionChunk condition{CodeCompletionChunk::Placeholder, Utf8StringLiteral("condition")};
    CodeCompletionChunk leftBrace{CodeCompletionChunk::LeftBrace, Utf8StringLiteral("{")};
    CodeCompletionChunk rightBrace{CodeCompletionChunk::RightBrace, Utf8StringLiteral("}")};
    CodeCompletionChunk verticalSpace{CodeCompletionChunk::VerticalSpace, Utf8StringLiteral("\n")};
    CodeCompletionChunk throwName{CodeCompletionChunk::TypedText, Utf8StringLiteral("throw")};
    CodeCompletionChunk voidResultType{CodeCompletionChunk::ResultType, Utf8StringLiteral("void")};
    CodeCompletionChunk forName{CodeCompletionChunk::TypedText, Utf8StringLiteral("for")};
    CodeCompletionChunk initStatement{CodeCompletionChunk::Placeholder, Utf8StringLiteral("init-statement")};
    CodeCompletionChunk initExpression{CodeCompletionChunk::Placeholder, Utf8StringLiteral("init-expression")};
    CodeCompletionChunk statements{CodeCompletionChunk::Placeholder, Utf8StringLiteral("statements")};
    CodeCompletionChunk constCastName{CodeCompletionChunk::TypedText, Utf8StringLiteral("const_cast")};
    CodeCompletionChunk leftAngle{CodeCompletionChunk::LeftAngle, Utf8StringLiteral("<")};
    CodeCompletionChunk rightAngle{CodeCompletionChunk::RightAngle, Utf8StringLiteral(">")};
75 76 77
    CodeCompletionChunk elseName{CodeCompletionChunk::TypedText, Utf8StringLiteral("else")};
    CodeCompletionChunk ifName{CodeCompletionChunk::TypedText, Utf8StringLiteral("if")};
    CodeCompletionChunk horizontalSpace{CodeCompletionChunk::HorizontalSpace, Utf8StringLiteral(" ")};
78 79
    CodeCompletionChunk enableIfT{CodeCompletionChunk::TypedText, Utf8StringLiteral("enable_if_t")};
    CodeCompletionChunk enableIfTCondition{CodeCompletionChunk::Placeholder, Utf8StringLiteral("_Cond")};
80 81 82
    CodeCompletionChunk  optionalEnableIfTType{CodeCompletionChunk::Placeholder, Utf8StringLiteral("_Tp"), true};
    CodeCompletionChunk optionalComma{CodeCompletionChunk::Comma, Utf8StringLiteral(", "), true};
    CodeCompletionChunk optionalFunctionArgumentY{CodeCompletionChunk::Placeholder, Utf8StringLiteral("int y"), true};
83
    CodeCompletionChunk optionalFunctionArgumentYAsCurrentParameter{CodeCompletionChunk::CurrentParameter, Utf8StringLiteral("int y"), true};
84
    CodeCompletionChunk optionalFunctionArgumentZ{CodeCompletionChunk::Placeholder, Utf8StringLiteral("int z"), true};
85 86 87 88
};

TEST_F(CompletionChunksToTextConverter, ParseIsClearingText)
{
89
    CodeCompletionChunks completionChunks({integerResultType, functionName, leftParen, rightParen});
90
    converter.setAddResultType(true);
91 92 93 94 95 96 97 98

    converter.parseChunks(completionChunks);

    ASSERT_THAT(converter.text(), QStringLiteral("int Function()"));
}

TEST_F(CompletionChunksToTextConverter, ConvertFunction)
{
99
    CodeCompletionChunks completionChunks({integerResultType, functionName, leftParen, rightParen});
100
    converter.setAddResultType(true);
101 102 103 104 105 106 107 108

    converter.parseChunks(completionChunks);

    ASSERT_THAT(converter.text(), QStringLiteral("int Function()"));
}

TEST_F(CompletionChunksToTextConverter, ConvertFunctionWithParameters)
{
109
    CodeCompletionChunks completionChunks({integerResultType, functionName, leftParen, functionArgumentX,rightParen});
110 111
    converter.setAddResultType(true);
    converter.setAddPlaceHolderText(true);
112 113 114 115 116 117

    converter.parseChunks(completionChunks);

    ASSERT_THAT(converter.text(), QStringLiteral("int Function(char x)"));
}

118 119 120 121 122
TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithOneArgument)
{
    CodeCompletionChunks completionChunks({integerResultType,
                                           functionName,
                                           leftParen,
123
                                            functionArgumentXAsCurrentParameter,
124 125
                                           rightParen});

126
    ASSERT_THAT(converter.convertToFunctionSignatureWithHtml(completionChunks),
127 128 129 130 131 132 133 134
                QStringLiteral("int Function(char x)"));
}

TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithOneParameterThatIsActive)
{
    CodeCompletionChunks completionChunks({integerResultType,
                                           functionName,
                                           leftParen,
135
                                            functionArgumentXAsCurrentParameter,
136 137
                                           rightParen});

138
    ASSERT_THAT(converter.convertToFunctionSignatureWithHtml(completionChunks, 1),
139 140 141 142 143 144 145 146 147 148
                QStringLiteral("int Function(<b>char x</b>)"));
}

TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithTwoParametersWhereOneIsActive)
{
    CodeCompletionChunks completionChunks({integerResultType,
                                           functionName,
                                           leftParen,
                                            functionArgumentX,
                                            comma,
149
                                            functionArgumentYAsCurrentParamter,
150 151
                                           rightParen});

152
    ASSERT_THAT(converter.convertToFunctionSignatureWithHtml(completionChunks, 2),
153 154 155 156 157 158 159 160 161 162
                QStringLiteral("int Function(char x, <b>int y</b>)"));
}

TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithTwoParametersWhereOneIsOptionalAndActive)
{
    CodeCompletionChunks completionChunks({integerResultType,
                                           functionName,
                                           leftParen,
                                            functionArgumentX,
                                            optionalComma,
163
                                            optionalFunctionArgumentYAsCurrentParameter,
164 165
                                           rightParen});

166
    ASSERT_THAT(converter.convertToFunctionSignatureWithHtml(completionChunks, 2),
167 168 169
                QStringLiteral("int Function(char x<i>, <b>int y</b></i>)"));
}

170 171 172 173 174 175 176 177 178 179
TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithTemplateReturnType)
{
    CodeCompletionChunks completionChunks({templateResultType,
                                           functionName,
                                           leftParen,
                                            functionArgumentX,
                                           rightParen});

    using ClangCodeModel::Internal::CompletionChunksToTextConverter;

180
    ASSERT_THAT(CompletionChunksToTextConverter::convertToFunctionSignatureWithHtml(completionChunks),
181 182 183 184 185 186 187 188 189 190 191 192 193
                QStringLiteral("Foo&lt;int&gt; Function(char x)"));
}

TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithTemplateArgument)
{
    CodeCompletionChunks completionChunks({integerResultType,
                                           functionName,
                                           leftParen,
                                            functionArgumentTemplate,
                                           rightParen});

    using ClangCodeModel::Internal::CompletionChunksToTextConverter;

194
    ASSERT_THAT(CompletionChunksToTextConverter::convertToFunctionSignatureWithHtml(completionChunks),
195 196 197
                QStringLiteral("int Function(const Foo&lt;int&gt; &amp;foo)"));
}

198 199
TEST_F(CompletionChunksToTextConverter, ConvertFunctionWithOptionalParameter)
{
200 201 202
    CodeCompletionChunks completionChunks({integerResultType,
                                           functionName,
                                           leftParen,
203 204 205 206 207
                                            functionArgumentX,
                                            optionalComma,
                                            optionalFunctionArgumentY,
                                            optionalComma,
                                            optionalFunctionArgumentZ,
208
                                           rightParen});
209

210
    ASSERT_THAT(Converter::convertToToolTipWithHtml(completionChunks),
211
                QStringLiteral("int Function (char x<i>, int y, int z</i>)"));
212 213 214 215
}

TEST_F(CompletionChunksToTextConverter, ConvertVariable)
{
216
    CodeCompletionChunks completionChunks({integerResultType, variableName});
217
    converter.setAddResultType(true);
218 219 220 221 222 223 224 225

    converter.parseChunks(completionChunks);

    ASSERT_THAT(converter.text(), QStringLiteral("int Variable"));
}

TEST_F(CompletionChunksToTextConverter, Enumerator)
{
226
    CodeCompletionChunks completionChunks({enumerationResultType, enumeratorName});
227
    converter.setAddResultType(true);
228 229 230 231 232 233 234 235

    converter.parseChunks(completionChunks);

    ASSERT_THAT(converter.text(), QStringLiteral("Enumeration Enumerator"));
}

TEST_F(CompletionChunksToTextConverter, Enumeration)
{
236
    CodeCompletionChunks completionChunks({className});
237 238 239 240 241 242

    converter.parseChunks(completionChunks);

    ASSERT_THAT(converter.text(), QStringLiteral("Class"));
}

243 244
TEST_F(CompletionChunksToTextConverter, Switch)
{
245
    CodeCompletionChunks completionChunks({switchName,
246 247 248 249 250 251
                                           leftParen,
                                           condition,
                                           rightParen,
                                           leftBrace,
                                           verticalSpace,
                                           rightBrace});
Nikolai Kosjar's avatar
Nikolai Kosjar committed
252
    converter.setupForKeywords();
253 254 255 256 257 258 259 260 261

    converter.parseChunks(completionChunks);

    ASSERT_THAT(converter.text(), QStringLiteral("switch () {\n\n}"));
    ASSERT_THAT(converter.placeholderPositions().at(0), 8);
}

TEST_F(CompletionChunksToTextConverter, For)
{
262
    CodeCompletionChunks completionChunks({forName,
263 264 265 266 267 268 269 270 271 272 273 274
                                           leftParen,
                                           initStatement,
                                           semicolon,
                                           initExpression,
                                           semicolon,
                                           condition,
                                           rightParen,
                                           leftBrace,
                                           verticalSpace,
                                           statements,
                                           verticalSpace,
                                           rightBrace});
Nikolai Kosjar's avatar
Nikolai Kosjar committed
275
    converter.setupForKeywords();
276 277 278 279 280 281 282 283

    converter.parseChunks(completionChunks);

    ASSERT_THAT(converter.text(), QStringLiteral("for (;;) {\n\n}"));
}

TEST_F(CompletionChunksToTextConverter, const_cast)
{
284
    CodeCompletionChunks completionChunks({constCastName,
285 286 287 288
                                           leftAngle,
                                           rightAngle,
                                           leftParen,
                                           rightParen});
Nikolai Kosjar's avatar
Nikolai Kosjar committed
289
    converter.setupForKeywords();
290 291 292

    converter.parseChunks(completionChunks);

293
    ASSERT_THAT(converter.text(), QStringLiteral("const_cast<>()"));
294 295 296 297
}

TEST_F(CompletionChunksToTextConverter, Throw)
{
298
    CodeCompletionChunks completionChunks({voidResultType, throwName});
299 300 301 302 303 304

    auto completionName = Converter::convertToName(completionChunks);

    ASSERT_THAT(completionName, QStringLiteral("throw"));
}

305 306
TEST_F(CompletionChunksToTextConverter, ElseIf)
{
307
    CodeCompletionChunks completionChunks({elseName,
308 309 310 311 312 313 314 315
                                           horizontalSpace,
                                           ifName,
                                           horizontalSpace,
                                           leftBrace,
                                           verticalSpace,
                                           statements,
                                           verticalSpace,
                                           rightBrace});
Nikolai Kosjar's avatar
Nikolai Kosjar committed
316
    converter.setupForKeywords();
317 318 319 320 321 322

    converter.parseChunks(completionChunks);

    ASSERT_THAT(converter.text(), QStringLiteral("else if {\n\n}"));
}

323 324 325 326 327
TEST_F(CompletionChunksToTextConverter, EnableIfT)
{
    CodeCompletionChunks completionChunks({enableIfT,
                                           leftAngle,
                                           enableIfTCondition,
328 329
                                           optionalComma,
                                           optionalEnableIfTType,
330
                                           rightAngle});
Nikolai Kosjar's avatar
Nikolai Kosjar committed
331
    converter.setupForKeywords();
332 333 334

    converter.parseChunks(completionChunks);

335
    ASSERT_THAT(converter.text(), QStringLiteral("enable_if_t<>"));
336 337
}

338 339 340 341 342 343 344 345 346
TEST_F(CompletionChunksToTextConverter, Namespace)
{
    CodeCompletionChunks completionChunks({namespaceName, colonColonText});

    converter.parseChunks(completionChunks);

    ASSERT_THAT(converter.text(), QStringLiteral("Namespace::"));
}

347
}