LibTest: Add TEST_SETUP macro that runs before all test cases

This commit is contained in:
Peter Elliott 2021-08-29 11:55:05 -07:00 committed by Andreas Kling
parent b0bd4be59a
commit 83680934e5
Notes: sideshowbarker 2024-07-18 04:59:39 +09:00
3 changed files with 22 additions and 1 deletions

View file

@ -39,9 +39,17 @@ private:
// Helper to hide implementation of TestSuite from users
void add_test_case_to_suite(const NonnullRefPtr<TestCase>& test_case);
void set_suite_setup_function(Function<void()> setup);
}
#define TEST_SETUP \
static void __setup(); \
struct __setup_type { \
__setup_type() { Test::set_suite_setup_function(__setup); } \
}; \
static struct __setup_type __setup_type; \
static void __setup()
#define __TESTCASE_FUNC(x) __test_##x
#define __TESTCASE_TYPE(x) __TestCase_##x

View file

@ -7,6 +7,7 @@
#include <LibTest/Macros.h> // intentionally first -- we redefine VERIFY and friends in here
#include <AK/Function.h>
#include <LibCore/ArgsParser.h>
#include <LibTest/TestSuite.h>
#include <stdlib.h>
@ -49,6 +50,12 @@ void add_test_case_to_suite(const NonnullRefPtr<TestCase>& test_case)
TestSuite::the().add_case(test_case);
}
// Declared in TestCase.h
void set_suite_setup_function(Function<void()> setup)
{
TestSuite::the().set_suite_setup(move(setup));
}
int TestSuite::main(const String& suite_name, int argc, char** argv)
{
m_suite_name = suite_name;
@ -66,6 +73,9 @@ int TestSuite::main(const String& suite_name, int argc, char** argv)
args_parser.add_positional_argument(search_string, "Only run matching cases.", "pattern", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv);
if (m_setup)
m_setup();
const auto& matching_tests = find_cases(search_string, !do_benchmarks_only, !do_tests_only);
if (do_list_cases) {

View file

@ -43,6 +43,8 @@ public:
void current_test_case_did_fail() { m_current_test_case_passed = false; }
void set_suite_setup(Function<void()> setup) { m_setup = move(setup); }
private:
static TestSuite* s_global;
NonnullRefPtrVector<TestCase> m_cases;
@ -50,6 +52,7 @@ private:
u64 m_benchtime = 0;
String m_suite_name;
bool m_current_test_case_passed = true;
Function<void()> m_setup;
};
}