LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
testing::BasicTesterEnvironment< ConfigurationClass > Class Template Reference

Environment for a test. More...

#include "unit_test_base.h"

Inheritance diagram for testing::BasicTesterEnvironment< ConfigurationClass >:
testing::TesterEnvironment< ConfigurationClass >

Classes

struct  Options_t
 Test environment options. More...
 

Public Types

using Configuration_t = ConfigurationClass
 

Public Member Functions

 BasicTesterEnvironment (bool bSetup=true)
 Constructor: sets everything up and declares the test started. More...
 
virtual ~BasicTesterEnvironment ()
 Destructor: closing remarks. More...
 
 BasicTesterEnvironment (Configuration_t const &configurer, bool bSetup=true)
 Setup from a configuration. More...
 
 BasicTesterEnvironment (Configuration_t &&configurer, bool bSetup=true)
 Setup from a configuration. More...
 
Configuration retrieval
fhicl::ParameterSet const & Parameters () const
 Returns the full configuration. More...
 
fhicl::ParameterSet ServiceParameters (std::string service_name) const
 Returns the configuration of the specified service. More...
 
fhicl::ParameterSet TesterParameters (std::string test_name) const
 Returns the configuration of the specified test. More...
 
fhicl::ParameterSet TesterParameters () const
 Returns the configuration of the main test (undefined if no main test) More...
 

Static Public Member Functions

static fhicl::ParameterSet CompileParameterSet (std::string cfg)
 Compiles a parameter set from a string. More...
 

Protected Member Functions

Configuration_t const & Config () const
 Returns a read-only version of the configuration. More...
 
virtual void Setup ()
 The complete initialization, ran at construction by default. More...
 
virtual void Configure ()
 Reads and translates the configuration. More...
 
virtual fhicl::ParameterSet DefaultParameters () const
 Creates a full configuration for the test. More...
 
virtual void SetupMessageFacility (fhicl::ParameterSet const &pset, std::string appl_name="") const
 Sets up the message facility. More...
 
virtual void SetupMessageFacility () const
 Sets up the message facility. More...
 

Static Protected Member Functions

static fhicl::ParameterSet ParseParameters (std::string config_path)
 Fills the test configuration from file or from default. More...
 

Private Member Functions

void ParseEnvironmentOptions ()
 Parses the configuration, looking for the test environment options. More...
 

Private Attributes

Configuration_t config
 instance of the configurer More...
 
Options_t options
 options for the test environment More...
 
fhicl::ParameterSet params
 full configuration of the test More...
 

Detailed Description

template<typename ConfigurationClass>
class testing::BasicTesterEnvironment< ConfigurationClass >

Environment for a test.


Template Parameters
ConfigurationClassa class providing compile-time configuration

The test environment is set up on construction.

The environment provides:

This class or a derived one can be used as global fixture for unit tests.

Unfortunately Boost does not give any control on the initialization of the object, so everything must be ready to go as hard coded. The ConfigurationClass class tries to alleviate that. That is another, small static class that BasicTesterEnvironment uses to get its parameters.

The requirements for the ConfigurationClass are:

  • std::string ApplicationName(): the application name
  • std::string ConfigurationPath(): path to the configuration file
  • std::string MainTesterParameterSetName(): name of the configuration of the main test (commodity)
  • std::string DefaultTesterConfiguration() returning a FHiCL string to be parsed to extract the default test configuration

Whether the configuration comes from a file or from the two provided defaults, it is always expected within the parameter set paths: the default configuration must also contain that path.

Note that there is no room for polymorphism here since the setup happens on construction. Some methods are declared virtual in order to allow to tweak some steps of the set up, but it's not trivial to create a derived class that works correctly: the derived class must declare a new default constructor, and that default constructor must call the protected constructor (BasicTesterEnvironment<ConfigurationClass>(no_setup))

Definition at line 561 of file unit_test_base.h.

Member Typedef Documentation

template<typename ConfigurationClass >
using testing::BasicTesterEnvironment< ConfigurationClass >::Configuration_t = ConfigurationClass

Definition at line 564 of file unit_test_base.h.

Constructor & Destructor Documentation

template<typename ConfigurationClass >
testing::BasicTesterEnvironment< ConfigurationClass >::BasicTesterEnvironment ( bool  bSetup = true)
inline

