File: jsontesthelpers.h

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (70 lines) | stat: -rw-r--r-- 2,734 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 2012 Olivier de Gaalon <olivier.jg@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#ifndef KDEVPLATFORM_JSONTESTHELPERS_H
#define KDEVPLATFORM_JSONTESTHELPERS_H

#include "testsuite.h"

#define VERIFY_NOT_NULL(ptr) \
    if (!ptr) \
        return JsonTestHelpers::INVALID_POINTER()

#define VERIFY_TYPE(qvariantType)                                                                                      \
    if (!value.canConvert<qvariantType>())                                                                             \
    return JsonTestHelpers::INVALID_JSON_TYPE().arg(QStringLiteral(#qvariantType),                                     \
                                                    QString::fromUtf8(value.metaType().name()))

#define __AddTest(testName, objType) \
    bool testName ## Added = KDevelop::TestSuite<objType>::get().addTest(QStringLiteral(# testName), &testName)

#define __DefineTest(testName, objType, objName) \
    QString testName(const QVariant&, objType); \
    __AddTest(testName, objType); \
    QString testName(const QVariant &value, objType objName)

#define DeclarationTest(testName) __DefineTest(testName, KDevelop::Declaration*, decl)
#define TypeTest(testName) __DefineTest(testName, KDevelop::AbstractType::Ptr, type)
#define ContextTest(testName) __DefineTest(testName, KDevelop::DUContext*, ctxt)

namespace KDevelop {
namespace JsonTestHelpers {
inline QString SUCCESS() { return QString(); }
inline QString INVALID_JSON_TYPE()
{
    return QStringLiteral("Incorrect JSON type provided for test. Actual: %1, Expected: %2");
}
inline QString INVALID_POINTER() { return QStringLiteral("Null pointer passed to test."); }

template<class Type>
inline QString compareValues(Type realValue, const QVariant& value, const QString& errorDesc)
{
    VERIFY_TYPE(Type);
    const QString ERROR_MESSAGE = QStringLiteral("%1 (\"%2\") doesn't match test data (\"%3\").");
    return realValue == value.value<Type>() ?
           SUCCESS() : ERROR_MESSAGE.arg(errorDesc).arg(realValue).arg(value.value<Type>());
}

template<class Object>
inline QString testObject(Object obj, const QVariant& value, const QString& errorDesc)
{
    VERIFY_TYPE(QVariantMap);
    const QString ERROR_MESSAGE = QStringLiteral("%1 did not pass tests.");
    return KDevelop::TestSuite<Object>::get().runTests(value.toMap(), obj) ? SUCCESS() : ERROR_MESSAGE.arg(errorDesc);
}

inline QString rangeStr(const KDevelop::RangeInRevision& range)
{
    return QStringLiteral("[(%1, %2), (%3, %4)]")
           .arg(range.start.line)
           .arg(range.start.column)
           .arg(range.end.line)
           .arg(range.end.column);
}
}
}

#endif //KDEVPLATFORM_JSONTESTHELPERS_H