catch_message.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Created by Phil Nash on 1/2/2013.
  3. * Copyright 2013 Two Blue Cubes Ltd. All rights reserved.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef TWOBLUECUBES_CATCH_MESSAGE_H_INCLUDED
  9. #define TWOBLUECUBES_CATCH_MESSAGE_H_INCLUDED
  10. #include "catch_result_type.h"
  11. #include "catch_common.h"
  12. #include "catch_stream.h"
  13. #include "catch_interfaces_capture.h"
  14. #include "catch_tostring.h"
  15. #include <string>
  16. #include <vector>
  17. namespace Catch {
  18. struct MessageInfo {
  19. MessageInfo( StringRef const& _macroName,
  20. SourceLineInfo const& _lineInfo,
  21. ResultWas::OfType _type );
  22. StringRef macroName;
  23. std::string message;
  24. SourceLineInfo lineInfo;
  25. ResultWas::OfType type;
  26. unsigned int sequence;
  27. bool operator == ( MessageInfo const& other ) const;
  28. bool operator < ( MessageInfo const& other ) const;
  29. private:
  30. static unsigned int globalCount;
  31. };
  32. struct MessageStream {
  33. template<typename T>
  34. MessageStream& operator << ( T const& value ) {
  35. m_stream << value;
  36. return *this;
  37. }
  38. ReusableStringStream m_stream;
  39. };
  40. struct MessageBuilder : MessageStream {
  41. MessageBuilder( StringRef const& macroName,
  42. SourceLineInfo const& lineInfo,
  43. ResultWas::OfType type );
  44. template<typename T>
  45. MessageBuilder& operator << ( T const& value ) {
  46. m_stream << value;
  47. return *this;
  48. }
  49. MessageInfo m_info;
  50. };
  51. class ScopedMessage {
  52. public:
  53. explicit ScopedMessage( MessageBuilder const& builder );
  54. ScopedMessage( ScopedMessage& duplicate ) = delete;
  55. ScopedMessage( ScopedMessage&& old );
  56. ~ScopedMessage();
  57. MessageInfo m_info;
  58. bool m_moved;
  59. };
  60. class Capturer {
  61. std::vector<MessageInfo> m_messages;
  62. IResultCapture& m_resultCapture = getResultCapture();
  63. size_t m_captured = 0;
  64. public:
  65. Capturer( StringRef macroName, SourceLineInfo const& lineInfo, ResultWas::OfType resultType, StringRef names );
  66. ~Capturer();
  67. void captureValue( size_t index, std::string const& value );
  68. template<typename T>
  69. void captureValues( size_t index, T const& value ) {
  70. captureValue( index, Catch::Detail::stringify( value ) );
  71. }
  72. template<typename T, typename... Ts>
  73. void captureValues( size_t index, T const& value, Ts const&... values ) {
  74. captureValue( index, Catch::Detail::stringify(value) );
  75. captureValues( index+1, values... );
  76. }
  77. };
  78. } // end namespace Catch
  79. #endif // TWOBLUECUBES_CATCH_MESSAGE_H_INCLUDED