]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/program_options/test/required_test.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / program_options / test / required_test.cpp
1 // Copyright Sascha Ochsenknecht 2009.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #include <boost/program_options.hpp>
7 using namespace boost::program_options;
8
9 #include <string>
10 #include <iostream>
11 #include <fstream>
12 using namespace std;
13
14 #include "minitest.hpp"
15
16
17 void required_throw_test()
18 {
19 options_description opts;
20 opts.add_options()
21 ("cfgfile,c", value<string>()->required(), "the configfile")
22 ("fritz,f", value<string>()->required(), "the output file")
23 ;
24
25 variables_map vm;
26 bool throwed = false;
27 {
28 // This test must throw exception
29 string cmdline = "prg -f file.txt";
30 vector< string > tokens = split_unix(cmdline);
31 throwed = false;
32 try {
33 store(command_line_parser(tokens).options(opts).run(), vm);
34 notify(vm);
35 }
36 catch (required_option& e) {
37 BOOST_CHECK_EQUAL(e.what(), string("the option '--cfgfile' is required but missing"));
38 throwed = true;
39 }
40 BOOST_CHECK(throwed);
41 }
42
43 {
44 // This test mustn't throw exception
45 string cmdline = "prg -c config.txt";
46 vector< string > tokens = split_unix(cmdline);
47 throwed = false;
48 try {
49 store(command_line_parser(tokens).options(opts).run(), vm);
50 notify(vm);
51 }
52 catch (required_option& e) {
53 throwed = true;
54 }
55 BOOST_CHECK(!throwed);
56 }
57 }
58
59
60
61 void simple_required_test(const char* config_file)
62 {
63 options_description opts;
64 opts.add_options()
65 ("cfgfile,c", value<string>()->required(), "the configfile")
66 ("fritz,f", value<string>()->required(), "the output file")
67 ;
68
69 variables_map vm;
70 bool throwed = false;
71 {
72 // This test must throw exception
73 string cmdline = "prg -f file.txt";
74 vector< string > tokens = split_unix(cmdline);
75 throwed = false;
76 try {
77 // options coming from different sources
78 store(command_line_parser(tokens).options(opts).run(), vm);
79 store(parse_config_file<char>(config_file, opts), vm);
80 notify(vm);
81 }
82 catch (required_option& e) {
83 throwed = true;
84 }
85 BOOST_CHECK(!throwed);
86 }
87 }
88
89
90
91 int main(int /*argc*/, char* av[])
92 {
93 required_throw_test();
94 simple_required_test(av[1]);
95
96 return 0;
97 }
98