diff options
author | Ben Wiederhake <BenWiederhake.GitHub@gmx.de> | 2020-08-22 23:37:10 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-08-23 11:24:55 +0200 |
commit | 53abc626c2a3d01481089d2c4a3e9018140658e9 (patch) | |
tree | f751414b03cc2c8faae86a5c0ba258118aa83573 /AK | |
parent | bedd15c3403849df3c386a540c4faa00b4c14602 (diff) | |
download | serenity-53abc626c2a3d01481089d2c4a3e9018140658e9.zip |
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"
Diffstat (limited to 'AK')
-rw-r--r-- | AK/StdLibExtras.h | 5 | ||||
-rw-r--r-- | AK/TestSuite.h | 49 |
2 files changed, 49 insertions, 5 deletions
diff --git a/AK/StdLibExtras.h b/AK/StdLibExtras.h index 4b38a7764c..cf13cfbf7a 100644 --- a/AK/StdLibExtras.h +++ b/AK/StdLibExtras.h @@ -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; diff --git a/AK/TestSuite.h b/AK/TestSuite.h index 6098c3b42f..797a7b4219 100644 --- a/AK/TestSuite.h +++ b/AK/TestSuite.h @@ -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) \ |