summaryrefslogtreecommitdiffstats
path: root/tests/manual/wasm/qtwasmtestlib/qtwasmtestlib.h
blob: 5dcc41419efc6a0ca3688da84d58ed031fba2d59 (plain)
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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#ifndef QT_WASM_TESTRUNNER_H
#define QT_WASM_TESTRUNNER_H

#include <QtCore/qobject.h>

#include <functional>

namespace QtWasmTest {

enum TestResult {
    Pass,
    Fail,
    Skip,
};

std::string formatMessage(std::string_view file,
                          int line,
                          std::string_view message);

void completeTestFunction(TestResult result, std::string message);
void completeTestFunction();
void initTestCase(QObject *testObject, std::function<void ()> cleanup);
template <typename App>
void initTestCase(int argc,
                  char **argv,
                std::shared_ptr<QObject> testObject)
{
    auto app = std::make_shared<App>(argc, argv);
    auto cleanup = [testObject, app]() mutable {
        // C++ lambda capture destruction order is unspecified;
        // delete test before app by calling reset().
        testObject.reset();
        app.reset();
    };
    initTestCase(testObject.get(), cleanup);
}
void verify(bool condition,
            std::string_view conditionString,
            std::string_view file,
            int line);

template<class L, class R>
void compare(const L& lhs,
             const R& rhs,
             std::string_view lhsString,
             std::string_view rhsString,
             std::string_view file,
             int line) {
    if (lhs != rhs) {
        completeTestFunction(
            TestResult::Fail,
            formatMessage(file, line, "Comparison failed: " + std::string(lhsString) + " == " + std::string(rhsString)));
    }
}

}  // namespace QtWasmTest

#define QWASMVERIFY(condition) \
    QtWasmTest::verify((condition), #condition, __FILE__, __LINE__);

#define QWASMCOMPARE(left, right) \
    QtWasmTest::compare((left), (right), #left, #right, __FILE__, __LINE__);

#define QWASMSUCCESS() \
    QtWasmTest::completeTestFunction(QtWasmTest::Pass, "")

#define QWASMFAIL(message) \
    QtWasmTest::completeTestFunction(QtWasmTest::Fail, QtWasmTest::formatMessage(__FILE__, __LINE__, message))

#endif