Tizen 2.1 base
[platform/upstream/libbullet.git] / UnitTests / cppunit / src / cppunit / TextTestRunner.cpp
1 // ==> Implementation of cppunit/ui/text/TestRunner.h
2
3 #include <cppunit/config/SourcePrefix.h>
4 #include <cppunit/TestSuite.h>
5 #include <cppunit/TextTestResult.h>
6 #include <cppunit/TextOutputter.h>
7 #include <cppunit/TextTestProgressListener.h>
8 #include <cppunit/TestResult.h>
9 #include <cppunit/ui/text/TextTestRunner.h>
10 #include <cppunit/portability/Stream.h>
11 #include <stdexcept>
12
13
14 CPPUNIT_NS_BEGIN
15
16
17 /*! Constructs a new text runner.
18  * \param outputter used to print text result. Owned by the runner.
19  */
20 TextTestRunner::TextTestRunner( Outputter *outputter ) 
21     : m_result( new TestResultCollector() )
22     , m_eventManager( new TestResult() )
23     , m_outputter( outputter )
24 {
25   if ( !m_outputter )
26     m_outputter = new TextOutputter( m_result, stdCOut() );
27   m_eventManager->addListener( m_result );
28 }
29
30
31 TextTestRunner::~TextTestRunner()
32 {
33   delete m_eventManager;
34   delete m_outputter;
35   delete m_result;
36 }
37
38
39 /*! Runs the named test case.
40  *
41  * \param testName Name of the test case to run. If an empty is given, then
42  *                 all added tests are run. The name can be the name of any
43  *                 test in the hierarchy.
44  * \param doWait if \c true then the user must press the RETURN key 
45  *               before the run() method exit.
46  * \param doPrintResult if \c true (default) then the test result are printed
47  *                      on the standard output.
48  * \param doPrintProgress if \c true (default) then TextTestProgressListener is
49  *                        used to show the progress.
50  * \return \c true is the test was successful, \c false if the test
51  *         failed or was not found.
52  */
53 bool
54 TextTestRunner::run( std::string testName,
55                        bool doWait,
56                        bool doPrintResult,
57                        bool doPrintProgress )
58 {
59   TextTestProgressListener progress;
60   if ( doPrintProgress )
61     m_eventManager->addListener( &progress );
62
63   TestRunner *pThis = this;
64   pThis->run( *m_eventManager, testName );
65
66   if ( doPrintProgress )
67     m_eventManager->removeListener( &progress );
68
69   printResult( doPrintResult );
70   wait( doWait );
71
72   return m_result->wasSuccessful();
73 }
74
75
76 void 
77 TextTestRunner::wait( bool doWait )
78 {
79 #if !defined( CPPUNIT_NO_STREAM )
80   if ( doWait ) 
81   {
82     stdCOut() << "<RETURN> to continue\n";
83     stdCOut().flush();
84     std::cin.get ();
85   }
86 #endif
87 }
88
89
90 void 
91 TextTestRunner::printResult( bool doPrintResult )
92 {
93   stdCOut() << "\n";
94   if ( doPrintResult )
95     m_outputter->write();
96 }
97
98
99 /*! Returns the result of the test run.
100  * Use this after calling run() to access the result of the test run.
101  */
102 TestResultCollector &
103 TextTestRunner::result() const
104 {
105   return *m_result;
106 }
107
108
109 /*! Returns the event manager.
110  * The instance of TestResult results returned is the one that is used to run the
111  * test. Use this to register additional TestListener before running the tests.
112  */
113 TestResult &
114 TextTestRunner::eventManager() const
115 {
116   return *m_eventManager;
117 }
118
119
120 /*! Specifies an alternate outputter.
121  *
122  * Notes that the outputter will be use after the test run only if \a printResult was
123  * \c true.
124  * \param outputter New outputter to use. The previous outputter is destroyed. 
125  *                  The TextTestRunner assumes ownership of the outputter.
126  * \see CompilerOutputter, XmlOutputter, TextOutputter.
127  */
128 void 
129 TextTestRunner::setOutputter( Outputter *outputter )
130 {
131   delete m_outputter;
132   m_outputter = outputter;
133 }
134
135
136 void 
137 TextTestRunner::run( TestResult &controller,
138                      const std::string &testPath )
139 {
140   TestRunner::run( controller, testPath );
141 }
142
143
144 CPPUNIT_NS_END