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 LERR_(LIFECYCLE);
51 
52 
58  class Interface : public TestTargetObj
59  {
60  public:
61  static int cnt;
62  static void setCountParam (uint c) { Interface::cnt = c; }
63 
64  virtual string identify() { return "Interface"; }
65 
66  protected:
67  Interface () : TestTargetObj(cnt) {}
68  virtual ~Interface() {}
69 
70  friend class lib::DependencyFactory<Interface>;
71  };
72 
73  int Interface::cnt = 0;
74 
75 
76  class Impl : public Interface
77  {
78  public:
79  virtual string identify() { return "Implementation"; }
80  };
81 
82 
83  // for checking the safety.....
84  class Impl_XXX : public Impl { };
85  class Unrelated { };
86 
87 
88 
89 
90  /***************************************************************/
98  class SingletonSubclass_test : public Test
99  {
100 
101  virtual void
102  run(Arg arg)
103  {
104  uint num= isnil(arg)? 1 : lexical_cast<uint>(arg[1]);
105 
106  cout << _Fmt("using the Singleton should create TargetObj(%d)...\n") % num;
107 
108  Interface::setCountParam(num);
109 
110  // configuration to use the subclass on demand
112 
113  // define an instance of the Singleton factory as always...
114  Depend<Interface> instance;
115 
116  // Now use the Singleton factory...
117  // Note: we get the Base type
118  Interface& t1 = instance();
119  Interface& t2 = instance();
120 
121  CHECK (isSameObject (t1, t2), "not a Singleton, got two different instances." );
122  CHECK ( INSTANCEOF (Impl,&t1)); // got the subclass as expected
123  CHECK ("Implementation" == t2.identify());
124 
125  cout << "calling a non-static method on the Singleton-"
126  << t1.identify() << endl
127  << t1 << endl;
128 
129  verify_error_detection ();
130  }
131 
132 
133 
134  void
135  verify_error_detection ()
136  {
138 
139  Depend<Interface> newFactory;
140  CHECK ( INSTANCEOF (Impl, &newFactory() )); // works as before
141 
143 // DependInject<Interface>::useSingleton<Unrelated>();
144  }
145  };
146 
147 
148 
150  LAUNCHER (SingletonSubclass_test, "unit common");
151 
152 
153 
154 }} // 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:492
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: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:372