mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
LibTest: Add TEST_SETUP macro that runs before all test cases
This commit is contained in:
parent
b0bd4be59a
commit
83680934e5
Notes:
sideshowbarker
2024-07-18 04:59:39 +09:00
Author: https://github.com/petelliott Commit: https://github.com/SerenityOS/serenity/commit/83680934e5f Pull-request: https://github.com/SerenityOS/serenity/pull/9679 Reviewed-by: https://github.com/ADKaster ✅
3 changed files with 22 additions and 1 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue