/[escript]/trunk/tools/CppUnitTest/src/TestRunner.cpp
ViewVC logotype

Contents of /trunk/tools/CppUnitTest/src/TestRunner.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2940 - (show annotations)
Fri Feb 19 00:38:45 2010 UTC (9 years, 6 months ago) by caltinay
File size: 5602 byte(s)
Minor potential optimization: prefer empty() over size()==0.

1 //
2 // Permission to reproduce and create derivative works from the Software ("Software Derivative Works")
3 // is hereby granted to you under the copyright of Michael Feathers. Michael Feathers also grants you
4 // the right to distribute the Software and Software Derivative Works.
5 //
6 // Michael Feathers licenses the Software to you on an "AS IS" basis, without warranty of any kind.
7 // Michael Feathers HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES OR CONDITIONS, EITHER EXPRESS OR IMPLIED,
8 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OR CONDITIONS OF MERCHANTABILITY, NON INFRINGEMENT
9 // AND FITNESS FOR A PARTICULAR PURPOSE.  You are solely responsible for determining the appropriateness
10 // of using the Software and assume all risks associated with the use and distribution of this Software,
11 // including but not limited to the risks of program errors, damage to or loss of data, programs or
12 // equipment, and unavailability or interruption of operations.  MICHAEL FEATHERS WILL NOT BE
13 // LIABLE FOR ANY DIRECT DAMAGES OR FOR ANY SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC
14 // CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF MICHAEL FEATHERS HAD BEEN ADVISED
15 // OF THE POSSIBILITY OF SUCH DAMAGE.  Michael Feathers will not be liable for the loss of, or damage
16 // to, your records or data, or any damages claimed by you based on a third party claim.
17 //
18 // You agree to distribute the Software and any Software Derivatives under a license agreement that:
19 //
20 // 1) is sufficient to notify all licensees of the Software and Software Derivatives that Michael
21 // Feathers assumes no liability for any claim that may arise regarding the Software or
22 // Software Derivatives, and
23 // 2) that disclaims all warranties, both express and implied, from Michael Feathers regarding the
24 // Software and Software Derivatives.  (If you include this Agreement with any distribution
25 // of the Software and Software Derivatives you will have meet this requirement) You agree that
26 // you will not delete any copyright notices in the Software.
27 //
28 // This Agreement is the exclusive statement of your rights in the Software as provided by Michael
29 // Feathers. Except for the licenses granted to you in the second paragraph above, no other licenses
30 // are granted hereunder, by estoppel, implication or otherwise.
31 //
32 /*
33 * A command line based tool to run tests.
34 * TestRunner expects as its only argument the name of a TestCase class.
35 * TestRunner prints out a trace as the tests are executed followed by a
36 * summary at the end.
37 *
38 * You can add to the tests that the TestRunner knows about by
39 * making additional calls to "addTest (...)" in main.
40 *
41 * Here is the synopsis:
42 *
43 * TestRunner [-wait] ExampleTestCase
44 *
45 */
46
47
48 #include <vector>
49 #include <cstdlib>
50
51 #include "TestRunner.h"
52 #include "TextTestResult.h"
53 #include "Test.h"
54
55 USING_NAMESPACE_CPPUNITTEST
56
57 void TestRunner::run (int ac, char **av)
58 {
59 std::string testCase;
60 int numberOfTests = 0;
61 std::vector<std::string> tCases;
62 std::vector<std::string> argList;
63 bool failuresOccurred=false;
64 //
65 // process the arguments
66 //
67 // suites to run are specified by suite=aaa,bbb,ccc
68 // more than one suite argument can be given
69 // no attempt is made to remove duplicates
70 //
71 std::string suiteKey = "suite=";
72 std::string suiteSep = ",";
73 for (int i = 0; i < ac; ++i) {
74 std::string testArg = av[i];
75 std::string::size_type loc = testArg.find(suiteKey);
76 if(i>0 && loc==0) {
77 testArg.erase(0,suiteKey.size());
78 while(testArg.size()>0) {
79 loc = testArg.find(suiteSep);
80 std::string suiteName;
81 if(loc==std::string::npos) {
82 suiteName = testArg;
83 testArg.erase();
84 }
85 else if(loc>0) {
86 suiteName = std::string(testArg,0,loc);
87 testArg.erase(0,loc+suiteSep.size());
88 }
89 else {
90 testArg.erase(0,suiteSep.size());
91 }
92 if(suiteName.size()>0) {
93 tCases.push_back(suiteName);
94 }
95 }
96 }
97 else {
98 argList.push_back(testArg);
99 }
100 }
101 mappings::iterator it;
102 Test *testToRun;
103 if (tCases.empty()) {
104 //
105 // no particular cases have been selected so run all cases
106 for (it = m_mappings.begin (); it!=m_mappings.end(); ++it) {
107 std::cout << "Suite: " << (*it).first << std::endl;
108 testToRun = (*it).second;
109 testToRun->setArgs(argList);
110 run (testToRun);
111 if(testToRun->hasFailure()) {
112 failuresOccurred=true;
113 }
114 numberOfTests++;
115 }
116 } else {
117 std::vector<std::string>::const_iterator tci;
118 for (tci=tCases.begin(); tci!=tCases.end(); ++tci) {
119 testToRun=NULL;
120 for (it = m_mappings.begin();
121 it != m_mappings.end() && testToRun==NULL;
122 ++it) {
123 testCase=*tci;
124 if ((*it).first == (*tci)) {
125 std::cout << "Suite: " << (*it).first << std::endl;
126 testToRun = (*it).second;
127 testToRun->setArgs(argList);
128 run (testToRun);
129 if(testToRun->hasFailure()) {
130 failuresOccurred=true;
131 }
132 numberOfTests++;
133 }
134 }
135 if (!testToRun) {
136 std::cout << "Test " << testCase << " not found." << std::endl;
137 failuresOccurred=true;
138 }
139 }
140 }
141 if(failuresOccurred) {
142 exit(1);
143 }
144 }
145
146
147 TestRunner::~TestRunner ()
148 {
149 for (mappings::iterator it = m_mappings.begin ();
150 it != m_mappings.end ();
151 ++it)
152 delete it->second;
153
154 }
155
156
157 void TestRunner::run (Test *test)
158 {
159 TextTestResult result;
160
161 test->run (&result);
162
163 std::cout << result << std::endl;
164 }
165

Properties

Name Value
svn:eol-style native
svn:executable *
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26