Question

I have a QtTest that is comparing two string values:

 // This definitely returns "5/6" 
 std::string actual_value = simple(actual);

 // Sets a string to the expected value
 std::string expected_value = "5/6";

and then a QCOMPARE:

QCOMPARE(QString(actual_value.c_str()), QString(expected_value.c_str()));

/* I'm Aware this could just be the code below (but this doesnt print values to
console so i can check they are actually the same)  */
QCOMPARE(actual_value, expected_value)

And i get the error: (ill post both so its obvious its not just the QString thing failing it, so below is the QCOMPARE using QString to display values)

FAIL!  : TestFraction::testFraction() Compared values are not the same
Actual   (QString(actual_value.c_str())): 5/6

Expected (QString(expected_value.c_str())): 5/6

and this is the normal QCOMPARE error message:

FAIL!  : TestFraction::testFraction() Compared values are not the same

There is obviously other code behind this but im not posting it now because i dont think its particularly relevant but ask if theres something you want to know, as far as i can see this test should pass but maybe theres something i dont know about comparing strings (something like how comparing doubles fails unless you use a fuzzy compare)

thanks in advance

Was it helpful?

Solution

Your actual string has a new line character. Hence the comparison fails.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top