Constructor: sets everything up and declares the test started.

Parameters
bSetup(default: true) call Setup() after construction

The configuration is from a default-constructed ConfigurationClass. This is suitable for use as Boost unit test fixture.

Definition at line 573 of file unit_test_base.h.

573 { if (bSetup) Setup(); }
virtual void Setup()
The complete initialization, ran at construction by default.
template<typename ConfigurationClass >
testing::BasicTesterEnvironment< ConfigurationClass >::BasicTesterEnvironment ( Configuration_t const &  configurer,
bool  bSetup = true 
)
inline

Setup from a configuration.

Parameters
configureran instance of ConfigurationClass
bSetup(default: true) call Setup() after construction

The configuration is from the specified configurer class.

This constructor allows to use a non-default-constructed configuration. This can't be used (at best of my knowledge) when using this class as Boost unit test fixture.

In the r-value-reference constructor, the configurer is moved.

Definition at line 590 of file unit_test_base.h.

590  :
591  config(configurer)
592  { if (bSetup) Setup(); }
Configuration_t config
instance of the configurer
virtual void Setup()
The complete initialization, ran at construction by default.
template<typename ConfigurationClass >
testing::BasicTesterEnvironment< ConfigurationClass >::BasicTesterEnvironment ( Configuration_t &&  configurer,
bool  bSetup = true 
)
inline

Setup from a configuration.

Parameters
configureran instance of ConfigurationClass
bSetup(default: true) call Setup() after construction

The configuration is from the specified configurer class.

This constructor allows to use a non-default-constructed configuration. This can't be used (at best of my knowledge) when using this class as Boost unit test fixture.

In the r-value-reference constructor, the configurer is moved.

Definition at line 593 of file unit_test_base.h.

593  :
594  config(configurer)
595  { if (bSetup) Setup(); }
Configuration_t config
instance of the configurer
virtual void Setup()
The complete initialization, ran at construction by default.
template<typename ConfigurationClass >
testing::BasicTesterEnvironment< ConfigurationClass >::~BasicTesterEnvironment ( )
virtual

Destructor: closing remarks.

Definition at line 1072 of file unit_test_base.h.

References testing::BasicTesterEnvironment< ConfigurationClass >::CompileParameterSet().

1072  {
1073 
1074  mf::LogInfo("Test") << config.ApplicationName() << " completed.";
1075 
1076  } // BasicTesterEnvironment<>::~BasicTesterEnvironment()
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
Configuration_t config
instance of the configurer

Member Function Documentation

template<typename ConfigurationClass >
fhicl::ParameterSet testing::BasicTesterEnvironment< ConfigurationClass >::CompileParameterSet ( std::string  cfg)
static

Compiles a parameter set from a string.


Returns
a parameters set with the complete configuration

Definition at line 1086 of file unit_test_base.h.

References fhicl::make_ParameterSet(), and testing::BasicTesterEnvironment< ConfigurationClass >::ParseParameters().

Referenced by testing::BasicTesterEnvironment< ConfigurationClass >::~BasicTesterEnvironment().

1087  {
1088  fhicl::ParameterSet global_pset;
1089  fhicl::make_ParameterSet(cfg, global_pset);
1090  return global_pset;
1091  } // BasicTesterEnvironment<>::CompileParameterSet()
void make_ParameterSet(intermediate_table const &tbl, ParameterSet &ps)
template<typename ConfigurationClass >
Configuration_t const& testing::BasicTesterEnvironment< ConfigurationClass >::Config ( ) const
inlineprotected

Returns a read-only version of the configuration.

Definition at line 636 of file unit_test_base.h.

636 { return config; }
Configuration_t config
instance of the configurer
template<typename ConfigurationClass >
void testing::BasicTesterEnvironment< ConfigurationClass >::Configure ( )
protectedvirtual

Reads and translates the configuration.

Fills the configuration.


The complete configuration (message facility and services) is read and saved, hence accessible by Parameters() method.

The configuration file path is taken by default from the first argument of the test. If that first argument is not present or empty, the default configuration path is received from the configurer. If the configuration path is still empty, a hard-coded configuration is used; otherwise, the FHiCL file specified in that path is parsed and used as full configuration.

Definition at line 1136 of file unit_test_base.h.

References testing::BasicTesterEnvironment< ConfigurationClass >::SetupMessageFacility().

