LArSoft  v10_04_05
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"
42 
43 // CET libraries
44 #include "cetlib/filepath_maker.h"
45 #include "cetlib/search_path.h"
46 
47 // C/C++ standard libraries
48 #include <iostream> // for output before message facility is set up
49 #include <map>
50 #include <memory> // std::unique_ptr<>
51 #include <stdexcept> // std::logic_error
52 #include <string>
53 #include <type_traits> // std::add_rvalue_reference()
54 #include <utility> // std::move(), std::forward()
55 
56 namespace testing {
57 
58  namespace details {
59 
62  public:
65 
67  CommandLineArguments(int argc, char** argv) { ParseArguments(argc, argv); }
68 
70  void ParseArguments(int argc, char** argv);
71 
73  std::string Executable() const { return exec_name; }
74 
76  std::vector<std::string> const& Arguments() const { return args; }
77 
79  bool hasArgument(size_t iArg) const { return iArg < args.size(); }
80 
82  std::string const& Argument(size_t iArg) const { return args[iArg]; }
83 
84  private:
85  std::string exec_name;
86  std::vector<std::string> args;
87 
89  void Clear()
90  {
91  exec_name.clear();
92  args.clear();
93  }
94 
95  }; // class CommandLineArguments
96 
97  inline void CommandLineArguments::ParseArguments(int argc, char** argv)
98  {
99  Clear();
100  if (argc == 0) return;
101 
102  exec_name = argv[0];
103 
104  args.resize(argc - 1);
105  std::copy(argv + 1, argv + argc, args.begin());
106 
107  } // CommandLineArguments:ParseArguments()
108 
109  // forward declaration
110  template <typename TestEnv, typename Pack, typename... Provs>
112 
113  } // namespace details
114 
125 
127  BasicEnvironmentConfiguration() { DefaultInit(); }
128 
131  {
132  ParseCommandLine(argc, argv);
133  }
134 
137  {
138  SetApplicationName(name);
139  }
140 
141  BasicEnvironmentConfiguration(int argc, char** argv, std::string name)
142  : BasicEnvironmentConfiguration(argc, argv)
143  {
144  SetApplicationName(name);
145  }
146 
150  std::string ApplicationName() const { return appl_name; }
151 
153  std::string ConfigurationPath() const { return config_path; }
154 
156  std::string TesterParameterSetPath(std::string name) const
157  {
158  auto iPath = test_paths.find(name);
159  return (iPath == test_paths.end()) ? ("physics.analyzers." + name) : iPath->second;
160  }
161 
163  std::string MainTesterParameterSetName() const { return main_test_name; }
164 
166  std::string MainTesterParameterSetPath() const
167  {
168  return MainTesterParameterSetName().empty() ?
169  "" :
170  TesterParameterSetPath(MainTesterParameterSetName());
171  }
172 
174  std::string ServiceParameterSetPath(std::string name) const
175  {
176  auto iPath = service_paths.find(name);
177  return (iPath == service_paths.end()) ? ("services." + name) : iPath->second;
178  } // ServiceParameterSetPath()
179 
181  std::string DefaultTesterConfiguration(std::string tester_name) const
182  {
183  return analyzers_default_cfg.at(tester_name);
184  }
185 
187  std::string DefaultServiceConfiguration(std::string service_name) const
188  {
189  return services_default_cfg.at(service_name);
190  }
191 
193  std::string DefaultConfiguration() const { return BuildDefaultConfiguration(); }
194 
196  std::string ExecutablePath() const { return arguments.Executable(); }
197 
199  std::vector<std::string> const& EexcutableArguments() const { return arguments.Arguments(); }
200 
202 
205 
207  void SetApplicationName(std::string name) { appl_name = name; }
208 
210  void SetConfigurationPath(std::string path) { config_path = path; }
211 
213  void SetMainTesterParameterSetName(std::string name) { main_test_name = name; }
214 
216  void SetTesterParameterSetPath(std::string test_name, std::string path)
217  {
218  test_paths[test_name] = path;
219  }
220 
222  void SetMainTesterParameterSetPath(std::string path)
223  {
224  if (MainTesterParameterSetName().empty()) {
225  throw std::logic_error("Request setting configuration of non-existent main tester");
226  }
227  SetTesterParameterSetPath(MainTesterParameterSetName(), path);
228  }
229 
231  void SetServiceParameterSetPath(std::string service_name, std::string path)
232  {
233  service_paths[service_name] = path;
234  }
235 
237  void AddDefaultServiceConfiguration(std::string service_name, std::string service_cfg)
238  {
239  services_default_cfg[service_name] = service_cfg;
240  }
241 
243  void AddDefaultTesterConfiguration(std::string tester_name, std::string tester_cfg)
244  {
245  analyzers_default_cfg[tester_name] = tester_cfg;
246  }
247 
249  void AddDefaultTesterConfiguration(std::string tester_cfg)
250  {
251  if (MainTesterParameterSetName().empty()) {
252  throw std::logic_error("Request adding configuration of non-existent main tester");
253  }
254  AddDefaultTesterConfiguration(MainTesterParameterSetName(), tester_cfg);
255  }
256 
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)) SetConfigurationPath(arguments.Argument(0)); // first argument
286  }
287 
289  void DefaultInit()
290  {
291  SetApplicationName(DefaultApplicationName());
292  SetMainTesterParameterSetName("");
293  // a destination which will react to all messages from DEBUG up:
294  AddDefaultServiceConfiguration("message",
295  R"(
296  debugModules: [ '*' ]
297  destinations : {
298  stdout: {
299  type: cout
300  threshold: DEBUG
301  categories: {
302  default: {
303  limit: -1
304  }
305  } // categories
306  } // stdout
307  statistics: {type:cout}
308  } // destinations
309  )");
310  } // DefaultInit()
311 
314  {
315  return BuildServiceConfiguration(services_default_cfg);
316  }
317 
319  std::string BuildDefaultTestConfiguration() const
320  {
321  return BuildTestConfiguration(analyzers_default_cfg);
322  }
323 
325  std::string BuildDefaultConfiguration() const
326  {
327  return BuildConfiguration(services_default_cfg, analyzers_default_cfg);
328  }
329 
331  static std::string BuildServiceConfiguration(ConfigurationMap_t const& services)
332  {
333  std::string cfg;
334  cfg += "\nservices: {";
335  for (auto const& service_info : services) {
336  cfg += "\n " + service_info.first + ": {";
337  cfg += "\n" + service_info.second;
338  cfg += "\n } # " + service_info.first;
339  } // for services
340  cfg += "\n} # services"
341  "\n";
342  return cfg;
343  } // BuildServiceConfiguration()
344 
346  static std::string BuildTestConfiguration(ConfigurationMap_t const& analyzers)
347  {
348  std::string cfg;
349  cfg += "\nphysics: {"
350  "\n analyzers: {";
351  for (auto const& module_info : analyzers) {
352  cfg += "\n " + module_info.first + ": {";
353  cfg += "\n" + module_info.second;
354  cfg += "\n } # " + module_info.first;
355  } // for analyzers
356  cfg += "\n } # analyzers"
357  "\n} # physics";
358  return cfg;
359  } // BuildServiceConfiguration()
360 
362  static std::string BuildConfiguration(ConfigurationMap_t const& services,
363  ConfigurationMap_t const& modules)
364  {
365  std::string cfg;
366  cfg += BuildServiceConfiguration(services);
367  cfg += BuildTestConfiguration(modules);
368  return cfg;
369  } // BuildConfiguration()
370 
371  private:
373 
374  }; // class BasicEnvironmentConfiguration<>
375 
382  template <typename RES>
384  using Resource_t = RES;
385 
386  public:
387  using ResourcePtr_t = std::shared_ptr<Resource_t>;
388 
391 
393  static void AddSharedResource(std::string res_name, ResourcePtr_t res_ptr)
394  {
395  Resources[res_name] = res_ptr;
396  }
397 
400  {
401  AddSharedResource(std::string(), res_ptr);
402  }
403 
405  template <typename... Args>
406  static ResourcePtr_t ProvideSharedResource(std::string res_name, ResourcePtr_t res_ptr)
407  {
408  if (hasResource(res_name)) return ResourcePtr_t();
409  AddSharedResource(res_name, res_ptr);
410  return res_ptr;
411  }
412 
414  template <typename... Args>
416  {
417  return ProvideSharedResource(std::string(), res_ptr);
418  }
419 
421  static bool ReplaceSharedResource(std::string res_name,
423  Resource_t const* old_res_ptr,
424  ResourcePtr_t res_ptr)
425  {
426  ResourcePtr_t current_res_ptr = ShareResource();
427  if (current_res_ptr.get() != old_res_ptr) return false;
428  AddSharedResource(res_name, res_ptr);
429  return true;
430  }
431  static bool ReplaceSharedResource(std::string res_name,
432  ResourcePtr_t old_res_ptr,
433  ResourcePtr_t res_ptr)
434  {
435  return ReplaceSharedResource(res_name, old_res_ptr.get(), res_ptr);
436  }
438 
440  static bool ReplaceDefaultSharedResource(Resource_t const* old_res_ptr, ResourcePtr_t res_ptr)
442  {
443  return ReplaceSharedResource(std::string(), old_res_ptr, res_ptr);
444  }
445  static bool ReplaceDefaultSharedResource(ResourcePtr_t old_res_ptr, ResourcePtr_t res_ptr)
446  {
447  return ReplaceSharedResource(std::string(), old_res_ptr, res_ptr);
448  }
450 
452  template <typename... Args>
453  static ResourcePtr_t CreateResource(std::string res_name, Args&&... args)
454  {
455  ResourcePtr_t res_ptr(new Resource_t(std::forward<Args>(args)...));
456  AddSharedResource(res_name, res_ptr);
457  return res_ptr;
458  }
459 
461  template <typename... Args>
462  static void CreateDefaultResource(Args&&... args)
463  {
464  CreateResource(std::string(), std::forward<Args>(args)...);
465  }
466 
468  template <typename... Args>
469  static ResourcePtr_t ProposeSharedResource(std::string res_name, Args&&... args)
470  {
471  return hasResource(res_name) ? ResourcePtr_t() :
472  CreateResource(res_name, std::forward<Args>(args)...);
473  }
474 
476  template <typename... Args>
478  {
479  return ProposeSharedResource(std::string(), std::forward<Args>(args)...);
480  }
481 
483 
486 
489  static bool hasResource(std::string name = "")
490  {
491  auto iRes = Resources.find(name);
492  return (iRes != Resources.end()) && bool(iRes->second);
493  }
494 
496  static ResourcePtr_t ShareResource(std::string name = "")
497  {
498  auto iRes = Resources.find(name);
499  return (iRes == Resources.end()) ? ResourcePtr_t() : iRes->second;
500  }
501 
503  static Resource_t& Resource(std::string name = "") { return *(Resources.at(name).get()); }
504 
506 
508  static Resource_t& DestroyResource(std::string name = "") { Resources.erase(name); }
509 
510  private:
511  static std::map<std::string, ResourcePtr_t> Resources;
512 
513  }; // class TestSharedGlobalResource<>
514 
515  template <typename RES>
516  std::map<std::string, typename TestSharedGlobalResource<RES>::ResourcePtr_t>
518 
557  template <typename ConfigurationClass>
559 
560  public:
561  using Configuration_t = ConfigurationClass;
562 
570  BasicTesterEnvironment(bool bSetup = true)
571  {
572  if (bSetup) Setup();
573  }
574 
576 
589  BasicTesterEnvironment(Configuration_t const& configurer, bool bSetup = true)
590  : config(configurer)
591  {
592  if (bSetup) Setup();
593  }
594  BasicTesterEnvironment(Configuration_t&& configurer, bool bSetup = true) : config(configurer)
595  {
596  if (bSetup) Setup();
597  }
599 
601  virtual ~BasicTesterEnvironment();
602 
605 
607  fhicl::ParameterSet const& Parameters() const { return params; }
608 
610  fhicl::ParameterSet ServiceParameters(std::string service_name) const
611  {
612  return params.get<fhicl::ParameterSet>(config.ServiceParameterSetPath(service_name));
613  }
614 
616  fhicl::ParameterSet TesterParameters(std::string test_name) const
617  {
618  return params.get<fhicl::ParameterSet>(config.TesterParameterSetPath(test_name));
619  }
620 
623  {
624  if (config.MainTesterParameterSetName().empty())
625  return {};
626  else
627  return TesterParameters(config.MainTesterParameterSetName());
628  }
629 
631 
632  static fhicl::ParameterSet CompileParameterSet(std::string cfg);
633 
634  protected:
636  Configuration_t const& Config() const { return config; }
637 
639  virtual void Setup();
640 
642  virtual void Configure();
643 
649  {
650  return CompileParameterSet(config.DefaultConfiguration());
651  }
652 
654  virtual void SetupMessageFacility(fhicl::ParameterSet const& pset,
656  std::string appl_name = "") const;
657  virtual void SetupMessageFacility() const
658  {
659  SetupMessageFacility(Parameters(), config.ApplicationName());
660  }
662 
670  static fhicl::ParameterSet ParseParameters(std::string config_path);
672 
673  private:
675  struct Options_t {
676  bool MessageLevels = false;
677  }; // Options_t
678 
681 
683  void ParseEnvironmentOptions();
684 
686 
687  }; // class BasicTesterEnvironment<>
688 
689  //****************************************************************************
748  template <typename ConfigurationClass>
749  class TesterEnvironment : public BasicTesterEnvironment<ConfigurationClass> {
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>(this->ServiceParameters(name, std::forward<Args>(args)...));
798  }
799 
812  template <typename Prov>
813  Prov* AcquireProvider(std::unique_ptr<Prov>&& prov)
814  {
815  if (!providers.acquire(std::move(prov))) throw std::runtime_error("Provider already exists!");
816  return providers.getPointer<Prov>();
817  }
818 
835  template <typename Interface, typename Prov, typename... Args>
836  Prov* SetupProviderFor(Args&&... args)
837  {
838  auto prov = SetupProvider<Prov>(std::forward<Args>(args)...);
839  providers.set_alias<Prov, Interface>();
840  return prov;
841  }
842 
862  template <typename Interface, typename Prov, typename... Args>
863  Prov* SetupProviderFromServiceFor(std::string name, Args&&... args)
864  {
865  auto* prov = SetupProviderFromService<Prov>(name, std::forward<Args>(args)...);
866  providers.set_alias<Prov, Interface>();
867  return prov;
868  }
869 
884  template <typename Interface, typename Prov>
885  Prov* AcquireProviderFor(std::unique_ptr<Prov>&& prov)
886  {
887  auto prov_ptr = prov.get();
888  providers.acquire(std::move(prov));
889  providers.set_alias<Prov, Interface>();
890  return prov_ptr;
891  }
892 
905  template <typename Prov>
907  {
908  return simpleEnvironmentSetup<Prov>(*this);
909  }
910 
916  template <typename Prov>
918  {
919  if (!providers.erase<Prov>()) throw std::runtime_error("Provider not present!");
920  }
921 
923  template <typename Prov>
924  Prov const* Provider() const
925  {
926  return providers.getPointer<Prov>();
927  }
928 
934  template <typename... Provs>
936  {
938  pack);
939  } // FillProviderPack()
940 
950  template <typename Prov>
951  typename Prov::providers_type ProviderPackFor() const
952  {
953  typename Prov::providers_type pack;
954  FillProviderPack(pack);
955  return pack;
956  } // ProviderPackFor()
957 
958  protected:
960  }; // class TesterEnvironment<>
961 
988  // Note: this function is expected to be used with automatic type detection;
989  // the rules on "universal references" dictate that if config is a (l-value)
990  // reference, CONFIG itself is a l-value reference. We don't want to create
991  // a TesterEnvironment<Config&>, so we explicitly remove the reference from
992  // CONFIG (decay does that and aso removes the constantness, that we also
993  // don't want to be embedded in CONFIG).
994  template <typename CONFIG,
995  typename TESTENV = TesterEnvironment<std::decay_t<CONFIG>>,
996  typename... ARGS>
997  TESTENV CreateTesterEnvironment(CONFIG&& config, ARGS... other_args)
998  {
999  return TESTENV(std::forward<CONFIG>(config), std::forward<ARGS>(other_args)...);
1000  }
1001 
1002  //****************************************************************************
1003  namespace details {
1005  template <typename TestEnv, typename Pack, typename Prov, typename... Others>
1006  struct ProviderPackFiller<TestEnv, Pack, Prov, Others...> {
1007  static void fill(TestEnv const& env, Pack& pack)
1008  {
1009  pack.set(env.template Provider<Prov>());
1011  } // fill()
1012 
1013  }; // ProviderPackFiller<TestEnv, Pack, Prov, Others...>
1014 
1015  // end-of-recursion specialisation
1016  template <typename TestEnv, typename Pack>
1017  struct ProviderPackFiller<TestEnv, Pack> {
1018  static void fill(TestEnv const&, Pack&) {}
1019  }; // ProviderPackFiller<>
1020 
1021  } // namespace details
1022 
1023  //****************************************************************************
1024  template <typename ConfigurationClass>
1026  {
1027  mf::LogInfo("Test") << config.ApplicationName() << " completed.";
1028  }
1029 
1034  template <typename ConfigurationClass>
1036  std::string cfg)
1037  {
1038  fhicl::ParameterSet global_pset;
1039  global_pset = fhicl::ParameterSet::make(cfg);
1040  return global_pset;
1041  }
1042 
1048  template <typename ConfigurationClass>
1050  std::string config_path)
1051  {
1052  // configuration file lookup policy
1053  char const* fhicl_env = getenv("FHICL_FILE_PATH");
1054  std::string search_path = fhicl_env ? std::string(fhicl_env) + ":" : ".:";
1055  cet::filepath_first_absolute_or_lookup_with_dot policy(search_path);
1056 
1057  // parse a configuration file; obtain intermediate form
1058  auto table = fhicl::parse_document(config_path, policy);
1059  return fhicl::ParameterSet::make(table);
1060  }
1061 
1076  template <typename ConfigurationClass>
1078  {
1079  std::string config_path = config.ConfigurationPath();
1080  params = config_path.empty() ? DefaultParameters() : ParseParameters(config_path);
1081  }
1082 
1089  template <typename ConfigurationClass>
1091  fhicl::ParameterSet const& pset,
1092  std::string appl_name /* = "" */) const
1093  {
1094  fhicl::ParameterSet mf_pset;
1095 
1096  if (!pset.get_if_present(config.ServiceParameterSetPath("message"), mf_pset)) {
1097  mf_pset = CompileParameterSet(config.DefaultServiceConfiguration("message"));
1098  std::cout << "Using default message facility configuration:\n"
1099  << mf_pset.to_indented_string(1) << std::endl;
1100  } // if no configuration is available
1101 
1102  mf::StartMessageFacility(mf_pset);
1103  if (!appl_name.empty()) mf::SetApplicationName(appl_name);
1104  mf::SetContextIteration("Initialization");
1105  if (options.MessageLevels) {
1106  std::cout << "Printing message levels in 'MessageFacility' category." << std::endl;
1107 
1108  mf::LogProblem("MessageFacility") << "Error messages are shown.";
1109  mf::LogPrint("MessageFacility") << "Warning messages are shown.";
1110  mf::LogVerbatim("MessageFacility") << "Info messages are shown.";
1111  mf::LogTrace("MessageFacility") << "Debug messages are shown.";
1112  MF_LOG_TRACE("MessageFacility")
1113  << "MF_LOG_TRACE/MF_LOG_DEBUG messages are not compiled away.";
1114  } // if print message levels
1115  mf::LogInfo("MessageFacility") << "MessageFacility started.";
1116  mf::SetContextSinglet("main");
1117  }
1118 
1119  template <typename ConfigurationClass>
1121  {
1122  Configure();
1123  ParseEnvironmentOptions();
1125 
1126  // Optionally print the configuration
1127  {
1128  mf::LogInfo msg("Configuration");
1129  msg << "Complete configuration (";
1130  if (config.ConfigurationPath().empty())
1131  msg << "default";
1132  else
1133  msg << "'" << config.ConfigurationPath() << "'";
1134  msg << "):\n" << Parameters().to_indented_string(1);
1135  }
1136  mf::LogInfo("Test") << config.ApplicationName() << " base setup complete.";
1137  }
1138 
1139  template <typename ConfigurationClass>
1141  {
1142 
1143  struct OptionsFromConfig_t {
1144  fhicl::Atom<bool> messageLevels{
1145  fhicl::Name("messageLevels"),
1146  fhicl::Comment("prints a message per level (to verify the visible ones"),
1147  false // default: no
1148  };
1149  fhicl::Atom<bool> printOptions{
1150  fhicl::Name("printOptions"),
1151  fhicl::Comment("prints a the list of options (this is one of them!)"),
1152  false // default: no
1153  };
1154  }; // OptionsFromConfig_t
1155 
1156  struct ValidationHelper {
1157  static void printDummy(std::ostream& out)
1158  {
1160  fhicl::Comment("Test environment options"))
1161  .print_allowed_configuration(out);
1162  } // printDummy()
1163 
1164  static fhicl::Table<OptionsFromConfig_t> validate(fhicl::ParameterSet const& pset)
1165  {
1166  try {
1167  return fhicl::Table<OptionsFromConfig_t>(pset, {});
1168  }
1169  catch (...) {
1170  std::cerr << "Error parsing environment test options! Valid options:" << std::endl;
1171  ValidationHelper::printDummy(std::cerr);
1172  throw;
1173  }
1174  } // validate()
1175  };
1176 
1177  fhicl::ParameterSet pset = params.get<fhicl::ParameterSet>("test", {});
1178 
1179  // automatically performs validation
1180  fhicl::Table<OptionsFromConfig_t> configTable(ValidationHelper::validate(pset));
1181 
1182  if (configTable().printOptions()) {
1183  std::cout << "The following options can be passed to the test environment"
1184  << " by putting them in the \"test: {}\" table of the configuration file:"
1185  << std::endl;
1186  ValidationHelper::printDummy(std::cout);
1187  }
1188 
1189  options.MessageLevels = configTable().messageLevels();
1190  } // BasicTesterEnvironment<>::ParseEnvironmentOptions()
1191 
1192 } // namespace testing
1193 
1194 #endif // TEST_UNIT_TEST_BASE_H
Container of service providers accessed by type and optional label.
Definition: ProviderList.h:162
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
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.
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()
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.