From fed3ef0b01704ee7fd9f41e8cbb706da38c004fe Mon Sep 17 00:00:00 2001 From: Baptiste Lepilleur Date: Sat, 19 May 2001 10:29:11 +0000 Subject: * Merged Steve M. Robbins patch to replace assertImplementation with assert in hierarchy example. * Added a TextTestRunner to runner tests. It is based on Michael Feather's version, but have been rewriten. * Removed traces that printed the test name in TextTestResult while running. * Added the test name to error and failure report in TextTestResult. * Updated hierarchy example to use TextTestRunner. --- src/cppunit/TextTestRunner.cpp | 106 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/cppunit/TextTestRunner.cpp (limited to 'src/cppunit/TextTestRunner.cpp') diff --git a/src/cppunit/TextTestRunner.cpp b/src/cppunit/TextTestRunner.cpp new file mode 100644 index 0000000..aaa6c86 --- /dev/null +++ b/src/cppunit/TextTestRunner.cpp @@ -0,0 +1,106 @@ + +#include +#include +#include +#include + +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 << " to continue" << std::endl; + std::cin.get (); + } +} + + +Test * +TextTestRunner::findTestByName( std::string name ) const +{ + for ( std::vector::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 -- cgit v1.2.1