1 //  (C) Copyright Gennadiy Rozental 2001-2015.
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 //  File        : $RCSfile$
9 //
10 //  Version     : $Revision$
11 //
12 //  Description : tests Unit Test Framework reporting facilities against
13 //  pattern file
14 // ***************************************************************************
15 
16 // Boost.Test
17 #define BOOST_TEST_MAIN
18 #include <boost/test/unit_test.hpp>
19 #include <boost/test/results_reporter.hpp>
20 #include <boost/test/tools/output_test_stream.hpp>
21 #include <boost/test/unit_test_log.hpp>
22 #include <boost/test/unit_test_suite.hpp>
23 #include <boost/test/framework.hpp>
24 #include <boost/test/unit_test_parameters.hpp>
25 #include <boost/test/utils/nullstream.hpp>
26 typedef boost::onullstream onullstream_type;
27 
28 // BOOST
29 #include <boost/lexical_cast.hpp>
30 
31 // STL
32 #include <iostream>
33 
34 using boost::test_tools::output_test_stream;
35 using namespace boost::unit_test;
36 
37 //____________________________________________________________________________//
38 
good_foo()39 void good_foo() {}
40 
almost_good_foo()41 void almost_good_foo() { BOOST_TEST_WARN( 2>3 ); }
42 
bad_foo()43 void bad_foo()  {
44     onullstream_type null_out;
45     unit_test_log.set_stream( null_out );
46     BOOST_ERROR( "" );
47     unit_test_log.set_stream( std::cout );
48 }
49 
50 struct log_guard {
~log_guardlog_guard51     ~log_guard()
52     {
53         unit_test_log.set_stream( std::cout );
54     }
55 };
56 
very_bad_foo()57 void very_bad_foo()  {
58     log_guard lg;
59     ut_detail::ignore_unused_variable_warning( lg );
60     onullstream_type null_out;
61     unit_test_log.set_stream( null_out );
62     BOOST_FAIL( "" );
63 }
64 
65 //____________________________________________________________________________//
66 
check(output_test_stream & output,output_format report_format,test_unit_id id)67 void check( output_test_stream& output, output_format report_format, test_unit_id id )
68 {
69     results_reporter::set_format( report_format );
70 
71     results_reporter::confirmation_report( id );
72     output << "*************************************************************************\n";
73     BOOST_TEST( output.match_pattern() );
74 
75     results_reporter::short_report( id );
76     output << "*************************************************************************\n";
77     BOOST_TEST( output.match_pattern() );
78 
79     results_reporter::detailed_report( id );
80     output << "*************************************************************************\n";
81     BOOST_TEST( output.match_pattern() );
82 }
83 
84 //____________________________________________________________________________//
85 
check(output_test_stream & output,test_suite * ts)86 void check( output_test_stream& output, test_suite* ts )
87 {
88     ts->p_default_status.value = test_unit::RS_ENABLED;
89 
90     framework::finalize_setup_phase( ts->p_id );
91     framework::run( ts );
92 
93     check( output, OF_CLF, ts->p_id );
94     check( output, OF_XML, ts->p_id );
95 }
96 
97 //____________________________________________________________________________//
98 
99 struct guard {
~guardguard100     ~guard()
101     {
102         results_reporter::set_stream( std::cerr );
103         results_reporter::set_format( runtime_config::report_format() );
104     }
105 };
106 
107 //____________________________________________________________________________//
108 
BOOST_AUTO_TEST_CASE(test_result_reports)109 BOOST_AUTO_TEST_CASE( test_result_reports )
110 {
111     guard G;
112     ut_detail::ignore_unused_variable_warning( G );
113 
114 #define PATTERN_FILE_NAME "result_report_test.pattern"
115 
116     std::string pattern_file_name(
117         framework::master_test_suite().argc == 1
118             ? (runtime_config::save_pattern() ? PATTERN_FILE_NAME : "./test_files/" PATTERN_FILE_NAME )
119             : framework::master_test_suite().argv[1] );
120 
121     output_test_stream test_output( pattern_file_name, !runtime_config::save_pattern() );
122     results_reporter::set_stream( test_output );
123 
124     test_suite* ts_0 = BOOST_TEST_SUITE( "0 test cases inside" );
125 
126     test_suite* ts_1 = BOOST_TEST_SUITE( "1 test cases inside" );
127         ts_1->add( BOOST_TEST_CASE( good_foo ) );
128 
129     test_suite* ts_1b = BOOST_TEST_SUITE( "1 bad test case inside" );
130         ts_1b->add( BOOST_TEST_CASE( bad_foo ), 1 );
131 
132     test_suite* ts_1c = BOOST_TEST_SUITE( "1 almost good test case inside" );
133         ts_1c->add( BOOST_TEST_CASE( almost_good_foo ) );
134 
135     test_suite* ts_2 = BOOST_TEST_SUITE( "2 test cases inside" );
136         ts_2->add( BOOST_TEST_CASE( good_foo ) );
137         ts_2->add( BOOST_TEST_CASE( bad_foo ), 1 );
138 
139     test_suite* ts_3 = BOOST_TEST_SUITE( "3 test cases inside" );
140         ts_3->add( BOOST_TEST_CASE( bad_foo ) );
141         test_case* tc1 = BOOST_TEST_CASE( very_bad_foo );
142         ts_3->add( tc1 );
143         test_case* tc2 = BOOST_TEST_CASE( bad_foo );
144         ts_3->add( tc2 );
145         tc2->depends_on( tc1 );
146 
147     test_suite* ts_main = BOOST_TEST_SUITE( "Fake Test Suite Hierarchy" );
148         ts_main->add( ts_0 );
149         ts_main->add( ts_1 );
150         ts_main->add( ts_2 );
151         ts_main->add( ts_3 );
152 
153     check( test_output, ts_1 );
154 
155     check( test_output, ts_1b );
156 
157     check( test_output, ts_1c );
158 
159     check( test_output, ts_2 );
160 
161     check( test_output, ts_3 );
162     ts_1->add( BOOST_TEST_CASE( bad_foo ) );
163     ts_3->depends_on( ts_1 );
164 
165     check( test_output, ts_main );
166 
167     results_reporter::set_stream( std::cout );
168 }
169 
170 //____________________________________________________________________________//
171 
172 // EOF
173