diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-03-09 18:14:24 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-03-09 18:14:24 +0530 |
| commit | 0e276c49adc1ef38993f1a90b0f87c732331598c (patch) | |
| tree | 85d9fc7521c6f4d85e89596fbd2333f45087fb3a | |
| parent | 571ce3472d8d1277b0028dfd8118c6c8619afc34 (diff) | |
| download | null-browser-0e276c49adc1ef38993f1a90b0f87c732331598c.tar.gz null-browser-0e276c49adc1ef38993f1a90b0f87c732331598c.zip | |
Add test registry macro to allow managing multiple specs
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | spec/CommandInputSpec.cpp | 2 | ||||
| -rw-r--r-- | spec/TestUtils.cpp | 20 | ||||
| -rw-r--r-- | spec/TestUtils.h | 20 | ||||
| -rw-r--r-- | spec/main.cpp | 8 |
5 files changed, 50 insertions, 4 deletions
@@ -7,7 +7,7 @@ build: @cd build/ && cmake .. && make -j4 # cp --no-preserve=mode,ownership -r ${CEF_PACKAGE_PATH}/lib/* ./build/lib/ # cp --no-preserve=mode,ownership -r ${CEF_PACKAGE_PATH}/share/cef/* ./build/lib/ - cp build/compile_commands.json . + @cp build/compile_commands.json . test: build cd build && ctest -V @@ -18,5 +18,3 @@ clean: run: build ./build/web-browser - -# -DCMAKE_BUILD_TYPE=Release diff --git a/spec/CommandInputSpec.cpp b/spec/CommandInputSpec.cpp index f6f7f3a..5636a63 100644 --- a/spec/CommandInputSpec.cpp +++ b/spec/CommandInputSpec.cpp @@ -100,5 +100,5 @@ private slots: } }; -QTEST_MAIN(CommandInputSpec) +QTEST_REGISTER(CommandInputSpec) #include "CommandInputSpec.moc" diff --git a/spec/TestUtils.cpp b/spec/TestUtils.cpp new file mode 100644 index 0000000..e496a9b --- /dev/null +++ b/spec/TestUtils.cpp @@ -0,0 +1,20 @@ +#include <QtTest/QtTest> +#include <QtTest/qtestcase.h> +#include <cstdio> + +std::vector<std::function<QObject *()>> &getQTestRegistry() { + static std::vector<std::function<QObject *()>> registry; + return registry; +} + +int runAllTests() { + int exitCode = 0; + for (const auto &runTest : getQTestRegistry()) { + auto test = runTest(); + // printf(":::: %s\n", test->objectName().toStdString().c_str()); + exitCode |= QTest::qExec(test); + delete test; + } + getQTestRegistry().clear(); + return exitCode; +} diff --git a/spec/TestUtils.h b/spec/TestUtils.h index b2c1cc6..20b3ba8 100644 --- a/spec/TestUtils.h +++ b/spec/TestUtils.h @@ -1,4 +1,7 @@ +#pragma once + #include <QtTest/QtTest> +#include <QtTest/qtestcase.h> #include <cstdio> #define ANSI_BOLD "\x1b[1m" @@ -18,3 +21,20 @@ #define xit(msg) \ printf(" ⚪" COLOR_SKIP "SKIPPED it %s" ANSI_RESET "\n", msg); \ if (0) + +#define STRINGIFY(x) #x + +std::vector<std::function<QObject *()>> &getQTestRegistry(); +int runAllTests(); + +#define QTEST_REGISTER(klass) \ + namespace { \ + const bool registered__##klass = []() { \ + getQTestRegistry().push_back([]() { \ + auto t = new klass; \ + t->setObjectName(#klass); \ + return t; \ + }); \ + return true; \ + }(); \ + }; diff --git a/spec/main.cpp b/spec/main.cpp new file mode 100644 index 0000000..c085886 --- /dev/null +++ b/spec/main.cpp @@ -0,0 +1,8 @@ +#include "TestUtils.h" +#include <QtWidgets/qapplication.h> + +int main(int argc, char **argv) { + QApplication app(argc, argv); + + return runAllTests(); +} |
