If you use the boost unit test framework, you probably have a need to test the string output of a function once in a while. If you use BOOST_CHECK_EQUAL Boost kindly reports string differences, by listing the input and output. Then you can start comparing. Not very usefull.
Instead you can use something like this:
boost::test_tools::predicate_result
compare_qstrings( QString const & actual, QString const & expected )
{
// Lets get the trivial case out of the way.
if ( actual == expected ) {
return true;
}
// We want output like this:
// boost_extract_xml_from_es_task_package.cc(106): error in "itemorder":
// check "todo" ==
// output_24575997 failed
// output differs at position 42
// expected ...ISO-8859-1"?>todo
// ^
boost::test_tools::predicate_result res( false );
int i = 0;
int actual_l = actual.size();
int expected_l = expected.size();
int max_l = actual_l < expected_l ? actual_l : expected_l;
if ( actual_l < expected_l ) {
max_l = actual_l;
res.message() << "actual is shorter than expected\n"; // < expected_l ) {
max_l = expected_l;
res.message() << "actual is longer than expected\n"; // << std::endl;
}
while( i < max_l && actual.at( i ) == expected.at( i ) ) {
++i;
}
res.message() << "actual and expected differs at position " << i < 20) ? (i - 20) : 0;
const char * prefix = begin > 0 ? "..." : "";
res.message() << "expected: " << prefix << expected.mid( begin, 70 )
<< ( begin + 70 < expected_l ? "..." : "" ) << "\n"; // std::endl;
res.message() << "actual: " << prefix << actual.mid( begin, 70 )
<< ( begin + 70 < actual_l ? "..." : "" ) << "\n"; // std::endl;
res.message() << " " << QString( ( i - begin ) + QString( prefix ).size(), ' ' ).toStdString()
<< "^"; // << std::endl;
return res;
}
And, call it like this:
BOOST_CHECK( compare_qstrings( "string a", "string b" ) )
and then you will get output like this:
boost_extract_xml_from_es_task_package.cc(470): error in "itemorder": check compare_qstrings( extract.extractXml( input_24229551 ), output_24229551 ) failed. actual is shorter than expected
actual and expected differs at position 23
expected: <ill5:ILL-APDU xmlns:ill5="...
actual: <ill5:ILL-APDU xmlns:ill5="...
^
which is way more useful in my opinion.
Of course, wordpress formats it so it is totally unreadable – so you just have to take my word for it
The above uses QString, but it is of course trivial to change it to use std::string.