From e0fee70d58bf33b05f4f8dbf4fb086b8aeace14f Mon Sep 17 00:00:00 2001 From: Martchus Date: Thu, 21 Jun 2018 23:27:05 +0200 Subject: [PATCH] Print vector and similar in hex notation on assert fail --- tests/testutils.h | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/tests/testutils.h b/tests/testutils.h index ca5cabc..8072245 100644 --- a/tests/testutils.h +++ b/tests/testutils.h @@ -169,7 +169,7 @@ template bool operator==(const AsHexNumber &lhs, const AsHexNumb */ template std::ostream &operator<<(std::ostream &out, const AsHexNumber &value) { - return out << std::hex << '0' << 'x' << unsigned(value.value) << std::dec; + return out << '0' << 'x' << std::hex << std::setfill('0') << std::setw(2) << unsigned(value.value) << std::dec; } /*! @@ -181,6 +181,26 @@ template AsHexNumber asHexNumber(const T &value) return AsHexNumber(value); } +/*! + * \brief Wraps a value to be printed using the hex system in the error case when asserted + * with cppunit (or similar test framework). + * \remarks Only affects integral types. Values of other types are printed as usual. + */ +template > * = nullptr> AsHexNumber integralsAsHexNumber(const T &value) +{ + return AsHexNumber(value); +} + +/*! + * \brief Wraps a value to be printed using the hex system in the error case when asserted + * with cppunit (or similar test framework). + * \remarks Only affects integral types. Values of other types are printed as usual. + */ +template > * = nullptr> const T &integralsAsHexNumber(const T &value) +{ + return value; +} + #ifndef TESTUTILS_ASSERT_EXEC /*! * \brief Asserts successful execution of application via TestApplication::execApp(). Output is stored in stdout and stderr. @@ -207,7 +227,7 @@ inline std::ostream &operator<<(std::ostream &out, const Iteratable &iteratable) out << '\n'; std::size_t index = 0; for (const auto &item : iteratable) { - out << std::setw(2) << index << ':' << ' ' << item << '\n'; + out << std::setw(2) << index << ':' << ' ' << integralsAsHexNumber(item) << '\n'; ++index; } return out;