1136  {
1137  std::string config_path = config.ConfigurationPath();
1138  params = config_path.empty()?
1139  DefaultParameters(): ParseParameters(config_path);
1140  } // BasicTesterEnvironment::Configure()
fhicl::ParameterSet params
full configuration of the test
static fhicl::ParameterSet ParseParameters(std::string config_path)
Fills the test configuration from file or from default.
virtual fhicl::ParameterSet DefaultParameters() const
Creates a full configuration for the test.
Configuration_t config
instance of the configurer
template<typename ConfigurationClass >
virtual fhicl::ParameterSet testing::BasicTesterEnvironment< ConfigurationClass >::DefaultParameters ( ) const
inlineprotectedvirtual

Creates a full configuration for the test.

Returns
a parameters set with the complete configuration

Definition at line 648 of file unit_test_base.h.

References lar::standalone::SetupMessageFacility().

649  { return CompileParameterSet(config.DefaultConfiguration()); }
Configuration_t config
instance of the configurer
static fhicl::ParameterSet CompileParameterSet(std::string cfg)
Compiles a parameter set from a string.
template<typename ConfigurationClass >
fhicl::ParameterSet const& testing::BasicTesterEnvironment< ConfigurationClass >::Parameters ( ) const
inline

Returns the full configuration.

Definition at line 606 of file unit_test_base.h.

606 { return params; }
fhicl::ParameterSet params
full configuration of the test
template<typename ConfigurationClass >
void testing::BasicTesterEnvironment< ConfigurationClass >::ParseEnvironmentOptions ( )
private

Parses the configuration, looking for the test environment options.

Definition at line 1222 of file unit_test_base.h.

References fhicl::ParameterSet::get().

1222  {
1223 
1224  struct OptionsFromConfig_t {
1225  fhicl::Atom<bool> messageLevels{
1226  fhicl::Name("messageLevels"),
1227  fhicl::Comment("prints a message per level (to verify the visible ones"),
1228  false // default: no
1229  };
1230  fhicl::Atom<bool> printOptions{
1231  fhicl::Name("printOptions"),
1232  fhicl::Comment("prints a the list of options (this is one of them!)"),
1233  false // default: no
1234  };
1235  }; // OptionsFromConfig_t
1236 
1237 
1238  struct ValidationHelper {
1239  static void printDummy(std::ostream& out)
1240  {
1242  (fhicl::Name("test"), fhicl::Comment("Test environment options"))
1243  .print_allowed_configuration(out);
1244  } // printDummy()
1245 
1246  static fhicl::Table<OptionsFromConfig_t> validate
1247  (fhicl::ParameterSet const& pset)
1248  {
1249  try {
1250  return fhicl::Table<OptionsFromConfig_t>(pset, {});
1251  }
1252  catch (...) {
1253  std::cerr << "Error parsing environment test options! Valid options:"
1254  << std::endl;
1255  ValidationHelper::printDummy(std::cerr);
1256  throw;
1257  }
1258  } // validate()
1259  };
1260 
1261 
1262  fhicl::ParameterSet pset = params.get<fhicl::ParameterSet>("test", {});
1263 
1264  // automatically performs validation
1266  (ValidationHelper::validate(pset));
1267 
1268  if (configTable().printOptions()) {
1269  std::cout
1270  << "The following options can be passed to the test environment"
1271  << " by putting them in the \"test: {}\" table of the configuration file:"
1272  << std::endl;
1273  ValidationHelper::printDummy(std::cout);
1274  }
1275 
1276  options.MessageLevels = configTable().messageLevels();
1277 
1278  } // BasicTesterEnvironment<>::ParseEnvironmentOptions()
fhicl::ParameterSet params
full configuration of the test
Options_t options
options for the test environment
T get(std::string const &key) const
Definition: ParameterSet.h:231
bool MessageLevels
print message levels on screen
template<typename ConfigurationClass >
fhicl::ParameterSet testing::BasicTesterEnvironment< ConfigurationClass >::ParseParameters ( std::string  config_path)
staticprotected

Fills the test configuration from file or from default.

Returns the configuration from a FHiCL file.

If a FHiCL configuration file is specified, the configuration of the test is read from it according to the parameter set path of the test. Otherwise, it is parsed from the default one provided by the configurer.Parses from file and returns a FHiCL data structure


