Classes | Namespaces | Macros | Functions
ProviderPack_test.cc File Reference
#include <boost/test/unit_test.hpp>
#include "larcorealg/CoreUtils/ProviderPack.h"
#include "larcorealg/CoreUtils/UncopiableAndUnmovableClass.h"
#include <string>
#include <ostream>

Go to the source code of this file.

Classes

struct  svc::ProviderA
 A service provider class. More...
 
struct  svc::ProviderB
 A service provider class. More...
 
struct  svc::ProviderB1
 A service provider class derived from B. More...
 
struct  svc::ProviderC
 A service provider class. More...
 
struct  svc::ProviderD
 A service provider class. More...
 

Namespaces

 svc
 

Macros

#define PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS   1
 
#define BOOST_TEST_MODULE   ( ProviderPack_test )
 

Functions

 BOOST_AUTO_TEST_CASE (ProviderPack_testcase)
 
 BOOST_AUTO_TEST_CASE (ProviderPackDerived_testcase)
 

Macro Definition Documentation

#define BOOST_TEST_MODULE   ( ProviderPack_test )

Definition at line 26 of file ProviderPack_test.cc.

#define PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS   1

Definition at line 13 of file ProviderPack_test.cc.

Function Documentation

BOOST_AUTO_TEST_CASE ( ProviderPack_testcase  )

Definition at line 114 of file ProviderPack_test.cc.

114  {
115 
116  // instantiate a ProviderPack with three classes
117  svc::ProviderA providerA;
118  svc::ProviderB providerB;
119  svc::ProviderC providerC;
120  auto SP1 = lar::makeProviderPack(&providerA, &providerB, &providerC);
121 
122  // get element A
123  static_assert
124  (decltype(SP1)::has<svc::ProviderA>(), "We don't believe to have ProviderA!!");
125  auto myA = SP1.get<svc::ProviderA>();
126  static_assert(std::is_same<decltype(myA), svc::ProviderA const*>(),
127  "Failed to get the element of type A");
128  BOOST_TEST(myA == &providerA);
129 
130  // get element B
131  static_assert
132  (decltype(SP1)::has<svc::ProviderB>(), "We don't believe to have ProviderB!!");
133  auto myB = SP1.get<svc::ProviderB>();
134  static_assert(std::is_same<decltype(myB), svc::ProviderB const*>(),
135  "Failed to get the element of type B");
136  BOOST_TEST(myB == &providerB);
137 
138  // get element C
139  static_assert
140  (decltype(SP1)::has<svc::ProviderC>(), "We don't believe to have ProviderC!!");
141  auto myC = SP1.get<svc::ProviderC>();
142  static_assert(std::is_same<decltype(myC), svc::ProviderC const*>(),
143  "Failed to get the element of type C");
144  BOOST_TEST(myC == &providerC);
145 
146 
147  // set element A
148  svc::ProviderA providerA2;
149  SP1.set(&providerA2);
150  myA = SP1.get<svc::ProviderA>();
151  BOOST_TEST(myA == &providerA2);
152 
153  // get element D
154  // should be a compilation error
155 #if PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS
156  BOOST_TEST_MESSAGE(" (test to get a non-existing provider type skipped)");
157 #else
158  SP1.get<svc::ProviderD>();
159 #endif // !PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS
160 
161  // check what we believe we have
162  static_assert
163  (!decltype(SP1)::has<svc::ProviderD>(), "We believe to have ProviderD!!");
164 
165  // default constructor: all null
167  BOOST_TEST(SP2.get<svc::ProviderA>() == nullptr);
168  BOOST_TEST(SP2.get<svc::ProviderB>() == nullptr);
169 
170  // extraction constructor
172  BOOST_TEST(SP3.get<svc::ProviderA>() == SP1.get<svc::ProviderA>());
173  BOOST_TEST(SP3.get<svc::ProviderB>() == SP1.get<svc::ProviderB>());
174 
175  // multiple elements of the same type
176  // should be a compilation error
177 #if PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS
178  BOOST_TEST_MESSAGE
179  (" (test to create a pack with many providers with same type skipped)");
180 #else
182  <svc::ProviderA, svc::ProviderB, svc::ProviderA, svc::ProviderD> SP3;
183 #endif // !PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS
184 
185 
186 } // BOOST_AUTO_TEST_CASE(ProviderPack_testcase)
A service provider class.
Provider const * get() const
Returns the provider with the specified type.
Definition: ProviderPack.h:193
A service provider class.
ProviderPack< Providers... > makeProviderPack(Providers const *...providers)
Function to create a ProviderPack from the function arguments.
Definition: ProviderPack.h:272
A service provider class.
A service provider class.
Container for a list of pointers to providers.
Definition: ProviderPack.h:114
BOOST_AUTO_TEST_CASE ( ProviderPackDerived_testcase  )

Definition at line 190 of file ProviderPack_test.cc.

190  {
191 
192  svc::ProviderA providerA;
193  svc::ProviderB1 providerB;
194 
195  //
196  // initialise a base class provider with a derived class
197  //
199  (&providerA, &providerB);
200 
201  BOOST_TEST(SP1.get<svc::ProviderA>() == &providerA);
202  BOOST_TEST(SP1.get<svc::ProviderB>() == &providerB);
203 
204  //
205  // initialise with a copy from makeProviderPack(),
206  // which should return lar::ProviderPack<svc::ProviderA, svc::ProviderB1>
207  //
209  = makeProviderPack(&providerA, &providerB);
210 
211  BOOST_TEST(SP2.get<svc::ProviderA>() == &providerA);
212  BOOST_TEST(SP2.get<svc::ProviderB>() == &providerB);
213 
214 } // BOOST_AUTO_TEST_CASE(test_ProviderPack)
A service provider class.
Provider const * get() const
Returns the provider with the specified type.
Definition: ProviderPack.h:193
A service provider class.
A service provider class derived from B.
Container for a list of pointers to providers.
Definition: ProviderPack.h:114
ProviderPack< Providers... > makeProviderPack(Providers const *...providers)
Function to create a ParameterPack from the function arguments.
Definition: ProviderPack.h:182