File indexing completed on 2024-05-12 04:38:44

0001 /*
0002     SPDX-FileCopyrightText: 2012 Olivier de Gaalon <olivier.jg@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef KDEVPLATFORM_JSONDUCONTEXTTESTS_H
0008 #define KDEVPLATFORM_JSONDUCONTEXTTESTS_H
0009 
0010 #include "language/duchain/ducontext.h"
0011 #include "jsontesthelpers.h"
0012 
0013 /**
0014  * JSON Object Specification:
0015  * FindDeclObject: Mapping of (string) search ids to DeclTestObjects
0016  * IndexDeclObject: Mapping of (string) declaration position indexes to DeclTestObjects
0017  * IndexCtxtObject: Mapping of (string) context position indexes to CtxtTestObjects
0018  *
0019  * Quick Reference:
0020  *   findDeclarations : FindDeclObject
0021  *   declarations : IndexDeclObject
0022  *   childCount : int
0023  *   localDeclarationCount : int
0024  *   type : string
0025  *   null : bool
0026  *   owner : DeclTestObject
0027  *   importedParents : IndexCtxtObject
0028  *   range : string
0029  */
0030 
0031 namespace KDevelop {
0032 namespace DUContextTests {
0033 using namespace JsonTestHelpers;
0034 
0035 ///JSON type: FindDeclObject
0036 ///@returns whether each declaration can be found and passes its tests
0037 ContextTest(findDeclarations)
0038 {
0039     VERIFY_TYPE(QVariantMap);
0040     const QString INVALID_ERROR = QStringLiteral("Attempted to test invalid context.");
0041     const QString NOT_FOUND_ERROR = QStringLiteral("Could not find declaration \"%1\".");
0042     const QString DECL_ERROR = QStringLiteral("Declaration found with \"%1\" did not pass tests.");
0043     if (!ctxt)
0044         return INVALID_ERROR;
0045     QVariantMap findDecls = value.toMap();
0046     for (QVariantMap::iterator it = findDecls.begin(); it != findDecls.end(); ++it) {
0047         QualifiedIdentifier searchId(it.key());
0048         QList<Declaration*> ret = ctxt->findDeclarations(searchId, CursorInRevision::invalid());
0049         if (!ret.size())
0050             return NOT_FOUND_ERROR.arg(it.key());
0051 
0052         if (!runTests(it.value().toMap(), ret.first()))
0053             return DECL_ERROR.arg(it.key());
0054     }
0055 
0056     return SUCCESS();
0057 }
0058 ///JSON type: IndexDeclObject
0059 ///@returns whether a declaration exists at each index and each declaration passes its tests
0060 ContextTest(declarations)
0061 {
0062     VERIFY_TYPE(QVariantMap);
0063     const QString INVALID_ERROR = QStringLiteral("Attempted to test invalid context.");
0064     const QString NOT_FOUND_ERROR = QStringLiteral("No declaration at index \"%1\".");
0065     const QString DECL_ERROR = QStringLiteral("Declaration at index \"%1\" did not pass tests.");
0066     if (!ctxt)
0067         return INVALID_ERROR;
0068     QVariantMap findDecls = value.toMap();
0069     for (QVariantMap::iterator it = findDecls.begin(); it != findDecls.end(); ++it) {
0070         int index = it.key().toInt();
0071         QVector<Declaration*> decls = ctxt->localDeclarations(nullptr);
0072         if (decls.size() <= index)
0073             return NOT_FOUND_ERROR;
0074 
0075         if (!runTests(it.value().toMap(), decls.at(index)))
0076             return DECL_ERROR.arg(it.key());
0077     }
0078 
0079     return SUCCESS();
0080 }
0081 ///JSON type: int
0082 ///@returns whether the number of child contexts matches the given value
0083 ContextTest(childCount)
0084 {
0085     return compareValues(ctxt->childContexts().size(), value, QStringLiteral("Context's child count"));
0086 }
0087 ///JSON type: int
0088 ///@returns whether the number of local declarations matches the given value
0089 ContextTest(localDeclarationCount)
0090 {
0091     return compareValues(ctxt->localDeclarations().size(), value, QStringLiteral("Context's local declaration count"));
0092 }
0093 ///JSON type: string
0094 ///@returns whether the context's type matches the given value
0095 ContextTest(type)
0096 {
0097     QString contextTypeString;
0098     switch (ctxt->type()) {
0099     case DUContext::Class: contextTypeString = QStringLiteral("Class"); break;
0100     case DUContext::Enum: contextTypeString = QStringLiteral("Enum"); break;
0101     case DUContext::Namespace: contextTypeString = QStringLiteral("Namespace"); break;
0102     case DUContext::Function: contextTypeString = QStringLiteral("Function"); break;
0103     case DUContext::Template: contextTypeString = QStringLiteral("Template"); break;
0104     case DUContext::Global: contextTypeString = QStringLiteral("Global"); break;
0105     case DUContext::Helper: contextTypeString = QStringLiteral("Helper"); break;
0106     case DUContext::Other: contextTypeString = QStringLiteral("Other"); break;
0107     }
0108     return compareValues(contextTypeString, value, QStringLiteral("Context's type"));
0109 }
0110 ///JSON type: bool
0111 ///@returns whether the context's nullity matches the given value
0112 ContextTest(null)
0113 {
0114     return compareValues(ctxt == nullptr, value, QStringLiteral("Context's nullity"));
0115 }
0116 
0117 //JSON type: DeclTestObject
0118 ///@returns the context's owner
0119 ContextTest(owner)
0120 {
0121     return testObject(ctxt->owner(), value, QStringLiteral("Context's owner"));
0122 }
0123 
0124 ///JSON type: IndexCtxtObject
0125 ///@returns whether a context exists at each index and each context passes its tests
0126 ContextTest(importedParents)
0127 {
0128     VERIFY_TYPE(QVariantMap);
0129     const QString INVALID_ERROR = QStringLiteral("Attempted to test invalid context.");
0130     const QString NOT_FOUND_ERROR = QStringLiteral("No imported context at index \"%1\".");
0131     const QString CONTEXT_ERROR = QStringLiteral("Context at index \"%1\" did not pass tests.");
0132     if (!ctxt)
0133         return INVALID_ERROR;
0134     QVariantMap findDecls = value.toMap();
0135     for (QVariantMap::iterator it = findDecls.begin(); it != findDecls.end(); ++it) {
0136         int index = it.key().toInt();
0137         QVector<DUContext::Import> imports = ctxt->importedParentContexts();
0138         if (imports.size() <= index)
0139             return NOT_FOUND_ERROR;
0140 
0141         if (!runTests(it.value().toMap(), imports.at(index).context(ctxt->topContext())))
0142             return CONTEXT_ERROR.arg(it.key());
0143     }
0144 
0145     return SUCCESS();
0146 }
0147 
0148 ///JSON type: string
0149 ///@returns stringified context range
0150 ContextTest(range)
0151 {
0152     if (!ctxt) {
0153         return QStringLiteral("Invalid Context");
0154     }
0155     return compareValues(rangeStr(ctxt->range()), value, QStringLiteral("Contexts's range"));
0156 }
0157 }
0158 }
0159 
0160 #endif //KDEVPLATFORM_JSONDUCONTEXTTESTS_H