Parameters
config_pathfull path of the FHiCL configuration file
Returns
a parameters set with the complete configuration from the file

Definition at line 1102 of file unit_test_base.h.

References fhicl::make_ParameterSet(), and fhicl::parse_document().

Referenced by testing::BasicTesterEnvironment< ConfigurationClass >::CompileParameterSet().

1103  {
1104  // configuration file lookup policy
1105  char const* fhicl_env = getenv("FHICL_FILE_PATH");
1106  std::string search_path = fhicl_env? std::string(fhicl_env) + ":": ".:";
1107  details::FirstAbsoluteOrLookupWithDotPolicy policy(search_path);
1108 
1109  // parse a configuration file; obtain intermediate form
1111  fhicl::parse_document(config_path, policy, table);
1112 
1113  // translate into a parameter set
1114  fhicl::ParameterSet global_pset;
1115  fhicl::make_ParameterSet(table, global_pset);
1116 
1117  return global_pset;
1118  } // BasicTesterEnvironment<>::ParseParameters()
void make_ParameterSet(intermediate_table const &tbl, ParameterSet &ps)
void parse_document(std::string const &filename, cet::filepath_maker &maker, intermediate_table &result)
Definition: parse.cc:856
template<typename ConfigurationClass >
fhicl::ParameterSet testing::BasicTesterEnvironment< ConfigurationClass >::ServiceParameters ( std::string  service_name) const
inline

Returns the configuration of the specified service.

Definition at line 609 of file unit_test_base.h.

References fhicl::ParameterSet::get().

610  {
612  (config.ServiceParameterSetPath(service_name));
613  }
fhicl::ParameterSet params
full configuration of the test
Configuration_t config
instance of the configurer
T get(std::string const &key) const
Definition: ParameterSet.h:231
template<typename ConfigurationClass >
void testing::BasicTesterEnvironment< ConfigurationClass >::Setup ( )
protectedvirtual

The complete initialization, ran at construction by default.

Definition at line 1185 of file unit_test_base.h.

References lar::standalone::SetupMessageFacility().

1186  {
1187 
1188  //
1189  // get the configuration
1190  //
1191  Configure();
1192 
1193  //
1194  // parse the options specific to the test environment
1195  //
1197 
1198  //
1199  // set up the message facility
1200  //
1202 
1203  //
1204  // Optionally print the configuration
1205  //
1206  {
1207  mf::LogInfo msg("Configuration");
1208  msg << "Complete configuration (";
1209  if (config.ConfigurationPath().empty()) msg << "default";
1210  else msg << "'" << config.ConfigurationPath() << "'";
1211  msg << "):\n" << Parameters().to_indented_string(1);
1212  }
1213 
1214 
1215  mf::LogInfo("Test") << config.ApplicationName() << " base setup complete.";
1216 
1217  } // BasicTesterEnvironment<>::Setup()
fhicl::ParameterSet const & Parameters() const
Returns the full configuration.
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
Configuration_t config
instance of the configurer
virtual void Configure()
Reads and translates the configuration.
void ParseEnvironmentOptions()
Parses the configuration, looking for the test environment options.
std::string to_indented_string() const
virtual void SetupMessageFacility() const
Sets up the message facility.
template<typename ConfigurationClass >
void testing::BasicTesterEnvironment< ConfigurationClass >::SetupMessageFacility ( fhicl::ParameterSet const &  pset,
std::string  appl_name = "" 
) const
protectedvirtual

Sets up the message facility.

Sets the message facility up.


Message facility configuration is expected in "services.message" parameter set. If not there, the default configuration is used.

Definition at line 1151 of file unit_test_base.h.

References fhicl::ParameterSet::get_if_present(), LOG_TRACE, mf::SetApplicationName(), mf::SetContextIteration(), mf::SetContextSinglet(), mf::StartMessageFacility(), and fhicl::ParameterSet::to_indented_string().

