Lumiera  0.pre.03
»edit your freedom«
singleton-subclass-test.cpp
Go to the documentation of this file.
1 /*
2  SingletonSubclass(Test) - actually creating a subclass of the Singleton Type
3 
4  Copyright (C)
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
19 #include "lib/test/run.hpp"
20 #include "lib/test/test-helper.hpp"
21 #include "lib/format-string.hpp"
22 #include "lib/format-cout.hpp"
23 #include "lib/util.hpp"
24 
25 #include "test-target-obj.hpp"
26 #include "lib/depend.hpp"
27 #include "lib/depend-inject.hpp"
28 
29 #include <boost/lexical_cast.hpp>
30 
31 using boost::lexical_cast;
32 using util::isSameObject;
33 using util::_Fmt;
34 using util::isnil;
35 using std::string;
36 
37 
38 namespace lib {
39 namespace test{
40 
41  using LERR_(LIFECYCLE);
42 
43 
49  class Interface : public TestTargetObj
50  {
51  public:
52  static int cnt;
53  static void setCountParam (uint c) { Interface::cnt = c; }
54 
55  virtual string identify() { return "Interface"; }
56 
57  protected:
58  Interface () : TestTargetObj(cnt) {}
59  virtual ~Interface() {}
60 
61  friend class lib::DependencyFactory<Interface>;
62  };
63 
64  int Interface::cnt = 0;
65 
66 
67  class Impl : public Interface
68  {
69  public:
70  virtual string identify() { return "Implementation"; }
71  };
72 
73 
74  // for checking the safety.....
75  class Impl_XXX : public Impl { };
76  class Unrelated { };
77 
78 
79 
80 
81  /***************************************************************/
89  class SingletonSubclass_test : public Test
90  {
91 
92  virtual void
93  run(Arg arg)
94  {
95  uint num{firstVal (arg)};
96  cout << _Fmt("using the Singleton should create TargetObj(%d)...\n") % num;
97 
98  Interface::setCountParam(num);
99 
100  // configuration to use the subclass on demand
102 
103  // define an instance of the Singleton factory as always...
104  Depend<Interface> instance;
105 
106  // Now use the Singleton factory...
107  // Note: we get the Base type
108  Interface& t1 = instance();
109  Interface& t2 = instance();
110 
111  CHECK (isSameObject (t1, t2), "not a Singleton, got two different instances." );
112  CHECK ( INSTANCEOF (Impl,&t1)); // got the subclass as expected
113  CHECK ("Implementation" == t2.identify());
114 
115  cout << "calling a non-static method on the Singleton-"
116  << t1.identify() << endl
117  << t1 << endl;
118 
119  verify_error_detection ();
120  }
121 
122 
123 
124  void
125  verify_error_detection ()
126  {
128 
129  Depend<Interface> newFactory;
130  CHECK ( INSTANCEOF (Impl, &newFactory() )); // works as before
131 
133 // DependInject<Interface>::useSingleton<Unrelated>();
134  }
135  };
136 
137 
138 
140  LAUNCHER (SingletonSubclass_test, "unit common");
141 
142 
143 
144 }} // namespace lib::test
Automatically use custom string conversion in C++ stream output.
Dummy target object to be created by factory for unit tests Used to verify sane memory management and...
Definition: run.hpp:40
#define INSTANCEOF(CLASS, EXPR)
shortcut for subclass test, intended for assertions only.
Definition: util.hpp:514
Per type specific configuration of instances created as service dependencies.
Front-end for printf-style string template interpolation.
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
A front-end for using printf-style formatting.
Access point to singletons and other kinds of dependencies designated by type.
Definition: depend.hpp:280
Implementation namespace for support and library code.
Target object to be created by Test-Factories or as Singleton.
This framework allows to (re)configure the lib::Depend front-end for dependency-injection.
Target object to be instantiated as Singleton Allocates a variable amount of additional heap memory a...
Simplistic test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
A collection of frequently used helper functions to support unit testing.
Singleton services and Dependency Injection.
Helper to abstract creation and lifecycle of a dependency.
Definition: depend.hpp:125
bool isSameObject(A const &a, B const &b)
compare plain object identity, based directly on the referee&#39;s memory identities. ...
Definition: util.hpp:421