Merge remote-tracking branch 'upstream/edge' into upstream-master
[clinton/Smoothieware.git] / src / testframework / easyunit / defaulttestprinter.cpp
1 /*
2 EasyUnit : Simple C++ Unit testing framework
3 Copyright (C) 2004 Barthelemy Dagenais
4
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.
9
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.
14
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
18
19 Barthelemy Dagenais
20 barthelemy@prologique.com
21 */
22
23 #include "defaulttestprinter.h"
24
25 #include "testpartresult.h"
26
27 #include <stdio.h>
28
29
30 DefaultTestPrinter::DefaultTestPrinter()
31 : testsTotal_(0),testFailuresTotal_(0),failuresTotal_(0),
32 level_(normal), showSuccessDetail_(false), output_(stdout)
33 {
34 }
35
36 DefaultTestPrinter::~DefaultTestPrinter()
37 {
38 }
39
40 void DefaultTestPrinter::print(const TestResult *testResult)
41 {
42 int failures;
43 int successes;
44 int errors;
45 SimpleString state;
46 SimpleString name;
47 TestCase *testCase = testResult->getTestCases();
48 int size = testResult->getTestCaseCount();
49
50 printHeader(testResult);
51
52 if (testResult->getTestCaseRanCount() == 0) {
53 fprintf(output_,"\nNo test ran\n");
54 }
55
56 for (int i=0;i<size;i++) {
57
58 if (testCase->ran()) {
59
60 name = testCase->getName();
61 failures = testCase->getFailuresCount();
62 successes = testCase->getSuccessesCount();
63 errors = testCase->getErrorsCount();
64
65 if (failures > 0 || errors > 0) {
66 state = "FAILED";
67 }
68 else {
69 state = "SUCCEEDED";
70 }
71
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);
73
74 printTests(testCase);
75 }
76
77 testCase = testCase->getNext();
78 }
79 }
80
81 void DefaultTestPrinter::setHeaderLevel(headerLevel level)
82 {
83 level_ = level;
84 }
85
86 void DefaultTestPrinter::showSuccessDetail(bool show)
87 {
88 showSuccessDetail_ = show;
89 }
90
91 void DefaultTestPrinter::setOutput(FILE *output)
92 {
93 output_ = output;
94 }
95
96 void DefaultTestPrinter::printHeader(const TestResult *testResult)
97 {
98 fprintf(output_ , "-- EasyUnit Results --\n");
99
100 if (level_ != off) {
101 fprintf(output_ , "\nSUMMARY\n\n");
102 fprintf(output_ , "Test summary: ");
103
104 if (testResult->getErrors() > 0 || testResult->getFailures() > 0) {
105 fprintf(output_ , "FAIL\n");
106 }
107 else {
108 fprintf(output_ , "SUCCESS\n");
109 }
110
111 if (level_ == normal) {
112 printNormalHeader(testResult);
113 }
114 else {
115 printCompleteHeader(testResult);
116 }
117 }
118
119 fprintf(output_ , "\n");
120 fprintf(output_ , "\nDETAILS");
121 }
122
123 void DefaultTestPrinter::printCompleteHeader(const TestResult *testResult)
124 {
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());
134
135 }
136
137 void DefaultTestPrinter::printNormalHeader(const TestResult *testResult)
138 {
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());
143 }
144
145 void DefaultTestPrinter::printTests(TestCase *testCase)
146 {
147 const char *indent = " ";
148 Test *test = testCase->getTests();
149 int size = testCase->getTestsCount();
150 SimpleString state;
151
152
153
154 for (int i=0;i<size;i++) {
155 if (test->getFailuresCount() > 0 || test->getErrorsCount() > 0) {
156 state = "FAILED :";
157 }
158 else {
159 state = "SUCCEEDED!";
160 }
161
162 fprintf(output_, "%s Test \"%s\" %s\n",indent,test->getTestName().asCharString(),state.asCharString());
163 printResults(test);
164 test = test->getNext();
165 }
166 }
167
168 void DefaultTestPrinter::printResults(Test *test)
169 {
170 const char *indent = " ";
171 TestPartResult *testPR = test->getTestPartResult();
172 int size = test->getFailuresCount() + test->getSuccessesCount() + test->getErrorsCount();
173 int type;
174
175 for (int i=0;i<size;i++) {
176
177 type = testPR->getType();
178
179 if (type == failure) {
180 fprintf (output_, "%s%s%s%s%s%ld%s%s\n",
181 indent,
182 "Failure: \"",
183 testPR->getMessage().asCharString (),
184 "\" " ,
185 "line ",
186 testPR->getLineNumber(),
187 " in ",
188 testPR->getFileName().asCharString ());
189 }
190 else if (type == error) {
191 fprintf (output_, "%s%s%s%s%s%s\n",
192 indent,
193 "Error in ",
194 test->getTestName().asCharString(),
195 ": \"",
196 testPR->getMessage().asCharString (),
197 "\"");
198 }
199 else if (type == success && showSuccessDetail_) {
200 fprintf (output_, "%s%s%s%s%s%ld%s%s\n",
201 indent,
202 "Success: \"",
203 testPR->getMessage().asCharString (),
204 "\" " ,
205 "line ",
206 testPR->getLineNumber(),
207 " in ",
208 testPR->getFileName().asCharString ());
209 }
210 testPR = testPR->getNext();
211 }
212 }
213
214
215