1152  {
1153  fhicl::ParameterSet mf_pset;
1154 
1155  if
1156  (!pset.get_if_present(config.ServiceParameterSetPath("message"), mf_pset))
1157  {
1158  mf_pset
1159  = CompileParameterSet(config.DefaultServiceConfiguration("message"));
1160  std::cout << "Using default message facility configuration:\n"
1161  << mf_pset.to_indented_string(1) << std::endl;
1162  } // if no configuration is available
1163 
1164  mf::StartMessageFacility(mf_pset);
1165  if (!appl_name.empty()) mf::SetApplicationName(appl_name);
1166  mf::SetContextIteration("Initialization");
1167  if (options.MessageLevels) {
1168  std::cout << "Printing message levels in 'MessageFacility' category."
1169  << std::endl;
1170 
1171  mf::LogProblem("MessageFacility") << "Error messages are shown.";
1172  mf::LogPrint("MessageFacility") << "Warning messages are shown.";
1173  mf::LogVerbatim("MessageFacility") << "Info messages are shown.";
1174  mf::LogTrace("MessageFacility") << "Debug messages are shown.";
1175  LOG_TRACE("MessageFacility")
1176  << "LOG_TRACE/LOG_DEBUG messages are not compiled away.";
1177  } // if print message levels
1178  mf::LogInfo("MessageFacility") << "MessageFacility started.";
1179  mf::SetContextSinglet("main");
1180  } // BasicTesterEnvironment::SetupMessageFacility()
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
Options_t options
options for the test environment
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
Configuration_t config
instance of the configurer
void SetContextIteration(string const &val)
MaybeLogger_< ELseverityLevel::ELsev_error, true > LogProblem
void StartMessageFacility(fhicl::ParameterSet const &pset, string const &applicationName)
std::string to_indented_string() const
bool MessageLevels
print message levels on screen
void SetContextSinglet(string const &val)
MaybeLogger_< ELseverityLevel::ELsev_success, true > LogTrace
static fhicl::ParameterSet CompileParameterSet(std::string cfg)
Compiles a parameter set from a string.
void SetApplicationName(string const &applicationName)
#define LOG_TRACE(id)
MaybeLogger_< ELseverityLevel::ELsev_warning, true > LogPrint
template<typename ConfigurationClass >
virtual void testing::BasicTesterEnvironment< ConfigurationClass >::SetupMessageFacility ( ) const
inlineprotectedvirtual

Sets up the message facility.

Sets the message facility up.


Message facility configuration is expected in "services.message" parameter set. If not there, the default configuration is used.

Definition at line 655 of file unit_test_base.h.

References lar::standalone::SetupMessageFacility().

Referenced by testing::BasicTesterEnvironment< ConfigurationClass >::Configure().

656  { SetupMessageFacility(Parameters(), config.ApplicationName()); }
fhicl::ParameterSet const & Parameters() const
Returns the full configuration.
Configuration_t config
instance of the configurer
virtual void SetupMessageFacility() const
Sets up the message facility.
template<typename ConfigurationClass >
fhicl::ParameterSet testing::BasicTesterEnvironment< ConfigurationClass >::TesterParameters ( std::string  test_name) const
inline

Returns the configuration of the specified test.

Definition at line 616 of file unit_test_base.h.

References fhicl::ParameterSet::get().

617  {
619  (config.TesterParameterSetPath(test_name));
620  }
fhicl::ParameterSet params
full configuration of the test
Configuration_t config
instance of the configurer
T get(std::string const &key) const
Definition: ParameterSet.h:231
template<typename ConfigurationClass >
fhicl::ParameterSet testing::BasicTesterEnvironment< ConfigurationClass >::TesterParameters ( ) const
inline

Returns the configuration of the main test (undefined if no main test)

Definition at line 623 of file unit_test_base.h.

624  {
625  if (config.MainTesterParameterSetName().empty()) return {};
626  else return TesterParameters(config.MainTesterParameterSetName());
627  }
Configuration_t config
instance of the configurer
fhicl::ParameterSet TesterParameters() const
Returns the configuration of the main test (undefined if no main test)

Member Data Documentation

template<typename ConfigurationClass >
Configuration_t testing::BasicTesterEnvironment< ConfigurationClass >::config
private

instance of the configurer

Definition at line 675 of file unit_test_base.h.

template<typename ConfigurationClass >
Options_t testing::BasicTesterEnvironment< ConfigurationClass >::options
private

options for the test environment

Definition at line 676 of file unit_test_base.h.

template<typename ConfigurationClass >
fhicl::ParameterSet testing::BasicTesterEnvironment< ConfigurationClass >::params
private

full configuration of the test

Definition at line 681 of file unit_test_base.h.


The documentation for this class was generated from the following file: