1 #include <catch.hpp> 2 #include "test_utils.hpp" 3 4 #include <hocon/program_options.hpp> 5 6 using namespace std; 7 using namespace hocon; 8 using namespace hocon::program_options; 9 10 TEST_CASE("converts hocon obj to boost::po") { 11 po::options_description opts(""); 12 opts.add_options()("foo", "description"); 13 po::variables_map vm; 14 po::store(parse_string(string("{foo:b}"), opts), vm); 15 REQUIRE(vm.count("foo") == 1); 16 } 17 18 TEST_CASE("converts structured hocon to boost::po") { 19 po::options_description opts(""); 20 opts.add_options()("foo.bar", "description"); 21 po::variables_map vm; 22 po::store(parse_string(string("{foo : { bar : baz }}"), opts), vm); 23 REQUIRE(vm.count("foo.bar") == 1); 24 } 25 26 TEST_CASE("arrays with only values are converted to boost::po") { 27 po::options_description opts(""); 28 opts.add_options()("foo", po::value<vector<string>>(), "description"); 29 po::variables_map vm; 30 po::store(parse_string(string("{foo : [ bar, baz ]}"), opts), vm); 31 REQUIRE(vm.count("foo") == 1); 32 } 33 34 TEST_CASE("unregistered keys cause an exception when `allow_unregistered` is false") { 35 po::options_description opts(""); 36 opts.add_options()("foo", "description"); 37 po::variables_map vm; 38 REQUIRE_THROWS(po::store(parse_string(string("{foo : baz, bar : quux}"), opts, false), vm)); 39 } 40 41 TEST_CASE("unregistered keys are ignored when `allow_unregistered` is true") { 42 po::options_description opts(""); 43 opts.add_options()("foo", "description"); 44 po::variables_map vm; 45 po::store(parse_string(string("{foo : baz, bar : quux}"), opts, true), vm); 46 REQUIRE(vm.count("foo") == 1); 47 REQUIRE(vm.count("bar") == 0); 48 } 49 50 TEST_CASE("arrays with objects cause an exception in boost::po") { 51 po::options_description opts(""); 52 opts.add_options()("foo", "description"); 53 po::variables_map vm; 54 REQUIRE_THROWS(po::store(parse_string(string("{foo : [ { bar : baz } ]}"), opts), vm)); 55 } 56 57 TEST_CASE("Arrays mixed with objects cause an exception in boost:po") { 58 po::options_description opts(""); 59 opts.add_options() 60 ("foo", po::value<vector<string>>(), "description"); 61 po::variables_map vm; 62 REQUIRE_THROWS(po::store(parse_string(string("{foo : [ quux, {bar : baz } ] }"), opts), vm)); 63 } 64