ServiceUtil_test.cc
Go to the documentation of this file.
1 /**
2  * @file ServiceUtil_test.cc
3  * @brief Tests the utilities in ServiceUtil.h
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date April 28, 2016
6  * @see ServiceUtil.h
7  *
8  * This test takes no command line argument.
9  *
10  */
11 
12 /*
13  * Boost Magic: define the name of the module;
14  * and do that before the inclusion of Boost unit test headers
15  * because it will change what they provide.
16  * Among the those, there is a main() function and some wrapping catching
17  * unhandled exceptions and considering them test failures, and probably more.
18  */
19 #define BOOST_TEST_MODULE ( ServiceUtil_test )
20 
21 // GArSoft libraries
22 #include "CoreUtils/UncopiableAndUnmovableClass.h"
23 #include "CoreUtils/ServiceUtil.h"
24 
25 // art libraries
28 
29 // Boost libraries
30 #include <cetlib/quiet_unit_test.hpp> // BOOST_AUTO_TEST_CASE()
31 #include <boost/test/test_tools.hpp> // BOOST_CHECK(), BOOST_CHECK_EQUAL()
32 
33 // C/C++ standard libraries
34 #include <type_traits>
35 
36 
37 //------------------------------------------------------------------------------
39 
40 class MyService {
41  MyProvider* prov = nullptr;
42 
43  public:
44  MyService(MyProvider* ptr = nullptr): prov(ptr) {}
45 
47 
48  provider_type const* provider() const { return prov; }
49 
50 }; // MyService
51 
52 namespace gar {
53  namespace details {
54 
55  template struct ServiceProviderRequirementsChecker<MyProvider>;
56 
57  template struct ServiceRequirementsChecker<MyService>;
58 
59  } // namespace details
60 } // namespace gar
61 
62 
63 
64 
65 // This is art. Well, kind of.
67  std::unique_ptr<MyService> myServicePtr = nullptr;
68 }; // GlobalServicesClass
70 
71 namespace art {
72 
73  namespace detail {
74  template <>
76  static constexpr art::ServiceScope scope_val
78  };
79  } // namespace detail
80 
81  template <>
84  public:
85  ServiceHandle(): instance(GlobalServices.myServicePtr.get()) {}
86  MyService* operator->() const { return instance; }
87  MyService& operator*() const { return *instance; }
88  }; // ServiceHandle<MyService>
89 
90 } // namespace art
91 
92 
93 BOOST_AUTO_TEST_CASE(providerFromTest) {
94 
95  // on the first try, there is no service provider.
96 
97  GlobalServices.myServicePtr = std::make_unique<MyService>();
98  BOOST_CHECK_EXCEPTION(gar::providerFrom<MyService>(), art::Exception,
99  [](art::Exception const& e)
100  { return e.categoryCode() == art::errors::NotFound; }
101  );
102 
103  // now let's create a "real" provider
104  MyProvider prov;
105  GlobalServices.myServicePtr = std::make_unique<MyService>(&prov);
106  BOOST_CHECK_EQUAL(gar::providerFrom<MyService>(), &prov);
107 
108  // that's enough; let's clean up
109  GlobalServices.myServicePtr.reset();
110 
111 } // BOOST_AUTO_TEST_CASE(larProviderFromTest)
112 
113 
114 
115 //------------------------------------------------------------------------------
BOOST_AUTO_TEST_CASE(providerFromTest)
ServiceScope
Definition: ServiceScope.h:7
MyService(MyProvider *ptr=nullptr)
std::unique_ptr< MyService > myServicePtr
const std::string instance
const double e
GlobalServicesClass GlobalServices
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
General GArSoft Utilities.
provider_type const * provider() const
An empty class that can&#39;t be copied nor moved.
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115