source: NonGTP/Boost/boost/test/impl/unit_test_parameters.ipp @ 857

Revision 857, 10.7 KB checked in by igarcia, 18 years ago (diff)
Line 
1//  (C) Copyright Gennadiy Rozental 2001-2005.
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: unit_test_parameters.ipp,v $
9//
10//  Version     : $Revision: 1.8 $
11//
12//  Description : simple implementation for Unit Test Framework parameter
13//  handling routines. May be rewritten in future to use some kind of
14//  command-line arguments parsing facility and environment variable handling
15//  facility
16// ***************************************************************************
17
18#ifndef BOOST_TEST_UNIT_TEST_PARAMETERS_IPP_012205GER
19#define BOOST_TEST_UNIT_TEST_PARAMETERS_IPP_012205GER
20
21// Boost.Test
22#include <boost/test/detail/unit_test_parameters.hpp>
23#include <boost/test/utils/basic_cstring/basic_cstring.hpp>
24#include <boost/test/utils/basic_cstring/compare.hpp>
25#include <boost/test/utils/basic_cstring/io.hpp>
26#include <boost/test/utils/fixed_mapping.hpp>
27
28// Boost
29#include <boost/config.hpp>
30#include <boost/lexical_cast.hpp>
31
32// STL
33#include <map>
34#include <cstdlib>
35
36#include <boost/test/detail/suppress_warnings.hpp>
37
38//____________________________________________________________________________//
39
40# ifdef BOOST_NO_STDC_NAMESPACE
41namespace std { using ::getenv; using ::strncmp; using ::strcmp; }
42# endif
43
44namespace boost {
45
46namespace unit_test {
47
48namespace {
49
50// framework parameters and there corresponding command-line arguments
51literal_string LOG_LEVEL         = "BOOST_TEST_LOG_LEVEL";
52literal_string NO_RESULT_CODE    = "BOOST_TEST_RESULT_CODE";
53literal_string REPORT_LEVEL      = "BOOST_TEST_REPORT_LEVEL";
54literal_string TESTS_TO_RUN      = "BOOST_TESTS_TO_RUN";
55literal_string SAVE_TEST_PATTERN = "BOOST_TEST_SAVE_PATTERN";
56literal_string BUILD_INFO        = "BOOST_TEST_BUILD_INFO";
57literal_string SHOW_PROGRESS     = "BOOST_TEST_SHOW_PROGRESS";
58literal_string CATCH_SYS_ERRORS  = "BOOST_TEST_CATCH_SYSTEM_ERRORS";
59literal_string REPORT_FORMAT     = "BOOST_TEST_REPORT_FORMAT";
60literal_string LOG_FORMAT        = "BOOST_TEST_LOG_FORMAT";
61literal_string OUTPUT_FORMAT     = "BOOST_TEST_OUTPUT_FORMAT";
62literal_string DETECT_MEM_LEAK   = "BOOST_TEST_DETECT_MEMORY_LEAK";
63literal_string RANDOM_SEED       = "BOOST_TEST_RANDOM";
64
65unit_test::log_level     s_log_level;
66bool                     s_no_result_code;
67unit_test::report_level  s_report_level;
68const_string             s_tests_to_run;
69bool                     s_save_pattern;
70bool                     s_show_build_info;
71bool                     s_show_progress;
72bool                     s_catch_sys_errors;
73output_format            s_report_format;
74output_format            s_log_format;
75long                     s_detect_mem_leaks;
76unsigned int             s_random_seed;
77
78// ************************************************************************** //
79// **************                 runtime_config               ************** //
80// ************************************************************************** //
81
82const_string
83retrieve_framework_parameter( const_string parameter_name, int* argc, char** argv )
84{
85    static fixed_mapping<const_string,const_string> parameter_2_cla_name_map(
86        LOG_LEVEL         , "--log_level",
87        NO_RESULT_CODE    , "--result_code",
88        REPORT_LEVEL      , "--report_level",
89        TESTS_TO_RUN      , "--run_test",
90        SAVE_TEST_PATTERN , "--save_pattern",
91        BUILD_INFO        , "--build_info",
92        SHOW_PROGRESS     , "--show_progress",
93        CATCH_SYS_ERRORS  , "--catch_system_errors",
94        REPORT_FORMAT     , "--report_format",
95        LOG_FORMAT        , "--log_format",
96        OUTPUT_FORMAT     , "--output_format",
97        DETECT_MEM_LEAK   , "--detect_memory_leak",
98        RANDOM_SEED       , "--random",
99       
100        ""
101    );
102
103    // first try to find parameter among command line arguments if present
104    if( argc ) {
105        // locate corresponding cla name
106        const_string cla_name = parameter_2_cla_name_map[parameter_name];
107
108        if( !cla_name.is_empty() ) {
109            for( int i = 1; i < *argc; ++i ) {
110                if( cla_name == const_string( argv[i], cla_name.size() ) && argv[i][cla_name.size()] == '=' ) {
111                    const_string result = argv[i] + cla_name.size() + 1;
112
113                    for( int j = i; j < *argc; ++j ) {
114                        argv[j] = argv[j+1];
115                    }
116                    --(*argc);
117
118                    return result;
119                }
120            }
121        }
122    }
123
124    return std::getenv( parameter_name.begin() );
125}
126
127long interpret_long( const_string from )
128{
129    bool negative = false;
130    long res = 0;
131
132    if( first_char( from ) == '-' ) {
133        negative = true;
134        from.trim_left( 1 );
135    }
136
137    const_string::iterator it = from.begin();
138    for( ;it != from.end(); ++it ) {
139        int d = *it - '0';
140
141        res = 10 * res + d;
142    }
143
144    if( negative )
145        res = -res;
146
147    return res;
148}
149
150} // local namespace
151
152//____________________________________________________________________________//
153
154namespace runtime_config {
155
156void
157init( int* argc, char** argv )
158{
159    fixed_mapping<const_string,unit_test::log_level,case_ins_less<char const> > log_level_name(
160        "all"           , log_successful_tests,
161        "success"       , log_successful_tests,
162        "test_suite"    , log_test_suites,
163        "message"       , log_messages,
164        "warning"       , log_warnings,
165        "error"         , log_all_errors,
166        "cpp_exception" , log_cpp_exception_errors,
167        "system_error"  , log_system_errors,
168        "fatal_error"   , log_fatal_errors,
169        "nothing"       , log_nothing,
170
171        invalid_log_level
172    );
173
174    fixed_mapping<const_string,unit_test::report_level,case_ins_less<char const> > report_level_name (
175        "confirm",  CONFIRMATION_REPORT,
176        "short",    SHORT_REPORT,
177        "detailed", DETAILED_REPORT,
178        "no",       NO_REPORT,
179
180        INV_REPORT_LEVEL
181    );
182
183    fixed_mapping<const_string,output_format,case_ins_less<char const> > output_format_name (
184        "HRF", CLF,
185        "CLF", CLF,
186        "XML", XML,
187
188        CLF
189    );
190
191    s_no_result_code    = retrieve_framework_parameter( NO_RESULT_CODE, argc, argv ) == "no";
192    s_save_pattern      = retrieve_framework_parameter( SAVE_TEST_PATTERN, argc, argv ) == "yes";
193    s_show_build_info   = retrieve_framework_parameter( BUILD_INFO, argc, argv ) == "yes";
194    s_show_progress     = retrieve_framework_parameter( SHOW_PROGRESS, argc, argv ) == "yes";
195    s_catch_sys_errors  = retrieve_framework_parameter( CATCH_SYS_ERRORS, argc, argv ) != "no";
196    s_tests_to_run      = retrieve_framework_parameter( TESTS_TO_RUN, argc, argv );
197
198    const_string rs_str = retrieve_framework_parameter( RANDOM_SEED, argc, argv );
199    s_random_seed       = rs_str.is_empty() ? 0 : lexical_cast<unsigned int>( rs_str );
200   
201    s_log_level         = log_level_name[retrieve_framework_parameter( LOG_LEVEL, argc, argv )];
202    s_report_level      = report_level_name[retrieve_framework_parameter( REPORT_LEVEL, argc, argv )];
203
204    s_report_format     = output_format_name[retrieve_framework_parameter( REPORT_FORMAT, argc, argv )];
205    s_log_format        = output_format_name[retrieve_framework_parameter( LOG_FORMAT, argc, argv )];
206
207    const_string output_format = retrieve_framework_parameter( OUTPUT_FORMAT, argc, argv );
208    if( !output_format.is_empty() ) {
209        s_report_format     = output_format_name[output_format];
210        s_log_format        = output_format_name[output_format];
211    }
212
213    const_string ml_str = retrieve_framework_parameter( DETECT_MEM_LEAK, argc, argv );
214    s_detect_mem_leaks  =  ml_str.is_empty() ? 1 : interpret_long( ml_str );
215}
216
217//____________________________________________________________________________//
218
219unit_test::log_level
220log_level()
221{
222    return s_log_level;
223}
224
225//____________________________________________________________________________//
226
227bool
228no_result_code()
229{
230    return s_no_result_code;
231}
232
233//____________________________________________________________________________//
234
235unit_test::report_level
236report_level()
237{
238    return s_report_level;
239}
240
241//____________________________________________________________________________//
242
243const_string
244test_to_run()
245{
246    return s_tests_to_run;
247}
248
249//____________________________________________________________________________//
250
251bool
252save_pattern()
253{
254    return s_save_pattern;
255}
256
257//____________________________________________________________________________//
258
259bool
260show_progress()
261{
262    return s_show_progress;
263}
264
265//____________________________________________________________________________//
266
267bool
268show_build_info()
269{
270    return s_show_build_info;
271}
272
273//____________________________________________________________________________//
274
275bool
276catch_sys_errors()
277{
278    return s_catch_sys_errors;
279}
280
281//____________________________________________________________________________//
282
283output_format
284report_format()
285{
286    return s_report_format;
287}
288
289//____________________________________________________________________________//
290
291output_format
292log_format()
293{
294    return s_log_format;
295}
296
297//____________________________________________________________________________//
298
299long
300detect_memory_leak()
301{
302    return s_detect_mem_leaks;
303}
304
305//____________________________________________________________________________//
306
307int
308random_seed()
309{
310    return s_random_seed;
311}
312
313} // namespace runtime_config
314
315} // namespace unit_test
316
317} // namespace boost
318
319//____________________________________________________________________________//
320
321#include <boost/test/detail/enable_warnings.hpp>
322
323// ***************************************************************************
324//  Revision History :
325//
326//  $Log: unit_test_parameters.ipp,v $
327//  Revision 1.8  2005/05/08 08:55:09  rogeeff
328//  typos and missing descriptions fixed
329//
330//  Revision 1.7  2005/04/05 07:23:21  rogeeff
331//  restore default
332//
333//  Revision 1.6  2005/04/05 06:11:37  rogeeff
334//  memory leak allocation point detection\nextra help with _WIN32_WINNT
335//
336//  Revision 1.5  2005/02/21 10:12:22  rogeeff
337//  Support for random order of test cases implemented
338//
339//  Revision 1.4  2005/02/20 08:27:07  rogeeff
340//  This a major update for Boost.Test framework. See release docs for complete list of fixes/updates
341//
342// ***************************************************************************
343
344#endif // BOOST_TEST_UNIT_TEST_PARAMETERS_IPP_012205GER
Note: See TracBrowser for help on using the repository browser.