When a test comparison fails, print the actual and expected values that did not match instead of the expressions in the code.

This requires a specialization of TestUtils::toString() for each distinct type,
otherwise a generic message is used.
This commit is contained in:
Robert Knight 2011-09-01 19:54:58 +01:00
parent b5edffc0b6
commit 1fb6d03886

View file

@ -37,11 +37,17 @@ class TestUtils
if (x != y)
{
throw "Actual and expected values differ. "
"Actual: " + std::string(xString) +
" Expected: " + std::string(yString);
"Actual: " + toString(x,xString) +
" Expected: " + toString(y,yString);
}
}
template <typename T>
static std::string toString(T value, const char* context)
{
return "Unprintable: " + std::string(context);
}
template <class T>
static int runTest(class TestList<T>& tests) throw ()
{
@ -80,6 +86,22 @@ class TestUtils
}
};
template <>
inline std::string TestUtils::toString(const std::string& value, const char*)
{
return value;
}
template <>
inline std::string TestUtils::toString(std::string value, const char*)
{
return value;
}
template <>
inline std::string TestUtils::toString(const char* value, const char*)
{
return value;
}
#define TEST_COMPARE(x,y) \
TestUtils::compare(x,y,#x,#y);