27 #ifndef TEST_UNIT_TEST_BASE_H 28 #define TEST_UNIT_TEST_BASE_H 31 #include "TestUtils/ProviderTestHelpers.h" 32 #include "TestUtils/ProviderList.h" 33 #include "CoreUtils/ProviderPack.h" 54 #include <type_traits> 89 std::vector<std::string>
args;
92 void Clear() { exec_name.clear(); args.clear(); }
99 if (argc == 0)
return;
103 args.resize(argc - 1);
111 <
typename TestEnv,
typename Pack,
typename... Provs>
134 { ParseCommandLine(argc, argv); }
156 auto iPath = test_paths.find(name);
157 return (iPath == test_paths.end())
158 ? (
"physics.analyzers." + name): iPath->second;
167 return MainTesterParameterSetName().empty()
168 ?
"": TesterParameterSetPath(MainTesterParameterSetName());
174 auto iPath = service_paths.find(name);
175 return (iPath == service_paths.end())
176 ? (
"services." + name): iPath->second;
181 {
return analyzers_default_cfg.at(tester_name); }
185 {
return services_default_cfg.at(service_name); }
189 {
return BuildDefaultConfiguration(); }
212 { main_test_name =
name; }
216 { test_paths[test_name] = path; }
221 if (MainTesterParameterSetName().
empty()) {
222 throw std::logic_error
223 (
"Request setting configuration of non-existent main tester");
225 SetTesterParameterSetPath(MainTesterParameterSetName(), path);
230 { service_paths[service_name] = path; }
233 void AddDefaultServiceConfiguration
235 { services_default_cfg[service_name] = service_cfg; }
238 void AddDefaultTesterConfiguration
240 { analyzers_default_cfg[tester_name] = tester_cfg; }
245 if (MainTesterParameterSetName().
empty()) {
246 throw std::logic_error
247 (
"Request adding configuration of non-existent main tester");
249 AddDefaultTesterConfiguration(MainTesterParameterSetName(), tester_cfg);
283 SetConfigurationPath(
arguments.Argument(0));
290 SetMainTesterParameterSetName(
"");
292 AddDefaultServiceConfiguration(
"message",
294 debugModules: [ '*' ] 312 {
return BuildServiceConfiguration(services_default_cfg); }
316 {
return BuildTestConfiguration(analyzers_default_cfg); }
321 return BuildConfiguration(services_default_cfg, analyzers_default_cfg);
330 cfg +=
"\nservices: {";
331 for (
auto const& service_info: services) {
332 cfg +=
"\n " + service_info.first +
": {";
333 cfg +=
"\n" + service_info.second;
334 cfg +=
"\n } # " + service_info.first;
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;
367 cfg += BuildServiceConfiguration(services);
368 cfg += BuildTestConfiguration(modules);
385 template <
typename RES>
397 { Resources[res_name] = res_ptr; }
404 template <
typename... Args>
409 AddSharedResource(res_name, res_ptr);
414 template <
typename... Args>
416 {
return ProvideSharedResource(
std::string(), res_ptr); }
426 if (current_res_ptr.get() != old_res_ptr)
return false;
427 AddSharedResource(res_name, res_ptr);
430 static bool ReplaceSharedResource
432 {
return ReplaceSharedResource(res_name, old_res_ptr.get(), res_ptr); }
437 static bool ReplaceDefaultSharedResource
439 {
return ReplaceSharedResource(
std::string(), old_res_ptr, res_ptr); }
440 static bool ReplaceDefaultSharedResource
442 {
return ReplaceSharedResource(
std::string(), old_res_ptr, res_ptr); }
446 template <
typename... Args>
450 AddSharedResource(res_name, res_ptr);
455 template <
typename... Args>
461 template <
typename... Args>
465 return hasResource(res_name)?
467 CreateResource(res_name, std::forward<Args>(
args)...);
471 template <
typename... Args>
474 return ProposeSharedResource
487 auto iRes = Resources.find(
name);
488 return (iRes != Resources.end()) &&
bool(iRes->second);
494 auto iRes = Resources.find(
name);
495 return (iRes == Resources.end())?
ResourcePtr_t(): iRes->second;
500 {
return *(Resources.at(
name).get()); }
506 { Resources.erase(
name); }
514 template <
typename RES>
515 std::map<std::string, typename TestSharedGlobalResource<RES>::ResourcePtr_t>
557 template <
typename ConfigurationClass>
586 {
if (bSetup) Setup(); }
589 {
if (bSetup) Setup(); }
606 (
config.ServiceParameterSetPath(service_name));
613 (
config.TesterParameterSetPath(test_name));
619 if (
config.MainTesterParameterSetName().empty())
return {};
620 else return TesterParameters(
config.MainTesterParameterSetName());
633 virtual void Setup();
643 {
return CompileParameterSet(
config.DefaultConfiguration()); }
667 void FillArgumentsFromCommandLine();
734 template <
typename ConfigurationClass>
743 using TesterEnvBase_t::TesterEnvBase_t;
760 template <
typename Prov,
typename... Args>
763 if (!providers.setup<Prov>(std::forward<Args>(
args)...))
764 throw std::runtime_error(
"Provider already exists!");
765 return providers.getPointer<Prov>();
781 template <
typename Prov>
783 {
return SetupProvider<Prov>(this->ServiceParameters(name)); }
797 template <
typename Prov>
801 throw std::runtime_error(
"Provider already exists!");
802 return providers.getPointer<Prov>();
821 template <
typename Interface,
typename Prov,
typename... Args>
824 auto prov = SetupProvider<Prov>(std::forward<Args>(
args)...);
825 providers.set_alias<Prov, Interface>();
847 template <
typename Interface,
typename Prov>
850 auto prov = SetupProviderFromService<Prov>(
name);
851 providers.set_alias<Prov, Interface>();
869 template <
typename Interface,
typename Prov>
872 auto prov_ptr = providers.acquire(prov);
873 providers.set_alias<Prov, Interface>();
889 template <
typename Prov>
897 template <
typename Prov>
900 if (!providers.erase<Prov>())
901 throw std::runtime_error(
"Provider not present!");
905 template <
typename Prov>
907 {
return providers.getPointer<Prov>(); }
914 template <
typename... Provs>
933 template <
typename Prov>
936 typename Prov::providers_type pack;
937 FillProviderPack(pack);
988 (std::forward<CONFIG>(
config), std::forward<ARGS>(other_args)...);
1002 first(true), after_paths(paths)
1016 std::string FirstAbsoluteOrLookupWithDotPolicy::operator()
1025 return after_paths.find_file(
filename);
1032 <
typename TestEnv,
typename Pack,
typename Prov,
typename... Others>
1034 static void fill(TestEnv
const& env, Pack& pack)
1036 pack.set(env.template Provider<Prov>());
1043 template <
typename TestEnv,
typename Pack>
1045 static void fill(TestEnv
const&, Pack&) {}
1053 template <
typename ConfigurationClass>
1065 template <
typename ConfigurationClass>
1082 template <
typename ConfigurationClass>
1088 char const* fhicl_env =
getenv(
"FHICL_FILE_PATH");
1121 template <
typename ConfigurationClass>
1124 params = config_path.empty()?
1125 DefaultParameters(): ParseParameters(config_path);
1135 template <
typename ConfigurationClass>
1145 = CompileParameterSet(
config.DefaultServiceConfiguration(
"message"));
1146 std::cout <<
"Using default message facility configuration:\n" 1167 template <
typename ConfigurationClass>
1186 msg <<
"Complete configuration (";
1187 if (
config.ConfigurationPath().empty()) msg <<
"default";
1188 else msg <<
"'" <<
config.ConfigurationPath() <<
"'";
1189 msg <<
"):\n" << Parameters().to_indented_string(1);
1200 #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.
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.
void ParseArguments(int argc, char **argv)
Parses arguments.
void msg(const char *fmt,...)
std::string BuildDefaultConfiguration() const
A string describing the full default parameter set.
std::map< std::string, std::string > PathMap_t
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.
BasicTesterEnvironment(Configuration_t const &cfg_obj, bool bSetup=true)
Setup from a configuration.
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.
static ParameterSet make(intermediate_table const &tbl)
Prov * SetupProvider(Args...args)
Sets a service provider up by calling its testing::setupProvider()
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.
BasicTesterEnvironment(Configuration_t &&cfg_obj, bool bSetup=true)
details::CommandLineArguments arguments
command line arguments
Class holding a configuration for a test environment.
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.
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.
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.
std::string ServiceParameterSetPath(std::string name) const
FHiCL path for the configuration of the service.
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.
Prov * SetupProviderFor(Args...args)
Sets a provider up, recording it as implementation of Interface.
ProviderList providers
list of available providers
static std::map< std::string, ResourcePtr_t > Resources
Container for a list of pointers to providers.
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.
std::vector< std::string > args
command line arguments (from argv[0])
static bool ReplaceSharedResource(std::string res_name, Resource_t const *old_res_ptr, ResourcePtr_t res_ptr)
Adds a shared resource only if it is old_res_ptr.
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::string getenv(std::string const &name)
std::vector< std::string > const & Arguments() const
Returns the list of non-Boost-test arguments on the command line.
std::string exec_name
name of the test executable (from argv[0])
Prov * SetupProviderFromService(std::string name)
Sets a service provider up by calling its testing::setupProvider()
TESTENV CreateTesterEnvironment(CONFIG &&config, ARGS...other_args)
Constructs and returns a TesterEnvironment object.
bool is_absolute_filepath(std::string const &qualified_filename)
std::string to_indented_string() const
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.
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.
Prov * SetupProviderFromServiceFor(std::string name)
Sets a provider up, recording it as implementation of Interface.
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.
std::string ConfigurationPath() const
Path to the configuration file.
intermediate_table parse_document(std::string const &filename, cet::filepath_maker &maker)
std::optional< T > get_if_present(std::string const &key) const
void Configure(string mesg)
std::string appl_name
name of the application
void FillProviderPack(gar::ProviderPack< Provs... > &pack) const
Fills the specified provider pack with providers.
std::string BuildDefaultServiceConfiguration() const
A string describing the full default parameter set.
std::vector< std::string > const & EexcutableArguments() const
Returns the list of non-Boost-test arguments on the command line.
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
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.
QTextStream & endl(QTextStream &s)
virtual void SetupMessageFacility() const
BasicEnvironmentConfiguration(int argc, char **argv)
Constructor: acquires parameters from the command line.