2 EasyUnit : Simple C++ Unit testing framework
3 Copyright (C) 2004 Barthelemy Dagenais
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 barthelemy@prologique.com
23 #include "defaulttestprinter.h"
25 #include "testpartresult.h"
30 DefaultTestPrinter::DefaultTestPrinter()
31 : testsTotal_(0),testFailuresTotal_(0),failuresTotal_(0),
32 level_(normal
), showSuccessDetail_(false), output_(stdout
)
36 DefaultTestPrinter::~DefaultTestPrinter()
40 void DefaultTestPrinter::print(const TestResult
*testResult
)
47 TestCase
*testCase
= testResult
->getTestCases();
48 int size
= testResult
->getTestCaseCount();
50 printHeader(testResult
);
52 if (testResult
->getTestCaseRanCount() == 0) {
53 fprintf(output_
,"\nNo test ran\n");
56 for (int i
=0;i
<size
;i
++) {
58 if (testCase
->ran()) {
60 name
= testCase
->getName();
61 failures
= testCase
->getFailuresCount();
62 successes
= testCase
->getSuccessesCount();
63 errors
= testCase
->getErrorsCount();
65 if (failures
> 0 || errors
> 0) {
72 fprintf(output_
, "\n\nTest case \"%s\" %s with %d error(s), %d failure(s) and %d success(es): \n",name
.asCharString(),state
.asCharString(),errors
,failures
,successes
);
77 testCase
= testCase
->getNext();
81 void DefaultTestPrinter::setHeaderLevel(headerLevel level
)
86 void DefaultTestPrinter::showSuccessDetail(bool show
)
88 showSuccessDetail_
= show
;
91 void DefaultTestPrinter::setOutput(FILE *output
)
96 void DefaultTestPrinter::printHeader(const TestResult
*testResult
)
98 fprintf(output_
, "-- EasyUnit Results --\n");
101 fprintf(output_
, "\nSUMMARY\n\n");
102 fprintf(output_
, "Test summary: ");
104 if (testResult
->getErrors() > 0 || testResult
->getFailures() > 0) {
105 fprintf(output_
, "FAIL\n");
108 fprintf(output_
, "SUCCESS\n");
111 if (level_
== normal
) {
112 printNormalHeader(testResult
);
115 printCompleteHeader(testResult
);
119 fprintf(output_
, "\n");
120 fprintf(output_
, "\nDETAILS");
123 void DefaultTestPrinter::printCompleteHeader(const TestResult
*testResult
)
125 fprintf(output_
, "Number of test cases: %d\n",testResult
->getTestCaseCount());
126 fprintf(output_
, "Number of test cases ran: %d\n",testResult
->getTestCaseRanCount());
127 fprintf(output_
, "Test cases that succeeded: %d\n",testResult
->getSuccesses());
128 fprintf(output_
, "Test cases with errors: %d\n",testResult
->getErrors());
129 fprintf(output_
, "Test cases that failed: %d\n",testResult
->getFailures());
130 fprintf(output_
, "Number of tests ran: %d\n",testResult
->getTestRanCount());
131 fprintf(output_
, "Tests that succeeded: %d\n",testResult
->getTotalSuccesses());
132 fprintf(output_
, "Tests with errors: %d\n",testResult
->getTotalErrors());
133 fprintf(output_
, "Tests that failed: %d\n",testResult
->getTotalFailures());
137 void DefaultTestPrinter::printNormalHeader(const TestResult
*testResult
)
139 fprintf(output_
, "Number of test cases ran: %d\n",testResult
->getTestCaseRanCount());
140 fprintf(output_
, "Test cases that succeeded: %d\n",testResult
->getSuccesses());
141 fprintf(output_
, "Test cases with errors: %d\n",testResult
->getErrors());
142 fprintf(output_
, "Test cases that failed: %d\n",testResult
->getFailures());
145 void DefaultTestPrinter::printTests(TestCase
*testCase
)
147 const char *indent
= " ";
148 Test
*test
= testCase
->getTests();
149 int size
= testCase
->getTestsCount();
154 for (int i
=0;i
<size
;i
++) {
155 if (test
->getFailuresCount() > 0 || test
->getErrorsCount() > 0) {
159 state
= "SUCCEEDED!";
162 fprintf(output_
, "%s Test \"%s\" %s\n",indent
,test
->getTestName().asCharString(),state
.asCharString());
164 test
= test
->getNext();
168 void DefaultTestPrinter::printResults(Test
*test
)
170 const char *indent
= " ";
171 TestPartResult
*testPR
= test
->getTestPartResult();
172 int size
= test
->getFailuresCount() + test
->getSuccessesCount() + test
->getErrorsCount();
175 for (int i
=0;i
<size
;i
++) {
177 type
= testPR
->getType();
179 if (type
== failure
) {
180 fprintf (output_
, "%s%s%s%s%s%ld%s%s\n",
183 testPR
->getMessage().asCharString (),
186 testPR
->getLineNumber(),
188 testPR
->getFileName().asCharString ());
190 else if (type
== error
) {
191 fprintf (output_
, "%s%s%s%s%s%s\n",
194 test
->getTestName().asCharString(),
196 testPR
->getMessage().asCharString (),
199 else if (type
== success
&& showSuccessDetail_
) {
200 fprintf (output_
, "%s%s%s%s%s%ld%s%s\n",
203 testPR
->getMessage().asCharString (),
206 testPR
->getLineNumber(),
208 testPR
->getFileName().asCharString ());
210 testPR
= testPR
->getNext();