tableAs_t.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // test tableAs
4 //
5 // ======================================================================
6 
7 #define BOOST_TEST_MODULE (tableAs test)
8 
9 #include "boost/test/unit_test.hpp"
10 
11 #include "FixtureBase.h"
14 #include "fhiclcpp/types/Table.h"
15 #include "fhiclcpp/types/TableAs.h"
16 
17 #include <string>
18 
19 using namespace fhicl;
20 using namespace std;
21 
22 namespace {
23 
24  // ===========================================================================
25  struct Person {
26 
27  string name_;
28  unsigned age_{};
29 
30  Person() = default;
31  Person(string name, unsigned age) : name_{name}, age_{age} {}
32 
33  bool
34  operator==(Person const& p) const
35  {
36  return (name_ == p.name_) && (age_ == p.age_);
37  }
38 
39  bool
40  operator!=(Person const& p) const
41  {
42  return !operator==(p);
43  }
44  };
45 
46  struct PersonConfig {
47  Atom<string> name{Name{"name"}};
48  Atom<unsigned> age{Name{"age"}};
49  };
50 
51  Person
52  convert(PersonConfig const& config)
53  {
54  return Person{config.name(), config.age()};
55  }
56 
57  ostream&
58  operator<<(ostream& os, Person const& p)
59  {
60  return os << "Name: " << p.name_ << " Age: " << p.age_;
61  }
62 
63  // ===========================================================================
64  struct ContactInfo {
65  string street_address;
66  string phone_number;
67  };
68 
69  bool
70  operator==(ContactInfo const& a, ContactInfo const& b)
71  {
72  return a.street_address == b.street_address &&
73  a.phone_number == b.phone_number;
74  }
75 
76  namespace address {
77  struct ContactInfoConfig {
78  Atom<string> street_address{Name{"street_address"}};
79  Atom<string> phone_number{Name{"phone_number"}};
80  };
81 
82  ContactInfo
83  convert(ContactInfoConfig const& config)
84  {
85  return {config.street_address(), config.phone_number()};
86  }
87  }
88 
89  ostream&
90  operator<<(ostream& os, ContactInfo const& info)
91  {
92  return os << info.street_address << ", " << info.phone_number;
93  }
94 
95  // ===========================================================================
96  struct Household {
97  TableAs<Person, PersonConfig> mother{Name{"mother"}};
98  TableAs<Person, PersonConfig> father{Name{"father"}, Person{"John", 30}};
101  Name("contact_info")};
102  };
103 
104  struct Fixture : fhiclcpp_types::FixtureBase<Household> {
105  Fixture() : FixtureBase("tableAs_t.fcl") {}
106  };
107 }
108 // provide use of 'Table<Config> config'
109 
110 BOOST_FIXTURE_TEST_SUITE(tableAs_test, Fixture)
111 
112 BOOST_AUTO_TEST_CASE(tableAs_simple)
113 {
114  auto const mother = config().mother();
115  auto const ref = Person{"Angela", 32};
116  BOOST_TEST(mother == ref);
117 }
118 
119 BOOST_AUTO_TEST_CASE(tableAs_default_simple)
120 {
121  auto const father = config().father();
122  auto const ref = Person{"John", 30};
123  BOOST_TEST(father == ref);
124 }
125 
126 BOOST_AUTO_TEST_CASE(tableAs_inSequence)
127 {
128  auto const kids = config().kids();
129  auto const ref = {Person{"Alice", 1},
130  Person{"Bob", 2},
131  Person{"Charlie", 3},
132  Person{"Danielle", 4},
133  Person{"Edgar", 5}};
134  BOOST_TEST(kids == ref, boost::test_tools::per_element{});
135 }
136 
137 BOOST_AUTO_TEST_CASE(optionalTableAs)
138 {
139  ContactInfo info;
140  BOOST_TEST(config().contact_info(info));
141  auto const ref = ContactInfo{"123 S. 45th Ave.", "(123) 456-7890"};
142  BOOST_TEST(info == ref);
143 }
144 
145 BOOST_AUTO_TEST_SUITE_END()
static QCString name
Definition: declinfo.cpp:673
ChannelGroupService::Name Name
STL namespace.
bool operator!=(ModuleKeyAndType const &a, ModuleKeyAndType const &b) noexcept
static Config * config
Definition: config.cpp:1054
const double a
def convert(inputfile, outputfile="wire-cell-garfield-fine-response.json.bz2", average=False, shaped=False)
Definition: garfield.py:262
p
Definition: test.py:223
BOOST_AUTO_TEST_CASE(tableAs_simple)
Definition: tableAs_t.cc:112
static bool * b
Definition: config.cpp:1043
std::ostream & operator<<(std::ostream &, ParameterSetID const &)
bool operator==(ModuleKeyAndType const &a, ModuleKeyAndType const &b) noexcept