fhicl-get-test.cc
Go to the documentation of this file.
1 #define BOOST_TEST_MODULE (fhicl - get test)
2 
3 #include "boost/test/unit_test.hpp"
4 
5 #include "tools/fhicl_get_impl.h"
6 
7 #include <initializer_list>
8 #include <iostream>
9 #include <tuple>
10 #include <vector>
11 
13 
14 namespace {
15  class redirect_streams {
16  public:
17  ~redirect_streams()
18  {
19  std::cout.rdbuf(oldstdout_);
20  std::cerr.rdbuf(oldstderr_);
21  }
22 
23  auto
24  stdout() const
25  {
26  return stdout_.str();
27  }
28  auto
29  stderr() const
30  {
31  return stderr_.str();
32  }
33 
34  private:
35  std::stringstream stdout_{};
36  std::stringstream stderr_{};
37  std::streambuf* oldstdout_{std::cout.rdbuf(stdout_.rdbuf())};
38  std::streambuf* oldstderr_{std::cerr.rdbuf(stderr_.rdbuf())};
39  };
40 
41  auto
42  run(std::vector<char const*> args)
43  {
44  args.insert(begin(args), "fhicl-get");
45  redirect_streams sentry{};
46  auto const rc = fhicl_get_impl(size(args), const_cast<char**>(data(args)));
47  return make_tuple(rc, sentry.stdout(), sentry.stderr());
48  }
49 }
50 
51 BOOST_AUTO_TEST_SUITE(fhicl_get_test)
52 
53 BOOST_AUTO_TEST_CASE(no_arguments)
54 {
55  auto const [rc, stdout, stderr] = run({});
56  BOOST_TEST(rc == 1);
57  BOOST_TEST(stdout == "");
58  BOOST_TEST(stderr.find("Missing input configuration file.") !=
59  std::string::npos);
60 }
61 
62 BOOST_AUTO_TEST_CASE(help_message)
63 {
64  auto const [rc, stdout, stderr] = run({"-h"});
65  BOOST_TEST(rc == 0);
66  BOOST_TEST(stdout.find("Usage: fhicl-get [options] [key] <file>") !=
67  std::string::npos);
68  BOOST_TEST(stderr == "");
69 }
70 
72 {
73  auto const [rc, stdout, stderr] =
74  run({"--atom-as", "string", "fhicl-get-t.fcl"});
75  BOOST_TEST(rc == 1);
76  BOOST_TEST(stdout == "");
77  BOOST_TEST(
78  stderr.find(
79  "A key must be specified unless the '--names-in' option is used.") !=
80  std::string::npos);
81 }
82 
83 BOOST_AUTO_TEST_CASE(retrieve_string)
84 {
85  auto const [rc, stdout, stderr] =
86  run({"--atom-as", "string", "a.billy", "fhicl-get-t.fcl"});
87  BOOST_TEST(rc == 0);
88  BOOST_TEST(stdout == "the kid\n");
89  BOOST_TEST(stderr == "");
90 }
91 
92 BOOST_AUTO_TEST_CASE(retrieve_double)
93 {
94  auto const [rc, stdout, stderr] =
95  run({"--atom-as", "double", "c", "fhicl-get-t.fcl"});
96  BOOST_TEST(rc == 0);
97  BOOST_TEST(stdout == "1.25\n");
98  BOOST_TEST(stderr == "");
99 }
100 
101 BOOST_AUTO_TEST_CASE(retrieve_int)
102 {
103  auto const [rc, stdout, stderr] =
104  run({"--atom-as", "int", "d", "fhicl-get-t.fcl"});
105  BOOST_TEST(rc == 0);
106  BOOST_TEST(stdout == "42\n");
107  BOOST_TEST(stderr == "");
108 }
109 
110 BOOST_AUTO_TEST_CASE(retrieve_bool)
111 {
112  auto const [rc, stdout, stderr] =
113  run({"--atom-as", "bool", "e", "fhicl-get-t.fcl"});
114  BOOST_TEST(rc == 0);
115  BOOST_TEST(stdout == "0\n");
116  BOOST_TEST(stderr == "");
117 }
118 
119 BOOST_AUTO_TEST_CASE(retrieve_sequence_int)
120 {
121  auto const [rc, stdout, stderr] =
122  run({"--sequence-of", "int", "f", "fhicl-get-t.fcl"});
123  BOOST_TEST(rc == 0);
124  BOOST_TEST(stdout == "1\n3\n");
125  BOOST_TEST(stderr == "");
126 }
127 
128 BOOST_AUTO_TEST_CASE(unsupported_types)
129 {
130  auto const [rc, stdout, stderr] =
131  run({"--atom-as", "float", "e", "fhicl-get-t.fcl"});
132  BOOST_TEST(rc == 1);
133  BOOST_TEST(stdout == "");
134  BOOST_TEST(stderr.find("The specified type 'float' is not supported") !=
135  std::string::npos);
136 }
137 
138 BOOST_AUTO_TEST_CASE(names_in_file)
139 {
140  auto const [rc, stdout, stderr] = run({"--names-in", "fhicl-get-t.fcl"});
141  BOOST_TEST(rc == 0);
142  BOOST_TEST(stdout == "a\nb\nc\nd\ne\nf\n");
143  BOOST_TEST(stderr == "");
144 }
145 
146 BOOST_AUTO_TEST_CASE(names_in_nonempty_table)
147 {
148  auto const [rc, stdout, stderr] = run({"--names-in", "a", "fhicl-get-t.fcl"});
149  BOOST_TEST(rc == 0);
150  BOOST_TEST(stdout == "billy\n");
151  BOOST_TEST(stderr == "");
152 }
153 
154 BOOST_AUTO_TEST_CASE(names_in_empty_table)
155 {
156  auto const [rc, stdout, stderr] = run({"--names-in", "b", "fhicl-get-t.fcl"});
157  BOOST_TEST(rc == 0);
158  BOOST_TEST(stdout == "");
159  BOOST_TEST(stderr == "");
160 }
161 
162 BOOST_AUTO_TEST_CASE(names_in_nonexistent_table_1)
163 {
164  auto const [rc, stdout, stderr] =
165  run({"--names-in", "g.h", "fhicl-get-t.fcl"});
166  BOOST_TEST(rc == 1);
167  BOOST_TEST(stdout == "");
168  BOOST_TEST(
169  stderr.find(
170  "A parameter with the fully-qualified key 'g.h' does not exist.") !=
171  std::string::npos);
172 }
173 
174 BOOST_AUTO_TEST_CASE(names_in_nonexistent_table_2)
175 {
176  auto const [rc, stdout, stderr] =
177  run({"--names-in", "g.h", "--allow-missing", "fhicl-get-t.fcl"});
178  BOOST_TEST(rc == 0);
179  BOOST_TEST(stdout == "");
180  BOOST_TEST(stderr == "");
181 }
182 
183 BOOST_AUTO_TEST_CASE(names_in_nontable)
184 {
185  auto const [rc, stdout, stderr] =
186  run({"--names-in", "c", "--allow-missing", "fhicl-get-t.fcl"});
187  BOOST_TEST(rc == 1);
188  BOOST_TEST(stdout == "");
189  BOOST_TEST(
190  stderr.find("The parameter named 'c' does not have a table value.") !=
191  std::string::npos);
192 }
193 
194 BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(no_arguments)
static QCString args
Definition: declinfo.cpp:674
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
int fhicl_get_impl(int argc, char **argv)
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72