From ba20ddb834df71a57473efcbf3037cb4e5568b5d Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Tue, 24 Oct 2023 01:43:23 +0200 Subject: [PATCH] LibTest: Add the GEN macro 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. --- Userland/Libraries/LibTest/TestCase.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Userland/Libraries/LibTest/TestCase.h b/Userland/Libraries/LibTest/TestCase.h index f73d36abc2..a98cc6ee09 100644 --- a/Userland/Libraries/LibTest/TestCase.h +++ b/Userland/Libraries/LibTest/TestCase.h @@ -82,3 +82,11 @@ void set_suite_setup_function(Function setup); }; \ 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))