File: jsondeclarationtests.h

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (377 lines) | stat: -rw-r--r-- 14,669 bytes parent folder | download
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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
144
145
146
147
148
149
150
151
152
153
154
155
156
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
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
373
374
375
376
377
/* This file is part of KDevelop
   Copyright 2012 Olivier de Gaalon <olivier.jg@gmail.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#ifndef KDEVPLATFORM_JSONDECLARATIONTESTS_H
#define KDEVPLATFORM_JSONDECLARATIONTESTS_H

#include "language/duchain/ducontext.h"
#include "language/duchain/declaration.h"
#include "language/duchain/indexeddeclaration.h"
#include "language/duchain/identifier.h"
#include "language/duchain/abstractfunctiondeclaration.h"
#include "language/duchain/types/typeutils.h"
#include "language/duchain/types/identifiedtype.h"
#include <language/duchain/types/functiontype.h>
#include "language/duchain/duchain.h"
#include "language/duchain/functiondefinition.h"
#include "language/duchain/definitions.h"
#include <language/duchain/classfunctiondeclaration.h>
#include "jsontesthelpers.h"

/**
 * JSON Object Specification:
 *   DeclTestObject: Mapping of (string) declaration test names to values
 *   TypeTestObject: Mapping of (string) type test names to values
 *   CtxtTestObject: Mapping of (string) context test names to values
 *
 * Quick Reference:
 *   useCount : int
 *   useRanges : string array
 *   identifier : string
 *   qualifiedIdentifier : string
 *   internalContext : CtxtTestObject
 *   internalFunctionContext : CtxtTestObject
 *   type : TypeTestObject
 *   unaliasedType : TypeTestObject
 *   targetType : TypeTestObject
 *   returnType : TypeTestObject
 *   isAbstract : bool
 *   isMutable : bool
 *   isVirtual : bool
 *   isStatic : bool
 *   declaration : DeclTestObject
 *   definition : DeclTestObject
 *   identifiedTypeDeclaration : DeclTestObject
 *   null : bool
 *   defaultParameter : string
 *   toString : string
 *   range : string
 *   kind : string
 *   isDeprecated : bool
 *   isDefinition : bool
 *   isExplicitlyTyped : bool
 */

