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 15 #define BOOST_TEST_MODULE form_message 16 17 #include <string> 18 #include <boost/test/unit_test.hpp> 19 #include <boost/log/attributes/constant.hpp> 20 #include <boost/log/attributes/attribute_set.hpp> 21 #include <boost/log/utility/formatting_ostream.hpp> 22 #include <boost/log/expressions.hpp> 23 #include <boost/log/core/record.hpp> 24 #include "char_definitions.hpp" 25 #include "make_record.hpp" 26 27 namespace logging = boost::log; 28 namespace attrs = logging::attributes; 29 namespace expr = logging::expressions; 30 31 namespace { 32 33 template< typename CharT > 34 struct message_test_data; 35 36 #ifdef BOOST_LOG_USE_CHAR 37 template< > 38 struct message_test_data< char > : 39 public test_data< char > 40 { message__anondcfbb3c30111::message_test_data41 static expr::smessage_type message() { return expr::smessage; } 42 }; 43 #endif // BOOST_LOG_USE_CHAR 44 45 #ifdef BOOST_LOG_USE_WCHAR_T 46 template< > 47 struct message_test_data< wchar_t > : 48 public test_data< wchar_t > 49 { message__anondcfbb3c30111::message_test_data50 static expr::wmessage_type message() { return expr::wmessage; } 51 }; 52 #endif // BOOST_LOG_USE_WCHAR_T 53 54 } // namespace 55 56 // The test checks that message formatting work BOOST_AUTO_TEST_CASE_TEMPLATE(message_formatting,CharT,char_types)57BOOST_AUTO_TEST_CASE_TEMPLATE(message_formatting, CharT, char_types) 58 { 59 typedef logging::attribute_set attr_set; 60 typedef std::basic_string< CharT > string; 61 typedef logging::basic_formatting_ostream< CharT > osstream; 62 typedef logging::record_view record_view; 63 typedef logging::basic_formatter< CharT > formatter; 64 typedef message_test_data< CharT > data; 65 66 attrs::constant< int > attr1(10); 67 attrs::constant< double > attr2(5.5); 68 69 attr_set set1; 70 set1[data::attr1()] = attr1; 71 set1[data::attr2()] = attr2; 72 set1[data::message().get_name()] = attrs::constant< string >(data::some_test_string()); 73 74 record_view rec = make_record_view(set1); 75 76 { 77 string str1, str2; 78 osstream strm1(str1), strm2(str2); 79 formatter f = expr::stream << data::message(); 80 f(rec, strm1); 81 strm2 << data::some_test_string(); 82 BOOST_CHECK(equal_strings(strm1.str(), strm2.str())); 83 } 84 } 85