All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Private Member Functions | Private Attributes | List of all members
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 > AtomicNumberTestFixture testing::GeometryTesterEnvironment< 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)
 
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
 

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:

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 560 of file unit_test_base.h.

Member Typedef Documentation

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

Definition at line 563 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 572 of file unit_test_base.h.

572 { 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 589 of file unit_test_base.h.

589  :
590  config(configurer)
591  { 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

Definition at line 592 of file unit_test_base.h.

592  :
593  config(configurer)
594  { 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 1071 of file unit_test_base.h.

1071  {
1072 
1073  mf::LogInfo("Test") << config.ApplicationName() << " completed.";
1074 
1075  } // BasicTesterEnvironment<>::~BasicTesterEnvironment()
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 1085 of file unit_test_base.h.

1086  {
1087  fhicl::ParameterSet global_pset;
1088  global_pset = fhicl::ParameterSet::make(cfg);
1089  return global_pset;
1090  } // BasicTesterEnvironment<>::CompileParameterSet()
template<typename ConfigurationClass>
Configuration_t const& testing::BasicTesterEnvironment< ConfigurationClass >::Config ( ) const
inlineprotected

Returns a read-only version of the configuration.

Definition at line 635 of file unit_test_base.h.

635 { 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 1135 of file unit_test_base.h.

1135  {
1136  std::string config_path = config.ConfigurationPath();
1137  params = config_path.empty()?
1138  DefaultParameters(): ParseParameters(config_path);
1139  } // 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 647 of file unit_test_base.h.

648  { 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 605 of file unit_test_base.h.

605 { 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 1221 of file unit_test_base.h.

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

1102  {
1103  // configuration file lookup policy
1104  char const* fhicl_env = getenv("FHICL_FILE_PATH");
1105  std::string search_path = fhicl_env? std::string(fhicl_env) + ":": ".:";
1106  details::FirstAbsoluteOrLookupWithDotPolicy policy(search_path);
1107 
1108  // parse a configuration file; obtain intermediate form
1109  fhicl::intermediate_table table;
1110  table = fhicl::parse_document(config_path, policy);
1111 
1112  // translate into a parameter set
1113  fhicl::ParameterSet global_pset;
1114  global_pset = fhicl::ParameterSet::make(table);
1115 
1116  return global_pset;
1117  } // BasicTesterEnvironment<>::ParseParameters()
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 608 of file unit_test_base.h.

609  {
610  return params.get<fhicl::ParameterSet>
611  (config.ServiceParameterSetPath(service_name));
612  }
fhicl::ParameterSet params
full configuration of the test
Configuration_t config
instance of the configurer
template<typename ConfigurationClass >
void testing::BasicTesterEnvironment< ConfigurationClass >::Setup ( )
protectedvirtual

The complete initialization, ran at construction by default.

Reimplemented in testing::GeometryTesterEnvironment< ConfigurationClass >, testing::GeometryTesterEnvironment< IcarusGeometryConfiguration >, testing::GeometryTesterEnvironment< SBNDGeometryConfiguration >, and testing::GeometryTesterEnvironment< StandardGeometryConfiguration >.

Definition at line 1184 of file unit_test_base.h.

1185  {
1186 
1187  //
1188  // get the configuration
1189  //
1190  Configure();
1191 
1192  //
1193  // parse the options specific to the test environment
1194  //
1196 
1197  //
1198  // set up the message facility
1199  //
1201 
1202  //
1203  // Optionally print the configuration
1204  //
1205  {
1206  mf::LogInfo msg("Configuration");
1207  msg << "Complete configuration (";
1208  if (config.ConfigurationPath().empty()) msg << "default";
1209  else msg << "'" << config.ConfigurationPath() << "'";
1210  msg << "):\n" << Parameters().to_indented_string(1);
1211  }
1212 
1213 
1214  mf::LogInfo("Test") << config.ApplicationName() << " base setup complete.";
1215 
1216  } // BasicTesterEnvironment<>::Setup()
fhicl::ParameterSet const & Parameters() const
Returns the full configuration.
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.
virtual void SetupMessageFacility() const
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 1150 of file unit_test_base.h.

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

Definition at line 654 of file unit_test_base.h.

655  { SetupMessageFacility(Parameters(), config.ApplicationName()); }
fhicl::ParameterSet const & Parameters() const
Returns the full configuration.
Configuration_t config
instance of the configurer
virtual void SetupMessageFacility() const
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 615 of file unit_test_base.h.

616  {
617  return params.get<fhicl::ParameterSet>
618  (config.TesterParameterSetPath(test_name));
619  }
fhicl::ParameterSet params
full configuration of the test
Configuration_t config
instance of the configurer
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 622 of file unit_test_base.h.

623  {
624  if (config.MainTesterParameterSetName().empty()) return {};
625  else return TesterParameters(config.MainTesterParameterSetName());
626  }
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 674 of file unit_test_base.h.

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

options for the test environment

Definition at line 675 of file unit_test_base.h.

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

full configuration of the test

Definition at line 680 of file unit_test_base.h.


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