diff options
author | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2015-06-20 00:22:50 -0400 |
---|---|---|
committer | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2015-06-20 10:56:02 -0400 |
commit | 9c2ed42daa8fbbef4a919c21ec564e2db55e8d60 (patch) | |
tree | 3814f79c10d7b490948d8cb7b112ac1dd41ceff1 /src/mongo/unittest/unittest.cpp | |
parent | 01965cf52bce6976637ecb8f4a622aeb05ab256a (diff) | |
download | mongo-9c2ed42daa8fbbef4a919c21ec564e2db55e8d60.tar.gz |
SERVER-18579: Clang-Format - reformat code, no comment reflow
Diffstat (limited to 'src/mongo/unittest/unittest.cpp')
-rw-r--r-- | src/mongo/unittest/unittest.cpp | 639 |
1 files changed, 315 insertions, 324 deletions
diff --git a/src/mongo/unittest/unittest.cpp b/src/mongo/unittest/unittest.cpp index d4f0729ab67..ac0820de084 100644 --- a/src/mongo/unittest/unittest.cpp +++ b/src/mongo/unittest/unittest.cpp @@ -47,383 +47,374 @@ namespace mongo { - using std::shared_ptr; - using std::string; +using std::shared_ptr; +using std::string; - namespace unittest { +namespace unittest { - namespace { - logger::MessageLogDomain* unittestOutput = - logger::globalLogManager()->getNamedDomain("unittest"); +namespace { +logger::MessageLogDomain* unittestOutput = logger::globalLogManager()->getNamedDomain("unittest"); - typedef std::map<std::string, std::shared_ptr<Suite> > SuiteMap; +typedef std::map<std::string, std::shared_ptr<Suite>> SuiteMap; - inline SuiteMap& _allSuites() { - static SuiteMap allSuites; - return allSuites; - } +inline SuiteMap& _allSuites() { + static SuiteMap allSuites; + return allSuites; +} - } // namespace +} // namespace - logger::LogstreamBuilder log() { - return LogstreamBuilder(unittestOutput, getThreadName(), logger::LogSeverity::Log()); +logger::LogstreamBuilder log() { + return LogstreamBuilder(unittestOutput, getThreadName(), logger::LogSeverity::Log()); +} + +MONGO_INITIALIZER_WITH_PREREQUISITES(UnitTestOutput, + ("GlobalLogManager", "default"))(InitializerContext*) { + unittestOutput->attachAppender(logger::MessageLogDomain::AppenderAutoPtr( + new logger::ConsoleAppender<logger::MessageLogDomain::Event>( + new logger::MessageEventDetailsEncoder))); + return Status::OK(); +} + +class Result { +public: + Result(const std::string& name) + : _name(name), _rc(0), _tests(0), _fails(), _asserts(0), _millis(0) {} + + std::string toString() { + std::stringstream ss; + + char result[128]; + sprintf(result, + "%-30s | tests: %4d | fails: %4d | assert calls: %10d | time secs: %6.3f\n", + _name.c_str(), + _tests, + static_cast<int>(_fails.size()), + _asserts, + _millis / 1000.0); + ss << result; + + for (std::vector<std::string>::iterator i = _messages.begin(); i != _messages.end(); i++) { + ss << "\t" << *i << '\n'; } - MONGO_INITIALIZER_WITH_PREREQUISITES(UnitTestOutput, ("GlobalLogManager", "default"))( - InitializerContext*) { + return ss.str(); + } - unittestOutput->attachAppender( - logger::MessageLogDomain::AppenderAutoPtr( - new logger::ConsoleAppender<logger::MessageLogDomain::Event>( - new logger::MessageEventDetailsEncoder))); - return Status::OK(); - } + int rc() { + return _rc; + } - class Result { - public: - Result( const std::string& name ) - : _name( name ) , _rc(0) , _tests(0) , _fails() , _asserts(0), _millis(0) {} + string _name; - std::string toString() { - std::stringstream ss; + int _rc; + int _tests; + std::vector<std::string> _fails; + int _asserts; + int _millis; + std::vector<std::string> _messages; - char result[128]; - sprintf(result, - "%-30s | tests: %4d | fails: %4d | assert calls: %10d | time secs: %6.3f\n", - _name.c_str(), _tests, static_cast<int>(_fails.size()), _asserts, _millis/1000.0 ); - ss << result; + static Result* cur; +}; - for ( std::vector<std::string>::iterator i=_messages.begin(); i!=_messages.end(); i++ ) { - ss << "\t" << *i << '\n'; - } +Result* Result::cur = 0; - return ss.str(); - } - - int rc() { - return _rc; - } +Test::Test() : _isCapturingLogMessages(false) {} - string _name; - - int _rc; - int _tests; - std::vector<std::string> _fails; - int _asserts; - int _millis; - std::vector<std::string> _messages; - - static Result * cur; - }; - - Result* Result::cur = 0; - - Test::Test() : _isCapturingLogMessages(false) {} - - Test::~Test() { - if (_isCapturingLogMessages) { - stopCapturingLogMessages(); - } - } +Test::~Test() { + if (_isCapturingLogMessages) { + stopCapturingLogMessages(); + } +} - void Test::run() { - setUp(); +void Test::run() { + setUp(); - // An uncaught exception does not prevent the tear down from running. But - // such an event still constitutes an error. To test this behavior we use a - // special exception here that when thrown does trigger the tear down but is - // not considered an error. - try { - _doTest(); - } - catch (FixtureExceptionForTesting&) { - tearDown(); - return; - } - catch (TestAssertionFailureException&) { - tearDown(); - throw; - } + // An uncaught exception does not prevent the tear down from running. But + // such an event still constitutes an error. To test this behavior we use a + // special exception here that when thrown does trigger the tear down but is + // not considered an error. + try { + _doTest(); + } catch (FixtureExceptionForTesting&) { + tearDown(); + return; + } catch (TestAssertionFailureException&) { + tearDown(); + throw; + } - tearDown(); - } + tearDown(); +} - void Test::setUp() {} - void Test::tearDown() {} +void Test::setUp() {} +void Test::tearDown() {} namespace { - class StringVectorAppender : public logger::MessageLogDomain::EventAppender { - public: - explicit StringVectorAppender(std::vector<std::string>* lines) : _lines(lines) {} - virtual ~StringVectorAppender() {} - virtual Status append(const logger::MessageLogDomain::Event& event) { - std::ostringstream _os; - if (!_encoder.encode(event, _os)) { - return Status(ErrorCodes::LogWriteFailed, "Failed to append to LogTestAppender."); - } - _lines->push_back(_os.str()); - return Status::OK(); +class StringVectorAppender : public logger::MessageLogDomain::EventAppender { +public: + explicit StringVectorAppender(std::vector<std::string>* lines) : _lines(lines) {} + virtual ~StringVectorAppender() {} + virtual Status append(const logger::MessageLogDomain::Event& event) { + std::ostringstream _os; + if (!_encoder.encode(event, _os)) { + return Status(ErrorCodes::LogWriteFailed, "Failed to append to LogTestAppender."); } - private: - logger::MessageEventDetailsEncoder _encoder; - std::vector<std::string>* _lines; - }; + _lines->push_back(_os.str()); + return Status::OK(); + } + +private: + logger::MessageEventDetailsEncoder _encoder; + std::vector<std::string>* _lines; +}; } // namespace - void Test::startCapturingLogMessages() { - invariant(!_isCapturingLogMessages); - _capturedLogMessages.clear(); - _captureAppenderHandle = logger::globalLogDomain()->attachAppender( - logger::MessageLogDomain::AppenderAutoPtr(new StringVectorAppender( - &_capturedLogMessages))); - _isCapturingLogMessages = true; +void Test::startCapturingLogMessages() { + invariant(!_isCapturingLogMessages); + _capturedLogMessages.clear(); + _captureAppenderHandle = logger::globalLogDomain()->attachAppender( + logger::MessageLogDomain::AppenderAutoPtr(new StringVectorAppender(&_capturedLogMessages))); + _isCapturingLogMessages = true; +} + +void Test::stopCapturingLogMessages() { + invariant(_isCapturingLogMessages); + logger::globalLogDomain()->detachAppender(_captureAppenderHandle); + _isCapturingLogMessages = false; +} + +Suite::Suite(const std::string& name) : _name(name) { + registerSuite(name, this); +} + +Suite::~Suite() {} + +void Suite::add(const std::string& name, const TestFunction& testFn) { + _tests.push_back(std::shared_ptr<TestHolder>(new TestHolder(name, testFn))); +} + +Result* Suite::run(const std::string& filter, int runsPerTest) { + LOG(1) << "\t about to setupTests" << std::endl; + setupTests(); + LOG(1) << "\t done setupTests" << std::endl; + + Timer timer; + Result* r = new Result(_name); + Result::cur = r; + + for (std::vector<std::shared_ptr<TestHolder>>::iterator i = _tests.begin(); i != _tests.end(); + i++) { + std::shared_ptr<TestHolder>& tc = *i; + if (filter.size() && tc->getName().find(filter) == std::string::npos) { + LOG(1) << "\t skipping test: " << tc->getName() << " because doesn't match filter" + << std::endl; + continue; } - void Test::stopCapturingLogMessages() { - invariant(_isCapturingLogMessages); - logger::globalLogDomain()->detachAppender(_captureAppenderHandle); - _isCapturingLogMessages = false; - } + r->_tests++; - Suite::Suite( const std::string& name ) : _name( name ) { - registerSuite( name , this ); - } - - Suite::~Suite() {} - - void Suite::add(const std::string& name, const TestFunction& testFn) { - _tests.push_back(std::shared_ptr<TestHolder>(new TestHolder(name, testFn))); - } - - Result * Suite::run( const std::string& filter, int runsPerTest ) { - - LOG(1) << "\t about to setupTests" << std::endl; - setupTests(); - LOG(1) << "\t done setupTests" << std::endl; - - Timer timer; - Result * r = new Result( _name ); - Result::cur = r; - - for ( std::vector< std::shared_ptr<TestHolder> >::iterator i=_tests.begin(); - i!=_tests.end(); i++ ) { - std::shared_ptr<TestHolder>& tc = *i; - if ( filter.size() && tc->getName().find( filter ) == std::string::npos ) { - LOG(1) << "\t skipping test: " << tc->getName() << " because doesn't match filter" << std::endl; - continue; - } - - r->_tests++; - - bool passes = false; - - onCurrentTestNameChange( tc->getName() ); - - log() << "\t going to run test: " << tc->getName() << std::endl; - - std::stringstream err; - err << tc->getName() << "\t"; - - try { - for ( int x=0; x<runsPerTest; x++ ) - tc->run(); - passes = true; - } - catch ( const TestAssertionFailureException& ae ) { - err << ae.toString(); - } - catch ( const std::exception& e ) { - err << " std::exception: " << e.what() << " in test " << tc->getName(); - } - catch ( int x ) { - err << " caught int " << x << " in test " << tc->getName(); - } - - if ( ! passes ) { - std::string s = err.str(); - log() << "FAIL: " << s << std::endl; - r->_fails.push_back(tc->getName()); - r->_messages.push_back( s ); - } - } + bool passes = false; - if ( !r->_fails.empty() ) - r->_rc = 17; + onCurrentTestNameChange(tc->getName()); - r->_millis = timer.millis(); + log() << "\t going to run test: " << tc->getName() << std::endl; - onCurrentTestNameChange( "" ); + std::stringstream err; + err << tc->getName() << "\t"; - log() << "\t DONE running tests" << std::endl; - - return r; + try { + for (int x = 0; x < runsPerTest; x++) + tc->run(); + passes = true; + } catch (const TestAssertionFailureException& ae) { + err << ae.toString(); + } catch (const std::exception& e) { + err << " std::exception: " << e.what() << " in test " << tc->getName(); + } catch (int x) { + err << " caught int " << x << " in test " << tc->getName(); } - int Suite::run( const std::vector<std::string>& suites , const std::string& filter , int runsPerTest ) { - - if (_allSuites().empty()) { - log() << "error: no suites registered."; - return EXIT_FAILURE; - } - - for ( unsigned int i = 0; i < suites.size(); i++ ) { - if ( _allSuites().count( suites[i] ) == 0 ) { - log() << "invalid test suite [" << suites[i] << "], use --list to see valid names" << std::endl; - return EXIT_FAILURE; - } - } - - std::vector<std::string> torun(suites); - - if ( torun.empty() ) { - for ( SuiteMap::const_iterator i = _allSuites().begin(); - i !=_allSuites().end(); ++i ) { + if (!passes) { + std::string s = err.str(); + log() << "FAIL: " << s << std::endl; + r->_fails.push_back(tc->getName()); + r->_messages.push_back(s); + } + } - torun.push_back( i->first ); - } - } + if (!r->_fails.empty()) + r->_rc = 17; - std::vector<Result*> results; + r->_millis = timer.millis(); - for ( std::vector<std::string>::iterator i=torun.begin(); i!=torun.end(); i++ ) { - std::string name = *i; - std::shared_ptr<Suite>& s = _allSuites()[name]; - fassert( 16145, s != NULL ); + onCurrentTestNameChange(""); - log() << "going to run suite: " << name << std::endl; - results.push_back( s->run( filter, runsPerTest ) ); - } + log() << "\t DONE running tests" << std::endl; - log() << "**************************************************" << std::endl; - - int rc = 0; - - int tests = 0; - int asserts = 0; - int millis = 0; - - Result totals ("TOTALS"); - std::vector<std::string> failedSuites; - - Result::cur = NULL; - for ( std::vector<Result*>::iterator i=results.begin(); i!=results.end(); i++ ) { - Result* r = *i; - log() << r->toString(); - if ( abs( r->rc() ) > abs( rc ) ) - rc = r->rc(); - - tests += r->_tests; - if ( !r->_fails.empty() ) { - failedSuites.push_back(r->toString()); - for ( std::vector<std::string>::const_iterator j=r->_fails.begin(); - j!=r->_fails.end(); j++ ) { - const std::string& s = (*j); - totals._fails.push_back(r->_name + "/" + s); - } - } - asserts += r->_asserts; - millis += r->_millis; - - delete r; - } + return r; +} - totals._tests = tests; - totals._asserts = asserts; - totals._millis = millis; - - log() << totals.toString(); // includes endl - - // summary - if ( !totals._fails.empty() ) { - log() << "Failing tests:" << std::endl; - for ( std::vector<std::string>::const_iterator i=totals._fails.begin(); - i!=totals._fails.end(); i++ ) { - const std::string& s = (*i); - log() << "\t " << s << " Failed"; - } - log() << "FAILURE - " << totals._fails.size() << " tests in " - << failedSuites.size() << " suites failed"; - } - else { - log() << "SUCCESS - All tests in all suites passed"; - } +int Suite::run(const std::vector<std::string>& suites, const std::string& filter, int runsPerTest) { + if (_allSuites().empty()) { + log() << "error: no suites registered."; + return EXIT_FAILURE; + } - return rc; + for (unsigned int i = 0; i < suites.size(); i++) { + if (_allSuites().count(suites[i]) == 0) { + log() << "invalid test suite [" << suites[i] << "], use --list to see valid names" + << std::endl; + return EXIT_FAILURE; } + } - void Suite::registerSuite( const std::string& name , Suite* s ) { - std::shared_ptr<Suite>& m = _allSuites()[name]; - fassert( 10162, !m ); - m.reset(s); - } + std::vector<std::string> torun(suites); - Suite* Suite::getSuite(const std::string& name) { - std::shared_ptr<Suite>& result = _allSuites()[name]; - if (!result) { - // Suites are self-registering. - new Suite(name); - } - invariant(result); - return result.get(); + if (torun.empty()) { + for (SuiteMap::const_iterator i = _allSuites().begin(); i != _allSuites().end(); ++i) { + torun.push_back(i->first); } + } - void Suite::setupTests() {} - - TestAssertionFailureException::TestAssertionFailureException( - const std::string& theFile, - unsigned theLine, - const std::string& theFailingExpression ) - : _file(theFile), _line(theLine), _message(theFailingExpression) {} + std::vector<Result*> results; - std::string TestAssertionFailureException::toString() const { - std::ostringstream os; - os << getMessage() << " @" << getFile() << ":" << getLine(); - return os.str(); - } + for (std::vector<std::string>::iterator i = torun.begin(); i != torun.end(); i++) { + std::string name = *i; + std::shared_ptr<Suite>& s = _allSuites()[name]; + fassert(16145, s != NULL); - TestAssertionFailure::TestAssertionFailure(const std::string& file, - unsigned line, - const std::string& message) - : _exception(file, line, message), _enabled(false) {} + log() << "going to run suite: " << name << std::endl; + results.push_back(s->run(filter, runsPerTest)); + } - TestAssertionFailure::TestAssertionFailure(const TestAssertionFailure& other) : - _exception(other._exception), _enabled(false) { + log() << "**************************************************" << std::endl; - invariant(!other._enabled); - } + int rc = 0; - TestAssertionFailure& TestAssertionFailure::operator=(const TestAssertionFailure& other) { - invariant(!_enabled); - invariant(!other._enabled); - _exception = other._exception; - return *this; - } + int tests = 0; + int asserts = 0; + int millis = 0; - TestAssertionFailure::~TestAssertionFailure() BOOST_NOEXCEPT_IF(false) - { - if (!_enabled) { - invariant(_stream.str().empty()); - return; - } - if (!_stream.str().empty()) { - _exception.setMessage(_exception.getMessage() + " " + _stream.str()); - } - throw _exception; - } + Result totals("TOTALS"); + std::vector<std::string> failedSuites; - std::ostream& TestAssertionFailure::stream() { - invariant(!_enabled); - _enabled = true; - return _stream; - } + Result::cur = NULL; + for (std::vector<Result*>::iterator i = results.begin(); i != results.end(); i++) { + Result* r = *i; + log() << r->toString(); + if (abs(r->rc()) > abs(rc)) + rc = r->rc(); - std::vector<std::string> getAllSuiteNames() { - std::vector<std::string> result; - for (SuiteMap::const_iterator i = _allSuites().begin(); i != _allSuites().end(); ++i) { - result.push_back(i->first); + tests += r->_tests; + if (!r->_fails.empty()) { + failedSuites.push_back(r->toString()); + for (std::vector<std::string>::const_iterator j = r->_fails.begin(); + j != r->_fails.end(); + j++) { + const std::string& s = (*j); + totals._fails.push_back(r->_name + "/" + s); } - return result; } - - } // namespace unittest + asserts += r->_asserts; + millis += r->_millis; + + delete r; + } + + totals._tests = tests; + totals._asserts = asserts; + totals._millis = millis; + + log() << totals.toString(); // includes endl + + // summary + if (!totals._fails.empty()) { + log() << "Failing tests:" << std::endl; + for (std::vector<std::string>::const_iterator i = totals._fails.begin(); + i != totals._fails.end(); + i++) { + const std::string& s = (*i); + log() << "\t " << s << " Failed"; + } + log() << "FAILURE - " << totals._fails.size() << " tests in " << failedSuites.size() + << " suites failed"; + } else { + log() << "SUCCESS - All tests in all suites passed"; + } + + return rc; +} + +void Suite::registerSuite(const std::string& name, Suite* s) { + std::shared_ptr<Suite>& m = _allSuites()[name]; + fassert(10162, !m); + m.reset(s); +} + +Suite* Suite::getSuite(const std::string& name) { + std::shared_ptr<Suite>& result = _allSuites()[name]; + if (!result) { + // Suites are self-registering. + new Suite(name); + } + invariant(result); + return result.get(); +} + +void Suite::setupTests() {} + +TestAssertionFailureException::TestAssertionFailureException( + const std::string& theFile, unsigned theLine, const std::string& theFailingExpression) + : _file(theFile), _line(theLine), _message(theFailingExpression) {} + +std::string TestAssertionFailureException::toString() const { + std::ostringstream os; + os << getMessage() << " @" << getFile() << ":" << getLine(); + return os.str(); +} + +TestAssertionFailure::TestAssertionFailure(const std::string& file, + unsigned line, + const std::string& message) + : _exception(file, line, message), _enabled(false) {} + +TestAssertionFailure::TestAssertionFailure(const TestAssertionFailure& other) + : _exception(other._exception), _enabled(false) { + invariant(!other._enabled); +} + +TestAssertionFailure& TestAssertionFailure::operator=(const TestAssertionFailure& other) { + invariant(!_enabled); + invariant(!other._enabled); + _exception = other._exception; + return *this; +} + +TestAssertionFailure::~TestAssertionFailure() BOOST_NOEXCEPT_IF(false) { + if (!_enabled) { + invariant(_stream.str().empty()); + return; + } + if (!_stream.str().empty()) { + _exception.setMessage(_exception.getMessage() + " " + _stream.str()); + } + throw _exception; +} + +std::ostream& TestAssertionFailure::stream() { + invariant(!_enabled); + _enabled = true; + return _stream; +} + +std::vector<std::string> getAllSuiteNames() { + std::vector<std::string> result; + for (SuiteMap::const_iterator i = _allSuites().begin(); i != _allSuites().end(); ++i) { + result.push_back(i->first); + } + return result; +} + +} // namespace unittest } // namespace mongo |