Catch2: CHECK and REQUIRE can't expand a custom type (QString)
I’ve implementing operator<< and Catch::toString for printing QString’s in CHECK statements but nothing seems to work. QString is a string class from the Qt framework.
#include "catch.hpp"
std::ostream& operator << ( std::ostream& os, QString const& value ) {
os << value.toStdString();
return os;
}
namespace Catch {
std::string toString( QVariant const& value ) {
return value.toString().toStdString();
}
}
TEST_CASE("My testcase")
{
CAPTURE(QString("moo"));
INFO(QString("Sauce"));
CHECK(QString("moo") == QString("sauce"));
This will prints out:
/Users/vpicaver/Documents/Projects/cavewhere/testcases/SurveyChunkTest.cpp:91: FAILED:
CHECK( QString("moo") == QString("sauce") )
with expansion:
{?} == {?}
with messages:
QString("moo") := moo
Sauce
CAPTURE and INFO work okay, but the expansion still returns {?} == {?}
I also try to add #define CATCH_CONFIG_SFINAE
it didn’t seem to help.
Is this a bug? Or am I doing something wrong?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 1
- Comments: 24 (6 by maintainers)
On that note we could probably use QString().toStdString() instead.
Aha, I found something that works!
So in
catch.hpp
it says to overloadtoString
, not to specialize its template:But lo and behold, specializing the template worked for me:
tostrings.h
:tostrings.cpp
:In a test file, I
#include "tostrings.h"
and do the following:Output:
Works for me (test code copied verbatim, added mock QString):