form_xml_decor.cpp 2.9 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_xml_decor.cpp
  9. * \author Andrey Semashev
  10. * \date 26.09.2015
  11. *
  12. * \brief This header contains tests for the \c xml_decor formatter.
  13. */
  14. #define BOOST_TEST_MODULE form_xml_decor
  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/type_dispatch/standard_types.hpp>
  20. #include <boost/log/utility/formatting_ostream.hpp>
  21. #include <boost/log/expressions/attr.hpp>
  22. #include <boost/log/expressions/formatter.hpp>
  23. #include <boost/log/expressions/formatters/xml_decorator.hpp>
  24. #include <boost/log/expressions/formatters/stream.hpp>
  25. #include <boost/log/core/record.hpp>
  26. #include "char_definitions.hpp"
  27. #include "make_record.hpp"
  28. namespace logging = boost::log;
  29. namespace attrs = logging::attributes;
  30. namespace expr = logging::expressions;
  31. namespace {
  32. template< typename >
  33. struct test_strings;
  34. #ifdef BOOST_LOG_USE_CHAR
  35. template< >
  36. struct test_strings< char > : public test_data< char >
  37. {
  38. static const char* printable_chars() { return " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; }
  39. static const char* escaped_chars() { return " !&quot;#$%&amp;&apos;()*+,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; }
  40. };
  41. #endif
  42. #ifdef BOOST_LOG_USE_WCHAR_T
  43. template< >
  44. struct test_strings< wchar_t > : public test_data< wchar_t >
  45. {
  46. static const wchar_t* printable_chars() { return L" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; }
  47. static const wchar_t* escaped_chars() { return L" !&quot;#$%&amp;&apos;()*+,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; }
  48. };
  49. #endif
  50. } // namespace
  51. BOOST_AUTO_TEST_CASE_TEMPLATE(decorator_formatting, CharT, char_types)
  52. {
  53. typedef logging::record_view record_view;
  54. typedef logging::attribute_set attr_set;
  55. typedef std::basic_string< CharT > string;
  56. typedef logging::basic_formatting_ostream< CharT > osstream;
  57. typedef logging::basic_formatter< CharT > formatter;
  58. typedef test_strings< CharT > data;
  59. attrs::constant< string > attr1(data::printable_chars());
  60. attr_set set1;
  61. set1[data::attr1()] = attr1;
  62. record_view rec = make_record_view(set1);
  63. {
  64. string str1, str2;
  65. osstream strm1(str1), strm2(str2);
  66. formatter f = expr::stream << expr::make_xml_decor< CharT >()[ expr::stream << expr::attr< string >(data::attr1()) ];
  67. f(rec, strm1);
  68. strm2 << data::escaped_chars();
  69. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  70. }
  71. }