25 #ifndef TEST_UNIT_TEST_BASE_H 26 #define TEST_UNIT_TEST_BASE_H 45 #include "cetlib/filepath_maker.h" 46 #include "cetlib/filesystem.h" 47 #include "cetlib/search_path.h" 55 #include <type_traits> 84 std::string
const&
Argument(
size_t iArg)
const {
return args[iArg]; }
88 std::vector<std::string>
args;
102 if (argc == 0)
return;
106 args.resize(argc - 1);
107 std::copy(argv + 1, argv + argc,
args.begin());
112 template <
typename TestEnv,
typename Pack,
typename... Provs>
134 ParseCommandLine(argc, argv);
160 auto iPath = test_paths.find(name);
161 return (iPath == test_paths.end()) ? (
"physics.analyzers." + name) : iPath->second;
170 return MainTesterParameterSetName().empty() ?
172 TesterParameterSetPath(MainTesterParameterSetName());
178 auto iPath = service_paths.find(name);
179 return (iPath == service_paths.end()) ? (
"services." + name) : iPath->second;
185 return analyzers_default_cfg.at(tester_name);
191 return services_default_cfg.at(service_name);
220 test_paths[test_name] = path;
226 if (MainTesterParameterSetName().
empty()) {
227 throw std::logic_error(
"Request setting configuration of non-existent main tester");
229 SetTesterParameterSetPath(MainTesterParameterSetName(), path);
235 service_paths[service_name] = path;
241 services_default_cfg[service_name] = service_cfg;
247 analyzers_default_cfg[tester_name] = tester_cfg;
253 if (MainTesterParameterSetName().
empty()) {
254 throw std::logic_error(
"Request adding configuration of non-existent main tester");
256 AddDefaultTesterConfiguration(MainTesterParameterSetName(), tester_cfg);
286 arguments.ParseArguments(argc, argv);
287 if (arguments.hasArgument(0)) SetConfigurationPath(arguments.Argument(0));
294 SetMainTesterParameterSetName(
"");
296 AddDefaultServiceConfiguration(
"message",
298 debugModules: [ '*' ] 309 statistics: {type:cout} 317 return BuildServiceConfiguration(services_default_cfg);
323 return BuildTestConfiguration(analyzers_default_cfg);
329 return BuildConfiguration(services_default_cfg, analyzers_default_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;
342 cfg +=
"\n} # services" 351 cfg +=
"\nphysics: {" 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;
358 cfg +=
"\n } # analyzers" 368 cfg += BuildServiceConfiguration(services);
369 cfg += BuildTestConfiguration(modules);
384 template <
typename RES>
397 Resources[res_name] = res_ptr;
403 AddSharedResource(std::string(), res_ptr);
407 template <
typename... Args>
411 AddSharedResource(res_name, res_ptr);
416 template <
typename... Args>
419 return ProvideSharedResource(std::string(), res_ptr);
423 static bool ReplaceSharedResource(std::string res_name,
429 if (current_res_ptr.get() != old_res_ptr)
return false;
430 AddSharedResource(res_name, res_ptr);
437 return ReplaceSharedResource(res_name, old_res_ptr.get(), res_ptr);
445 return ReplaceSharedResource(std::string(), old_res_ptr, res_ptr);
449 return ReplaceSharedResource(std::string(), old_res_ptr, res_ptr);
454 template <
typename... Args>
458 AddSharedResource(res_name, res_ptr);
463 template <
typename... Args>
466 CreateResource(std::string(), std::forward<Args>(
args)...);
470 template <
typename... Args>
474 CreateResource(res_name, std::forward<Args>(
args)...);
478 template <
typename... Args>
481 return ProposeSharedResource(std::string(), std::forward<Args>(
args)...);
493 auto iRes = Resources.find(name);
494 return (iRes != Resources.end()) &&
bool(iRes->second);
500 auto iRes = Resources.find(name);
501 return (iRes == Resources.end()) ?
ResourcePtr_t() : iRes->second;
517 template <
typename RES>
518 std::map<std::string, typename TestSharedGlobalResource<RES>::ResourcePtr_t>
559 template <
typename ConfigurationClass>
626 if (config.MainTesterParameterSetName().empty())
629 return TesterParameters(config.MainTesterParameterSetName());
641 virtual void Setup();
644 virtual void Configure();
652 return CompileParameterSet(config.DefaultConfiguration());
658 std::string appl_name =
"")
const;
678 bool MessageLevels =
false;
685 void ParseEnvironmentOptions();
750 template <
typename ConfigurationClass>
757 using TesterEnvBase_t::TesterEnvBase_t;
774 template <
typename Prov,
typename... Args>
777 if (!providers.setup<Prov>(std::forward<Args>(
args)...))
778 throw std::runtime_error(
"Provider already exists!");
779 return providers.getPointer<Prov>();
796 template <
typename Prov,
typename... Args>
799 return SetupProvider<Prov>(this->ServiceParameters(name, std::forward<Args>(
args)...));
814 template <
typename Prov>
817 if (!providers.acquire(std::move(prov)))
throw std::runtime_error(
"Provider already exists!");
818 return providers.getPointer<Prov>();
837 template <
typename Interface,
typename Prov,
typename... Args>
840 auto prov = SetupProvider<Prov>(std::forward<Args>(
args)...);
841 providers.set_alias<Prov, Interface>();
864 template <
typename Interface,
typename Prov,
typename... Args>
867 auto* prov = SetupProviderFromService<Prov>(name, std::forward<Args>(
args)...);
868 providers.set_alias<Prov, Interface>();
886 template <
typename Interface,
typename Prov>
889 auto prov_ptr = providers.acquire(prov);
890 providers.set_alias<Prov, Interface>();
906 template <
typename Prov>
909 return simpleEnvironmentSetup<Prov>(*this);
917 template <
typename Prov>
920 if (!providers.erase<Prov>())
throw std::runtime_error(
"Provider not present!");
924 template <
typename Prov>
927 return providers.getPointer<Prov>();
935 template <
typename... Provs>
951 template <
typename Prov>
954 typename Prov::providers_type pack;
955 FillProviderPack(pack);
995 template <
typename CONFIG,
1000 return TESTENV(std::forward<CONFIG>(config), std::forward<ARGS>(other_args)...);
1014 virtual std::string operator()(std::string
const& filename);
1028 if (cet::is_absolute_filepath(filename))
return filename;
1029 return cet::search_path(
"./:" + after_paths.to_string()).find_file(filename);
1032 return after_paths.find_file(filename);
1037 template <
typename TestEnv,
typename Pack,
typename Prov,
typename... Others>
1039 static void fill(TestEnv
const& env, Pack& pack)
1041 pack.set(env.template Provider<Prov>());
1048 template <
typename TestEnv,
typename Pack>
1050 static void fill(TestEnv
const&, Pack&) {}
1056 template <
typename ConfigurationClass>
1060 mf::LogInfo(
"Test") << config.ApplicationName() <<
" completed.";
1068 template <
typename ConfigurationClass>
1082 template <
typename ConfigurationClass>
1084 std::string config_path)
1087 char const* fhicl_env = getenv(
"FHICL_FILE_PATH");
1088 std::string search_path = fhicl_env ? std::string(fhicl_env) +
":" :
".:";
1116 template <
typename ConfigurationClass>
1119 std::string config_path = config.ConfigurationPath();
1120 params = config_path.empty() ? DefaultParameters() : ParseParameters(config_path);
1129 template <
typename ConfigurationClass>
1132 std::string appl_name )
const 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" 1145 if (options.MessageLevels) {
1146 std::cout <<
"Printing message levels in 'MessageFacility' category." << std::endl;
1148 mf::LogProblem(
"MessageFacility") <<
"Error messages are shown.";
1149 mf::LogPrint(
"MessageFacility") <<
"Warning messages are shown.";
1151 mf::LogTrace(
"MessageFacility") <<
"Debug messages are shown.";
1153 <<
"MF_LOG_TRACE/MF_LOG_DEBUG messages are not compiled away.";
1155 mf::LogInfo(
"MessageFacility") <<
"MessageFacility started.";
1159 template <
typename ConfigurationClass>
1171 ParseEnvironmentOptions();
1183 msg <<
"Complete configuration (";
1184 if (config.ConfigurationPath().empty())
1187 msg <<
"'" << config.ConfigurationPath() <<
"'";
1188 msg <<
"):\n" << Parameters().to_indented_string(1);
1191 mf::LogInfo(
"Test") << config.ApplicationName() <<
" base setup complete.";
1195 template <
typename ConfigurationClass>
1199 struct OptionsFromConfig_t {
1202 fhicl::Comment(
"prints a message per level (to verify the visible ones"),
1207 fhicl::Comment(
"prints a the list of options (this is one of them!)"),
1212 struct ValidationHelper {
1213 static void printDummy(std::ostream& out)
1217 .print_allowed_configuration(out);
1226 std::cerr <<
"Error parsing environment test options! Valid options:" << std::endl;
1227 ValidationHelper::printDummy(std::cerr);
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:" 1242 ValidationHelper::printDummy(std::cout);
1245 options.MessageLevels = configTable().messageLevels();
1251 #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
virtual std::string operator()(std::string const &filename)
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.
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.
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
FirstAbsoluteOrLookupWithDotPolicy(std::string const &paths)
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()
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)
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.