Protector.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <cppunit/Exception.h>
  2. #include <cppunit/Message.h>
  3. #include <cppunit/Protector.h>
  4. #include <cppunit/TestResult.h>
  5. #include "ProtectorContext.h"
  6. #include <memory>
  7. CPPUNIT_NS_BEGIN
  8. Functor::~Functor()
  9. {
  10. }
  11. Protector::~Protector()
  12. {
  13. }
  14. void
  15. Protector::reportError( const ProtectorContext &context,
  16. const Exception &error ) const
  17. {
  18. std::auto_ptr<Exception> actualError( error.clone() );
  19. actualError->setMessage( actualMessage( actualError->message(), context ) );
  20. context.m_result->addError( context.m_test,
  21. actualError.release() );
  22. }
  23. void
  24. Protector::reportError( const ProtectorContext &context,
  25. const Message &message,
  26. const SourceLine &sourceLine ) const
  27. {
  28. reportError( context, Exception( message, sourceLine ) );
  29. }
  30. void
  31. Protector::reportFailure( const ProtectorContext &context,
  32. const Exception &failure ) const
  33. {
  34. std::auto_ptr<Exception> actualFailure( failure.clone() );
  35. actualFailure->setMessage( actualMessage( actualFailure->message(), context ) );
  36. context.m_result->addFailure( context.m_test,
  37. actualFailure.release() );
  38. }
  39. Message
  40. Protector::actualMessage( const Message &message,
  41. const ProtectorContext &context ) const
  42. {
  43. Message theActualMessage;
  44. if ( context.m_shortDescription.empty() )
  45. theActualMessage = message;
  46. else
  47. {
  48. theActualMessage = Message( context.m_shortDescription,
  49. message.shortDescription() );
  50. theActualMessage.addDetail( message );
  51. }
  52. return theActualMessage;
  53. }
  54. ProtectorGuard::ProtectorGuard( TestResult *result,
  55. Protector *protector )
  56. : m_result( result )
  57. {
  58. m_result->pushProtector( protector );
  59. }
  60. ProtectorGuard::~ProtectorGuard()
  61. {
  62. m_result->popProtector();
  63. }
  64. CPPUNIT_NS_END