util_manip_to_log.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file util_manip_to_log.cpp
  9. * \author Andrey Semashev
  10. * \date 05.07.2015
  11. *
  12. * \brief This header contains tests for the \c to_log stream manipulator.
  13. */
  14. #define BOOST_TEST_MODULE util_manip_to_log
  15. #include <string>
  16. #include <sstream>
  17. #include <algorithm>
  18. #include <boost/test/unit_test.hpp>
  19. #include <boost/log/utility/formatting_ostream.hpp>
  20. #include <boost/log/utility/manipulators/to_log.hpp>
  21. #include "char_definitions.hpp"
  22. namespace logging = boost::log;
  23. namespace tag {
  24. struct a_my_class;
  25. } // namespace tag
  26. namespace {
  27. struct my_class
  28. {
  29. int m_Data;
  30. explicit my_class(int data) : m_Data(data) {}
  31. };
  32. template< typename CharT, typename TraitsT >
  33. inline std::basic_ostream< CharT, TraitsT >&
  34. operator<< (std::basic_ostream< CharT, TraitsT >& strm, my_class const& obj)
  35. {
  36. strm << "my_class: [data: " << obj.m_Data << "]";
  37. return strm;
  38. }
  39. template< typename StreamT >
  40. inline StreamT&
  41. operator<< (StreamT& strm, logging::to_log_manip< my_class > const& obj)
  42. {
  43. strm << "to_log(my_class: [data: " << obj.get().m_Data << "])";
  44. return strm;
  45. }
  46. template< typename StreamT >
  47. inline StreamT&
  48. operator<< (StreamT& strm, logging::to_log_manip< my_class, tag::a_my_class > const& obj)
  49. {
  50. strm << "to_log<a_my_class>(my_class: [data: " << obj.get().m_Data << "])";
  51. return strm;
  52. }
  53. template< typename CharT, typename StreamT >
  54. struct tests
  55. {
  56. typedef CharT char_type;
  57. typedef StreamT stream_type;
  58. typedef std::basic_string< char_type > string;
  59. typedef std::basic_ostringstream< char_type > std_stream;
  60. //! The test verifies that the default behavior of the manipulator is equivalent to the standard operator<<
  61. static void default_operator()
  62. {
  63. string str;
  64. stream_type strm1(str);
  65. strm1 << logging::to_log(10);
  66. std_stream strm2;
  67. strm2 << 10;
  68. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  69. }
  70. //! The test verifies that operator overrides work
  71. static void operator_overrides()
  72. {
  73. {
  74. string str;
  75. stream_type strm1(str);
  76. strm1 << my_class(10);
  77. std_stream strm2;
  78. strm2 << "my_class: [data: " << 10 << "]";
  79. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  80. }
  81. {
  82. string str;
  83. stream_type strm1(str);
  84. strm1 << logging::to_log(my_class(10));
  85. std_stream strm2;
  86. strm2 << "to_log(my_class: [data: " << 10 << "])";
  87. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  88. }
  89. {
  90. string str;
  91. stream_type strm1(str);
  92. strm1 << logging::to_log< tag::a_my_class >(my_class(10));
  93. std_stream strm2;
  94. strm2 << "to_log<a_my_class>(my_class: [data: " << 10 << "])";
  95. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  96. }
  97. }
  98. };
  99. } // namespace
  100. //! The test verifies that the default behavior of the manipulator is equivalent to the standard operator<<
  101. BOOST_AUTO_TEST_CASE_TEMPLATE(default_operator, CharT, char_types)
  102. {
  103. tests< CharT, std::basic_ostringstream< CharT > >::default_operator();
  104. tests< CharT, logging::basic_formatting_ostream< CharT > >::default_operator();
  105. }
  106. //! The test verifies that operator overrides work
  107. BOOST_AUTO_TEST_CASE_TEMPLATE(operator_overrides, CharT, char_types)
  108. {
  109. tests< CharT, std::basic_ostringstream< CharT > >::operator_overrides();
  110. tests< CharT, logging::basic_formatting_ostream< CharT > >::operator_overrides();
  111. }