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:
Ben Wiederhake 2020-08-22 23:37:10 +02:00 committed by Andreas Kling
parent bedd15c340
commit 53abc626c2
2 changed files with 49 additions and 5 deletions

View file

@ -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;

View file

@ -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) \