Lumiera  0.pre.03
»edit your freedom«
singleton-testmock-test.cpp
Go to the documentation of this file.
1 /*
2  SingletonTestMock(Test) - using Singleton for injecting Test-Mocks
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 
29 #include "lib/test/run.hpp"
30 #include "lib/depend-inject.hpp"
31 #include "lib/util.hpp"
32 
33 #include "lib/format-cout.hpp"
34 #include "lib/format-string.hpp"
35 
36 using util::_Fmt;
37 using util::isnil;
38 
39 
40 namespace lib {
41 namespace test{
42 
43 
48  class TestSingO
49  {
50  int callCnt_;
51  Symbol typid_;
52  _Fmt msg_;
53 
54  public:
55  TestSingO(Symbol ty="TestSingO")
56  : callCnt_(0)
57  , typid_(ty)
58  , msg_("%s::doIt() call=%d\n")
59  {
60  TRACE (test, "ctor %s", typid_.c());
61  }
62 
63  virtual
64  ~TestSingO()
65  {
66  TRACE (test, "dtor %s", typid_.c());
67  }
68 
69  void doIt ()
70  {
71  ++callCnt_;
72  cout << msg_ % typid_ % callCnt_;
73  }
74 
75  int getCnt ()
76  {
77  return callCnt_;
78  }
79 
80  };
81 
82 
86  struct Mock_1 : TestSingO
87  {
88  Mock_1() : TestSingO("Mock_1") { };
89  };
90 
95  struct Mock_2 : TestSingO
96  {
97  int id;
98 
99  Mock_2(Literal specialID, int i)
100  : TestSingO{Symbol (_Fmt{"%s_%d"} % specialID % i)}
101  , id{i}
102  { };
103  };
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114  /***************************************************************/
124  class SingletonTestMock_test : public Test
125  {
126 
127  void
128  run (Arg)
129  {
130  Depend<TestSingO> sing;
131 
132  sing().doIt();
133  sing().doIt();
134  CHECK (sing().getCnt() == 2);
135 
136  {
137  // shadow by local Mock instance
139  sing().doIt();
140  sing().doIt();
141  sing().doIt();
142  sing().doIt();
143  sing().doIt();
144  CHECK (sing().getCnt() == 5);
145 
146  // shadow again by different local Mock, this time with special ctor call
147  int instanceID = 0;
148  DependInject<TestSingO>::Local<Mock_2> mock_2 ([&]{ return new Mock_2{"Mock", instanceID}; });
149 
150  // NOTE: the ctor call for the Mock really happens delayed...
151  instanceID = rand() % 10;
152  sing().doIt(); // ctor invoked on first access
153  CHECK (sing().getCnt() == 1);
154 
155  // can access the Mock for instrumentation
156  CHECK (instanceID == mock_2->id);
157 
158  }// original instance automatically un-shadowed here
159 
160  CHECK (sing().getCnt() == 2);
161  sing().doIt();
162  CHECK (sing().getCnt() == 3);
163  }
164  };
165 
166 
167 
169  LAUNCHER (SingletonTestMock_test, "unit common");
170 
171 
172 
173 }} // namespace lib::test
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:49
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:85
Per type specific configuration of instances created as service dependencies.
Front-end for printf-style string template interpolation.
Client Class normally to be instantiated as Singleton.
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.
Token or Atom with distinct identity.
Definition: symbol.hpp:126
Simple test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Mock-1 to replace the Client Class...
Mock-2 to replace the Client Class...
Configuration handle for temporarily shadowing a dependency by a test mock instance.