mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 21:07:34 +00:00
LibTest: Add TEST_SETUP macro that runs before all test cases
This commit is contained in:
parent
b0bd4be59a
commit
83680934e5
3 changed files with 22 additions and 1 deletions
|
@ -39,9 +39,17 @@ private:
|
||||||
|
|
||||||
// Helper to hide implementation of TestSuite from users
|
// Helper to hide implementation of TestSuite from users
|
||||||
void add_test_case_to_suite(const NonnullRefPtr<TestCase>& test_case);
|
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_FUNC(x) __test_##x
|
||||||
#define __TESTCASE_TYPE(x) __TestCase_##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 <LibTest/Macros.h> // intentionally first -- we redefine VERIFY and friends in here
|
||||||
|
|
||||||
|
#include <AK/Function.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibTest/TestSuite.h>
|
#include <LibTest/TestSuite.h>
|
||||||
#include <stdlib.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);
|
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)
|
int TestSuite::main(const String& suite_name, int argc, char** argv)
|
||||||
{
|
{
|
||||||
m_suite_name = suite_name;
|
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.add_positional_argument(search_string, "Only run matching cases.", "pattern", Core::ArgsParser::Required::No);
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
|
if (m_setup)
|
||||||
|
m_setup();
|
||||||
|
|
||||||
const auto& matching_tests = find_cases(search_string, !do_benchmarks_only, !do_tests_only);
|
const auto& matching_tests = find_cases(search_string, !do_benchmarks_only, !do_tests_only);
|
||||||
|
|
||||||
if (do_list_cases) {
|
if (do_list_cases) {
|
||||||
|
|
|
@ -43,6 +43,8 @@ public:
|
||||||
|
|
||||||
void current_test_case_did_fail() { m_current_test_case_passed = false; }
|
void current_test_case_did_fail() { m_current_test_case_passed = false; }
|
||||||
|
|
||||||
|
void set_suite_setup(Function<void()> setup) { m_setup = move(setup); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static TestSuite* s_global;
|
static TestSuite* s_global;
|
||||||
NonnullRefPtrVector<TestCase> m_cases;
|
NonnullRefPtrVector<TestCase> m_cases;
|
||||||
|
@ -50,6 +52,7 @@ private:
|
||||||
u64 m_benchtime = 0;
|
u64 m_benchtime = 0;
|
||||||
String m_suite_name;
|
String m_suite_name;
|
||||||
bool m_current_test_case_passed = true;
|
bool m_current_test_case_passed = true;
|
||||||
|
Function<void()> m_setup;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue