Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
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
27using util::_Fmt;
28using util::isnil;
29
30
31namespace lib {
32namespace test{
33
34
40 {
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
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
78 {
79 Mock_1() : TestSingO("Mock_1") { };
80 };
81
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 {
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
161
162
163
164}} // namespace lib::test
Configuration handle for temporarily shadowing a dependency by a test mock instance.
Access point to singletons and other kinds of dependencies designated by type.
Definition depend.hpp:281
Inline string literal.
Definition symbol.hpp:78
constexpr const char * c() const
Definition symbol.hpp:93
Token or Atom with distinct identity.
Definition symbol.hpp:120
Client Class normally to be instantiated as Singleton.
TestSingO(Symbol ty="TestSingO")
A front-end for using printf-style formatting.
Per type specific configuration of instances created as service dependencies.
Automatically use custom string conversion in C++ stream output.
Front-end for printf-style string template interpolation.
Implementation namespace for support and library code.
int rani(uint bound=_iBOUND())
Definition random.hpp:135
Test runner and basic definitions for tests.
bool isnil(lib::time::Duration const &dur)
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
Mock-1 to replace the Client Class...
Mock-2 to replace the Client Class...
Mock_2(Literal specialID, int i)
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...