LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
unit_test_base.h
Go to the documentation of this file.
1 
26 #ifndef TEST_UNIT_TEST_BASE_H
27 #define TEST_UNIT_TEST_BASE_H
28 
29 // LArSoft libraries
33 
34 // utility libraries
35 #include "fhiclcpp/ParameterSet.h"
38 #include "fhiclcpp/parse.h"
39 #include "fhiclcpp/types/Comment.h"
40 #include "fhiclcpp/types/Name.h"
41 #include "fhiclcpp/types/Atom.h"
42 #include "fhiclcpp/types/Table.h"
43 // #include "fhiclcpp/exception.h"
45 
46 // CET libraries
47 #include "cetlib/filesystem.h" // cet::is_absolute_filepath()
48 #include "cetlib/filepath_maker.h"
49 #include "cetlib/search_path.h"
50 
51 // C/C++ standard libraries
52 #include <iostream> // for output before message facility is set up
53 #include <string>
54 #include <memory> // std::unique_ptr<>
55 #include <utility> // std::move(), std::forward()
56 #include <map>
57 #include <type_traits> // std::add_rvalue_reference()
58 #include <stdexcept> // std::logic_error
59 
60 
61 namespace testing {
62 
63  namespace details {
64 
67  public:
70 
72  CommandLineArguments(int argc, char** argv)
73  { ParseArguments(argc, argv); }
74 
76  void ParseArguments(int argc, char** argv);
77 
79  std::string Executable() const { return exec_name; }
80 
82  std::vector<std::string> const& Arguments() const { return args; }
83 
85  bool hasArgument(size_t iArg) const { return iArg < args.size(); }
86 
88  std::string const& Argument(size_t iArg) const { return args[iArg]; }
89 
90  private:
91  std::string exec_name;
92  std::vector<std::string> args;
93 
95  void Clear() { exec_name.clear(); args.clear(); }
96 
97  }; // class CommandLineArguments
98 
99 
100  inline void CommandLineArguments::ParseArguments(int argc, char** argv) {
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 
112  // forward declaration
113  template
114  <typename TestEnv, typename Pack, typename... Provs>
116 
117  } // namespace details
118 
119 
130 
132  BasicEnvironmentConfiguration() { DefaultInit(); }
133 
135  BasicEnvironmentConfiguration(int argc, char** argv):
137  { ParseCommandLine(argc, argv); }
138 
140  BasicEnvironmentConfiguration(std::string name):
142  { SetApplicationName(name); }
143 
144  BasicEnvironmentConfiguration(int argc, char** argv, std::string name):
146  { SetApplicationName(name); }
147 
151  std::string ApplicationName() const { return appl_name; }
152 
154  std::string ConfigurationPath() const { return config_path; }
155 
157  std::string TesterParameterSetPath(std::string name) const
158  {
159  auto iPath = test_paths.find(name);
160  return (iPath == test_paths.end())
161  ? ("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  ? "": TesterParameterSetPath(MainTesterParameterSetName());
172  }
173 
175  std::string ServiceParameterSetPath(std::string name) const
176  {
177  auto iPath = service_paths.find(name);
178  return (iPath == service_paths.end())
179  ? ("services." + name): iPath->second;
180  } // ServiceParameterSetPath()
181 
183  std::string DefaultTesterConfiguration(std::string tester_name) const
184  { return analyzers_default_cfg.at(tester_name); }
185 
187  std::string DefaultServiceConfiguration(std::string service_name) const
188  { return services_default_cfg.at(service_name); }
189 
191  std::string DefaultConfiguration() const
192  { return BuildDefaultConfiguration(); }
193 
195  std::string ExecutablePath() const { return arguments.Executable(); }
196 
198  std::vector<std::string> const& EexcutableArguments() const
199  { return arguments.Arguments(); }
200 
202 
203 
206 
208  void SetApplicationName(std::string name) { appl_name = name; }
209 
211  void SetConfigurationPath(std::string path) { config_path = path; }
212 
214  void SetMainTesterParameterSetName(std::string name)
215  { main_test_name = name; }
216 
218  void SetTesterParameterSetPath(std::string test_name, std::string path)
219  { test_paths[test_name] = path; }
220 
222  void SetMainTesterParameterSetPath(std::string path)
223  {
224  if (MainTesterParameterSetName().empty()) {
225  throw std::logic_error
226  ("Request setting configuration of non-existent main tester");
227  }
228  SetTesterParameterSetPath(MainTesterParameterSetName(), path);
229  }
230 
232  void SetServiceParameterSetPath(std::string service_name, std::string path)
233  { service_paths[service_name] = path; }
234 
236  void AddDefaultServiceConfiguration
237  (std::string service_name, std::string service_cfg)
238  { services_default_cfg[service_name] = service_cfg; }
239 
241  void AddDefaultTesterConfiguration
242  (std::string tester_name, std::string tester_cfg)
243  { analyzers_default_cfg[tester_name] = tester_cfg; }
244 
246  void AddDefaultTesterConfiguration(std::string tester_cfg)
247  {
248  if (MainTesterParameterSetName().empty()) {
249  throw std::logic_error
250  ("Request adding configuration of non-existent main tester");
251  }
252  AddDefaultTesterConfiguration(MainTesterParameterSetName(), tester_cfg);
253  }
254 
256 
257 
258 
259  protected:
260  using ConfigurationMap_t = std::map<std::string, std::string>;
261  using PathMap_t = std::map<std::string, std::string>;
262 
263  std::string appl_name;
264  std::string config_path;
265  std::string main_test_name;
266  std::string main_test_path;
267 
269  static std::string DefaultApplicationName() { return "Test"; }
270 
275 
280 
282  void ParseCommandLine(int argc, char** argv)
283  {
284  arguments.ParseArguments(argc, argv);
285  if (arguments.hasArgument(0))
286  SetConfigurationPath(arguments.Argument(0)); // first argument
287  }
288 
290  void DefaultInit()
291  {
292  SetApplicationName(DefaultApplicationName());
293  SetMainTesterParameterSetName("");
294  // a destination which will react to all messages from DEBUG up:
295  AddDefaultServiceConfiguration("message",
296  R"(
297  debugModules: [ '*' ]
298  destinations : {
299  stdout: {
300  type: cout
301  threshold: DEBUG
302  categories: {
303  default: {
304  limit: -1
305  }
306  } // categories
307  } // stdout
308  statistics: {type:cout}
309  } // destinations
310  )");
311  } // DefaultInit()
312 
315  { return BuildServiceConfiguration(services_default_cfg); }
316 
318  std::string BuildDefaultTestConfiguration() const
319  { return BuildTestConfiguration(analyzers_default_cfg); }
320 
322  std::string BuildDefaultConfiguration() const
323  {
324  return BuildConfiguration(services_default_cfg, analyzers_default_cfg);
325  }
326 
327 
329  static std::string BuildServiceConfiguration
330  (ConfigurationMap_t const& services)
331  {
332  std::string cfg;
333  cfg += "\nservices: {";
334  for (auto const& service_info: services) {
335  cfg += "\n " + service_info.first + ": {";
336  cfg += "\n" + service_info.second;
337  cfg += "\n } # " + service_info.first;
338  } // for services
339  cfg +=
340  "\n} # services"
341  "\n";
342  return cfg;
343  } // BuildServiceConfiguration()
344 
346  static std::string BuildTestConfiguration
347  (ConfigurationMap_t const& analyzers)
348  {
349  std::string cfg;
350  cfg +=
351  "\nphysics: {"
352  "\n analyzers: {"
353  ;
354  for (auto const& module_info: analyzers) {
355  cfg += "\n " + module_info.first + ": {";
356  cfg += "\n" + module_info.second;
357  cfg += "\n } # " + module_info.first;
358  } // for analyzers
359  cfg +=
360  "\n } # analyzers"
361  "\n} # physics";
362  return cfg;
363  } // BuildServiceConfiguration()
364 
366  static std::string BuildConfiguration
367  (ConfigurationMap_t const& services, ConfigurationMap_t const& modules)
368  {
369  std::string cfg;
370  cfg += BuildServiceConfiguration(services);
371  cfg += BuildTestConfiguration(modules);
372  return cfg;
373  } // BuildConfiguration()
374 
375  private:
377 
378  }; // class BasicEnvironmentConfiguration<>
379 
380 
381 
388  template <typename RES>
390  using Resource_t = RES;
391 
392  public:
393  using ResourcePtr_t = std::shared_ptr<Resource_t>;
394 
397 
399  static void AddSharedResource(std::string res_name, ResourcePtr_t res_ptr)
400  { Resources[res_name] = res_ptr; }
401 
404  { AddSharedResource(std::string(), res_ptr); }
405 
407  template <typename... Args>
408  static ResourcePtr_t ProvideSharedResource
409  (std::string res_name, ResourcePtr_t res_ptr)
410  {
411  if (hasResource(res_name)) return ResourcePtr_t();
412  AddSharedResource(res_name, res_ptr);
413  return res_ptr;
414  }
415 
417  template <typename... Args>
419  { return ProvideSharedResource(std::string(), res_ptr); }
420 
422  static bool ReplaceSharedResource(
424  std::string res_name,
425  Resource_t const* old_res_ptr, ResourcePtr_t res_ptr
426  )
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
434  (std::string res_name, ResourcePtr_t old_res_ptr, ResourcePtr_t res_ptr)
435  { return ReplaceSharedResource(res_name, old_res_ptr.get(), res_ptr); }
437 
439  static bool ReplaceDefaultSharedResource
441  (Resource_t const* old_res_ptr, ResourcePtr_t res_ptr)
442  { return ReplaceSharedResource(std::string(), old_res_ptr, res_ptr); }
443  static bool ReplaceDefaultSharedResource
444  (ResourcePtr_t old_res_ptr, ResourcePtr_t res_ptr)
445  { return ReplaceSharedResource(std::string(), old_res_ptr, res_ptr); }
447 
449  template <typename... Args>
450  static ResourcePtr_t CreateResource(std::string res_name, Args&&... args)
451  {
452  ResourcePtr_t res_ptr(new Resource_t(std::forward<Args>(args)...));
453  AddSharedResource(res_name, res_ptr);
454  return res_ptr;
455  }
456 
458  template <typename... Args>
459  static void CreateDefaultResource(Args&&... args)
460  { CreateResource(std::string(), std::forward<Args>(args)...); }
461 
462 
464  template <typename... Args>
465  static ResourcePtr_t ProposeSharedResource
466  (std::string res_name, Args&&... args)
467  {
468  return hasResource(res_name)?
469  ResourcePtr_t():
470  CreateResource(res_name, std::forward<Args>(args)...);
471  }
472 
474  template <typename... Args>
476  {
477  return ProposeSharedResource
478  (std::string(), std::forward<Args>(args)...);
479  }
480 
482 
485 
488  static bool hasResource(std::string name = "")
489  {
490  auto iRes = Resources.find(name);
491  return (iRes != Resources.end()) && bool(iRes->second);
492  }
493 
495  static ResourcePtr_t ShareResource(std::string name = "")
496  {
497  auto iRes = Resources.find(name);
498  return (iRes == Resources.end())? ResourcePtr_t(): iRes->second;
499  }
500 
502  static Resource_t& Resource(std::string name = "")
503  { return *(Resources.at(name).get()); }
504 
506 
508  static Resource_t& DestroyResource(std::string name = "")
509  { Resources.erase(name); }
510 
511  private:
512  static std::map<std::string, ResourcePtr_t> Resources;
513 
514  }; // class TestSharedGlobalResource<>
515 
516 
517  template <typename RES>
518  std::map<std::string, typename TestSharedGlobalResource<RES>::ResourcePtr_t>
520 
521 
560  template <typename ConfigurationClass>
562 
563  public:
564  using Configuration_t = ConfigurationClass;
565 
573  BasicTesterEnvironment(bool bSetup = true) { if (bSetup) Setup(); }
574 
576 
590  (Configuration_t const& configurer, bool bSetup = true):
591  config(configurer)
592  { if (bSetup) Setup(); }
593  BasicTesterEnvironment(Configuration_t&& configurer, bool bSetup = true):
594  config(configurer)
595  { if (bSetup) Setup(); }
597 
599  virtual ~BasicTesterEnvironment();
600 
601 
604 
606  fhicl::ParameterSet const& Parameters() const { return params; }
607 
609  fhicl::ParameterSet ServiceParameters(std::string service_name) const
610  {
611  return params.get<fhicl::ParameterSet>
612  (config.ServiceParameterSetPath(service_name));
613  }
614 
616  fhicl::ParameterSet TesterParameters(std::string test_name) const
617  {
618  return params.get<fhicl::ParameterSet>
619  (config.TesterParameterSetPath(test_name));
620  }
621 
624  {
625  if (config.MainTesterParameterSetName().empty()) return {};
626  else return TesterParameters(config.MainTesterParameterSetName());
627  }
628 
630 
631  static fhicl::ParameterSet CompileParameterSet(std::string cfg);
632 
633  protected:
634 
636  Configuration_t const& Config() const { return config; }
637 
639  virtual void Setup();
640 
642  virtual void Configure();
643 
649  { return CompileParameterSet(config.DefaultConfiguration()); }
650 
652  virtual void SetupMessageFacility
654  (fhicl::ParameterSet const& pset, std::string appl_name = "") const;
655  virtual void SetupMessageFacility() const
656  { SetupMessageFacility(Parameters(), config.ApplicationName()); }
658 
666  static fhicl::ParameterSet ParseParameters(std::string config_path);
668 
669  private:
671  struct Options_t {
672  bool MessageLevels = false;
673  }; // Options_t
674 
677 
679  void ParseEnvironmentOptions();
680 
682 
683  }; // class BasicTesterEnvironment<>
684 
685 
686 
687  //****************************************************************************
746  template <typename ConfigurationClass>
748  : public BasicTesterEnvironment<ConfigurationClass>
749  {
752 
753  public:
754  // inherit constructors
755  using TesterEnvBase_t::TesterEnvBase_t;
756 
772  template <typename Prov, typename... Args>
773  Prov* SetupProvider(Args&&... args)
774  {
775  if (!providers.setup<Prov>(std::forward<Args>(args)...))
776  throw std::runtime_error("Provider already exists!");
777  return providers.getPointer<Prov>();
778  }
779 
794  template <typename Prov, typename... Args>
795  Prov* SetupProviderFromService(std::string name, Args&&... args)
796  {
797  return SetupProvider<Prov>
798  (this->ServiceParameters(name, std::forward<Args>(args)...));
799  }
800 
813  template <typename Prov>
814  Prov* AcquireProvider(std::unique_ptr<Prov>&& prov)
815  {
816  if (!providers.acquire(std::move(prov)))
817  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
868  = SetupProviderFromService<Prov>(name, std::forward<Args>(args)...);
869  providers.set_alias<Prov, Interface>();
870  return prov;
871  }
872 
887  template <typename Interface, typename Prov>
888  Prov* AcquireProviderFor(std::unique_ptr<Prov>&& prov)
889  {
890  auto prov_ptr = providers.acquire(prov);
891  providers.set_alias<Prov, Interface>();
892  return prov_ptr;
893  }
894 
907  template <typename Prov>
908  Prov* SimpleProviderSetup() { return simpleEnvironmentSetup<Prov>(*this); }
909 
915  template <typename Prov>
917  {
918  if (!providers.erase<Prov>())
919  throw std::runtime_error("Provider not present!");
920  }
921 
923  template <typename Prov>
924  Prov const* Provider() const
925  { return providers.getPointer<Prov>(); }
926 
932  template <typename... Provs>
934  {
936  <TesterEnv_t, lar::ProviderPack<Provs...>, Provs...>
937  ::fill
938  (*this, pack);
939  } // FillProviderPack()
940 
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 
960  protected:
962  }; // class TesterEnvironment<>
963 
964 
965 
992  // Note: this function is expected to be used with automatic type detection;
993  // the rules on "universal references" dictate that if config is a (l-value)
994  // reference, CONFIG itself is a l-value reference. We don't want to create
995  // a TesterEnvironment<Config&>, so we explicitly remove the reference from
996  // CONFIG (decay does that and aso removes the constantness, that we also
997  // don't want to be embedded in CONFIG).
998  template <
999  typename CONFIG,
1000  typename TESTENV = TesterEnvironment<std::decay_t<CONFIG>>,
1001  typename... ARGS
1002  >
1003  TESTENV CreateTesterEnvironment(CONFIG&& config, ARGS... other_args)
1004  {
1005  return TESTENV
1006  (std::forward<CONFIG>(config), std::forward<ARGS>(other_args)...);
1007  }
1008 
1009 
1010 
1011  //****************************************************************************
1012  namespace details {
1013  // Class to implement FHiCL file search.
1014  // This is badly ripped off from ART, but we need to stay out of it
1015  // so I have to replicate that functionality.
1016  // I used the same class name.
1017  class FirstAbsoluteOrLookupWithDotPolicy: public cet::filepath_maker {
1018  public:
1019  FirstAbsoluteOrLookupWithDotPolicy(std::string const& paths):
1020  first(true), after_paths(paths)
1021  {}
1022 
1023  virtual std::string operator() (std::string const& filename);
1024 
1025  void reset() { first = true; }
1026 
1027  private:
1028  bool first;
1029  cet::search_path after_paths;
1030 
1031  }; // class FirstAbsoluteOrLookupWithDotPolicy
1032 
1033 
1034  inline std::string FirstAbsoluteOrLookupWithDotPolicy::operator()
1035  (std::string const &filename)
1036  {
1037  if (first) {
1038  first = false;
1039  if (cet::is_absolute_filepath(filename)) return filename;
1040  return cet::search_path("./:" + after_paths.to_string())
1041  .find_file(filename);
1042  } else {
1043  return after_paths.find_file(filename);
1044  }
1045  } // FirstAbsoluteOrLookupWithDotPolicy::operator()
1046 
1047 
1049  template
1050  <typename TestEnv, typename Pack, typename Prov, typename... Others>
1051  struct ProviderPackFiller<TestEnv, Pack, Prov, Others...> {
1052  static void fill(TestEnv const& env, Pack& pack)
1053  {
1054  pack.set(env.template Provider<Prov>());
1056  } // fill()
1057 
1058  }; // ProviderPackFiller<TestEnv, Pack, Prov, Others...>
1059 
1060  // end-of-recursion specialisation
1061  template <typename TestEnv, typename Pack>
1062  struct ProviderPackFiller<TestEnv, Pack> {
1063  static void fill(TestEnv const&, Pack&) {}
1064  }; // ProviderPackFiller<>
1065 
1066 
1067  } // namespace details
1068 
1069 
1070  //****************************************************************************
1071  template <typename ConfigurationClass>
1073 
1074  mf::LogInfo("Test") << config.ApplicationName() << " completed.";
1075 
1076  } // BasicTesterEnvironment<>::~BasicTesterEnvironment()
1077 
1078 
1083  template <typename ConfigurationClass>
1086  (std::string cfg)
1087  {
1088  fhicl::ParameterSet global_pset;
1089  fhicl::make_ParameterSet(cfg, global_pset);
1090  return global_pset;
1091  } // BasicTesterEnvironment<>::CompileParameterSet()
1092 
1093 
1099  template <typename ConfigurationClass>
1102  (std::string config_path)
1103  {
1104  // configuration file lookup policy
1105  char const* fhicl_env = getenv("FHICL_FILE_PATH");
1106  std::string search_path = fhicl_env? std::string(fhicl_env) + ":": ".:";
1107  details::FirstAbsoluteOrLookupWithDotPolicy policy(search_path);
1108 
1109  // parse a configuration file; obtain intermediate form
1111  fhicl::parse_document(config_path, policy, table);
1112 
1113  // translate into a parameter set
1114  fhicl::ParameterSet global_pset;
1115  fhicl::make_ParameterSet(table, global_pset);
1116 
1117  return global_pset;
1118  } // BasicTesterEnvironment<>::ParseParameters()
1119 
1120 
1135  template <typename ConfigurationClass>
1137  std::string config_path = config.ConfigurationPath();
1138  params = config_path.empty()?
1139  DefaultParameters(): ParseParameters(config_path);
1140  } // BasicTesterEnvironment::Configure()
1141 
1142 
1149  template <typename ConfigurationClass>
1151  (fhicl::ParameterSet const& pset, std::string appl_name /* = "" */) const
1152  {
1153  fhicl::ParameterSet mf_pset;
1154 
1155  if
1156  (!pset.get_if_present(config.ServiceParameterSetPath("message"), mf_pset))
1157  {
1158  mf_pset
1159  = CompileParameterSet(config.DefaultServiceConfiguration("message"));
1160  std::cout << "Using default message facility configuration:\n"
1161  << mf_pset.to_indented_string(1) << std::endl;
1162  } // if no configuration is available
1163 
1164  mf::StartMessageFacility(mf_pset);
1165  if (!appl_name.empty()) mf::SetApplicationName(appl_name);
1166  mf::SetContextIteration("Initialization");
1167  if (options.MessageLevels) {
1168  std::cout << "Printing message levels in 'MessageFacility' category."
1169  << std::endl;
1170 
1171  mf::LogProblem("MessageFacility") << "Error messages are shown.";
1172  mf::LogPrint("MessageFacility") << "Warning messages are shown.";
1173  mf::LogVerbatim("MessageFacility") << "Info messages are shown.";
1174  mf::LogTrace("MessageFacility") << "Debug messages are shown.";
1175  LOG_TRACE("MessageFacility")
1176  << "LOG_TRACE/LOG_DEBUG messages are not compiled away.";
1177  } // if print message levels
1178  mf::LogInfo("MessageFacility") << "MessageFacility started.";
1179  mf::SetContextSinglet("main");
1180  } // BasicTesterEnvironment::SetupMessageFacility()
1181 
1182 
1183 
1184  template <typename ConfigurationClass>
1186  ) {
1187 
1188  //
1189  // get the configuration
1190  //
1191  Configure();
1192 
1193  //
1194  // parse the options specific to the test environment
1195  //
1196  ParseEnvironmentOptions();
1197 
1198  //
1199  // set up the message facility
1200  //
1202 
1203  //
1204  // Optionally print the configuration
1205  //
1206  {
1207  mf::LogInfo msg("Configuration");
1208  msg << "Complete configuration (";
1209  if (config.ConfigurationPath().empty()) msg << "default";
1210  else msg << "'" << config.ConfigurationPath() << "'";
1211  msg << "):\n" << Parameters().to_indented_string(1);
1212  }
1213 
1214 
1215  mf::LogInfo("Test") << config.ApplicationName() << " base setup complete.";
1216 
1217  } // BasicTesterEnvironment<>::Setup()
1218 
1219 
1220 
1221  template <typename ConfigurationClass>
1223 
1224  struct OptionsFromConfig_t {
1225  fhicl::Atom<bool> messageLevels{
1226  fhicl::Name("messageLevels"),
1227  fhicl::Comment("prints a message per level (to verify the visible ones"),
1228  false // default: no
1229  };
1230  fhicl::Atom<bool> printOptions{
1231  fhicl::Name("printOptions"),
1232  fhicl::Comment("prints a the list of options (this is one of them!)"),
1233  false // default: no
1234  };
1235  }; // OptionsFromConfig_t
1236 
1237 
1238  struct ValidationHelper {
1239  static void printDummy(std::ostream& out)
1240  {
1242  (fhicl::Name("test"), fhicl::Comment("Test environment options"))
1243  .print_allowed_configuration(out);
1244  } // printDummy()
1245 
1246  static fhicl::Table<OptionsFromConfig_t> validate
1247  (fhicl::ParameterSet const& pset)
1248  {
1249  try {
1250  return fhicl::Table<OptionsFromConfig_t>(pset, {});
1251  }
1252  catch (...) {
1253  std::cerr << "Error parsing environment test options! Valid options:"
1254  << std::endl;
1255  ValidationHelper::printDummy(std::cerr);
1256  throw;
1257  }
1258  } // validate()
1259  };
1260 
1261 
1262  fhicl::ParameterSet pset = params.get<fhicl::ParameterSet>("test", {});
1263 
1264  // automatically performs validation
1266  (ValidationHelper::validate(pset));
1267 
1268  if (configTable().printOptions()) {
1269  std::cout
1270  << "The following options can be passed to the test environment"
1271  << " by putting them in the \"test: {}\" table of the configuration file:"
1272  << std::endl;
1273  ValidationHelper::printDummy(std::cout);
1274  }
1275 
1276  options.MessageLevels = configTable().messageLevels();
1277 
1278  } // BasicTesterEnvironment<>::ParseEnvironmentOptions()
1279 
1280 
1281 } // namespace testing
1282 
1283 #endif // TEST_UNIT_TEST_BASE_H
Container of service providers accessed by type and optional label.
Definition: ProviderList.h:160
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
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
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.
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
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.
virtual void Configure()
Reads and translates the configuration.
details::CommandLineArguments arguments
command line arguments
Class holding a configuration for a test environment.
Environment for a test.
void FillProviderPack(lar::ProviderPack< Provs... > &pack) const
Fills the specified provider pack with providers.
void make_ParameterSet(intermediate_table const &tbl, ParameterSet &ps)
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)
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.
CommandLineArguments(int argc, char **argv)
Constructor: parses from specified arguments.
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.
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)
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:231
std::string exec_name
name of the test executable (from argv[0])
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)
bool get_if_present(std::string const &key, T &value) const
Definition: ParameterSet.h:208
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.
Container for a list of pointers to providers.
Definition: ProviderPack.h:114
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)
#define LOG_TRACE(id)
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.
void parse_document(std::string const &filename, cet::filepath_maker &maker, intermediate_table &result)
Definition: parse.cc:856
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.