form_if.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_if.cpp
  9. * \author Andrey Semashev
  10. * \date 05.02.2009
  11. *
  12. * \brief This header contains tests for the \c if_ formatter.
  13. */
  14. #define BOOST_TEST_MODULE form_if
  15. #include <string>
  16. #include <ostream>
  17. #include <boost/test/unit_test.hpp>
  18. #include <boost/log/attributes/constant.hpp>
  19. #include <boost/log/attributes/attribute_set.hpp>
  20. #include <boost/log/expressions.hpp>
  21. #include <boost/log/core/record.hpp>
  22. #include <boost/log/utility/formatting_ostream.hpp>
  23. #include "char_definitions.hpp"
  24. #include "make_record.hpp"
  25. namespace logging = boost::log;
  26. namespace attrs = logging::attributes;
  27. namespace expr = logging::expressions;
  28. // The test checks that conditional formatting work
  29. BOOST_AUTO_TEST_CASE_TEMPLATE(conditional_formatting, CharT, char_types)
  30. {
  31. typedef logging::attribute_set attr_set;
  32. typedef std::basic_string< CharT > string;
  33. typedef logging::basic_formatting_ostream< CharT > osstream;
  34. typedef logging::record_view record_view;
  35. typedef logging::basic_formatter< CharT > formatter;
  36. typedef test_data< CharT > data;
  37. attrs::constant< int > attr1(10);
  38. attrs::constant< double > attr2(5.5);
  39. attr_set set1;
  40. set1[data::attr1()] = attr1;
  41. set1[data::attr2()] = attr2;
  42. record_view rec = make_record_view(set1);
  43. // Check for various modes of attribute value type specification
  44. {
  45. string str1, str2;
  46. osstream strm1(str1), strm2(str2);
  47. formatter f = expr::stream <<
  48. expr::if_(expr::has_attr< int >(data::attr1()))
  49. [
  50. expr::stream << expr::attr< int >(data::attr1())
  51. ];
  52. f(rec, strm1);
  53. strm2 << 10;
  54. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  55. }
  56. {
  57. string str1;
  58. osstream strm1(str1);
  59. formatter f = expr::stream <<
  60. expr::if_(expr::has_attr< int >(data::attr2()))
  61. [
  62. expr::stream << expr::attr< int >(data::attr2())
  63. ];
  64. f(rec, strm1);
  65. BOOST_CHECK(equal_strings(strm1.str(), string()));
  66. }
  67. {
  68. string str1, str2;
  69. osstream strm1(str1), strm2(str2);
  70. formatter f = expr::stream <<
  71. expr::if_(expr::has_attr< int >(data::attr1()))
  72. [
  73. expr::stream << expr::attr< int >(data::attr1())
  74. ]
  75. .else_
  76. [
  77. expr::stream << expr::attr< double >(data::attr2())
  78. ];
  79. f(rec, strm1);
  80. strm2 << 10;
  81. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  82. }
  83. {
  84. string str1, str2;
  85. osstream strm1(str1), strm2(str2);
  86. formatter f = expr::stream <<
  87. expr::if_(expr::has_attr< int >(data::attr2()))
  88. [
  89. expr::stream << expr::attr< int >(data::attr1())
  90. ]
  91. .else_
  92. [
  93. expr::stream << expr::attr< double >(data::attr2())
  94. ];
  95. f(rec, strm1);
  96. strm2 << 5.5;
  97. BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
  98. }
  99. }