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