diff options
Diffstat (limited to 'tests/manual/wasm/qtwasmtestlib/qtwasmtestlib.cpp')
-rw-r--r-- | tests/manual/wasm/qtwasmtestlib/qtwasmtestlib.cpp | 70 |
1 files changed, 55 insertions, 15 deletions
diff --git a/tests/manual/wasm/qtwasmtestlib/qtwasmtestlib.cpp b/tests/manual/wasm/qtwasmtestlib/qtwasmtestlib.cpp index eeac82a266..ec03c7209a 100644 --- a/tests/manual/wasm/qtwasmtestlib/qtwasmtestlib.cpp +++ b/tests/manual/wasm/qtwasmtestlib/qtwasmtestlib.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2022 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include "qtwasmtestlib.h" @@ -10,9 +10,11 @@ #include <emscripten/threading.h> namespace QtWasmTest { - +namespace { QObject *g_testObject = nullptr; +std::string g_currentTestName; std::function<void ()> g_cleanup; +} void runOnMainThread(std::function<void(void)> fn); static bool isValidSlot(const QMetaMethod &sl); @@ -30,31 +32,56 @@ void initTestCase(QObject *testObject, std::function<void ()> cleanup) g_cleanup = cleanup; } +void verify(bool condition, std::string_view conditionString, std::string_view file, int line) +{ + if (!condition) { + completeTestFunction( + TestResult::Fail, + formatMessage(file, line, "Condition failed: " + std::string(conditionString))); + } +} + // Completes the currently running test function with a result. This function is // thread-safe and call be called from any thread. -void completeTestFunction(TestResult result) +void completeTestFunction(TestResult result, std::string message) { - // Report test result to JavaScript test runner, on the main thread - runOnMainThread([result](){ - const char *resultString; + auto resultString = [](TestResult result) { switch (result) { - case TestResult::Pass: - resultString = "PASS"; - break; - case TestResult::Fail: - resultString = "FAIL"; - break; - }; + case TestResult::Pass: + return "PASS"; + break; + case TestResult::Fail: + return "FAIL"; + break; + case TestResult::Skip: + return "SKIP"; + break; + } + }; + + // Report test result to JavaScript test runner, on the main thread + runOnMainThread([resultString = resultString(result), message](){ EM_ASM({ - completeTestFunction(UTF8ToString($0)); - }, resultString); + completeTestFunction(UTF8ToString($0), UTF8ToString($1), UTF8ToString($2)); + }, g_currentTestName.c_str(), resultString, message.c_str()); }); } +// Completes the currently running test function with a Pass result. +void completeTestFunction() +{ + completeTestFunction(TestResult::Pass, std::string()); +} + // // Private API for the Javascript test runnner // +std::string formatMessage(std::string_view file, int line, std::string_view message) +{ + return "[" + std::string(file) + ":" + QString::number(line).toStdString() + "] " + std::string(message); +} + void cleanupTestCase() { g_testObject = nullptr; @@ -83,13 +110,26 @@ std::string getTestFunctions() void runTestFunction(std::string name) { + g_currentTestName = name; QMetaObject::invokeMethod(g_testObject, name.c_str()); } +void failTest(std::string message) +{ + completeTestFunction(QtWasmTest::Fail, std::move(message)); +} + +void passTest() +{ + completeTestFunction(QtWasmTest::Pass, ""); +} + EMSCRIPTEN_BINDINGS(qtwebtestrunner) { emscripten::function("cleanupTestCase", &cleanupTestCase); emscripten::function("getTestFunctions", &getTestFunctions); emscripten::function("runTestFunction", &runTestFunction); + emscripten::function("qtWasmFail", &failTest); + emscripten::function("qtWasmPass", &passTest); } // |