Macros | Functions
NameSelector_test.cxx File Reference
#include "TestUtils/NameSelector.h"
#include "canvas/Utilities/Exception.h"
#include <cetlib/quiet_unit_test.hpp>
#include <boost/test/test_tools.hpp>
#include <string>
#include <iostream>

Go to the source code of this file.

Macros

#define BOOST_TEST_MODULE   ( NameSelector_test )
 

Functions

void CheckNames (testing::NameSelector const &selector, std::vector< std::string > const &names)
 
 BOOST_AUTO_TEST_CASE (SimpleTest)
 
 BOOST_AUTO_TEST_CASE (MissingQuery)
 
 BOOST_AUTO_TEST_CASE (DefaultConstructorTest)
 
 BOOST_AUTO_TEST_CASE (DefaultThrowTest)
 
 BOOST_AUTO_TEST_CASE (DefaultRejectTest)
 
 BOOST_AUTO_TEST_CASE (DefaultAcceptTest)
 
 BOOST_AUTO_TEST_CASE (SetDefinitionTest)
 
 BOOST_AUTO_TEST_CASE (OverrideTest)
 
 BOOST_AUTO_TEST_CASE (GlobalSpecTest)
 
 BOOST_AUTO_TEST_CASE (ClearAllTest)
 

Macro Definition Documentation

#define BOOST_TEST_MODULE   ( NameSelector_test )

Definition at line 15 of file NameSelector_test.cxx.

Function Documentation

BOOST_AUTO_TEST_CASE ( SimpleTest  )

Definition at line 86 of file NameSelector_test.cxx.

86  {
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)
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.
void ParseName(Name_t name)
Parses a name and adds it to the selector.
Manages a set of names.
Definition: NameSelector.h:36
bool CheckQueryRegistry() const
Checks that no known element with valid response was left unqueried.
Definition: NameSelector.h:160
QTextStream & endl(QTextStream &s)
BOOST_AUTO_TEST_CASE ( MissingQuery  )

Definition at line 122 of file NameSelector_test.cxx.

122  {
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)
void ParseName(Name_t name)
Parses a name and adds it to the selector.
Manages a set of names.
Definition: NameSelector.h:36
bool CheckQueryRegistry() const
Checks that no known element with valid response was left unqueried.
Definition: NameSelector.h:160
QTextStream & endl(QTextStream &s)
BOOST_AUTO_TEST_CASE ( DefaultConstructorTest  )

Definition at line 144 of file NameSelector_test.cxx.

144  {
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)
void CheckNames(testing::NameSelector const &selector, std::vector< std::string > const &names)
void PrintConfiguration(std::ostream &) const
Prints the configuration into a stream.
Manages a set of names.
Definition: NameSelector.h:36
QTextStream & endl(QTextStream &s)
BOOST_AUTO_TEST_CASE ( DefaultThrowTest  )

Definition at line 171 of file NameSelector_test.cxx.

171  {
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)
void CheckNames(testing::NameSelector const &selector, std::vector< std::string > const &names)
Manages a set of names.
Definition: NameSelector.h:36
throw art::Exception (art::errors::Configuration)
Definition: NameSelector.h:46
QTextStream & endl(QTextStream &s)
BOOST_AUTO_TEST_CASE ( DefaultRejectTest  )

Definition at line 193 of file NameSelector_test.cxx.

193  {
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)
void CheckNames(testing::NameSelector const &selector, std::vector< std::string > const &names)
Manages a set of names.
Definition: NameSelector.h:36
QTextStream & endl(QTextStream &s)
BOOST_AUTO_TEST_CASE ( DefaultAcceptTest  )

Definition at line 215 of file NameSelector_test.cxx.

215  {
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)
void CheckNames(testing::NameSelector const &selector, std::vector< std::string > const &names)
Manages a set of names.
Definition: NameSelector.h:36
QTextStream & endl(QTextStream &s)
BOOST_AUTO_TEST_CASE ( SetDefinitionTest  )

Definition at line 237 of file NameSelector_test.cxx.

237  {
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)
void Define(std::string set_name, LIST const &items)
Defines a set.
Definition: NameSelector.h:281
void AddToDefinition(std::string set_name, NAMES...names)
Parses a list of names and add them to the specified definition.
Definition: NameSelector.h:127
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.
Manages a set of names.
Definition: NameSelector.h:36
QTextStream & endl(QTextStream &s)
BOOST_AUTO_TEST_CASE ( OverrideTest  )

Definition at line 278 of file NameSelector_test.cxx.

278  {
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)
void AddToDefinition(std::string set_name, NAMES...names)
Parses a list of names and add them to the specified definition.
Definition: NameSelector.h:127
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.
Manages a set of names.
Definition: NameSelector.h:36
QTextStream & endl(QTextStream &s)
BOOST_AUTO_TEST_CASE ( GlobalSpecTest  )

Definition at line 313 of file NameSelector_test.cxx.

313  {
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)
void AddToDefinition(std::string set_name, NAMES...names)
Parses a list of names and add them to the specified definition.
Definition: NameSelector.h:127
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.
Manages a set of names.
Definition: NameSelector.h:36
QTextStream & endl(QTextStream &s)
BOOST_AUTO_TEST_CASE ( ClearAllTest  )

Definition at line 348 of file NameSelector_test.cxx.

348  {
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)
void CheckNames(testing::NameSelector const &selector, std::vector< std::string > const &names)
Manages a set of names.
Definition: NameSelector.h:36
throw art::Exception (art::errors::Configuration)
Definition: NameSelector.h:46
QTextStream & endl(QTextStream &s)
void CheckNames ( testing::NameSelector const &  selector,
std::vector< std::string > const &  names 
)

In the following tests, the names are conventionally called "acceptXXX" if the final result of the query is expected to be acceptance, "rejectXXX" if the final result of the query is expected to be rejection, or "throwXXX" if the final result of the query is expected to be the throw of an exception.

If an error does happen, run the test with '–log_level=all' to get enough information to track the error location:

  • for an unexpected exception (even without '–log_level=all'):

    .../NameSelector_test.cxx(53): error in "DefaultThrowTest": check selector.Query(name) == testing::NameSelector::rsAccepted failed [2 != 1] unknown location(0): fatal error in "DefaultThrowTest": std::exception: -— Configuration BEGIN NameSelector: name 'accept_unknown' not configured. -— Configuration END

    Boost error points to the test name ("DefaultThrowTest") and to the check that failed (line 53), while the message of the exception fills in with the name being checked

  • for a wrong result, running with '–log_level=all':

    Testing 'accept_unknown' ../NameSelector_test.cxx(53): error in "DefaultRejectTest": check selector.Query(name) == testing::NameSelector::rsAccepted failed [0 != 1]

    Boost points to the test and the failed check as before, while BOOST_TEST_MESSAGE informs about which name was being tested.

Definition at line 53 of file NameSelector_test.cxx.

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
66  (selector.Query(name), testing::NameSelector::rsRejected);
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
73  (selector.Query(name), testing::NameSelector::rsAccepted);
74  BOOST_CHECK(selector(name));
75  BOOST_CHECK(selector.Accepted(name));
76  BOOST_CHECK(!selector.Rejected(name));
77  }
78  } // for
79 
80 } // CheckNames()
static QCString name
Definition: declinfo.cpp:673
std::string string
Definition: nybbler.cc:12
int find(char c, int index=0, bool cs=TRUE) const
Definition: qcstring.cpp:41
throw art::Exception (art::errors::Configuration)
Definition: NameSelector.h:46
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66