]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/test/test/framework-ts/log-formatter-test.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / test / test / framework-ts / log-formatter-test.cpp
1 // (C) Copyright Raffi Enficiaud 2016.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5
6 // See http://www.boost.org/libs/test for the library home page.
7 //
8 // tests Unit Test Framework logging facilities against
9 // pattern file
10 // ***************************************************************************
11
12 // Boost.Test
13 #define BOOST_TEST_MAIN
14 #include <boost/test/unit_test.hpp>
15 #include <boost/test/unit_test_log.hpp>
16 #include <boost/test/unit_test_suite.hpp>
17 #include <boost/test/framework.hpp>
18 #include <boost/test/unit_test_parameters.hpp>
19 #include <boost/test/utils/nullstream.hpp>
20 typedef boost::onullstream onullstream_type;
21
22 // BOOST
23 #include <boost/lexical_cast.hpp>
24
25 // our special logger for tests
26 #include "logger-for-tests.hpp"
27
28 // STL
29 #include <iostream>
30 #include <ios>
31
32 using boost::test_tools::output_test_stream;
33 using namespace boost::unit_test;
34 namespace utf = boost::unit_test;
35
36
37 //____________________________________________________________________________//
38
39 // this one should generate a message as it does not execute any assertion
40 void good_foo() {}
41
42 void almost_good_foo()
43 {
44 BOOST_TEST_WARN( 2>3 );
45 }
46
47 void bad_foo() {
48 BOOST_ERROR( "" );
49
50 BOOST_TEST_MESSAGE("this is a message");
51 BOOST_CHECK(true);
52
53 BOOST_TEST_INFO("Context value=something");
54 BOOST_TEST_INFO("Context value2=something different");
55 BOOST_ERROR( "with some message" );
56
57 BOOST_CHECK_MESSAGE( 1 == 2.3, "non sense" );
58 }
59
60 void very_bad_foo() {
61 BOOST_TEST_CONTEXT("some context") {
62 BOOST_FAIL( "very_bad_foo is fatal" );
63 }
64 }
65
66 struct local_exception {};
67
68 void very_bad_exception() {
69 BOOST_TEST_INFO("Context value=something");
70 BOOST_TEST_INFO("Context value2=something different");
71 BOOST_ERROR( "with some message" );
72
73 BOOST_TEST_INFO("exception context should be shown");
74 throw local_exception();
75 }
76
77
78 //____________________________________________________________________________//
79
80 void check( output_test_stream& output,
81 output_format log_format,
82 test_unit_id id,
83 log_level ll = log_successful_tests )
84 {
85 boost::unit_test::unit_test_log.set_format(log_format);
86 boost::unit_test::unit_test_log.set_stream(output);
87 boost::unit_test::unit_test_log.set_threshold_level(ll);
88
89 output << "* " << log_format << "-format *******************************************************************";
90 output << std::endl;
91 framework::finalize_setup_phase( id );
92 framework::run( id, false ); // do not continue the test tree to have the test_log_start/end
93 output << std::endl;
94
95 boost::unit_test::unit_test_log.set_format(OF_CLF);
96 boost::unit_test::unit_test_log.set_stream(std::cout);
97
98 BOOST_TEST( output.match_pattern(true) ); // flushes the stream at the end of the comparison.
99 }
100
101 //____________________________________________________________________________//
102
103 void check( output_test_stream& output, test_suite* ts )
104 {
105 ts->p_default_status.value = test_unit::RS_ENABLED;
106
107 check( output, OF_CLF, ts->p_id );
108 check( output, OF_XML, ts->p_id );
109 check( output, OF_JUNIT, ts->p_id, log_successful_tests );
110 check( output, OF_JUNIT, ts->p_id, log_cpp_exception_errors ); // should branch to the log log_all_errors
111 }
112
113 //____________________________________________________________________________//
114
115 struct guard {
116 ~guard()
117 {
118 boost::unit_test::unit_test_log.set_format( runtime_config::get<output_format>( runtime_config::btrt_log_format ) );
119 boost::unit_test::unit_test_log.set_stream( std::cout );
120 }
121 };
122
123 //____________________________________________________________________________//
124
125
126 BOOST_AUTO_TEST_CASE( test_result_reports )
127 {
128 guard G;
129 ut_detail::ignore_unused_variable_warning( G );
130
131 #define PATTERN_FILE_NAME "log-formatter-test.pattern"
132
133 std::string pattern_file_name(
134 framework::master_test_suite().argc == 1
135 ? (runtime_config::save_pattern() ? PATTERN_FILE_NAME : "./baseline-outputs/" PATTERN_FILE_NAME )
136 : framework::master_test_suite().argv[1] );
137
138 output_test_stream_for_loggers test_output( pattern_file_name,
139 !runtime_config::save_pattern(),
140 true,
141 __FILE__ );
142
143 #line 207
144 test_suite* ts_0 = BOOST_TEST_SUITE( "0 test cases inside" );
145
146 test_suite* ts_1 = BOOST_TEST_SUITE( "1 test cases inside" );
147 ts_1->add( BOOST_TEST_CASE( good_foo ) );
148
149 test_suite* ts_1b = BOOST_TEST_SUITE( "1 bad test case inside" );
150 ts_1b->add( BOOST_TEST_CASE( bad_foo ), 1 );
151
152 test_suite* ts_1c = BOOST_TEST_SUITE( "1 almost good test case inside" );
153 ts_1c->add( BOOST_TEST_CASE( almost_good_foo ) );
154
155 test_suite* ts_2 = BOOST_TEST_SUITE( "2 test cases inside" );
156 ts_2->add( BOOST_TEST_CASE( good_foo ) );
157 ts_2->add( BOOST_TEST_CASE( bad_foo ), 1 );
158
159 test_suite* ts_3 = BOOST_TEST_SUITE( "3 test cases inside" );
160 ts_3->add( BOOST_TEST_CASE( bad_foo ) );
161 test_case* tc1 = BOOST_TEST_CASE( very_bad_foo );
162 ts_3->add( tc1 );
163 test_case* tc2 = BOOST_TEST_CASE( bad_foo );
164 ts_3->add( tc2 );
165 tc2->depends_on( tc1 );
166
167 test_suite* ts_4 = BOOST_TEST_SUITE( "4 test cases inside" );
168 ts_4->add( BOOST_TEST_CASE( bad_foo ) );
169 ts_4->add( BOOST_TEST_CASE( very_bad_foo ) );
170 ts_4->add( BOOST_TEST_CASE( very_bad_exception ) );
171 ts_4->add( BOOST_TEST_CASE( bad_foo ) );
172
173 test_suite* ts_main = BOOST_TEST_SUITE( "Fake Test Suite Hierarchy" );
174 ts_main->add( ts_0 );
175 ts_main->add( ts_1 );
176 ts_main->add( ts_2 );
177 ts_main->add( ts_3 );
178 ts_main->add( ts_4 );
179
180 check( test_output, ts_1 );
181
182 check( test_output, ts_1b );
183
184 check( test_output, ts_1c );
185
186 check( test_output, ts_2 );
187
188 check( test_output, ts_3 );
189
190 check( test_output, ts_4 );
191
192 ts_1->add( BOOST_TEST_CASE( bad_foo ) );
193 ts_3->depends_on( ts_1 );
194
195 check( test_output, ts_main );
196 }
197
198 //____________________________________________________________________________//
199
200 // EOF