2022-01-12 09:40:08 +00:00
|
|
|
// 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,
|
|
|
|
};
|
2022-06-30 14:06:54 +00:00
|
|
|
|
|
|
|
std::string formatMessage(std::string_view file,
|
|
|
|
int line,
|
|
|
|
std::string_view message);
|
|
|
|
|
|
|
|
void completeTestFunction(TestResult result, std::string message);
|
2022-07-06 21:01:59 +00:00
|
|
|
void completeTestFunction();
|
2022-01-12 09:40:08 +00:00
|
|
|
void initTestCase(QObject *testObject, std::function<void ()> cleanup);
|
|
|
|
template <typename App>
|
2022-06-30 14:06:54 +00:00
|
|
|
void initTestCase(int argc,
|
|
|
|
char **argv,
|
|
|
|
std::shared_ptr<QObject> testObject)
|
2022-01-12 09:40:08 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2022-06-30 14:06:54 +00:00
|
|
|
void verify(bool condition,
|
|
|
|
std::string_view conditionString,
|
|
|
|
std::string_view file,
|
|
|
|
int line);
|
2022-01-12 09:40:08 +00:00
|
|
|
|
2022-06-30 14:06:54 +00:00
|
|
|
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)));
|
|
|
|
}
|
2022-01-12 09:40:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 14:06:54 +00:00
|
|
|
} // namespace QtWasmTest
|
|
|
|
|
|
|
|
#define QWASMVERIFY(condition) \
|
|
|
|
QtWasmTest::verify((condition), #condition, __FILE__, __LINE__);
|
2022-01-12 09:40:08 +00:00
|
|
|
|
2022-06-30 14:06:54 +00:00
|
|
|
#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
|