NameSelector_test.cxx
Go to the documentation of this file.
1 /**
2  * @file NameSelector_test.cxx
3  * @brief Test of NameSelector class
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date July 24th, 2015
6  */
7 
8 // LArSoft libraries
10 
11 // Boost libraries
13 
14 // Boost libraries
15 #define BOOST_TEST_MODULE ( NameSelector_test )
16 #include <boost/test/unit_test.hpp>
17 
18 // C/C++ standard library
19 #include <string>
20 #include <iostream>
21 
22 /**
23  * In the following tests, the names are conventionally called "acceptXXX" if
24  * the final result of the query is expected to be acceptance, "rejectXXX" if
25  * the final result of the query is expected to be rejection, or "throwXXX" if
26  * the final result of the query is expected to be the throw of an exception.
27  *
28  * If an error does happen, run the test with '--log_level=all' to get enough
29  * information to track the error location:
30  *
31  * - for an unexpected exception (even without '--log_level=all'):
32  *
33  * .../NameSelector_test.cxx(53): error in "DefaultThrowTest": check selector.Query(name) == testing::NameSelector::rsAccepted failed [2 != 1]
34  * unknown location(0): fatal error in "DefaultThrowTest": std::exception: ---- Configuration BEGIN
35  * NameSelector: name 'accept_unknown' not configured.
36  * ---- Configuration END
37  *
38  * Boost error points to the test name ("DefaultThrowTest") and to the check
39  * that failed (line 53), while the message of the exception fills in with
40  * the name being checked
41  *
42  * - for a wrong result, running with '--log_level=all':
43  *
44  * Testing 'accept_unknown'
45  * ../NameSelector_test.cxx(53): error in "DefaultRejectTest": check selector.Query(name) == testing::NameSelector::rsAccepted failed [0 != 1]
46  *
47  * Boost points to the test and the failed check as before, while
48  * BOOST_TEST_MESSAGE informs about which name was being tested.
49  *
50  */
51 void CheckNames
52  (testing::NameSelector const& selector, std::vector<std::string> const& names)
53 {
54 
55  for (std::string const& name: names) {
56  BOOST_TEST_MESSAGE("Testing '" << name << "'");
57  if (name.find("throw") == 0U) {
58  BOOST_TEST(selector.Query(name) == testing::NameSelector::rsThrow);
59  BOOST_CHECK_THROW(selector(name), art::Exception);
60  BOOST_CHECK_THROW(selector.Accepted(name), art::Exception);
61  BOOST_CHECK_THROW(selector.Rejected(name), art::Exception);
62  }
63  else if (name.find("reject") == 0U) {
64  BOOST_TEST
66  BOOST_TEST(!selector(name));
67  BOOST_TEST(!selector.Accepted(name));
68  BOOST_TEST(selector.Rejected(name));
69  }
70  else { // accept
71  BOOST_TEST
73  BOOST_TEST(selector(name));
74  BOOST_TEST(selector.Accepted(name));
75  BOOST_TEST(!selector.Rejected(name));
76  }
77  } // for
78 
79 } // CheckNames()
80 
81 //------------------------------------------------------------------------------
82 //
83 // Simple use case
84 //
85 BOOST_AUTO_TEST_CASE(SimpleTest) {
86 
87  // default answer: yes
88  testing::NameSelector selector;
89 
90  //
91  // initialize with simple elements
92  //
93  std::cout << "SimpleTest" << std::endl;
94  selector.ParseNames("accept_one", "+accept_two");
95  selector.ParseName("-reject_three");
96 
97  //
98  // visually verify the configuration
99  //
100  selector.PrintConfiguration(std::cout);
101  std::cout << std::endl;
102 
103  //
104  // check all the symbols, plus one
105  //
106  CheckNames(selector, {
107  "accept_one", "accept_two", "reject_three", "accept_unknown"
108  });
109 
110  //
111  // verify that we did not miss anything
112  //
113  BOOST_TEST(selector.CheckQueryRegistry(std::cout));
114 
115 } // BOOST_AUTO_TEST_CASE(SimpleTest)
116 
117 
118 //
119 // Test for check of missing queries
120 //
121 BOOST_AUTO_TEST_CASE(MissingQuery) {
122 
123  // default answer: yes
124  testing::NameSelector selector;
125 
126  //
127  // initialize with simple elements
128  //
129  std::cout << "MissingQuery" << std::endl;
130  selector.ParseName("accept_one");
131 
132  //
133  // verify that we did miss something
134  //
135  BOOST_TEST(!selector.CheckQueryRegistry(std::cout));
136 
137 } // BOOST_AUTO_TEST_CASE(MissingQuery)
138 
139 
140 //
141 // Test for default throw
142 //
143 BOOST_AUTO_TEST_CASE(DefaultConstructorTest) {
144 
145  // default answer: yes
146  testing::NameSelector selector;
147 
148  //
149  // initialize with simple elements
150  //
151  std::cout << "DefaultConstructorTest" << std::endl;
152 
153  //
154  // visually verify the configuration
155  //
156  selector.PrintConfiguration(std::cout);
157  std::cout << std::endl;
158 
159  //
160  // check all the symbols
161  //
162  CheckNames(selector, { "accept_one", "accept_two" });
163 
164 } // BOOST_AUTO_TEST_CASE(DefaultConstructorTest)
165 
166 
167 //
168 // Test for default throw
169 //
170 BOOST_AUTO_TEST_CASE(DefaultThrowTest) {
171 
172  // default answer: yes
174 
175  //
176  // initialize with simple elements
177  //
178  std::cout << "DefaultThrowTest" << std::endl;
179  selector.ParseNames("accept_one", "-reject_three");
180 
181  //
182  // check all the symbols
183  //
184  CheckNames(selector, { "accept_one", "reject_three", "throw_two" });
185 
186 } // BOOST_AUTO_TEST_CASE(DefaultThrowTest)
187 
188 
189 //
190 // Test for default rejection
191 //
192 BOOST_AUTO_TEST_CASE(DefaultRejectTest) {
193 
194  // default answer: yes
196 
197  //
198  // initialize with simple elements
199  //
200  std::cout << "DefaultRejectTest" << std::endl;
201  selector.ParseNames("accept_one", "-reject_three");
202 
203  //
204  // check all the symbols, plus one
205  //
206  CheckNames(selector, { "accept_one", "reject_two", "reject_unknown" });
207 
208 } // BOOST_AUTO_TEST_CASE(DefaultRejectTest)
209 
210 
211 //
212 // Test for default acceptance
213 //
214 BOOST_AUTO_TEST_CASE(DefaultAcceptTest) {
215 
216  // default answer: yes
218 
219  //
220  // initialize with simple elements
221  //
222  std::cout << "DefaultAcceptTest" << std::endl;
223  selector.ParseNames("accept_one", "-reject_three");
224 
225  //
226  // check all the symbols, plus one
227  //
228  CheckNames(selector, { "accept_one", "accept_two", "accept_unknown" });
229 
230 } // BOOST_AUTO_TEST_CASE(DefaultAcceptTest)
231 
232 
233 //
234 // Test for adding name set
235 //
236 BOOST_AUTO_TEST_CASE(SetDefinitionTest) {
237 
238  // default answer: yes
239  testing::NameSelector selector;
240 
241  // set 2 does not follow the naming convention because it will be subtracted
242  selector.AddToDefinition
243  ("set1", "accept_set1_one", "+accept_set1_two", "-reject_set1_three");
244  selector.AddToDefinition
245  ("set2", "-accept_set2_one", "-accept_set2_two", "+reject_set2_three");
246  selector.Define("set3", std::vector<std::string>
247  { "accept_set3_one", "+accept_set3_two", "-reject_set3_three" }
248  );
249 
250  //
251  // initialize with simple elements
252  //
253  std::cout << "SetDefinitionTest" << std::endl;
254  selector.ParseNames("accept_one", "set1", "-@set2", "+set3", "-reject_two");
255 
256  //
257  // visually verify the configuration
258  //
259  selector.PrintConfiguration(std::cout);
260  std::cout << std::endl;
261 
262  //
263  // check all the symbols, plus one
264  //
265  CheckNames(selector, {
266  "accept_set1_one", "accept_set1_two", "reject_set1_three",
267  "accept_set2_one", "accept_set2_two", "reject_set2_three",
268  "accept_one", "reject_two", "accept_unknown"
269  });
270 
271 } // BOOST_AUTO_TEST_CASE(SetDefinitionTest)
272 
273 
274 //
275 // Test for double specification
276 //
277 BOOST_AUTO_TEST_CASE(OverrideTest) {
278 
279  // default answer: yes
280  testing::NameSelector selector;
281 
282  // set does not follow the naming convention because it will be subtracted
283  selector.AddToDefinition("default",
284  "accept_one", "-accept_two", "+reject_three"
285  );
286 
287  //
288  // initialize with simple elements
289  //
290  std::cout << "OverrideTest" << std::endl;
291  selector.ParseNames("+reject_three", "-default", "accept_one", "accept_four");
292 
293  //
294  // visually verify the configuration
295  //
296  selector.PrintConfiguration(std::cout);
297  std::cout << std::endl;
298 
299  //
300  // check all the symbols
301  //
302  CheckNames(selector, {
303  "accept_one", "accept_two", "reject_three", "accept_four"
304  });
305 
306 } // BOOST_AUTO_TEST_CASE(OverrideTest)
307 
308 
309 //
310 // Global specifications
311 //
312 BOOST_AUTO_TEST_CASE(GlobalSpecTest) {
313 
314  // default answer: yes
315  testing::NameSelector selector;
316 
317  // set 2 does not follow the naming convention because it will be subtracted
318  selector.AddToDefinition("default",
319  "accept_one", "-accept_two", "+reject_three"
320  );
321 
322  //
323  // initialize with simple elements
324  //
325  std::cout << "GlobalSpecTest" << std::endl;
326  selector.ParseNames("accept_one", "-*", "-reject_three", "accept_four");
327 
328  //
329  // visually verify the configuration
330  //
331  selector.PrintConfiguration(std::cout);
332  std::cout << std::endl;
333 
334  //
335  // check all the symbols, plus one
336  //
337  CheckNames(selector, {
338  "accept_one", "reject_unknown", "reject_three", "accept_four"
339  });
340 
341 } // BOOST_AUTO_TEST_CASE(GlobalSpecTest)
342 
343 
344 //
345 // Clear-all directive
346 //
347 BOOST_AUTO_TEST_CASE(ClearAllTest) {
348 
349  // default answer: yes
351 
352  //
353  // initialize
354  //
355  std::cout << "ClearAllTest" << std::endl;
356  selector.ParseNames("throw_lost", "!", "-reject_three", "accept_four");
357 
358  //
359  // visually verify the configuration
360  //
361  selector.PrintConfiguration(std::cout);
362  std::cout << std::endl;
363 
364  //
365  // check all the symbols, plus one
366  //
367  CheckNames(selector, {
368  "throw_lost", "throw_unknown", "reject_three", "accept_four"
369  });
370 
371 } // BOOST_AUTO_TEST_CASE(ClearAllTest)
static QCString name
Definition: declinfo.cpp:673
void Define(std::string set_name, LIST const &items)
Defines a set.
Definition: NameSelector.h:281
BOOST_AUTO_TEST_CASE(SimpleTest)
std::string string
Definition: nybbler.cc:12
void AddToDefinition(std::string set_name, NAMES...names)
Parses a list of names and add them to the specified definition.
Definition: NameSelector.h:127
bool Accepted(Name_t name) const
Returns whether the name is accepted as good.
void CheckNames(testing::NameSelector const &selector, std::vector< std::string > const &names)
void ParseNames(NAMES...names)
Parses a list of names and adds them to the selector.
Definition: NameSelector.h:97
void PrintConfiguration(std::ostream &) const
Prints the configuration into a stream.
int find(char c, int index=0, bool cs=TRUE) const
Definition: qcstring.cpp:41
void ParseName(Name_t name)
Parses a name and adds it to the selector.
Manages a set of names.
Definition: NameSelector.h:36
bool Rejected(Name_t name) const
Returns whether the name is rejected as bad.
Definition: NameSelector.h:137
bool CheckQueryRegistry() const
Checks that no known element with valid response was left unqueried.
Definition: NameSelector.h:160
throw art::Exception (art::errors::Configuration)
Definition: NameSelector.h:46
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
static std::vector< std::string > const names
Definition: FragmentType.hh:8
QTextStream & endl(QTextStream &s)
Response_t Query(Name_t name) const
Returns the response for the specified name (does not throw)