25 #ifndef TEST_UNIT_TEST_BASE_H 26 #define TEST_UNIT_TEST_BASE_H 44 #include "cetlib/filepath_maker.h" 45 #include "cetlib/search_path.h" 53 #include <type_traits> 82 std::string
const&
Argument(
size_t iArg)
const {
return args[iArg]; }
86 std::vector<std::string>
args;
100 if (argc == 0)
return;
104 args.resize(argc - 1);
105 std::copy(argv + 1, argv + argc,
args.begin());
110 template <
typename TestEnv,
typename Pack,
typename... Provs>
132 ParseCommandLine(argc, argv);
158 auto iPath = test_paths.find(name);
159 return (iPath == test_paths.end()) ? (
"physics.analyzers." + name) : iPath->second;
168 return MainTesterParameterSetName().empty() ?
170 TesterParameterSetPath(MainTesterParameterSetName());
176 auto iPath = service_paths.find(name);
177 return (iPath == service_paths.end()) ? (
"services." + name) : iPath->second;
183 return analyzers_default_cfg.at(tester_name);
189 return services_default_cfg.at(service_name);
218 test_paths[test_name] = path;
224 if (MainTesterParameterSetName().
empty()) {
225 throw std::logic_error(
"Request setting configuration of non-existent main tester");
227 SetTesterParameterSetPath(MainTesterParameterSetName(), path);
233 service_paths[service_name] = path;
239 services_default_cfg[service_name] = service_cfg;
245 analyzers_default_cfg[tester_name] = tester_cfg;
251 if (MainTesterParameterSetName().
empty()) {
252 throw std::logic_error(
"Request adding configuration of non-existent main tester");
254 AddDefaultTesterConfiguration(MainTesterParameterSetName(), tester_cfg);
284 arguments.ParseArguments(argc, argv);
285 if (arguments.hasArgument(0)) SetConfigurationPath(arguments.Argument(0));
292 SetMainTesterParameterSetName(
"");
294 AddDefaultServiceConfiguration(
"message",
296 debugModules: [ '*' ] 307 statistics: {type:cout} 315 return BuildServiceConfiguration(services_default_cfg);
321 return BuildTestConfiguration(analyzers_default_cfg);
327 return BuildConfiguration(services_default_cfg, analyzers_default_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;
340 cfg +=
"\n} # services" 349 cfg +=
"\nphysics: {" 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;
356 cfg +=
"\n } # analyzers" 366 cfg += BuildServiceConfiguration(services);
367 cfg += BuildTestConfiguration(modules);
382 template <
typename RES>
395 Resources[res_name] = res_ptr;
401 AddSharedResource(std::string(), res_ptr);
405 template <
typename... Args>
409 AddSharedResource(res_name, res_ptr);
414 template <
typename... Args>
417 return ProvideSharedResource(std::string(), res_ptr);
421 static bool ReplaceSharedResource(std::string res_name,
427 if (current_res_ptr.get() != old_res_ptr)
return false;
428 AddSharedResource(res_name, res_ptr);
435 return ReplaceSharedResource(res_name, old_res_ptr.get(), res_ptr);
443 return ReplaceSharedResource(std::string(), old_res_ptr, res_ptr);
447 return ReplaceSharedResource(std::string(), old_res_ptr, res_ptr);
452 template <
typename... Args>
456 AddSharedResource(res_name, res_ptr);
461 template <
typename... Args>
464 CreateResource(std::string(), std::forward<Args>(
args)...);
468 template <
typename... Args>
472 CreateResource(res_name, std::forward<Args>(
args)...);
476 template <
typename... Args>
479 return ProposeSharedResource(std::string(), std::forward<Args>(
args)...);
491 auto iRes = Resources.find(name);
492 return (iRes != Resources.end()) &&
bool(iRes->second);
498 auto iRes = Resources.find(name);
499 return (iRes == Resources.end()) ?
ResourcePtr_t() : iRes->second;
515 template <
typename RES>
516 std::map<std::string, typename TestSharedGlobalResource<RES>::ResourcePtr_t>
557 template <
typename ConfigurationClass>
624 if (config.MainTesterParameterSetName().empty())
627 return TesterParameters(config.MainTesterParameterSetName());
639 virtual void Setup();
642 virtual void Configure();
650 return CompileParameterSet(config.DefaultConfiguration());
656 std::string appl_name =
"")
const;
676 bool MessageLevels =
false;
683 void ParseEnvironmentOptions();
748 template <
typename ConfigurationClass>
755 using TesterEnvBase_t::TesterEnvBase_t;
772 template <
typename Prov,
typename... Args>
775 if (!providers.setup<Prov>(std::forward<Args>(
args)...))
776 throw std::runtime_error(
"Provider already exists!");
777 return providers.getPointer<Prov>();
794 template <
typename Prov,
typename... Args>
797 return SetupProvider<Prov>(this->ServiceParameters(name, std::forward<Args>(
args)...));
812 template <
typename Prov>
815 if (!providers.acquire(std::move(prov)))
throw std::runtime_error(
"Provider already exists!");
816 return providers.getPointer<Prov>();
835 template <
typename Interface,
typename Prov,
typename... Args>
838 auto prov = SetupProvider<Prov>(std::forward<Args>(
args)...);
839 providers.set_alias<Prov, Interface>();
862 template <
typename Interface,
typename Prov,
typename... Args>
865 auto* prov = SetupProviderFromService<Prov>(name, std::forward<Args>(
args)...);
866 providers.set_alias<Prov, Interface>();
884 template <
typename Interface,
typename Prov>
887 auto prov_ptr = prov.get();
888 providers.acquire(std::move(prov));
889 providers.set_alias<Prov, Interface>();
905 template <
typename Prov>
908 return simpleEnvironmentSetup<Prov>(*this);
916 template <
typename Prov>
919 if (!providers.erase<Prov>())
throw std::runtime_error(
"Provider not present!");
923 template <
typename Prov>
926 return providers.getPointer<Prov>();
934 template <
typename... Provs>
950 template <
typename Prov>
953 typename Prov::providers_type pack;
954 FillProviderPack(pack);
994 template <
typename CONFIG,
999 return TESTENV(std::forward<CONFIG>(config), std::forward<ARGS>(other_args)...);
1005 template <
typename TestEnv,
typename Pack,
typename Prov,
typename... Others>
1007 static void fill(TestEnv
const& env, Pack& pack)
1009 pack.set(env.template Provider<Prov>());
1016 template <
typename TestEnv,
typename Pack>
1018 static void fill(TestEnv
const&, Pack&) {}
1024 template <
typename ConfigurationClass>
1027 mf::LogInfo(
"Test") << config.ApplicationName() <<
" completed.";
1034 template <
typename ConfigurationClass>
1048 template <
typename ConfigurationClass>
1050 std::string config_path)
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);
1076 template <
typename ConfigurationClass>
1079 std::string config_path = config.ConfigurationPath();
1080 params = config_path.empty() ? DefaultParameters() : ParseParameters(config_path);
1089 template <
typename ConfigurationClass>
1092 std::string appl_name )
const 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" 1105 if (options.MessageLevels) {
1106 std::cout <<
"Printing message levels in 'MessageFacility' category." << std::endl;
1108 mf::LogProblem(
"MessageFacility") <<
"Error messages are shown.";
1109 mf::LogPrint(
"MessageFacility") <<
"Warning messages are shown.";
1111 mf::LogTrace(
"MessageFacility") <<
"Debug messages are shown.";
1113 <<
"MF_LOG_TRACE/MF_LOG_DEBUG messages are not compiled away.";
1115 mf::LogInfo(
"MessageFacility") <<
"MessageFacility started.";
1119 template <
typename ConfigurationClass>
1123 ParseEnvironmentOptions();
1129 msg <<
"Complete configuration (";
1130 if (config.ConfigurationPath().empty())
1133 msg <<
"'" << config.ConfigurationPath() <<
"'";
1134 msg <<
"):\n" << Parameters().to_indented_string(1);
1136 mf::LogInfo(
"Test") << config.ApplicationName() <<
" base setup complete.";
1139 template <
typename ConfigurationClass>
1143 struct OptionsFromConfig_t {
1146 fhicl::Comment(
"prints a message per level (to verify the visible ones"),
1151 fhicl::Comment(
"prints a the list of options (this is one of them!)"),
1156 struct ValidationHelper {
1157 static void printDummy(std::ostream& out)
1161 .print_allowed_configuration(out);
1170 std::cerr <<
"Error parsing environment test options! Valid options:" << std::endl;
1171 ValidationHelper::printDummy(std::cerr);
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:" 1186 ValidationHelper::printDummy(std::cout);
1189 options.MessageLevels = configTable().messageLevels();
1194 #endif // TEST_UNIT_TEST_BASE_H Container of service providers accessed by type and optional label.
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
static void fill(TestEnv const &env, Pack &pack)
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'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)
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.
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.
Test environment options.
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
std::string exec_name
name of the test executable (from argv[0])
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)
Container for a list of pointers to providers.
std::optional< T > get_if_present(std::string const &key) const
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
static void fill(TestEnv const &, Pack &)
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.
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.