form_format.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_format.cpp
  9. * \author Andrey Semashev
  10. * \date 07.02.2009
  11. *
  12. * \brief This header contains tests for the Boost.Format-style formatting.
  13. */
  14. #define BOOST_TEST_MODULE form_format
  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/expressions.hpp>
  20. #include <boost/log/core/record.hpp>
  21. #include <boost/log/utility/formatting_ostream.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 >
  29. struct format_test_data;
  30. #ifdef BOOST_LOG_USE_CHAR
  31. template< >
  32. struct format_test_data< char > :
  33. public test_data< char >
  34. {
  35. static const char* format1() { return "%1%, %2%"; }
  36. };
  37. #endif // BOOST_LOG_USE_CHAR
  38. #ifdef BOOST_LOG_USE_WCHAR_T
  39. template< >
  40. struct format_test_data< wchar_t > :
  41. public test_data< wchar_t >
  42. {
  43. static const wchar_t* format1() { return L"%1%, %2%"; }
  44. };
  45. #endif // BOOST_LOG_USE_WCHAR_T
  46. } // namespace
  47. // The test checks that Boost.Format formatting works
  48. BOOST_AUTO_TEST_CASE_TEMPLATE(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 format_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. record_view rec = make_record_view(set1);
  62. {
  63. string str1, str2;
  64. osstream strm1(str1), strm2(str2);
  65. formatter f = expr::format(data::format1()) % expr::attr< int >(data::attr1()) % expr::attr< double >(data::attr2());
  66. f(rec, strm1);
  67. strm2 << 10 << ", " << 5.5;
  68. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  69. }
  70. }