form_message.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 form_message.cpp
  9. * \author Andrey Semashev
  10. * \date 01.02.2009
  11. *
  12. * \brief This header contains tests for the \c message formatter.
  13. */
  14. #define BOOST_TEST_MODULE form_message
  15. #include <string>
  16. #include <boost/test/unit_test.hpp>
  17. #include <boost/log/attributes/constant.hpp>
  18. #include <boost/log/attributes/attribute_set.hpp>
  19. #include <boost/log/utility/formatting_ostream.hpp>
  20. #include <boost/log/expressions.hpp>
  21. #include <boost/log/core/record.hpp>
  22. #include "char_definitions.hpp"
  23. #include "make_record.hpp"
  24. namespace logging = boost::log;
  25. namespace attrs = logging::attributes;
  26. namespace expr = logging::expressions;
  27. namespace {
  28. template< typename CharT >
  29. struct message_test_data;
  30. #ifdef BOOST_LOG_USE_CHAR
  31. template< >
  32. struct message_test_data< char > :
  33. public test_data< char >
  34. {
  35. static expr::smessage_type message() { return expr::smessage; }
  36. };
  37. #endif // BOOST_LOG_USE_CHAR
  38. #ifdef BOOST_LOG_USE_WCHAR_T
  39. template< >
  40. struct message_test_data< wchar_t > :
  41. public test_data< wchar_t >
  42. {
  43. static expr::wmessage_type message() { return expr::wmessage; }
  44. };
  45. #endif // BOOST_LOG_USE_WCHAR_T
  46. } // namespace
  47. // The test checks that message formatting work
  48. BOOST_AUTO_TEST_CASE_TEMPLATE(message_formatting, CharT, char_types)
  49. {
  50. typedef logging::attribute_set attr_set;
  51. typedef std::basic_string< CharT > string;
  52. typedef logging::basic_formatting_ostream< CharT > osstream;
  53. typedef logging::record_view record_view;
  54. typedef logging::basic_formatter< CharT > formatter;
  55. typedef message_test_data< CharT > data;
  56. attrs::constant< int > attr1(10);
  57. attrs::constant< double > attr2(5.5);
  58. attr_set set1;
  59. set1[data::attr1()] = attr1;
  60. set1[data::attr2()] = attr2;
  61. set1[data::message().get_name()] = attrs::constant< string >(data::some_test_string());
  62. record_view rec = make_record_view(set1);
  63. {
  64. string str1, str2;
  65. osstream strm1(str1), strm2(str2);
  66. formatter f = expr::stream << data::message();
  67. f(rec, strm1);
  68. strm2 << data::some_test_string();
  69. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  70. }
  71. }