LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
unit_test_base.h
Go to the documentation of this file.
1 
25 #ifndef TEST_UNIT_TEST_BASE_H
26 #define TEST_UNIT_TEST_BASE_H
27 
28 // LArSoft libraries
32 
33 // utility libraries
34 #include "fhiclcpp/ParameterSet.h"
36 #include "fhiclcpp/parse.h"
37 #include "fhiclcpp/types/Atom.h"
38 #include "fhiclcpp/types/Comment.h"
39 #include "fhiclcpp/types/Name.h"
40 #include "fhiclcpp/types/Table.h"
41 // #include "fhiclcpp/exception.h"
43 
44 // CET libraries
45 #include "cetlib/filepath_maker.h"
46 #include "cetlib/filesystem.h" // cet::is_absolute_filepath()
47 #include "cetlib/search_path.h"
48 
49 // C/C++ standard libraries
50 #include <iostream> // for output before message facility is set up
51 #include <map>
52 #include <memory> // std::unique_ptr<>
53 #include <stdexcept> // std::logic_error
54 #include <string>
55 #include <type_traits> // std::add_rvalue_reference()
56 #include <utility> // std::move(), std::forward()
57 
58 namespace testing {
59 
60  namespace details {
61 
64  public:
67 
69  CommandLineArguments(int argc, char** argv) { ParseArguments(argc, argv); }
70 
72  void ParseArguments(int argc, char** argv);
73 
75  std::string Executable() const { return exec_name; }
76 
78  std::vector<std::string> const& Arguments() const { return args; }
79 
81  bool hasArgument(size_t iArg) const { return iArg < args.size(); }
82 
84  std::string const& Argument(size_t iArg) const { return args[iArg]; }
85 
86  private:
87  std::string exec_name;
88  std::vector<std::string> args;
89 
91  void Clear()
92  {
93  exec_name.clear();
94  args.clear();
95  }
96 
97  }; // class CommandLineArguments
98 
99  inline void CommandLineArguments::ParseArguments(int argc, char** argv)
100  {
101  Clear();
102  if (argc == 0) return;
103 
104  exec_name = argv[0];
105 
106  args.resize(argc - 1);
107  std::copy(argv + 1, argv + argc, args.begin());
108 
109  } // CommandLineArguments:ParseArguments()
110 
111  // forward declaration
112  template <typename TestEnv, typename Pack, typename... Provs>
114 
115  } // namespace details
116 
127 
129  BasicEnvironmentConfiguration() { DefaultInit(); }
130 
133  {
134  ParseCommandLine(argc, argv);
135  }
136 
139  {
140  SetApplicationName(name);
141  }
142 
143  BasicEnvironmentConfiguration(int argc, char** argv, std::string name)
144  : BasicEnvironmentConfiguration(argc, argv)
145  {
146  SetApplicationName(name);
147  }
148 
152  std::string ApplicationName() const { return appl_name; }
153 
155  std::string ConfigurationPath() const { return config_path; }
156 
158  std::string TesterParameterSetPath(std::string name) const
159  {
160  auto iPath = test_paths.find(name);
161  return (iPath == test_paths.end()) ? ("physics.analyzers." + name) : iPath->second;
162  }
163 
165  std::string MainTesterParameterSetName() const { return main_test_name; }
166 
168  std::string MainTesterParameterSetPath() const
169  {
170  return MainTesterParameterSetName().empty() ?
171  "" :
172  TesterParameterSetPath(MainTesterParameterSetName());
173  }
174 
176  std::string ServiceParameterSetPath(std::string name) const
177  {
178  auto iPath = service_paths.find(name);
179  return (iPath == service_paths.end()) ? ("services." + name) : iPath->second;
180  } // ServiceParameterSetPath()
181 
183  std::string DefaultTesterConfiguration(std::string tester_name) const
184  {
185  return analyzers_default_cfg.at(tester_name);
186  }
187 
189  std::string DefaultServiceConfiguration(std::string service_name) const
190  {
191  return services_default_cfg.at(service_name);
192  }
193 
195  std::string DefaultConfiguration() const { return BuildDefaultConfiguration(); }
196 
198  std::string ExecutablePath() const { return arguments.Executable(); }
199 
201  std::vector<std::string> const& EexcutableArguments() const { return arguments.Arguments(); }
202 
204 
207 
209  void SetApplicationName(std::string name) { appl_name = name; }
210 
212  void SetConfigurationPath(std::string path) { config_path = path; }
213 
215  void SetMainTesterParameterSetName(std::string name) { main_test_name = name; }
216 
218  void SetTesterParameterSetPath(std::string test_name, std::string path)
219  {
220  test_paths[test_name] = path;
221  }
222 
224  void SetMainTesterParameterSetPath(std::string path)
225  {
226  if (MainTesterParameterSetName().empty()) {
227  throw std::logic_error("Request setting configuration of non-existent main tester");
228  }
229  SetTesterParameterSetPath(MainTesterParameterSetName(), path);
230  }
231 
233  void SetServiceParameterSetPath(std::string service_name, std::string path)
234  {
235  service_paths[service_name] = path;
236  }
237 
239  void AddDefaultServiceConfiguration(std::string service_name, std::string service_cfg)
240  {
241  services_default_cfg[service_name] = service_cfg;
242  }
243 
245  void AddDefaultTesterConfiguration(std::string tester_name, std::string tester_cfg)
246  {
247  analyzers_default_cfg[tester_name] = tester_cfg;
248  }
249 
251  void AddDefaultTesterConfiguration(std::string tester_cfg)
252  {
253  if (MainTesterParameterSetName().empty()) {
254  throw std::logic_error("Request adding configuration of non-existent main tester");
255  }
256  AddDefaultTesterConfiguration(MainTesterParameterSetName(), tester_cfg);
257  }
258 
260 
261  protected:
262  using ConfigurationMap_t = std::map<std::string, std::string>;
263  using PathMap_t = std::map<std::string, std::string>;
264 
265  std::string appl_name;
266  std::string config_path;
267  std::string main_test_name;
268  std::string main_test_path;
269 
271  static std::string DefaultApplicationName() { return "Test"; }
272 
277 
282 
284  void ParseCommandLine(int argc, char** argv)
285  {
286  arguments.ParseArguments(argc, argv);
287  if (arguments.hasArgument(0)) SetConfigurationPath(arguments.Argument(0)); // first argument
288  }
289 
291  void DefaultInit()
292  {
293  SetApplicationName(DefaultApplicationName());
294  SetMainTesterParameterSetName("");
295  // a destination which will react to all messages from DEBUG up:
296  AddDefaultServiceConfiguration("message",
297  R"(
298  debugModules: [ '*' ]
299  destinations : {
300  stdout: {
301  type: cout
302  threshold: DEBUG
303  categories: {
304  default: {
305  limit: -1
306  }
307  } // categories
308  } // stdout
309  statistics: {type:cout}
310  } // destinations
311  )");
312  } // DefaultInit()
313 
316  {
317  return BuildServiceConfiguration(services_default_cfg);
318  }
319 
321  std::string BuildDefaultTestConfiguration() const
322  {
323  return BuildTestConfiguration(analyzers_default_cfg);
324  }
325 
327  std::string BuildDefaultConfiguration() const
328  {
329  return BuildConfiguration(services_default_cfg, analyzers_default_cfg);
330  }
331 
333  static std::string BuildServiceConfiguration(ConfigurationMap_t const& services)
334  {
335  std::string cfg;
336  cfg += "\nservices: {";
337  for (auto const& service_info : services) {
338  cfg += "\n " + service_info.first + ": {";
339  cfg += "\n" + service_info.second;
340  cfg += "\n } # " + service_info.first;
341  } // for services
342  cfg += "\n} # services"
343  "\n";
344  return cfg;
345  } // BuildServiceConfiguration()
346 
348  static std::string BuildTestConfiguration(ConfigurationMap_t const& analyzers)
349  {
350  std::string cfg;
351  cfg += "\nphysics: {"
352  "\n analyzers: {";
353  for (auto const& module_info : analyzers) {
354  cfg += "\n " + module_info.first + ": {";
355  cfg += "\n" + module_info.second;
356  cfg += "\n } # " + module_info.first;
357  } // for analyzers
358  cfg += "\n } # analyzers"
359  "\n} # physics";
360  return cfg;
361  } // BuildServiceConfiguration()
362 
364  static std::string BuildConfiguration(ConfigurationMap_t const& services,
365  ConfigurationMap_t const& modules)
366  {
367  std::string cfg;
368  cfg += BuildServiceConfiguration(services);
369  cfg += BuildTestConfiguration(modules);
370  return cfg;
371  } // BuildConfiguration()
372 
373  private:
375 
376  }; // class BasicEnvironmentConfiguration<>
377 
384  template <typename RES>
386  using Resource_t = RES;
387 
388  public:
389  using ResourcePtr_t = std::shared_ptr<Resource_t>;
390 
393 
395  static void AddSharedResource(std::string res_name, ResourcePtr_t res_ptr)
396  {
397  Resources[res_name] = res_ptr;
398  }
399 
402  {
403  AddSharedResource(std::string(), res_ptr);
404  }
405 
407  template <typename... Args>
408  static ResourcePtr_t ProvideSharedResource(std::string res_name, ResourcePtr_t res_ptr)
409  {
410  if (hasResource(res_name)) return ResourcePtr_t();
411  AddSharedResource(res_name, res_ptr);
412  return res_ptr;
413  }
414 
416  template <typename... Args>
418  {
419  return ProvideSharedResource(std::string(), res_ptr);
420  }
421 
423  static bool ReplaceSharedResource(std::string res_name,
425  Resource_t const* old_res_ptr,
426  ResourcePtr_t res_ptr)
427  {
428  ResourcePtr_t current_res_ptr = ShareResource();
429  if (current_res_ptr.get() != old_res_ptr) return false;
430  AddSharedResource(res_name, res_ptr);
431  return true;
432  }
433  static bool ReplaceSharedResource(std::string res_name,
434  ResourcePtr_t old_res_ptr,
435  ResourcePtr_t res_ptr)
436  {
437  return ReplaceSharedResource(res_name, old_res_ptr.get(), res_ptr);
438  }
440 
442  static bool ReplaceDefaultSharedResource(Resource_t const* old_res_ptr, ResourcePtr_t res_ptr)
444  {
445  return ReplaceSharedResource(std::string(), old_res_ptr, res_ptr);
446  }
447  static bool ReplaceDefaultSharedResource(ResourcePtr_t old_res_ptr, ResourcePtr_t res_ptr)
448  {
449  return ReplaceSharedResource(std::string(), old_res_ptr, res_ptr);
450  }
452 
454  template <typename... Args>
455  static ResourcePtr_t CreateResource(std::string res_name, Args&&... args)
456  {
457  ResourcePtr_t res_ptr(new Resource_t(std::forward<Args>(args)...));
458  AddSharedResource(res_name, res_ptr);
459  return res_ptr;
460  }
461 
463  template <typename... Args>
464  static void CreateDefaultResource(Args&&... args)
465  {
466  CreateResource(std::string(), std::forward<Args>(args)...);
467  }
468 
470  template <typename... Args>
471  static ResourcePtr_t ProposeSharedResource(std::string res_name, Args&&... args)
472  {
473  return hasResource(res_name) ? ResourcePtr_t() :
474  CreateResource(res_name, std::forward<Args>(args)...);
475  }
476 
478  template <typename... Args>
480  {
481  return ProposeSharedResource(std::string(), std::forward<Args>(args)...);
482  }
483 
485 
488 
491  static bool hasResource(std::string name = "")
492  {
493  auto iRes = Resources.find(name);
494  return (iRes != Resources.end()) && bool(iRes->second);
495  }
496 
498  static ResourcePtr_t ShareResource(std::string name = "")
499  {
500  auto iRes = Resources.find(name);
501  return (iRes == Resources.end()) ? ResourcePtr_t() : iRes->second;
502  }
503 
505  static Resource_t& Resource(std::string name = "") { return *(Resources.at(name).get()); }
506 
508 
510  static Resource_t& DestroyResource(std::string name = "") { Resources.erase(name); }
511 
512  private:
513  static std::map<std::string, ResourcePtr_t> Resources;
514 
515  }; // class TestSharedGlobalResource<>
516 
517  template <typename RES>
518  std::map<std::string, typename TestSharedGlobalResource<RES>::ResourcePtr_t>
520 
559  template <typename ConfigurationClass>
561 
562  public:
563  using Configuration_t = ConfigurationClass;
564 
572  BasicTesterEnvironment(bool bSetup = true)
573  {
574  if (bSetup) Setup();
575  }
576 
578 
591  BasicTesterEnvironment(Configuration_t const& configurer, bool bSetup = true)
592  : config(configurer)
593  {
594  if (bSetup) Setup();
595  }
596  BasicTesterEnvironment(Configuration_t&& configurer, bool bSetup = true) : config(configurer)
597  {
598  if (bSetup) Setup();
599  }
601 
603  virtual ~BasicTesterEnvironment();
604 
607 
609  fhicl::ParameterSet const& Parameters() const { return params; }
610 
612  fhicl::ParameterSet ServiceParameters(std::string service_name) const
613  {
614  return params.get<fhicl::ParameterSet>(config.ServiceParameterSetPath(service_name));
615  }
616 
618  fhicl::ParameterSet TesterParameters(std::string test_name) const
619  {
620  return params.get<fhicl::ParameterSet>(config.TesterParameterSetPath(test_name));
621  }
622 
625  {
626  if (config.MainTesterParameterSetName().empty())
627  return {};
628  else
629  return TesterParameters(config.MainTesterParameterSetName());
630  }
631 
633 
634  static fhicl::ParameterSet CompileParameterSet(std::string cfg);
635 
636  protected:
638  Configuration_t const& Config() const { return config; }
639 
641  virtual void Setup();
642 
644  virtual void Configure();
645 
651  {
652  return CompileParameterSet(config.DefaultConfiguration());
653  }
654 
656  virtual void SetupMessageFacility(fhicl::ParameterSet const& pset,
658  std::string appl_name = "") const;
659  virtual void SetupMessageFacility() const
660  {
661  SetupMessageFacility(Parameters(), config.ApplicationName());
662  }
664 
672  static fhicl::ParameterSet ParseParameters(std::string config_path);
674 
675  private:
677  struct Options_t {
678  bool MessageLevels = false;
679  }; // Options_t
680 
683 
685  void ParseEnvironmentOptions();
686 
688 
689  }; // class BasicTesterEnvironment<>
690 
691  //****************************************************************************
750  template <typename ConfigurationClass>
751  class TesterEnvironment : public BasicTesterEnvironment<ConfigurationClass> {
754 
755  public:
756  // inherit constructors
757  using TesterEnvBase_t::TesterEnvBase_t;
758 
774  template <typename Prov, typename... Args>
775  Prov* SetupProvider(Args&&... args)
776  {
777  if (!providers.setup<Prov>(std::forward<Args>(args)...))
778  throw std::runtime_error("Provider already exists!");
779  return providers.getPointer<Prov>();
780  }
781 
796  template <typename Prov, typename... Args>
797  Prov* SetupProviderFromService(std::string name, Args&&... args)
798  {
799  return SetupProvider<Prov>(this->ServiceParameters(name, std::forward<Args>(args)...));
800  }
801 
814  template <typename Prov>
815  Prov* AcquireProvider(std::unique_ptr<Prov>&& prov)
816  {
817  if (!providers.acquire(std::move(prov))) throw std::runtime_error("Provider already exists!");
818  return providers.getPointer<Prov>();
819  }
820 
837  template <typename Interface, typename Prov, typename... Args>
838  Prov* SetupProviderFor(Args&&... args)
839  {
840  auto prov = SetupProvider<Prov>(std::forward<Args>(args)...);
841  providers.set_alias<Prov, Interface>();
842  return prov;
843  }
844 
864  template <typename Interface, typename Prov, typename... Args>
865  Prov* SetupProviderFromServiceFor(std::string name, Args&&... args)
866  {
867  auto* prov = SetupProviderFromService<Prov>(name, std::forward<Args>(args)...);
868  providers.set_alias<Prov, Interface>();
869  return prov;
870  }
871 
886  template <typename Interface, typename Prov>
887  Prov* AcquireProviderFor(std::unique_ptr<Prov>&& prov)
888  {
889  auto prov_ptr = providers.acquire(prov);
890  providers.set_alias<Prov, Interface>();
891  return prov_ptr;
892  }
893 
906  template <typename Prov>
908  {
909  return simpleEnvironmentSetup<Prov>(*this);
910  }
911 
917  template <typename Prov>
919  {
920  if (!providers.erase<Prov>()) throw std::runtime_error("Provider not present!");
921  }
922 
924  template <typename Prov>
925  Prov const* Provider() const
926  {
927  return providers.getPointer<Prov>();
928  }
929 
935  template <typename... Provs>
937  {
939  pack);
940  } // FillProviderPack()
941 
951  template <typename Prov>
952  typename Prov::providers_type ProviderPackFor() const
953  {
954  typename Prov::providers_type pack;
955  FillProviderPack(pack);
956  return pack;
957  } // ProviderPackFor()
958 
959  protected:
961  }; // class TesterEnvironment<>
962 
989  // Note: this function is expected to be used with automatic type detection;
990  // the rules on "universal references" dictate that if config is a (l-value)
991  // reference, CONFIG itself is a l-value reference. We don't want to create
992  // a TesterEnvironment<Config&>, so we explicitly remove the reference from
993  // CONFIG (decay does that and aso removes the constantness, that we also
994  // don't want to be embedded in CONFIG).
995  template <typename CONFIG,
996  typename TESTENV = TesterEnvironment<std::decay_t<CONFIG>>,
997  typename... ARGS>
998  TESTENV CreateTesterEnvironment(CONFIG&& config, ARGS... other_args)
999  {
1000  return TESTENV(std::forward<CONFIG>(config), std::forward<ARGS>(other_args)...);
1001  }
1002 
1003  //****************************************************************************
1004  namespace details {
1005  // Class to implement FHiCL file search.
1006  // This is badly ripped off from ART, but we need to stay out of it
1007  // so I have to replicate that functionality.
1008  // I used the same class name.
1009  class FirstAbsoluteOrLookupWithDotPolicy : public cet::filepath_maker {
1010  public:
1011  FirstAbsoluteOrLookupWithDotPolicy(std::string const& paths) : first(true), after_paths(paths)
1012  {}
1013 
1014  virtual std::string operator()(std::string const& filename);
1015 
1016  void reset() { first = true; }
1017 
1018  private:
1019  bool first;
1020  cet::search_path after_paths;
1021 
1022  }; // class FirstAbsoluteOrLookupWithDotPolicy
1023 
1024  inline std::string FirstAbsoluteOrLookupWithDotPolicy::operator()(std::string const& filename)
1025  {
1026  if (first) {
1027  first = false;
1028  if (cet::is_absolute_filepath(filename)) return filename;
1029  return cet::search_path("./:" + after_paths.to_string()).find_file(filename);
1030  }
1031  else {
1032  return after_paths.find_file(filename);
1033  }
1034  } // FirstAbsoluteOrLookupWithDotPolicy::operator()
1035 
1037  template <typename TestEnv, typename Pack, typename Prov, typename... Others>
1038  struct ProviderPackFiller<TestEnv, Pack, Prov, Others...> {
1039  static void fill(TestEnv const& env, Pack& pack)
1040  {
1041  pack.set(env.template Provider<Prov>());
1043  } // fill()
1044 
1045  }; // ProviderPackFiller<TestEnv, Pack, Prov, Others...>
1046 
1047  // end-of-recursion specialisation
1048  template <typename TestEnv, typename Pack>
1049  struct ProviderPackFiller<TestEnv, Pack> {
1050  static void fill(TestEnv const&, Pack&) {}
1051  }; // ProviderPackFiller<>
1052 
1053  } // namespace details
1054 
1055  //****************************************************************************
1056  template <typename ConfigurationClass>
1058  {
1059 
1060  mf::LogInfo("Test") << config.ApplicationName() << " completed.";
1061 
1062  } // BasicTesterEnvironment<>::~BasicTesterEnvironment()
1063 
1068  template <typename ConfigurationClass>
1070  std::string cfg)
1071  {
1072  fhicl::ParameterSet global_pset;
1073  global_pset = fhicl::ParameterSet::make(cfg);
1074  return global_pset;
1075  } // BasicTesterEnvironment<>::CompileParameterSet()
1076 
1082  template <typename ConfigurationClass>
1084  std::string config_path)
1085  {
1086  // configuration file lookup policy
1087  char const* fhicl_env = getenv("FHICL_FILE_PATH");
1088  std::string search_path = fhicl_env ? std::string(fhicl_env) + ":" : ".:";
1089  details::FirstAbsoluteOrLookupWithDotPolicy policy(search_path);
1090 
1091  // parse a configuration file; obtain intermediate form
1093  table = fhicl::parse_document(config_path, policy);
1094 
1095  // translate into a parameter set
1096  fhicl::ParameterSet global_pset;
1097  global_pset = fhicl::ParameterSet::make(table);
1098 
1099  return global_pset;
1100  } // BasicTesterEnvironment<>::ParseParameters()
1101 
1116  template <typename ConfigurationClass>
1118  {
1119  std::string config_path = config.ConfigurationPath();
1120  params = config_path.empty() ? DefaultParameters() : ParseParameters(config_path);
1121  } // BasicTesterEnvironment::Configure()
1122 
1129  template <typename ConfigurationClass>
1131  fhicl::ParameterSet const& pset,
1132  std::string appl_name /* = "" */) const
1133  {
1134  fhicl::ParameterSet mf_pset;
1135 
1136  if (!pset.get_if_present(config.ServiceParameterSetPath("message"), mf_pset)) {
1137  mf_pset = CompileParameterSet(config.DefaultServiceConfiguration("message"));
1138  std::cout << "Using default message facility configuration:\n"
1139  << mf_pset.to_indented_string(1) << std::endl;
1140  } // if no configuration is available
1141 
1142  mf::StartMessageFacility(mf_pset);
1143  if (!appl_name.empty()) mf::SetApplicationName(appl_name);
1144  mf::SetContextIteration("Initialization");
1145  if (options.MessageLevels) {
1146  std::cout << "Printing message levels in 'MessageFacility' category." << std::endl;
1147 
1148  mf::LogProblem("MessageFacility") << "Error messages are shown.";
1149  mf::LogPrint("MessageFacility") << "Warning messages are shown.";
1150  mf::LogVerbatim("MessageFacility") << "Info messages are shown.";
1151  mf::LogTrace("MessageFacility") << "Debug messages are shown.";
1152  MF_LOG_TRACE("MessageFacility")
1153  << "MF_LOG_TRACE/MF_LOG_DEBUG messages are not compiled away.";
1154  } // if print message levels
1155  mf::LogInfo("MessageFacility") << "MessageFacility started.";
1156  mf::SetContextSinglet("main");
1157  } // BasicTesterEnvironment::SetupMessageFacility()
1158 
1159  template <typename ConfigurationClass>
1161  {
1162 
1163  //
1164  // get the configuration
1165  //
1166  Configure();
1167 
1168  //
1169  // parse the options specific to the test environment
1170  //
1171  ParseEnvironmentOptions();
1172 
1173  //
1174  // set up the message facility
1175  //
1177 
1178  //
1179  // Optionally print the configuration
1180  //
1181  {
1182  mf::LogInfo msg("Configuration");
1183  msg << "Complete configuration (";
1184  if (config.ConfigurationPath().empty())
1185  msg << "default";
1186  else
1187  msg << "'" << config.ConfigurationPath() << "'";
1188  msg << "):\n" << Parameters().to_indented_string(1);
1189  }
1190 
1191  mf::LogInfo("Test") << config.ApplicationName() << " base setup complete.";
1192 
1193  } // BasicTesterEnvironment<>::Setup()
1194 
1195  template <typename ConfigurationClass>
1197  {
1198 
1199  struct OptionsFromConfig_t {
1200  fhicl::Atom<bool> messageLevels{
1201  fhicl::Name("messageLevels"),
1202  fhicl::Comment("prints a message per level (to verify the visible ones"),
1203  false // default: no
1204  };
1205  fhicl::Atom<bool> printOptions{
1206  fhicl::Name("printOptions"),
1207  fhicl::Comment("prints a the list of options (this is one of them!)"),
1208  false // default: no
1209  };
1210  }; // OptionsFromConfig_t
1211 
1212  struct ValidationHelper {
1213  static void printDummy(std::ostream& out)
1214  {
1216  fhicl::Comment("Test environment options"))
1217  .print_allowed_configuration(out);
1218  } // printDummy()
1219 
1220  static fhicl::Table<OptionsFromConfig_t> validate(fhicl::ParameterSet const& pset)
1221  {
1222  try {
1223  return fhicl::Table<OptionsFromConfig_t>(pset, {});
1224  }
1225  catch (...) {
1226  std::cerr << "Error parsing environment test options! Valid options:" << std::endl;
1227  ValidationHelper::printDummy(std::cerr);
1228  throw;
1229  }
1230  } // validate()
1231  };
1232 
1233  fhicl::ParameterSet pset = params.get<fhicl::ParameterSet>("test", {});
1234 
1235  // automatically performs validation
1236  fhicl::Table<OptionsFromConfig_t> configTable(ValidationHelper::validate(pset));
1237 
1238  if (configTable().printOptions()) {
1239  std::cout << "The following options can be passed to the test environment"
1240  << " by putting them in the \"test: {}\" table of the configuration file:"
1241  << std::endl;
1242  ValidationHelper::printDummy(std::cout);
1243  }
1244 
1245  options.MessageLevels = configTable().messageLevels();
1246 
1247  } // BasicTesterEnvironment<>::ParseEnvironmentOptions()
1248 
1249 } // namespace testing
1250 
1251 #endif // TEST_UNIT_TEST_BASE_H
Container of service providers accessed by type and optional label.
Definition: ProviderList.h:159
std::string main_test_name
name of main test algorithm
fhicl::ParameterSet const & Parameters() const
Returns the full configuration.
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
virtual std::string operator()(std::string const &filename)
LArSoft test utilities.
fhicl::ParameterSet params
full configuration of the test
Prov const * Provider() const
Return the specified provider (throws if not available)
static fhicl::ParameterSet ParseParameters(std::string config_path)
Fills the test configuration from file or from default.
static void CreateDefaultResource(Args &&...args)
Constructs and registers a new resource with no name.
Helper classes to be used together with LArSoft&#39;s unit test.
void ParseArguments(int argc, char **argv)
Parses arguments.
std::string BuildDefaultConfiguration() const
A string describing the full default parameter set.
std::map< std::string, std::string > PathMap_t
Options_t options
options for the test environment
static ResourcePtr_t ProvideSharedResource(std::string res_name, ResourcePtr_t res_ptr)
Registers a shared resource only if none exists yet.
void SetApplicationName(std::string name)
Sets the name of the application.
std::map< std::string, std::string > ConfigurationMap_t
std::string DefaultConfiguration() const
A string describing the full default parameter set.
Prov * SetupProviderFromService(std::string name, Args &&...args)
Sets a service provider up by calling its testing::setupProvider()
virtual fhicl::ParameterSet DefaultParameters() const
Creates a full configuration for the test.
fhicl::ParameterSet TesterParameters(std::string test_name) const
Returns the configuration of the specified test.
Container for service providers used in a test.
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
ConfigurationMap_t services_default_cfg
Configuration of all the services.
std::shared_ptr< Resource_t > ResourcePtr_t
BasicEnvironmentConfiguration(std::string name)
Constructor; accepts the name as parameter.
BasicTesterEnvironment(bool bSetup=true)
Constructor: sets everything up and declares the test started.
Configuration_t config
instance of the configurer
static ResourcePtr_t ProposeSharedResource(std::string res_name, Args &&...args)
Creates a shared resource only if none exists yet.
bool first
whether we are waiting for the first query
std::string main_test_path
path of main test algorithm configuration
void SetMainTesterParameterSetName(std::string name)
Sets the FHiCL name for the configuration of the test algorithm.
std::string DefaultTesterConfiguration(std::string tester_name) const
A string describing default parameter set to configure specified test.
std::string ExecutablePath() const
Returns the name of the executable as started.
static bool ReplaceSharedResource(std::string res_name, ResourcePtr_t old_res_ptr, ResourcePtr_t res_ptr)
Adds a shared resource to the resource registry.
virtual void Configure()
Reads and translates the configuration.
details::CommandLineArguments arguments
command line arguments
Class holding a configuration for a test environment.
static std::string BuildConfiguration(ConfigurationMap_t const &services, ConfigurationMap_t const &modules)
A string describing the full default parameter set.
Environment for a test.
void FillProviderPack(lar::ProviderPack< Provs... > &pack) const
Fills the specified provider pack with providers.
static bool hasResource(std::string name="")
BasicEnvironmentConfiguration(int argc, char **argv, std::string name)
std::string Executable() const
Returns the name of the executable as started.
std::string TesterParameterSetPath(std::string name) const
FHiCL path for the configuration of the test algorithm.
static std::string DefaultApplicationName()
Returns the default test name.
Prov * SetupProviderFromServiceFor(std::string name, Args &&...args)
Sets a provider up, recording it as implementation of Interface.
Configuration_t const & Config() const
Returns a read-only version of the configuration.
Prov * AcquireProvider(std::unique_ptr< Prov > &&prov)
Acquires a service provider.
static void AddDefaultSharedResource(ResourcePtr_t res_ptr)
Adds a shared resource to the resource registry (empty name)
static std::string BuildServiceConfiguration(ConfigurationMap_t const &services)
A string with the service section from service parameter sets.
void ParseCommandLine(int argc, char **argv)
Extracts arguments from the command line, uses first one as config path.
void SetContextIteration(string const &val)
CommandLineArguments()
Constructor: automatically parses from Boost arguments.
PathMap_t test_paths
Set of paths for tester configuration.
std::string MainTesterParameterSetPath() const
FHiCL path for the configuration of the test algorithm.
void AddDefaultServiceConfiguration(std::string service_name, std::string service_cfg)
Adds a default configuration for the specified service.
CommandLineArguments(int argc, char **argv)
Constructor: parses from specified arguments.
static std::string BuildTestConfiguration(ConfigurationMap_t const &analyzers)
A string with the physics section from analyzer parameter sets.
void SetupMessageFacility(fhicl::ParameterSet const &pset, std::string applName="standalone")
Sets up the message facility service.
MaybeLogger_< ELseverityLevel::ELsev_error, true > LogProblem
std::string ServiceParameterSetPath(std::string name) const
FHiCL path for the configuration of the service.
void StartMessageFacility(fhicl::ParameterSet const &pset, string const &applicationName)
Prov::providers_type ProviderPackFor() const
Returns a provider pack for the specified provider.
static Resource_t & DestroyResource(std::string name="")
Destroys the specified resource (does nothing if no such resource)
Prov * SimpleProviderSetup()
Oversimplified provider setup.
static bool ReplaceDefaultSharedResource(ResourcePtr_t old_res_ptr, ResourcePtr_t res_ptr)
Adds a shared resource as default resource only if it is old_res_ptr.
ProviderList providers
list of available providers
static std::map< std::string, ResourcePtr_t > Resources
bool hasArgument(size_t iArg) const
Returns whether we have arguments up to the iArg-th (0-based)
void DropProvider()
Removes and destroys the specified provider.
std::string config_path
configuration file path
std::string MainTesterParameterSetName() const
Name of the test algorithm instance.
void ParseEnvironmentOptions()
Parses the configuration, looking for the test environment options.
std::string DefaultServiceConfiguration(std::string service_name) const
A string describing the default parameter set to configure the test.
static Resource_t & Resource(std::string name="")
Retrieves the specified resource, or throws if not available.
static ResourcePtr_t ShareResource(std::string name="")
Retrieves the specified resource for sharing (nullptr if none)
void AddDefaultTesterConfiguration(std::string tester_name, std::string tester_cfg)
Adds a default configuration for the specified tester.
std::vector< std::string > const & Arguments() const
Returns the list of non-Boost-test arguments on the command line.
T get(std::string const &key) const
Definition: ParameterSet.h:314
std::string exec_name
name of the test executable (from argv[0])
#define MF_LOG_TRACE(id)
BasicTesterEnvironment(Configuration_t const &configurer, bool bSetup=true)
Setup from a configuration.
TESTENV CreateTesterEnvironment(CONFIG &&config, ARGS...other_args)
Constructs and returns a TesterEnvironment object.
std::vector< std::string > args
command line arguments (from argv[0])
std::string to_indented_string() const
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
ConfigurationClass Configuration_t
fhicl::ParameterSet ServiceParameters(std::string service_name) const
Returns the configuration of the specified service.
std::string const & Argument(size_t iArg) const
Returns the value of the iArg-th (0-based; no range check!)
Reads and makes available the command line parameters.
Utility class providing singleton objects to the derived classes.
void SetTesterParameterSetPath(std::string test_name, std::string path)
Sets the FHiCL path for the configuration of a test algorithm.
std::string BuildDefaultTestConfiguration() const
A string describing the full default parameter set.
PathMap_t service_paths
Set of paths for service configuration.
Prov * SetupProvider(Args &&...args)
Sets a service provider up by calling its testing::setupProvider()
cet::search_path after_paths
path for the other queries
virtual void Setup()
The complete initialization, ran at construction by default.
A test environment with some support for service providers.
BasicEnvironmentConfiguration()
Default constructor; this is what is used in Boost unit test.
Prov * AcquireProviderFor(std::unique_ptr< Prov > &&prov)
Acquires a service provider implementing an interface.
void SetConfigurationPath(std::string path)
Sets the path to the configuration file.
fhicl::ParameterSet TesterParameters() const
Returns the configuration of the main test (undefined if no main test)
void AddDefaultTesterConfiguration(std::string tester_cfg)
Adds a default configuration for the main tester.
static ResourcePtr_t ProvideDefaultSharedResource(ResourcePtr_t res_ptr)
Creates a shared resource as default only if none exists yet.
BasicTesterEnvironment(Configuration_t &&configurer, bool bSetup=true)
Setup from a configuration.
std::string ConfigurationPath() const
Path to the configuration file.
intermediate_table parse_document(std::string const &filename, cet::filepath_maker &maker)
Definition: parse.cc:709
Container for a list of pointers to providers.
Definition: ProviderPack.h:111
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:267
std::string appl_name
name of the application
void SetContextSinglet(string const &val)
std::string BuildDefaultServiceConfiguration() const
A string describing the full default parameter set.
Data structure containing constant pointers to classes.
std::vector< std::string > const & EexcutableArguments() const
Returns the list of non-Boost-test arguments on the command line.
MaybeLogger_< ELseverityLevel::ELsev_success, true > LogTrace
void Clear()
Erases the stored arguments.
void SetServiceParameterSetPath(std::string service_name, std::string path)
Sets the FHiCL path for the configuration of a test algorithm.
std::string ApplicationName() const
Path to the configuration file.
void SetMainTesterParameterSetPath(std::string path)
Sets the FHiCL path for the configuration of the main test algorithm.
void DefaultInit()
Initialize with some default values.
static fhicl::ParameterSet CompileParameterSet(std::string cfg)
Compiles a parameter set from a string.
static ResourcePtr_t ProposeDefaultSharedResource(Args &&...args)
Creates a shared resource as default only if none exists yet.
ConfigurationMap_t analyzers_default_cfg
Configuration of all the analyzer modules.
void SetApplicationName(string const &applicationName)
static ResourcePtr_t CreateResource(std::string res_name, Args &&...args)
Constructs and registers a new resource with a specified name.
static void AddSharedResource(std::string res_name, ResourcePtr_t res_ptr)
Adds a shared resource to the resource registry.
virtual ~BasicTesterEnvironment()
Destructor: closing remarks.
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:109
MaybeLogger_< ELseverityLevel::ELsev_warning, true > LogPrint
virtual void SetupMessageFacility() const
Sets up the message facility.
Prov * SetupProviderFor(Args &&...args)
Sets a provider up, recording it as implementation of Interface.
BasicEnvironmentConfiguration(int argc, char **argv)
Constructor: acquires parameters from the command line.