Skip to content

Commit 8c54115

Browse files
PKEuSaggro80
authored andcommitted
Improvement of testrunner's output
- Separation between failures - \n is translated into "\\n\n" instead of "\\n", so that a newline is inserted - Succeeded TODOs are no longer mentioned twice - Removed "" around messages
1 parent f8181df commit 8c54115

2 files changed

Lines changed: 38 additions & 21 deletions

File tree

test/testsuite.cpp

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ unsigned int TestFixture::countTests;
6060

6161
size_t TestFixture::fails_counter = 0;
6262
size_t TestFixture::todos_counter = 0;
63+
size_t TestFixture::succeeded_todos_counter = 0;
6364

6465
TestFixture::TestFixture(const std::string &_name)
6566
:classname(_name)
@@ -77,29 +78,34 @@ bool TestFixture::runTest(const char testname[])
7778
if (quiet_tests) {
7879
std::cout << '.';
7980
} else {
80-
std::cout << classname << "::" << testname << "\n";
81+
std::cout << classname << "::" << testname << std::endl;
8182
}
8283
return true;
8384
}
8485
return false;
8586
}
8687

87-
static std::string writestr(const std::string &str)
88+
static std::string writestr(const std::string &str, bool gccStyle = false)
8889
{
8990
std::ostringstream ostr;
90-
ostr << "\"";
91-
for (unsigned int i = 0; i < str.length(); ++i) {
92-
char ch = str[i];
93-
if (ch == '\n')
91+
if (gccStyle)
92+
ostr << '\"';
93+
for (std::string::const_iterator i = str.begin(); i != str.end(); ++i) {
94+
if (*i == '\n') {
9495
ostr << "\\n";
95-
else if (ch == '\t')
96+
if ((i+1) != str.end() && !gccStyle)
97+
ostr << std::endl;
98+
} else if (*i == '\t')
9699
ostr << "\\t";
97-
else if (ch == '\"')
100+
else if (*i == '\"')
98101
ostr << "\\\"";
99102
else
100-
ostr << std::string(1, ch);
103+
ostr << *i;
101104
}
102-
ostr << "\"";
105+
if (!str.empty() && !gccStyle)
106+
ostr << std::endl;
107+
else if (gccStyle)
108+
ostr << '\"';
103109
return ostr.str();
104110
}
105111

@@ -110,7 +116,7 @@ void TestFixture::assert_(const char *filename, int linenr, bool condition)
110116
if (gcc_style_errors) {
111117
errmsg << filename << ':' << linenr << ": Assertion failed." << std::endl;
112118
} else {
113-
errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl;
119+
errmsg << "_____" << std::endl << "Assertion failed in " << filename << " at line " << linenr << std::endl;
114120
}
115121
}
116122
}
@@ -122,17 +128,17 @@ void TestFixture::assertEquals(const char *filename, int linenr, const std::stri
122128
if (gcc_style_errors) {
123129
errmsg << filename << ':' << linenr << ": Assertion failed. "
124130
<< "Expected: "
125-
<< writestr(expected)
131+
<< writestr(expected, true)
126132
<< ". Actual: "
127-
<< writestr(actual)
128-
<< "."
133+
<< writestr(actual, true)
134+
<< '.'
129135
<< std::endl;
130136
} else {
131137
errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl
132138
<< "Expected:" << std::endl
133139
<< writestr(expected) << std::endl
134140
<< "Actual:" << std::endl
135-
<< writestr(actual) << std::endl;
141+
<< writestr(actual) << std::endl << "_____" << std::endl;
136142
}
137143
if (!msg.empty()) {
138144
errmsg << msg << std::endl;
@@ -163,10 +169,17 @@ void TestFixture::todoAssertEquals(const char *filename, int linenr,
163169
const std::string &current,
164170
const std::string &actual)
165171
{
166-
assertEquals(filename, linenr, current, actual);
167172
if (wanted == actual) {
168-
assertEquals(filename, linenr, "TODO assertion", "The assertion succeeded");
173+
if (gcc_style_errors) {
174+
errmsg << filename << ':' << linenr << ": Assertion succeeded unexpectedly. "
175+
<< "Result: " << writestr(wanted, true) << "." << std::endl;
176+
} else {
177+
errmsg << "Assertion succeeded unexpectedly in " << filename << " at line " << linenr << std::endl
178+
<< "Result:" << std::endl << writestr(wanted) << std::endl << "_____" << std::endl;
179+
}
180+
++succeeded_todos_counter;
169181
} else {
182+
assertEquals(filename, linenr, current, actual);
170183
++todos_counter;
171184
}
172185
}
@@ -187,7 +200,7 @@ void TestFixture::assertThrowFail(const char *filename, int linenr)
187200
errmsg << filename << ':' << linenr << " Assertion failed. "
188201
<< "The expected exception was not thrown" << std::endl;
189202
} else {
190-
errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl
203+
errmsg << "_____" << std::endl << "Assertion failed in " << filename << " at line " << linenr << std::endl
191204
<< "The expected exception was not thrown" << std::endl;
192205
}
193206
}
@@ -237,12 +250,15 @@ size_t TestFixture::runTests(const options& args)
237250
}
238251
}
239252

240-
std::cout << "\n\nTesting Complete\nNumber of tests: " << countTests << "\n";
241-
std::cout << "Number of todos: " << todos_counter << "\n";
253+
std::cout << "\n\nTesting Complete\nNumber of tests: " << countTests << std::endl;
254+
std::cout << "Number of todos: " << todos_counter;
255+
if (succeeded_todos_counter > 0)
256+
std::cout << " (" << succeeded_todos_counter << " succeeded)";
257+
std::cout << std::endl;
242258
// calling flush here, to do all output before the error messages (in case the output is buffered)
243259
std::cout.flush();
244260

245-
std::cerr << "Tests failed: " << fails_counter << "\n";
261+
std::cerr << "Tests failed: " << fails_counter << std::endl << std::endl;
246262
std::cerr << errmsg.str();
247263
std::cerr.flush();
248264
return fails_counter;

test/testsuite.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class TestFixture : public ErrorLogger {
3434
static unsigned int countTests;
3535
static size_t fails_counter;
3636
static size_t todos_counter;
37+
static size_t succeeded_todos_counter;
3738

3839
protected:
3940
std::string classname;

0 commit comments

Comments
 (0)