summaryrefslogtreecommitdiff
path: root/src/cppunit/TextTestRunner.cpp
blob: aaa6c861c3993dcff09ad121337665f12f97b21e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

#include <iostream>
#include <cppunit/TestSuite.h>
#include <cppunit/TextTestRunner.h>
#include <cppunit/TextTestResult.h>

namespace CppUnit {

TextTestRunner::TextTestRunner()
{
  m_suite = new TestSuite( "All Tests" );
}


TextTestRunner::~TextTestRunner()
{
  delete m_suite;
}


/** Adds the specified test.
 *
 * \param test Test to add.
 */
void 
TextTestRunner::addTest( Test *test )
{
  if ( test != NULL )
    m_suite->addTest( test );
}


/** Runs the named test case.
 *
 * \param testName Name of the test case to run. If an empty is given, then
 *                 all added test are run. The name must be the name of
 *                 of an added test.
 * \param doWait if \c true then the user must press the RETURN key 
 *               before the run() method exit.
 */

void 
TextTestRunner::run( std::string testName,
                     bool doWait )
{
  runTestByName( testName );
  wait( doWait );
}


void 
TextTestRunner::runTestByName( std::string testName )
{
  if ( testName.empty() )
  {
    runTest( m_suite );
  }
  else
  { 
    Test *test = findTestByName( testName );
    if ( test != NULL )
      runTest( test );
    else
    {
      std::cout << "Test " << testName << " not found." << std::endl;
    }
  }
}


void 
TextTestRunner::wait( bool doWait )
{
  if ( doWait ) 
  {
    std::cout << "<RETURN> to continue" << std::endl;
    std::cin.get ();
  }
}


Test * 
TextTestRunner::findTestByName( std::string name ) const
{
  for ( std::vector<Test *>::const_iterator it = m_suite->getTests().begin(); 
        it != m_suite->getTests().end(); 
        ++it )
  {
    Test *test = *it;
    if ( test->getName() == name )
      return test;
  }
  return NULL;
}


void 
TextTestRunner::runTest( Test *test )
{
  TextTestResult result;
  test->run( &result );
  std::cout << result << std::endl;
}


}  //  namespace CppUnit