| 1 | // config_file_test.cpp
|
|---|
| 2 | //
|
|---|
| 3 |
|
|---|
| 4 | #include <boost/test/unit_test.hpp>
|
|---|
| 5 | #include <boost/program_options.hpp>
|
|---|
| 6 |
|
|---|
| 7 | #include <fstream>
|
|---|
| 8 | #include <stdexcept>
|
|---|
| 9 |
|
|---|
| 10 | using namespace std;
|
|---|
| 11 | using namespace boost::program_options;
|
|---|
| 12 |
|
|---|
| 13 | BOOST_AUTO_TEST_SUITE( Config_File_Test_Suite )
|
|---|
| 14 |
|
|---|
| 15 | BOOST_AUTO_TEST_CASE( Config_File_With_Single_Option )
|
|---|
| 16 | {
|
|---|
| 17 | // Step 1: test fixture setup
|
|---|
| 18 | options_description configOptions( "Configuration" );
|
|---|
| 19 |
|
|---|
| 20 | configOptions.add_options()
|
|---|
| 21 | ( "option1", value<string>() )
|
|---|
| 22 | ( "option2", value<string>() )
|
|---|
| 23 | ;
|
|---|
| 24 |
|
|---|
| 25 | variables_map variablesMap;
|
|---|
| 26 |
|
|---|
| 27 | ifstream configFile( "config_file_1" );
|
|---|
| 28 |
|
|---|
| 29 | BOOST_REQUIRE( configFile );
|
|---|
| 30 |
|
|---|
| 31 | // nothrow ( 'section2.option' doesn't exist in config file )
|
|---|
| 32 | store( parse_config_file( configFile, configOptions ),
|
|---|
| 33 | variablesMap
|
|---|
| 34 | );
|
|---|
| 35 |
|
|---|
| 36 | // Step 2: call SUT
|
|---|
| 37 | // &&
|
|---|
| 38 | // Step 3: result check
|
|---|
| 39 | variablesMap[ "option1" ].as<string>();
|
|---|
| 40 |
|
|---|
| 41 | BOOST_CHECK_THROW( variablesMap[ "option2" ].as<string>(),
|
|---|
| 42 | bad_cast
|
|---|
| 43 | );
|
|---|
| 44 |
|
|---|
| 45 | // Step 4: test fixture cleanup
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | BOOST_AUTO_TEST_CASE( Config_File_With_Two_Options )
|
|---|
| 49 | {
|
|---|
| 50 | // Step 1: test fixture setup
|
|---|
| 51 | options_description configOptions( "Configuration" );
|
|---|
| 52 |
|
|---|
| 53 | configOptions.add_options()
|
|---|
| 54 | ( "option1", value<string>() )
|
|---|
| 55 | ;
|
|---|
| 56 |
|
|---|
| 57 | variables_map variablesMap;
|
|---|
| 58 |
|
|---|
| 59 | ifstream configFile( "config_file_2" );
|
|---|
| 60 |
|
|---|
| 61 | BOOST_REQUIRE( configFile );
|
|---|
| 62 |
|
|---|
| 63 | // Step 2: call SUT
|
|---|
| 64 | // &&
|
|---|
| 65 | // Step 3: result check
|
|---|
| 66 | // ( 'section2.option' doesn't exist in 'options_description' )
|
|---|
| 67 | BOOST_CHECK_THROW( parse_config_file( configFile, configOptions ),
|
|---|
| 68 | logic_error
|
|---|
| 69 | );
|
|---|
| 70 |
|
|---|
| 71 | // Step 4: test fixture cleanup
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | BOOST_AUTO_TEST_SUITE_END()
|
|---|