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) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/test/test-helper.hpp"
30 #include "lib/format-string.hpp"
31 #include "lib/format-cout.hpp"
32 #include "lib/util.hpp"
33 
34 #include "test-target-obj.hpp"
35 #include "lib/depend.hpp"
36 #include "lib/depend-inject.hpp"
37 
38 #include <boost/lexical_cast.hpp>
39 
40 using boost::lexical_cast;
41 using util::isSameObject;
42 using util::_Fmt;
43 using util::isnil;
44 using std::string;
45 
46 
47 namespace lib {
48 namespace test{
49 
50  using lumiera::error::LERR_(LIFECYCLE);
51 
57  class Interface : public TestTargetObj
58  {
59  public:
60  static int cnt;
61  static void setCountParam (uint c) { Interface::cnt = c; }
62 
63  virtual string identify() { return "Interface"; }
64 
65  protected:
66  Interface () : TestTargetObj(cnt) {}
67  virtual ~Interface() {}
68 
69  friend class lib::DependencyFactory<Interface>;
70  };
71 
72  int Interface::cnt = 0;
73 
74 
75  class Impl : public Interface
76  {
77  public:
78  virtual string identify() { return "Implementation"; }
79  };
80 
81 
82  // for checking the safety.....
83  class Impl_XXX : public Impl { };
84  class Unrelated { };
85 
86 
87 
88 
89  /***************************************************************/
97  class SingletonSubclass_test : public Test
98  {
99 
100  virtual void
101  run(Arg arg)
102  {
103  uint num= isnil(arg)? 1 : lexical_cast<uint>(arg[1]);
104 
105  cout << _Fmt("using the Singleton should create TargetObj(%d)...\n") % num;
106 
107  Interface::setCountParam(num);
108 
109  // configuration to use the subclass on demand
111 
112  // define an instance of the Singleton factory as always...
113  Depend<Interface> instance;
114 
115  // Now use the Singleton factory...
116  // Note: we get the Base type
117  Interface& t1 = instance();
118  Interface& t2 = instance();
119 
120  CHECK (isSameObject (t1, t2), "not a Singleton, got two different instances." );
121  CHECK ( INSTANCEOF (Impl,&t1)); // got the subclass as expected
122  CHECK ("Implementation" == t2.identify());
123 
124  cout << "calling a non-static method on the Singleton-"
125  << t1.identify() << endl
126  << t1 << endl;
127 
128  verify_error_detection ();
129  }
130 
131 
132 
133  void
134  verify_error_detection ()
135  {
137 
138  Depend<Interface> newFactory;
139  CHECK ( INSTANCEOF (Impl, &newFactory() )); // works as before
140 
142 // DependInject<Interface>::useSingleton<Unrelated>();
143  }
144  };
145 
146 
147 
149  LAUNCHER (SingletonSubclass_test, "unit common");
150 
151 
152 
153 }} // 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:49
#define INSTANCEOF(CLASS, EXPR)
shortcut for subclass test, intended for assertions only.
Definition: util.hpp:447
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 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:289
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...
Simple 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:134
bool isSameObject(A const &a, B const &b)
compare plain object identity, bypassing any custom comparison operators.
Definition: util.hpp:347