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)
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 
20 #include "lib/test/run.hpp"
21 #include "lib/depend-inject.hpp"
22 #include "lib/util.hpp"
23 
24 #include "lib/format-cout.hpp"
25 #include "lib/format-string.hpp"
26 
27 using util::_Fmt;
28 using util::isnil;
29 
30 
31 namespace lib {
32 namespace test{
33 
34 
39  class TestSingO
40  {
41  int callCnt_;
42  Symbol typid_;
43  _Fmt msg_;
44 
45  public:
46  TestSingO(Symbol ty="TestSingO")
47  : callCnt_(0)
48  , typid_(ty)
49  , msg_("%s::doIt() call=%d\n")
50  {
51  TRACE (test, "ctor %s", typid_.c());
52  }
53 
54  virtual
55  ~TestSingO()
56  {
57  TRACE (test, "dtor %s", typid_.c());
58  }
59 
60  void doIt ()
61  {
62  ++callCnt_;
63  cout << msg_ % typid_ % callCnt_;
64  }
65 
66  int getCnt ()
67  {
68  return callCnt_;
69  }
70 
71  };
72 
73 
77  struct Mock_1 : TestSingO
78  {
79  Mock_1() : TestSingO("Mock_1") { };
80  };
81 
86  struct Mock_2 : TestSingO
87  {
88  int id;
89 
90  Mock_2(Literal specialID, int i)
91  : TestSingO{Symbol (_Fmt{"%s_%d"} % specialID % i)}
92  , id{i}
93  { };
94  };
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105  /***************************************************************/
115  class SingletonTestMock_test : public Test
116  {
117 
118  void
119  run (Arg)
120  {
121  Depend<TestSingO> sing;
122 
123  sing().doIt();
124  sing().doIt();
125  CHECK (sing().getCnt() == 2);
126 
127  {
128  // shadow by local Mock instance
130  sing().doIt();
131  sing().doIt();
132  sing().doIt();
133  sing().doIt();
134  sing().doIt();
135  CHECK (sing().getCnt() == 5);
136 
137  // shadow again by different local Mock, this time with special ctor call
138  int instanceID = 0;
139  DependInject<TestSingO>::Local<Mock_2> mock_2 ([&]{ return new Mock_2{"Mock", instanceID}; });
140 
141  // NOTE: the ctor call for the Mock really happens delayed...
142  instanceID = rani(10);
143  sing().doIt(); // ctor invoked on first access
144  CHECK (sing().getCnt() == 1);
145 
146  // can access the Mock for instrumentation
147  CHECK (instanceID == mock_2->id);
148 
149  }// original instance automatically un-shadowed here
150 
151  CHECK (sing().getCnt() == 2);
152  sing().doIt();
153  CHECK (sing().getCnt() == 3);
154  }
155  };
156 
157 
158 
160  LAUNCHER (SingletonTestMock_test, "unit common");
161 
162 
163 
164 }} // namespace lib::test
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:40
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:76
Per type specific configuration of instances created as service dependencies.
Front-end for printf-style string template interpolation.
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
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:280
Implementation namespace for support and library code.
Token or Atom with distinct identity.
Definition: symbol.hpp:117
Simplistic 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.