Print vector<char> and similar in hex notation on assert fail

This commit is contained in:
Martchus 2018-06-21 23:27:05 +02:00
parent 0c7e7eb91d
commit e0fee70d58
1 changed files with 22 additions and 2 deletions

View File

@ -169,7 +169,7 @@ template <typename T> bool operator==(const AsHexNumber<T> &lhs, const AsHexNumb
*/
template <typename T> std::ostream &operator<<(std::ostream &out, const AsHexNumber<T> &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 <typename T> AsHexNumber<T> asHexNumber(const T &value)
return AsHexNumber<T>(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 <typename T, Traits::EnableIf<std::is_integral<T>> * = nullptr> AsHexNumber<T> integralsAsHexNumber(const T &value)
{
return AsHexNumber<T>(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 <typename T, Traits::DisableIf<std::is_integral<T>> * = 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;