26 #ifndef TEST_UNIT_TEST_BASE_H 27 #define TEST_UNIT_TEST_BASE_H 56 #include <type_traits> 65 class CommandLineArguments {
91 std::vector<std::string>
args;
94 void Clear() { exec_name.clear(); args.clear(); }
101 if (argc == 0)
return;
105 args.resize(argc - 1);
113 <
typename TestEnv,
typename Pack,
typename... Provs>
136 { ParseCommandLine(argc, argv); }
158 auto iPath = test_paths.find(name);
159 return (iPath == test_paths.end())
160 ? (
"physics.analyzers." + name): iPath->second;
169 return MainTesterParameterSetName().empty()
170 ?
"": TesterParameterSetPath(MainTesterParameterSetName());
176 auto iPath = service_paths.find(name);
177 return (iPath == service_paths.end())
178 ? (
"services." + name): iPath->second;
183 {
return analyzers_default_cfg.at(tester_name); }
187 {
return services_default_cfg.at(service_name); }
191 {
return BuildDefaultConfiguration(); }
214 { main_test_name =
name; }
218 { test_paths[test_name] = path; }
223 if (MainTesterParameterSetName().
empty()) {
224 throw std::logic_error
225 (
"Request setting configuration of non-existent main tester");
227 SetTesterParameterSetPath(MainTesterParameterSetName(), path);
232 { service_paths[service_name] = path; }
235 void AddDefaultServiceConfiguration
237 { services_default_cfg[service_name] = service_cfg; }
240 void AddDefaultTesterConfiguration
242 { analyzers_default_cfg[tester_name] = tester_cfg; }
247 if (MainTesterParameterSetName().
empty()) {
248 throw std::logic_error
249 (
"Request adding configuration of non-existent main tester");
251 AddDefaultTesterConfiguration(MainTesterParameterSetName(), tester_cfg);
285 SetConfigurationPath(
arguments.Argument(0));
292 SetMainTesterParameterSetName(
"");
294 AddDefaultServiceConfiguration(
"message",
296 debugModules: [ '*' ] 307 statistics: {type:cout} 314 {
return BuildServiceConfiguration(services_default_cfg); }
318 {
return BuildTestConfiguration(analyzers_default_cfg); }
323 return BuildConfiguration(services_default_cfg, analyzers_default_cfg);
332 cfg +=
"\nservices: {";
333 for (
auto const& service_info: services) {
334 cfg +=
"\n " + service_info.first +
": {";
335 cfg +=
"\n" + service_info.second;
336 cfg +=
"\n } # " + service_info.first;
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;
369 cfg += BuildServiceConfiguration(services);
370 cfg += BuildTestConfiguration(modules);
387 template <
typename RES>
399 { Resources[res_name] = res_ptr; }
406 template <
typename... Args>
411 AddSharedResource(res_name, res_ptr);
416 template <
typename... Args>
418 {
return ProvideSharedResource(
std::string(), res_ptr); }
428 if (current_res_ptr.get() != old_res_ptr)
return false;
429 AddSharedResource(res_name, res_ptr);
432 static bool ReplaceSharedResource
434 {
return ReplaceSharedResource(res_name, old_res_ptr.get(), res_ptr); }
439 static bool ReplaceDefaultSharedResource
441 {
return ReplaceSharedResource(
std::string(), old_res_ptr, res_ptr); }
442 static bool ReplaceDefaultSharedResource
444 {
return ReplaceSharedResource(
std::string(), old_res_ptr, res_ptr); }
448 template <
typename... Args>
452 AddSharedResource(res_name, res_ptr);
457 template <
typename... Args>
463 template <
typename... Args>
467 return hasResource(res_name)?
469 CreateResource(res_name, std::forward<Args>(
args)...);
473 template <
typename... Args>
476 return ProposeSharedResource
489 auto iRes = Resources.find(
name);
490 return (iRes != Resources.end()) &&
bool(iRes->second);
496 auto iRes = Resources.find(
name);
497 return (iRes == Resources.end())?
ResourcePtr_t(): iRes->second;
502 {
return *(Resources.at(
name).get()); }
508 { Resources.erase(
name); }
511 static std::map<std::string, ResourcePtr_t> Resources;
516 template <
typename RES>
517 std::map<std::string, typename TestSharedGlobalResource<RES>::ResourcePtr_t>
559 template <
typename ConfigurationClass>
591 {
if (bSetup) Setup(); }
594 {
if (bSetup) Setup(); }
611 (
config.ServiceParameterSetPath(service_name));
618 (
config.TesterParameterSetPath(test_name));
624 if (
config.MainTesterParameterSetName().empty())
return {};
625 else return TesterParameters(
config.MainTesterParameterSetName());
638 virtual void Setup();
648 {
return CompileParameterSet(
config.DefaultConfiguration()); }
671 bool MessageLevels =
false;
678 void ParseEnvironmentOptions();
745 template <
typename ConfigurationClass>
754 using TesterEnvBase_t::TesterEnvBase_t;
771 template <
typename Prov,
typename... Args>
774 if (!providers.setup<Prov>(std::forward<Args>(
args)...))
775 throw std::runtime_error(
"Provider already exists!");
776 return providers.getPointer<Prov>();
793 template <
typename Prov,
typename... Args>
796 return SetupProvider<Prov>
797 (this->ServiceParameters(name, std::forward<Args>(
args)...));
812 template <
typename Prov>
816 throw std::runtime_error(
"Provider already exists!");
817 return providers.getPointer<Prov>();
836 template <
typename Interface,
typename Prov,
typename... Args>
839 auto prov = SetupProvider<Prov>(std::forward<Args>(
args)...);
840 providers.set_alias<Prov, Interface>();
863 template <
typename Interface,
typename Prov,
typename... Args>
867 = 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>
914 template <
typename Prov>
917 if (!providers.erase<Prov>())
918 throw std::runtime_error(
"Provider not present!");
922 template <
typename Prov>
924 {
return providers.getPointer<Prov>(); }
931 template <
typename... Provs>
950 template <
typename Prov>
953 typename Prov::providers_type pack;
954 FillProviderPack(pack);
1005 (std::forward<CONFIG>(
config), std::forward<ARGS>(other_args)...);
1019 first(true), after_paths(paths)
1033 inline std::string FirstAbsoluteOrLookupWithDotPolicy::operator()
1042 return after_paths.find_file(
filename);
1049 <
typename TestEnv,
typename Pack,
typename Prov,
typename... Others>
1051 static void fill(TestEnv
const& env, Pack& pack)
1053 pack.set(env.template Provider<Prov>());
1060 template <
typename TestEnv,
typename Pack>
1062 static void fill(TestEnv
const&, Pack&) {}
1070 template <
typename ConfigurationClass>
1082 template <
typename ConfigurationClass>
1098 template <
typename ConfigurationClass>
1104 char const* fhicl_env =
getenv(
"FHICL_FILE_PATH");
1134 template <
typename ConfigurationClass>
1137 params = config_path.empty()?
1138 DefaultParameters(): ParseParameters(config_path);
1148 template <
typename ConfigurationClass>
1158 = CompileParameterSet(
config.DefaultServiceConfiguration(
"message"));
1159 std::cout <<
"Using default message facility configuration:\n" 1167 std::cout <<
"Printing message levels in 'MessageFacility' category." 1170 mf::LogProblem(
"MessageFacility") <<
"Error messages are shown.";
1171 mf::LogPrint(
"MessageFacility") <<
"Warning messages are shown.";
1173 mf::LogTrace(
"MessageFacility") <<
"Debug messages are shown.";
1175 <<
"MF_LOG_TRACE/MF_LOG_DEBUG messages are not compiled away.";
1177 mf::LogInfo(
"MessageFacility") <<
"MessageFacility started.";
1183 template <
typename ConfigurationClass>
1195 ParseEnvironmentOptions();
1207 msg <<
"Complete configuration (";
1208 if (
config.ConfigurationPath().empty()) msg <<
"default";
1209 else msg <<
"'" <<
config.ConfigurationPath() <<
"'";
1210 msg <<
"):\n" << Parameters().to_indented_string(1);
1220 template <
typename ConfigurationClass>
1223 struct OptionsFromConfig_t {
1226 fhicl::Comment(
"prints a message per level (to verify the visible ones"),
1231 fhicl::Comment(
"prints a the list of options (this is one of them!)"),
1237 struct ValidationHelper {
1238 static void printDummy(std::ostream& out)
1252 std::cerr <<
"Error parsing environment test options! Valid options:" 1254 ValidationHelper::printDummy(std::cerr);
1267 if (configTable().printOptions()) {
1269 <<
"The following options can be passed to the test environment" 1270 <<
" by putting them in the \"test: {}\" table of the configuration file:" 1272 ValidationHelper::printDummy(std::cout);
1275 options.MessageLevels = configTable().messageLevels();
1282 #endif // TEST_UNIT_TEST_BASE_H
Container of service providers accessed by type and optional label.
fhicl::ParameterSet const & Parameters() const
Returns the full configuration.
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
static void fill(TestEnv const &env, Pack &pack)
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
Options_t options
options for the test environment
void SetApplicationName(std::string name)
Sets the name of the application.
std::map< std::string, std::string > ConfigurationMap_t
std::string DefaultConfiguration() const
A string describing the full default parameter set.
Prov * SetupProviderFromService(std::string name, Args &&...args)
Sets a service provider up by calling its testing::setupProvider()
virtual fhicl::ParameterSet DefaultParameters() const
Creates a full configuration for the test.
fhicl::ParameterSet TesterParameters(std::string test_name) const
Returns the configuration of the specified test.
static ParameterSet make(intermediate_table const &tbl)
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
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.
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.
ChannelGroupService::Name Name
virtual void Configure()
Reads and translates the configuration.
Class holding a configuration for a test environment.
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.
Data structure containing constant pointers to classes.
std::string TesterParameterSetPath(std::string name) const
FHiCL path for the configuration of the test algorithm.
static std::string DefaultApplicationName()
Returns the default test name.
Prov * SetupProviderFromServiceFor(std::string name, Args &&...args)
Sets a provider up, recording it as implementation of Interface.
Configuration_t const & Config() const
Returns a read-only version of the configuration.
Prov * AcquireProvider(std::unique_ptr< Prov > &&prov)
Acquires a service provider.
static void AddDefaultSharedResource(ResourcePtr_t res_ptr)
Adds a shared resource to the resource registry (empty name)
void ParseCommandLine(int argc, char **argv)
Extracts arguments from the command line, uses first one as config path.
void SetContextIteration(string const &val)
CommandLineArguments()
Constructor: automatically parses from Boost arguments.
Test environment options.
std::string MainTesterParameterSetPath() const
FHiCL path for the configuration of the test algorithm.
CommandLineArguments(int argc, char **argv)
Constructor: parses from specified arguments.
void SetupMessageFacility(fhicl::ParameterSet const &pset, std::string applName="standalone")
Sets up the message facility service.
MaybeLogger_< ELseverityLevel::ELsev_error, true > LogProblem
std::string ServiceParameterSetPath(std::string name) const
FHiCL path for the configuration of the service.
void StartMessageFacility(fhicl::ParameterSet const &pset, string const &applicationName)
Prov::providers_type ProviderPackFor() const
Returns a provider pack for the specified provider.
static Resource_t & DestroyResource(std::string name="")
Destroys the specified resource (does nothing if no such resource)
Prov * SimpleProviderSetup()
Oversimplified provider setup.
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 MainTesterParameterSetName() const
Name of the test algorithm instance.
void ParseEnvironmentOptions()
Parses the configuration, looking for the test environment options.
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 print_allowed_configuration(LibraryInfo const &li, std::string const &prefix, std::string const &type_spec)
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])
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
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.
def validate(nxgraph, desc)
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)
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
void Configure(string mesg)
void SetContextSinglet(string const &val)
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.
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
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.
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
QTextStream & endl(QTextStream &s)
virtual void SetupMessageFacility() const
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.