mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 08:18:11 +00:00

Generators are callable as-is: u32 my_int = Gen::unsigned_int(); // -> 1, 5, 8, 3, 2 But there is little visibility in the test fail message into what went wrong. Showing what values were generated helps a lot, and that's what this macro does: GEN(my_int, Gen::unsigned_int()); expands into the above declaration and (crucially) a conditional warnln() call looking like "my_int = {}". It will only run if error reporting is enabled (see Test::can_report()), so it will only give the final shrunk value instead of spamming the output with each generated value.
92 lines
4.4 KiB
C++
92 lines
4.4 KiB
C++
/*
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
* Copyright (c) 2021, Andrew Kaster <akaster@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <LibTest/Macros.h> // intentionally first -- we redefine VERIFY and friends in here
|
|
|
|
#include <AK/DeprecatedString.h>
|
|
#include <AK/Function.h>
|
|
#include <AK/NonnullRefPtr.h>
|
|
#include <AK/RefCounted.h>
|
|
|
|
namespace Test {
|
|
|
|
using TestFunction = Function<void()>;
|
|
|
|
class TestCase : public RefCounted<TestCase> {
|
|
public:
|
|
TestCase(DeprecatedString const& name, TestFunction&& fn, bool is_benchmark)
|
|
: m_name(name)
|
|
, m_function(move(fn))
|
|
, m_is_benchmark(is_benchmark)
|
|
{
|
|
}
|
|
|
|
bool is_benchmark() const { return m_is_benchmark; }
|
|
DeprecatedString const& name() const { return m_name; }
|
|
TestFunction const& func() const { return m_function; }
|
|
|
|
private:
|
|
DeprecatedString m_name;
|
|
TestFunction m_function;
|
|
bool m_is_benchmark;
|
|
};
|
|
|
|
// Helper to hide implementation of TestSuite from users
|
|
void add_test_case_to_suite(NonnullRefPtr<TestCase> const& 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
|
|
|
|
#define TEST_CASE(x) \
|
|
static void __TESTCASE_FUNC(x)(); \
|
|
struct __TESTCASE_TYPE(x) { \
|
|
__TESTCASE_TYPE(x) \
|
|
() \
|
|
{ \
|
|
add_test_case_to_suite(adopt_ref(*new ::Test::TestCase(#x, __TESTCASE_FUNC(x), false))); \
|
|
} \
|
|
}; \
|
|
static struct __TESTCASE_TYPE(x) __TESTCASE_TYPE(x); \
|
|
static void __TESTCASE_FUNC(x)()
|
|
|
|
#define __BENCHMARK_FUNC(x) __benchmark_##x
|
|
#define __BENCHMARK_TYPE(x) __BenchmarkCase_##x
|
|
|
|
#define BENCHMARK_CASE(x) \
|
|
static void __BENCHMARK_FUNC(x)(); \
|
|
struct __BENCHMARK_TYPE(x) { \
|
|
__BENCHMARK_TYPE(x) \
|
|
() \
|
|
{ \
|
|
add_test_case_to_suite(adopt_ref(*new ::Test::TestCase(#x, __BENCHMARK_FUNC(x), true))); \
|
|
} \
|
|
}; \
|
|
static struct __BENCHMARK_TYPE(x) __BENCHMARK_TYPE(x); \
|
|
static void __BENCHMARK_FUNC(x)()
|
|
|
|
// This allows us to print the generated locals in the test after a failure is fully shrunk.
|
|
#define GEN(identifier, value) \
|
|
auto identifier = (value); \
|
|
if (::Test::current_test_result() == ::Test::TestResult::Overrun) \
|
|
return; \
|
|
if (::Test::is_reporting_enabled()) \
|
|
::AK::warnln("{} = {}", #identifier, (identifier))
|