mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 12:05:15 +00:00
AK: Print RHS and LHS in EXPECT_EQ if we can
This makes error messages more useful during debugging. Old: START Running test compare_views FAIL: ../AK/Tests/TestStringView.cpp:59: EXPECT_EQ(view1, "foobar") failed New: START Running test compare_views FAIL: ../AK/Tests/TestStringView.cpp:59: EXPECT_EQ(view1, "foobar") failed: LHS="foo", RHS="foobar"
This commit is contained in:
parent
bedd15c340
commit
53abc626c2
Notes:
sideshowbarker
2024-07-19 03:15:54 +09:00
Author: https://github.com/BenWiederhake Commit: https://github.com/SerenityOS/serenity/commit/53abc626c2a Pull-request: https://github.com/SerenityOS/serenity/pull/3265
2 changed files with 49 additions and 5 deletions
|
@ -40,7 +40,6 @@ inline constexpr unsigned round_up_to_power_of_two(unsigned value, unsigned powe
|
|||
|
||||
namespace AK {
|
||||
|
||||
|
||||
template<typename T, typename SizeType = decltype(sizeof(T)), SizeType N>
|
||||
constexpr SizeType array_size(T (&)[N])
|
||||
{
|
||||
|
@ -501,6 +500,9 @@ template<typename ReferenceType, typename T>
|
|||
using CopyConst =
|
||||
typename Conditional<IsConst<ReferenceType>::value, typename AddConst<T>::Type, typename RemoveConst<T>::Type>::Type;
|
||||
|
||||
template<typename... Ts>
|
||||
using Void = void;
|
||||
|
||||
}
|
||||
|
||||
using AK::AddConst;
|
||||
|
@ -523,3 +525,4 @@ using AK::min;
|
|||
using AK::move;
|
||||
using AK::RemoveConst;
|
||||
using AK::swap;
|
||||
using AK::Void;
|
||||
|
|
|
@ -220,6 +220,35 @@ void TestSuite::run(const NonnullRefPtrVector<TestCase>& tests)
|
|||
<< m_testtime << " tests, " << m_benchtime << " benchmarks, " << (global_timer.elapsed_milliseconds() - (m_testtime + m_benchtime)) << " other)";
|
||||
}
|
||||
|
||||
// Use SFINAE to print if we can.
|
||||
// This trick is good enough for TestSuite.h, but not flexible enough to be put into LogStream.h.
|
||||
template<typename Stream, typename LHS, typename RHS, typename = void>
|
||||
struct MaybeStream {
|
||||
static const Stream& call(const Stream& stream, const LHS&, const RHS&)
|
||||
{
|
||||
return stream;
|
||||
}
|
||||
};
|
||||
template<typename Stream, typename LHS, typename RHS>
|
||||
struct MaybeStream<Stream, LHS, RHS, AK::Void<decltype(*reinterpret_cast<const Stream*>(0) << "" << *reinterpret_cast<const LHS*>(0) << "" << *reinterpret_cast<const RHS*>(0) << "")>> {
|
||||
static const Stream& call(const Stream& stream, const LHS& lhs, const RHS& rhs)
|
||||
{
|
||||
return stream << ": LHS=\"" << lhs << "\", RHS=\"" << rhs << "\"";
|
||||
}
|
||||
};
|
||||
template<typename Stream, typename LHS, typename RHS>
|
||||
static const Stream& maybe_print_rhs_lhs(const Stream& stream, const LHS& lhs, const RHS& rhs)
|
||||
{
|
||||
return MaybeStream<Stream, LHS, RHS>::call(stream, lhs, rhs);
|
||||
}
|
||||
template<typename Stream, typename LHS, typename RHS>
|
||||
static const Stream& force_print_rhs_lhs(const Stream& stream, const LHS& lhs, const RHS& rhs)
|
||||
{
|
||||
using _ = decltype(*reinterpret_cast<const Stream*>(0) << "" << *reinterpret_cast<const LHS*>(0) << "" << *reinterpret_cast<const RHS*>(0) << "");
|
||||
(void)sizeof(_);
|
||||
return MaybeStream<Stream, LHS, RHS>::call(stream, lhs, rhs);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
using AK::TestCase;
|
||||
|
@ -263,10 +292,22 @@ using AK::TestSuite;
|
|||
TestSuite::release(); \
|
||||
}
|
||||
|
||||
#define EXPECT_EQ(a, b) \
|
||||
{ \
|
||||
if ((a) != (b)) \
|
||||
warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT_EQ(" #a ", " #b ") failed"; \
|
||||
#define EXPECT_EQ(a, b) \
|
||||
{ \
|
||||
auto lhs = (a); \
|
||||
auto rhs = (b); \
|
||||
if (lhs != rhs) \
|
||||
AK::maybe_print_rhs_lhs(warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT_EQ(" #a ", " #b ") failed", lhs, rhs); \
|
||||
}
|
||||
|
||||
// If you're stuck and `EXPECT_EQ` seems to refuse to print anything useful,
|
||||
// try this: It'll spit out a nice compiler error telling you why it doesn't print.
|
||||
#define EXPECT_EQ_FORCE(a, b) \
|
||||
{ \
|
||||
auto lhs = (a); \
|
||||
auto rhs = (b); \
|
||||
if (lhs != rhs) \
|
||||
AK::force_print_rhs_lhs(warn() << "\033[31;1mFAIL\033[0m: " __FILE__ ":" << __LINE__ << ": EXPECT_EQ(" #a ", " #b ") failed", lhs, rhs); \
|
||||
}
|
||||
|
||||
#define EXPECT(x) \
|
||||
|
|
Loading…
Add table
Reference in a new issue