namespace KDevelop {
template<>
QString TestSuite<Declaration*>::objectInformation(Declaration* decl)
{
    VERIFY_NOT_NULL(decl);
    return QStringLiteral("(Declaration on line %1 in %2)")
           .arg(decl->range().start.line + 1)
           .arg(decl->topContext()->url().str());
}

namespace DeclarationTests {
using namespace JsonTestHelpers;

///JSON type: int
///@returns whether the declaration's number of uses matches the given value
DeclarationTest(useCount)
{
    int uses = 0;
    const auto declarationUses = decl->uses();
    for (const auto& useRanges : declarationUses) {
        uses += useRanges.size();
    }

    return compareValues(uses, value, QStringLiteral("Declaration's use count "));
}
///JSON type: string array
///@returns whether the declaration's ranges match the given value
DeclarationTest(useRanges)
{
    QStringList ranges;
    const auto declarationUses = decl->uses();
    for (const auto& useRanges : declarationUses) {
        for (const RangeInRevision range : useRanges) {
            ranges << rangeStr(range);
        }
    }

    const QStringList testValues = value.toStringList();
    return ranges == testValues ? SUCCESS()
           : QStringLiteral("Declaration's use ranges (\"%1\") don't match test data (\"%2\").").arg(ranges.join(
                                                                                                         QStringLiteral(
                                                                                                             ", ")),
                                                                                                     testValues.join(
                                                                                                         QStringLiteral(
                                                                                                             ", ")));
}
///JSON type: string
///@returns whether the declaration's identifier matches the given value
DeclarationTest(identifier)
{
    VERIFY_NOT_NULL(decl);
    return compareValues(decl->identifier().toString(), value, QStringLiteral("Declaration's identifier"));
}
///JSON type: string
///@returns whether the declaration's qualified identifier matches the given value
DeclarationTest(qualifiedIdentifier)
{
    VERIFY_NOT_NULL(decl);
    return compareValues(decl->qualifiedIdentifier().toString(), value,
                         QStringLiteral("Declaration's qualified identifier"));
}
///JSON type: CtxtTestObject
///@returns whether the tests for the declaration's internal context pass
DeclarationTest(internalContext)
{
    VERIFY_NOT_NULL(decl);
    return testObject(decl->internalContext(), value, QStringLiteral("Declaration's internal context"));
}
///JSON type: CtxtTestObject
///@returns whether the tests for the declaration's internal function context pass
DeclarationTest(internalFunctionContext)
{
    const QString NO_INTERNAL_CTXT = QStringLiteral("%1 has no internal function context.");
    auto* absFuncDecl = dynamic_cast<AbstractFunctionDeclaration*>(decl);
    if (!absFuncDecl || !absFuncDecl->internalFunctionContext())
        return NO_INTERNAL_CTXT.arg(decl->qualifiedIdentifier().toString());
    return testObject(absFuncDecl->internalFunctionContext(), value,
                      QStringLiteral("Declaration's internal function context"));
}
/*FIXME: The type functions need some renaming and moving around
 * Some (all?) functions from cpp's TypeUtils should be moved to the kdevplatform type utils
 * targetType is exactly like realType except it also tosses pointers
 * shortenTypeForViewing should go to type utils (and it's broken, it places const to the left of all *'s when it should be left or right of the type)
 * UnaliasedType seems to be unable to understand aliases involving templates, perhaps a cpp version is in order
 */
///JSON type: TypeTestObject
///@returns whether the tests for the declaration's type pass
DeclarationTest(type)
{
    VERIFY_NOT_NULL(decl);
    return testObject(decl->abstractType(), value, QStringLiteral("Declaration's type"));
}
///JSON type: TypeTestObject
///@returns whether the tests for the declaration's unaliased type pass (TypeUtils::unaliasedType)
DeclarationTest(unaliasedType)
{
    VERIFY_NOT_NULL(decl);
    return testObject(TypeUtils::unAliasedType(decl->abstractType()), value,
                      QStringLiteral("Declaration's unaliased type"));
}
///JSON type: TypeTestObject
///@returns whether the tests for the declaration's target type pass (TypeUtils::targetType)
DeclarationTest(targetType)
{
    VERIFY_NOT_NULL(decl);
    return testObject(TypeUtils::targetType(decl->abstractType(), decl->topContext()), value,
                      QStringLiteral("Declaration's target type"));
}
///JSON type: TestTypeObject
///@returns the
DeclarationTest(returnType)
{
    FunctionType::Ptr functionType = decl->abstractType().cast<FunctionType>();
    AbstractType::Ptr returnType;
    if (functionType) {
        returnType = functionType->returnType();
    }
    return testObject(returnType, value, QStringLiteral("Declaration's return type"));
}
///JSON type: DeclTestObject
///@returns The IdentifiedType's declaration
DeclarationTest(identifiedTypeDeclaration)
{
    const QString UN_ID_ERROR = QStringLiteral("Unable to identify declaration of type \"%1\".");
    AbstractType::Ptr type = TypeUtils::targetType(decl->abstractType(), decl->topContext());
    auto* idType = dynamic_cast<IdentifiedType*>(type.data());
    Declaration* idDecl = idType ? idType->declaration(decl->topContext()) : nullptr;
    if (!idDecl)
        return UN_ID_ERROR.arg(type->toString());

    return testObject(idDecl, value, QStringLiteral("IdentifiedType's declaration"));
}
///JSON type: bool
///@returns whether the (function) declaration's isVirtual matches the given value
DeclarationTest(isVirtual)
{
    const QString NOT_A_FUNCTION = QStringLiteral("Non-function declaration cannot be virtual.");
    auto* absFuncDecl = dynamic_cast<AbstractFunctionDeclaration*>(decl);
    if (!absFuncDecl)
        return NOT_A_FUNCTION;

    return compareValues(absFuncDecl->isVirtual(), value, QStringLiteral("Declaration's isVirtual"));
}

///JSON type: bool
///@returns whether the (function) declaration's isAbstract matches the given value
DeclarationTest(isAbstract)
{
    const QString NOT_A_FUNCTION = QStringLiteral("Non-class-function declaration cannot be abstract.");
    auto* absFuncDecl = dynamic_cast<ClassFunctionDeclaration*>(decl);
    if (!absFuncDecl)
        return NOT_A_FUNCTION;

    return compareValues(absFuncDecl->isAbstract(), value, QStringLiteral("Declaration's isAbstract"));
}
///JSON type: bool
///@returns whether the (function) declaration's isAbstract matches the given value
DeclarationTest(isFinal)
{
    const QString NOT_A_FUNCTION = QStringLiteral("Non-class-function declaration cannot be final.");
    auto* classFuncDecl = dynamic_cast<ClassFunctionDeclaration*>(decl);
    if (!classFuncDecl)
        return NOT_A_FUNCTION;

    return compareValues(classFuncDecl->isFinal(), value, QStringLiteral("Declaration's isFinal"));
}
///JSON type: bool
///@returns whether the (class-member) declaration's isStatic matches the given value
DeclarationTest(isStatic)
{
    const QString NOT_A_MEMBER = QStringLiteral("Non-class-member declaration cannot be static.");
    auto memberDecl = dynamic_cast<ClassMemberDeclaration*>(decl);
    if (!memberDecl)
        return NOT_A_MEMBER;

    return compareValues(memberDecl->isStatic(), value, QStringLiteral("Declaration's isStatic"));
}
///JSON type: bool
///@returns whether the (class-member) declaration's isMutable matches the given value
DeclarationTest(isMutable)
{
    const QString NOT_A_MEMBER = QStringLiteral("Non-class-member declaration cannot be mutable.");
    auto memberDecl = dynamic_cast<ClassMemberDeclaration*>(decl);
    if (!memberDecl)
        return NOT_A_MEMBER;

    return compareValues(memberDecl->isMutable(), value, QStringLiteral("Declaration's isMutable"));
}
///JSON type: DeclTestObject
///@returns whether the tests for the function declaration's definition pass
DeclarationTest(definition)
{
    KDevVarLengthArray<IndexedDeclaration> definitions = DUChain::definitions()->definitions(decl->id());
    Declaration* declDef = nullptr;
    if (!definitions.isEmpty())
        declDef = definitions.at(0).declaration();
    return testObject(declDef, value, QStringLiteral("Declaration's definition"));
}
///JSON type: DeclTestObject
///@returns whether the tests for the function definition's declaration pass
DeclarationTest(declaration)
{
    auto* def = dynamic_cast<FunctionDefinition*>(decl);
    Declaration* defDecl = nullptr;
    if (def)
        defDecl = def->declaration(decl->topContext());
    return testObject(defDecl, value, QStringLiteral("Definition's declaration"));
}
///JSON type: bool
///@returns whether the declaration's nullity matches the given value
DeclarationTest(null)
{
    return compareValues(decl == nullptr, value, QStringLiteral("Declaration's nullity"));
}
///JSON type: bool
///@returns whether the declaration's default parameter matches the given value
DeclarationTest(defaultParameter)
{
    const QString NOT_IN_FUNC_CTXT = QStringLiteral(
        "Asked for a default parameter for a declaration outside of a function context.");
    const QString OWNER_NOT_FUNC = QStringLiteral(
        "Function context not owned by function declaration (what on earth did you do?).");
    DUContext* context = decl->context();
    if (!context || context->type() != DUContext::Function)
        return NOT_IN_FUNC_CTXT;
    auto* funcDecl = dynamic_cast<AbstractFunctionDeclaration*>(context->owner());
    if (!funcDecl)
        return OWNER_NOT_FUNC;
    int argIndex = context->localDeclarations().indexOf(decl);
    return compareValues(funcDecl->defaultParameterForArgument(argIndex).str(), value,
                         QStringLiteral("Declaration's default parameter"));
}

///JSON type: string
///@returns stringified declaration
DeclarationTest(toString)
{
    VERIFY_NOT_NULL(decl);
    return compareValues(decl->toString(), value, QStringLiteral("Declaration's toString"));
}

///JSON type: string
///@returns stringified declaration range
DeclarationTest(range)
{
    VERIFY_NOT_NULL(decl);
    return compareValues(rangeStr(decl->range()), value, QStringLiteral("Declaration's range"));
}

///JSON type: string
///@returns stringified declaration kind
DeclarationTest(kind)
{
    VERIFY_NOT_NULL(decl);
    QString kind;
    switch (decl->kind()) {
    case KDevelop::Declaration::Alias:
        kind = QStringLiteral("Alias");
        break;
    case KDevelop::Declaration::Import:
        kind = QStringLiteral("Import");
        break;
    case KDevelop::Declaration::Instance:
        kind = QStringLiteral("Instance");
        break;
    case KDevelop::Declaration::Namespace:
        kind = QStringLiteral("Namespace");
        break;
    case KDevelop::Declaration::NamespaceAlias:
        kind = QStringLiteral("NamespaceAlias");
        break;
    case KDevelop::Declaration::Type:
        kind = QStringLiteral("Type");
        break;
    case KDevelop::Declaration::Macro:
        kind = QStringLiteral("Macro");
        break;
    }
    return compareValues(kind, value, QStringLiteral("Declaration's kind"));
}

///JSON type: bool
///@returns whether the declaration's isDeprecated matches the given value
DeclarationTest(isDeprecated)
{
    VERIFY_NOT_NULL(decl);
    return compareValues(decl->isDeprecated(), value, QStringLiteral("Declaration's isDeprecated"));
}

///JSON type: bool
///@returns whether the declaration's isDefinition matches the given value
DeclarationTest(isDefinition)
{
    VERIFY_NOT_NULL(decl);
    return compareValues(decl->isDefinition(), value, QStringLiteral("Declaration's isDefinition"));
}

///JSON type: bool
///@returns whether the declaration's isExplicitlyTyped matches the given value
DeclarationTest(isExplicitlyTyped)
{
    VERIFY_NOT_NULL(decl);
    return compareValues(decl->isExplicitlyTyped(), value, QStringLiteral("Declaration's isExplicitlyTyped"));
}
}
}

#endif //KDEVPLATFORM_JSONDECLARATIONTESTS_H