nestedValidation_03_t.cc
Go to the documentation of this file.
1 #define BOOST_TEST_MODULE (Nested Validation)
2 
3 #include "boost/test/unit_test.hpp"
4 
6 #include "fhiclcpp/types/Atom.h"
7 #include "fhiclcpp/types/Name.h"
9 #include "fhiclcpp/types/Table.h"
10 #include "fhiclcpp/types/Tuple.h"
11 
12 #include <string>
13 
14 using namespace fhicl;
15 using namespace std;
16 using namespace string_literals;
17 
18 namespace {
20 
21  struct NestedConfig {
22 
23  using Default_t = typename Val_t::default_type;
24  using DefaultElement_t = typename Val_t::default_type::value_type;
25 
26  explicit NestedConfig(Default_t defaultPars = {})
27  : pars(Name("pars"), std::move(defaultPars))
28  {}
29 
30  Val_t pars;
31  };
32 
33  struct Config {
34  Table<NestedConfig> nested{Name("nested")};
35  Table<NestedConfig> nestedWithDef{
36  Name("nestedWithDef"),
37  NestedConfig::Default_t{
38  NestedConfig::DefaultElement_t{"T", {"U", "V", "W"}}}};
39  };
40 
42  validateConfig(std::string const& cfg)
43  {
44  auto const ps = ParameterSet::make(cfg);
45  Table<Config> validatedConfig{Name("validatedConfig")};
46  validatedConfig.validate(ps);
47  return validatedConfig;
48  }
49 }
50 
51 BOOST_AUTO_TEST_SUITE(Nested_Validation)
52 
54 {
55  string const good{"nested: { pars: [[A, [B]], [X, [Y, Z]]] }"s};
56  std::vector<std::string> const ref1{"B"s};
57  std::vector<std::string> const ref2{"Y"s, "Z"s};
58  std::vector<std::string> const ref3{"U"s, "V"s, "W"s};
59  auto validatedConfig = validateConfig(good);
60  auto const& pars = validatedConfig().nested().pars();
61  BOOST_TEST_REQUIRE(pars.size() == 2ull);
62  BOOST_TEST(std::get<0>(pars[0]) == "A"s);
63  BOOST_TEST(std::get<1>(pars[0]) == ref1);
64  BOOST_TEST(std::get<0>(pars[1]) == "X"s);
65  BOOST_TEST(std::get<1>(pars[1]) == ref2);
66  auto const& defaultedPars = validatedConfig().nestedWithDef().pars();
67  BOOST_TEST_REQUIRE(defaultedPars.size() == 1ull);
68  BOOST_TEST(std::get<0>(defaultedPars[0]) == "T"s);
69  BOOST_TEST(std::get<1>(defaultedPars[0]) == ref3);
70 }
71 
73 {
74  string const good{"nested: { pars: [[A, [B]], [X, [Y, Z]]] }\n"s +
75  "nestedWithDef: { pars: [[M, [N, O, P, Q]], [R, [S]]] }"s};
76  std::vector<std::string> const ref1{"B"s};
77  std::vector<std::string> const ref2{"Y"s, "Z"s};
78  std::vector<std::string> const ref3{"N"s, "O"s, "P"s, "Q"s};
79  std::vector<std::string> const ref4{"S"s};
80  auto validatedConfig = validateConfig(good);
81  auto const& pars = validatedConfig().nested().pars();
82  BOOST_TEST_REQUIRE(pars.size() == 2ull);
83  BOOST_TEST(std::get<0>(pars[0]) == "A"s);
84  BOOST_TEST(std::get<1>(pars[0]) == ref1);
85  BOOST_TEST(std::get<0>(pars[1]) == "X"s);
86  BOOST_TEST(std::get<1>(pars[1]) == ref2);
87  auto const& defaultedPars = validatedConfig().nestedWithDef().pars();
88  BOOST_TEST_REQUIRE(defaultedPars.size() == 2ull);
89  BOOST_TEST(std::get<0>(defaultedPars[0]) == "M"s);
90  BOOST_TEST(std::get<1>(defaultedPars[0]) == ref3);
91  BOOST_TEST(std::get<0>(defaultedPars[1]) == "R"s);
92  BOOST_TEST(std::get<1>(defaultedPars[1]) == ref4);
93 }
94 
96 {
97  string const bad{"nested: { pars: [[A], [X, [Y, Z]]] }"s};
98  BOOST_REQUIRE_THROW(validateConfig(bad), detail::validationException);
100 }
101 
103 {
104  string const bad{"nested: { pars: [[A]] }"s};
105  BOOST_REQUIRE_THROW(validateConfig(bad), detail::validationException);
107 }
108 
110 {
111  string const bad{"nested: { pars: [[A, 7]] }"s};
112  BOOST_REQUIRE_THROW(validateConfig(bad), fhicl::exception);
114 }
115 
117 {
118  string const bad{"nested: { pars: [[A, [7]]] }\n"s +
119  "nestedWithDef: { pars: [[A]]}"s};
120  BOOST_REQUIRE_THROW(validateConfig(bad), detail::validationException);
122 }
123 
124 BOOST_AUTO_TEST_SUITE_END()
std::string string
Definition: nybbler.cc:12
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
fhicl::Table< Config > validateConfig(std::string const &configStr)
ChannelGroupService::Name Name
STL namespace.
BOOST_AUTO_TEST_CASE(GoodTuple1)
BOOST_TEST_REQUIRE(static_cast< bool >(inFile))
typename config_impl< T >::type Config
Definition: ModuleMacros.h:52
def move(depos, offset)
Definition: depos.py:107
static constexpr double ps
Definition: Units.h:99
unsigned int bad()
static QCString * s
Definition: config.cpp:1042
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33