• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeunittest
 

tdeunittest

  • tdeunittest
runner.cpp
1 /*
2  *
3  * Copyright (C) 2004 Zack Rusin <zack@kde.org>
4  * Copyright (C) 2005 Jeroen Wijnhout <Jeroen.Wijnhout@kdemail.net>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <stdio.h>
29 #include <iostream>
30 using namespace std;
31 
32 #include <tqregexp.h>
33 #include <tqdir.h>
34 #include <tqmetaobject.h>
35 
36 #include <kdebug.h>
37 #include <klibloader.h>
38 #include <tdeglobal.h>
39 #include <kstandarddirs.h>
40 
41 #include "runner.h"
42 #include "tester.h"
43 
44 namespace KUnitTest
45 {
46  Runner *Runner::s_self = 0L;
47  bool Runner::s_debugCapturingEnabled = false;
48 
49  void Runner::registerTester(const char *name, Tester *test)
50  {
51  Runner::self()->m_registry.insert(name, test);
52  }
53 
54  void Runner::loadModules(const TQString &folder, const TQString &query)
55  {
56  TQRegExp reQuery(query);
57  TQDir dir(folder, "tdeunittest_*.la");
58 
59  // Add the folder to the "module" resource such that the KLibLoader can
60  // find the modules in this folder.
61  TDEGlobal::dirs()->addResourceDir("module", folder);
62  kdDebug() << "Looking in folder: " << dir.absPath() << endl;
63 
64  // Get a list of all modules.
65  TQStringList modules = dir.entryList();
66 
67  for ( uint i = 0; i < modules.count(); ++i )
68  {
69  TQString module = modules[i];
70  kdDebug() << "Module: " << dir.absPath() + "/" + module << endl;
71 
72  if ( reQuery.search(module) != -1 )
73  {
74  // strip the .la extension
75  module.truncate(module.length()-3);
76  KLibFactory *factory = KLibLoader::self()->factory(module.local8Bit());
77  if ( factory )
78  factory->create();
79  else {
80  kdWarning() << "\tError loading " << module << " : " << KLibLoader::self()->lastErrorMessage() << endl;
81  ::exit( 1 );
82  }
83  }
84  else
85  kdDebug() << "\tModule doesn't match." << endl;
86  }
87  }
88 
89  void Runner::setDebugCapturingEnabled(bool enabled)
90  {
91  s_debugCapturingEnabled = enabled;
92  }
93 
94  RegistryType &Runner::registry()
95  {
96  return m_registry;
97  }
98 
99  int Runner::numberOfTestCases()
100  {
101  return m_registry.count();
102  }
103 
104  Runner *Runner::self()
105  {
106  if ( s_self == 0L ) s_self = new Runner();
107 
108  return s_self;
109  }
110 
111  Runner::Runner()
112  {
113  reset();
114  }
115 
116  int Runner::numberOfTests() const
117  {
118  return globalSteps;
119  }
120 
121  int Runner::numberOfPassedTests() const
122  {
123  return globalPasses;
124  }
125 
126  int Runner::numberOfFailedTests() const
127  {
128  return globalFails;
129  }
130 
131  int Runner::numberOfExpectedFailures() const
132  {
133  return globalXFails;
134  }
135 
136  int Runner::numberOfSkippedTests() const
137  {
138  return globalSkipped;
139  }
140 
141  void Runner::reset()
142  {
143  globalSteps = 0;
144  globalPasses = 0;
145  globalFails = 0;
146  globalXFails = 0;
147  globalXPasses = 0;
148  globalSkipped = 0;
149  }
150 
151  int Runner::runTests()
152  {
153  globalSteps = 0;
154  globalPasses = 0;
155  globalFails = 0;
156  globalXFails = 0;
157  globalXPasses = 0;
158  globalSkipped = 0;
159 
160  cout << "# Running normal tests... #" << endl << endl;
161  RegistryIteratorType it(m_registry);
162 
163  for( ; it.current(); ++it )
164  runTest(it.currentKey());
165 
166 #if 0 // very thorough, but not very readable
167  cout << "# Done with normal tests:" << endl;
168  cout << " Total test cases: " << m_registry.count() << endl;
169  cout << " Total test steps : " << globalSteps << endl;
170  cout << " Total passed test steps (including unexpected) : " << globalPasses << endl;
171  cout << " Total unexpected passed test steps : " << globalXPasses << endl;
172  cout << " Total failed test steps (including expected) : " << globalFails << endl;
173  cout << " Total expected failed test steps : " << globalXFails << endl;
174  cout << " Total skipped test steps : " << globalSkipped << endl;
175 #else
176  unsigned int numTests = m_registry.count(); // should this be globalSteps instead?
177  TQString str;
178  if ( globalFails == 0 )
179  if ( globalXFails == 0 )
180  str = TQString( "All %1 tests passed" ).arg( numTests );
181  else
182  str = TQString( "All %1 tests behaved as expected (%2 expected failures)" ).arg( numTests ).arg( globalXFails );
183  else
184  if ( globalXPasses == 0 )
185  str = TQString( "%1 of %2 tests failed" ).arg( globalFails ).arg( numTests );
186  else
187  str = TQString( "%1 of %2 tests did not behave as expected (%1 unexpected passes)" ).arg( globalFails ).arg( numTests ).arg( globalXPasses );
188  if ( globalSkipped )
189  str += TQString( " (%1 tests skipped)" ).arg( globalSkipped );
190  cout << str.local8Bit().data() << endl;
191 #endif
192 
193  return m_registry.count();
194  }
195 
196  void Runner::runMatchingTests(const TQString &prefix)
197  {
198  RegistryIteratorType it(m_registry);
199  for( ; it.current(); ++it )
200  if ( TQString(it.currentKey()).startsWith(prefix) )
201  runTest(it.currentKey());
202  }
203 
204  void Runner::runTest(const char *name)
205  {
206  Tester *test = m_registry.find(name);
207  if ( test == 0L ) return;
208 
209  if ( s_debugCapturingEnabled )
210  {
211  cout << "KUnitTest_Debug_Start[" << name << "]" << endl;
212  }
213 
214  test->results()->clear();
215  test->allTests();
216 
217  if ( s_debugCapturingEnabled )
218  {
219  cout << "KUnitTest_Debug_End[" << name << "]" << endl << endl << flush;
220  }
221 
222  int numPass = 0;
223  int numFail = 0;
224  int numXFail = 0;
225  int numXPass = 0;
226  int numSkip = 0;
227  TQStringList xpassList;
228  TQStringList errorList;
229  TQStringList xfailList;
230  TQStringList skipList;
231 
232  if ( test->inherits("KUnitTest::SlotTester") )
233  {
234  SlotTester *sltest = static_cast<SlotTester*>(test);
235  TestResultsListIteratorType it(sltest->resultsList());
236  for ( ; it.current(); ++it)
237  {
238  numPass += it.current()->passed() + it.current()->xpasses();
239  numFail += it.current()->errors() + it.current()->xfails();
240  numXFail += it.current()->xfails();
241  numXPass += it.current()->xpasses();
242  numSkip += it.current()->skipped();
243  globalSteps += it.current()->testsFinished();
244 
245  xpassList += it.current()->xpassList();
246  errorList += it.current()->errorList();
247  xfailList += it.current()->xfailList();
248  skipList += it.current()->skipList();
249  }
250  }
251  else
252  {
253  numPass= test->results()->passed() + test->results()->xpasses();
254  numFail= test->results()->errors() + test->results()->xfails();
255  numXFail = test->results()->xfails();
256  numXPass = test->results()->xpasses();
257  numSkip= test->results()->skipped();
258  globalSteps += test->results()->testsFinished();
259 
260  xpassList += test->results()->xpassList();
261  errorList += test->results()->errorList();
262  xfailList += test->results()->xfailList();
263  skipList += test->results()->skipList();
264  }
265 
266 
267  globalPasses += numPass;
268  globalFails += numFail;
269  globalXFails += numXFail;
270  globalXPasses += numXPass;
271  globalSkipped += numSkip;
272 
273  cout << name << " - ";
274  cout << numPass << " test" << ( ( 1 == numPass )?"":"s") << " passed";
275  if ( 0 < xpassList.count() ) {
276  cout << " (" << numXPass << " unexpected pass" << ( ( 1 == numXPass )?"":"es") << ")";
277  }
278  cout << ", " << numFail << " test" << ( ( 1 == numFail )?"":"s") << " failed";
279  if ( 0 < numXFail ) {
280  cout << " (" << numXFail << " expected failure" << ( ( 1 == numXFail )?"":"s") << ")";
281  }
282  if ( 0 < numSkip ) {
283  cout << "; also " << numSkip << " skipped";
284  }
285  cout << endl;
286 
287  if ( 0 < numXPass ) {
288  cout << " Unexpected pass" << ( ( 1 == numXPass )?"":"es") << ":" << endl;
289  TQStringList list = xpassList;
290  for ( TQStringList::Iterator itr = list.begin(); itr != list.end(); ++itr ) {
291  cout << "\t" << (*itr).latin1() << endl;
292  }
293  }
294  if ( 0 < (numFail - numXFail) ) {
295  cout << " Unexpected failure" << ( ( 1 == numFail )?"":"s") << ":" << endl;
296  TQStringList list = errorList;
297  for ( TQStringList::Iterator itr = list.begin(); itr != list.end(); ++itr ) {
298  cout << "\t" << (*itr).latin1() << endl;
299  }
300  }
301  if ( 0 < numXFail ) {
302  cout << " Expected failure" << ( ( 1 == numXFail)?"":"s") << ":" << endl;
303  TQStringList list = xfailList;
304  for ( TQStringList::Iterator itr = list.begin(); itr != list.end(); ++itr ) {
305  cout << "\t" << (*itr).latin1() << endl;
306  }
307  }
308  if ( 0 < numSkip ) {
309  cout << " Skipped test" << ( ( 1 == numSkip )?"":"s") << ":" << endl;
310  TQStringList list = skipList;
311  for ( TQStringList::Iterator itr = list.begin(); itr != list.end(); ++itr ) {
312  cout << "\t" << (*itr).latin1() << endl;
313  }
314  }
315  cout << endl;
316 
317  emit finished(name, test);
318  }
319 }
320 
321 #include "runner.moc"
322 
KUnitTest::Tester
Definition: tester.h:522
KUnitTest::TestResults::xpasses
int xpasses() const
Definition: tester.h:476
KUnitTest::TestResults::passed
int passed() const
Definition: tester.h:482
KUnitTest::TestResults::clear
virtual void clear()
Definition: tester.h:443
tester.h
KUnitTest::TestResultsListIteratorType
TQAsciiDictIterator< TestResults > TestResultsListIteratorType
Definition: tester.h:512
std
runner.h
KUnitTest::TestResults::errorList
TQStringList errorList() const
Definition: tester.h:485
KUnitTest::TestResults::xpassList
TQStringList xpassList() const
Definition: tester.h:491
KUnitTest::TestResults::testsFinished
int testsFinished() const
Definition: tester.h:467
KUnitTest
Namespace for Unit testing classes.
Definition: module.h:39
KUnitTest::Runner
Definition: runner.h:97
KUnitTest::TestResults::errors
int errors() const
Definition: tester.h:470
KUnitTest::SlotTester
Definition: tester.h:686
KUnitTest::TestResults::xfails
int xfails() const
Definition: tester.h:473
KUnitTest::TestResults::xfailList
TQStringList xfailList() const
Definition: tester.h:488
KUnitTest::TestResults::skipList
TQStringList skipList() const
Definition: tester.h:494
KUnitTest::Tester::results
virtual TestResults * results()
Definition: tester.h:539
KUnitTest::RegistryIteratorType
TQAsciiDictIterator< Tester > RegistryIteratorType
Definition: runner.h:78
KUnitTest::Tester::allTests
virtual void allTests()=0
KUnitTest::TestResults::skipped
int skipped() const
Definition: tester.h:479
KUnitTest::RegistryType
TQAsciiDict< Tester > RegistryType
Definition: runner.h:75

tdeunittest

Skip menu "tdeunittest"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

tdeunittest

Skip menu "tdeunittest"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeunittest by doxygen 1.8.8
This website is maintained by Timothy Pearson.