]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | |
b32b8144 | 14 | #include <boost/test/unit_test.hpp> |
7c673cae | 15 | #include <boost/test/unit_test_log.hpp> |
7c673cae FG |
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; | |
7c673cae FG |
21 | |
22 | // BOOST | |
23 | #include <boost/lexical_cast.hpp> | |
24 | ||
b32b8144 FG |
25 | // our special logger for tests |
26 | #include "logger-for-tests.hpp" | |
27 | ||
7c673cae FG |
28 | // STL |
29 | #include <iostream> | |
30 | #include <ios> | |
31 | ||
32 | using boost::test_tools::output_test_stream; | |
33 | using namespace boost::unit_test; | |
11fdf7f2 | 34 | |
7c673cae FG |
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 | ||
7c673cae FG |
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 | ||
11fdf7f2 | 77 | void bad_foo2() { bad_foo(); } // tests with clashing names |
7c673cae FG |
78 | |
79 | //____________________________________________________________________________// | |
80 | ||
b32b8144 FG |
81 | void check( output_test_stream& output, |
82 | output_format log_format, | |
83 | test_unit_id id, | |
84 | log_level ll = log_successful_tests ) | |
7c673cae FG |
85 | { |
86 | boost::unit_test::unit_test_log.set_format(log_format); | |
87 | boost::unit_test::unit_test_log.set_stream(output); | |
b32b8144 | 88 | boost::unit_test::unit_test_log.set_threshold_level(ll); |
7c673cae FG |
89 | |
90 | output << "* " << log_format << "-format *******************************************************************"; | |
91 | output << std::endl; | |
92 | framework::finalize_setup_phase( id ); | |
93 | framework::run( id, false ); // do not continue the test tree to have the test_log_start/end | |
94 | output << std::endl; | |
95 | ||
96 | boost::unit_test::unit_test_log.set_format(OF_CLF); | |
97 | boost::unit_test::unit_test_log.set_stream(std::cout); | |
98 | ||
99 | BOOST_TEST( output.match_pattern(true) ); // flushes the stream at the end of the comparison. | |
100 | } | |
101 | ||
102 | //____________________________________________________________________________// | |
103 | ||
104 | void check( output_test_stream& output, test_suite* ts ) | |
105 | { | |
106 | ts->p_default_status.value = test_unit::RS_ENABLED; | |
107 | ||
108 | check( output, OF_CLF, ts->p_id ); | |
109 | check( output, OF_XML, ts->p_id ); | |
b32b8144 FG |
110 | check( output, OF_JUNIT, ts->p_id, log_successful_tests ); |
111 | check( output, OF_JUNIT, ts->p_id, log_cpp_exception_errors ); // should branch to the log log_all_errors | |
7c673cae FG |
112 | } |
113 | ||
114 | //____________________________________________________________________________// | |
115 | ||
116 | struct guard { | |
117 | ~guard() | |
118 | { | |
b32b8144 | 119 | boost::unit_test::unit_test_log.set_format( runtime_config::get<output_format>( runtime_config::btrt_log_format ) ); |
7c673cae FG |
120 | boost::unit_test::unit_test_log.set_stream( std::cout ); |
121 | } | |
122 | }; | |
123 | ||
7c673cae FG |
124 | //____________________________________________________________________________// |
125 | ||
126 | ||
11fdf7f2 | 127 | BOOST_AUTO_TEST_CASE( test_logs ) |
7c673cae FG |
128 | { |
129 | guard G; | |
130 | ut_detail::ignore_unused_variable_warning( G ); | |
131 | ||
132 | #define PATTERN_FILE_NAME "log-formatter-test.pattern" | |
133 | ||
134 | std::string pattern_file_name( | |
11fdf7f2 | 135 | framework::master_test_suite().argc <= 1 |
7c673cae FG |
136 | ? (runtime_config::save_pattern() ? PATTERN_FILE_NAME : "./baseline-outputs/" PATTERN_FILE_NAME ) |
137 | : framework::master_test_suite().argv[1] ); | |
138 | ||
b32b8144 FG |
139 | output_test_stream_for_loggers test_output( pattern_file_name, |
140 | !runtime_config::save_pattern(), | |
141 | true, | |
142 | __FILE__ ); | |
7c673cae FG |
143 | |
144 | #line 207 | |
145 | test_suite* ts_0 = BOOST_TEST_SUITE( "0 test cases inside" ); | |
146 | ||
147 | test_suite* ts_1 = BOOST_TEST_SUITE( "1 test cases inside" ); | |
148 | ts_1->add( BOOST_TEST_CASE( good_foo ) ); | |
149 | ||
150 | test_suite* ts_1b = BOOST_TEST_SUITE( "1 bad test case inside" ); | |
151 | ts_1b->add( BOOST_TEST_CASE( bad_foo ), 1 ); | |
152 | ||
153 | test_suite* ts_1c = BOOST_TEST_SUITE( "1 almost good test case inside" ); | |
154 | ts_1c->add( BOOST_TEST_CASE( almost_good_foo ) ); | |
155 | ||
156 | test_suite* ts_2 = BOOST_TEST_SUITE( "2 test cases inside" ); | |
157 | ts_2->add( BOOST_TEST_CASE( good_foo ) ); | |
158 | ts_2->add( BOOST_TEST_CASE( bad_foo ), 1 ); | |
159 | ||
160 | test_suite* ts_3 = BOOST_TEST_SUITE( "3 test cases inside" ); | |
161 | ts_3->add( BOOST_TEST_CASE( bad_foo ) ); | |
162 | test_case* tc1 = BOOST_TEST_CASE( very_bad_foo ); | |
163 | ts_3->add( tc1 ); | |
11fdf7f2 | 164 | test_case* tc2 = BOOST_TEST_CASE( bad_foo2 ); |
7c673cae FG |
165 | ts_3->add( tc2 ); |
166 | tc2->depends_on( tc1 ); | |
167 | ||
168 | test_suite* ts_4 = BOOST_TEST_SUITE( "4 test cases inside" ); | |
169 | ts_4->add( BOOST_TEST_CASE( bad_foo ) ); | |
170 | ts_4->add( BOOST_TEST_CASE( very_bad_foo ) ); | |
171 | ts_4->add( BOOST_TEST_CASE( very_bad_exception ) ); | |
11fdf7f2 | 172 | ts_4->add( BOOST_TEST_CASE( bad_foo2 ) ); |
7c673cae FG |
173 | |
174 | test_suite* ts_main = BOOST_TEST_SUITE( "Fake Test Suite Hierarchy" ); | |
175 | ts_main->add( ts_0 ); | |
176 | ts_main->add( ts_1 ); | |
177 | ts_main->add( ts_2 ); | |
178 | ts_main->add( ts_3 ); | |
179 | ts_main->add( ts_4 ); | |
180 | ||
181 | check( test_output, ts_1 ); | |
182 | ||
183 | check( test_output, ts_1b ); | |
184 | ||
185 | check( test_output, ts_1c ); | |
186 | ||
187 | check( test_output, ts_2 ); | |
188 | ||
189 | check( test_output, ts_3 ); | |
190 | ||
191 | check( test_output, ts_4 ); | |
192 | ||
193 | ts_1->add( BOOST_TEST_CASE( bad_foo ) ); | |
194 | ts_3->depends_on( ts_1 ); | |
195 | ||
196 | check( test_output, ts_main ); | |
197 | } | |
198 | ||
199 | //____________________________________________________________________________// | |
200 | ||
11fdf7f2 TL |
201 | BOOST_AUTO_TEST_CASE( test_logs_junit_info_closing_tags ) |
202 | { | |
203 | guard G; | |
204 | ut_detail::ignore_unused_variable_warning( G ); | |
205 | ||
206 | #define PATTERN_FILE_NAME_JUNIT "log-formatter-test.pattern.junit" | |
207 | ||
208 | std::string pattern_file_name( | |
209 | framework::master_test_suite().argc <= 2 | |
210 | ? (runtime_config::save_pattern() ? PATTERN_FILE_NAME_JUNIT : "./baseline-outputs/" PATTERN_FILE_NAME_JUNIT ) | |
211 | : framework::master_test_suite().argv[2] ); | |
212 | ||
213 | output_test_stream_for_loggers test_output( pattern_file_name, | |
214 | !runtime_config::save_pattern(), | |
215 | true, | |
216 | __FILE__ ); | |
217 | ||
218 | #line 218 | |
219 | test_suite* ts_main = BOOST_TEST_SUITE( "1 test cases inside" ); | |
220 | ts_main->add( BOOST_TEST_CASE( almost_good_foo ) ); | |
221 | ||
222 | ts_main->p_default_status.value = test_unit::RS_ENABLED; | |
223 | char const* argv[] = { "a.exe", "--run_test=*", "--build_info" }; | |
224 | int argc = sizeof(argv)/sizeof(argv[0]); | |
225 | boost::unit_test::runtime_config::init( argc, (char**)argv ); | |
226 | boost::unit_test::framework::impl::setup_for_execution( *ts_main ); | |
227 | ||
228 | check( test_output, OF_JUNIT, ts_main->p_id, log_successful_tests ); | |
229 | } | |
230 | ||
7c673cae | 231 | // EOF |