Merge remote-tracking branch 'upstream/edge' into upstream-master
[clinton/Smoothieware.git] / src / testframework / easyunit / testcase.cpp
1 #define ECPP
2 /*
3 EasyUnit : Simple C++ Unit testing framework
4 Copyright (C) 2004 Barthelemy Dagenais
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 Barthelemy Dagenais
21 barthelemy@prologique.com
22 */
23
24 #include "testcase.h"
25 #include "test.h"
26 #include "testresult.h"
27
28 #ifndef ECPP
29 #include <exception>
30 #endif
31
32
33 TestCase::TestCase(const SimpleString& name, TestResult *testResult)
34 : name_(name), testResult_(testResult)
35 {
36 }
37
38 TestCase::~TestCase()
39 {
40 }
41
42 void TestCase::addTest(Test *test)
43 {
44 Test *tmp;
45
46 if (tests_ == 0) {
47 tests_ = test;
48 tests_->setNext(tests_);
49 }
50 else {
51 tmp = tests_;
52 tests_ = test;
53 tests_->setNext(tmp->getNext());
54 tmp->setNext(tests_);
55 }
56
57 testsCount_++;
58 }
59
60 Test* TestCase::getTests() const
61 {
62 Test *test = tests_;
63
64 if (test != 0) {
65 test = test->getNext();
66 }
67
68 return test;
69 }
70
71 void TestCase::run()
72 {
73 Test *test = tests_->getNext();
74
75 runTests(test);
76
77 ran_ = true;
78
79 testResult_->addResult(this);
80 }
81
82 int TestCase::getTestsCount() const
83 {
84 return testsCount_;
85 }
86
87 int TestCase::getFailuresCount() const
88 {
89 return failuresCount_;
90 }
91
92 int TestCase::getSuccessesCount() const
93 {
94 return successesCount_;
95 }
96
97 int TestCase::getErrorsCount() const
98 {
99 return errorsCount_;
100 }
101
102 bool TestCase::ran() const
103 {
104 return ran_;
105 }
106
107 const SimpleString& TestCase::getName() const
108 {
109 return name_;
110 }
111
112 void TestCase::updateCount(Test *test)
113 {
114 if (test->getErrorsCount() > 0) {
115 errorsCount_++;
116 }
117 else if (test->getFailuresCount() > 0) {
118 failuresCount_++;
119 }
120 else {
121 successesCount_++;
122 }
123 }
124
125 TestCase* TestCase::getNext() const
126 {
127 return nextTestCase_;
128 }
129
130 void TestCase::setNext(TestCase *testCase)
131 {
132 nextTestCase_ = testCase;
133 }
134
135 void TestCase::runTests(Test *test)
136 {
137
138 for (int i = 0; i<testsCount_; i++) {
139 test->setUp();
140 runTest(test);
141 test->tearDown();
142 updateCount(test);
143 test = test->getNext();
144 }
145
146 }
147
148 #ifdef ECPP
149
150 void TestCase::runTest(Test *test)
151 {
152 test->run();
153 }
154
155 #else
156
157 void TestCase::runTest(Test *test)
158 {
159 try {
160 test->run();
161 }
162 catch (std::exception &e) {
163 test->addTestPartResult(new TestPartResult(test,"",-1,e.what(),error));
164 }
165 catch (...) {
166 test->addTestPartResult(new TestPartResult(test,"",-1,"Unexpected error occured",error));
167 }
168 }
169 #endif
170