Lumiera  0.pre.03
»edit your freedom«
handling-pattern-basics-test.cpp
Go to the documentation of this file.
1 /*
2  HandlingPatternBasics(Test) - verify elementary operation of a command handling pattern
3 
4  Copyright (C) Lumiera.org
5  2009, 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"
34 #include "lib/test/event-log.hpp"
35 
37 
38 #include <cstdlib>
39 
40 
41 namespace steam {
42 namespace control {
43 namespace test {
44 
45 
46  using std::function;
47  using std::rand;
48 
49 
50  namespace { // test fixture...
51 
52  string TEST_CMD = "test.command1.handling";
53  HandlingPattern::ID TEST_PATTERN = HandlingPattern::DUMMY;
54 
55 
57  : public HandlingPattern
58  {
59  mutable
60  lib::test::EventLog log_{"custom command handler"};
61 
62 
63  /* ==== HandlingPattern - Interface ==== */
64 
65  void
66  performExec (CommandImpl& command) const override
67  {
68  log_.call (TEST_CMD, "exec");
69  command.invokeCapture();
70  command.invokeOperation();
71  }
72 
73  void
74  performUndo (CommandImpl& command) const override
75  {
76  log_.call (TEST_CMD, "undo");
77  command.invokeUndo();
78  }
79 
80  bool
81  isValid() const override
82  {
83  return true;
84  }
85 
86 
87  public:
88  bool
89  invokedExec()
90  {
91  return log_.verifyCall("exec").on(TEST_CMD);
92  }
93 
94  bool
95  invokedUndo()
96  {
97  return log_.verifyCall("undo").on(TEST_CMD)
98  .afterCall("exec");
99  }
100  };
101 
102  }//(End) test fixture
103 
105  typedef HandlingPattern const& HaPatt;
106 
107 
108 
109 
110 
111 
112  /******************************************************************************/
122  class HandlingPatternBasics_test : public Test
123  {
124 
125  uint cnt_inst;
126 
127 
128  virtual void
129  run (Arg)
130  {
132  CHECK (&registry);
133 
134  cnt_inst = registry.instance_count();
135 
136  {
137  PCommandImpl pCom = buildTestCommand(registry);
138  checkExec (pCom);
139  checkUndo (pCom);
140 
141  useCustomHandler (pCom);
142  }
143 
144  CHECK (cnt_inst == registry.instance_count());
145  }
146 
147 
153  PCommandImpl
155  {
156 
157  typedef void Sig_oper(int);
158  typedef long Sig_capt(int);
159  typedef void Sig_undo(int,long);
160 
161  function<Sig_oper> o_Fun (command1::operate);
162  function<Sig_capt> c_Fun (command1::capture);
163  function<Sig_undo> u_Fun (command1::undoIt);
164 
165  CHECK (o_Fun && c_Fun && u_Fun);
166 
167  // when the CommandDef is complete, it issues the
168  // allocation call to the registry behind the scenes....
169 
170  PCommandImpl pImpl = registry.newCommandImpl(o_Fun,c_Fun,u_Fun);
171  CHECK (pImpl);
172  CHECK (*pImpl);
173  return pImpl;
174  }
175 
176 
178  void
179  checkExec (PCommandImpl com)
180  {
181  CHECK (com);
182  CHECK (!com->canExec());
183 
184  typedef Types<int> ArgType;
185  const int ARGR (1 + rand() % 1000);
186  Tuple<ArgType> tuple(ARGR);
187  TypedArguments<Tuple<ArgType>> arg(tuple);
188  com->setArguments(arg);
189 
190  CHECK (com->canExec());
191  CHECK (!com->canUndo());
192  command1::check_ = 0;
193 
194  HaPatt patt = HandlingPattern::get(TEST_PATTERN);
195  ExecResult res = patt.exec (*com, TEST_CMD);
196 
197  CHECK (res);
198  CHECK (ARGR == command1::check_);
199  CHECK (com->canUndo());
200  }
201 
202 
204  void
205  checkUndo (PCommandImpl com)
206  {
207  CHECK (com);
208  CHECK (com->canExec());
209  CHECK (com->canUndo());
210 
211  CHECK (command1::check_ > 0);
212 
213  HaPatt ePatt = HandlingPattern::get(TEST_PATTERN);
214  ExecResult res = ePatt.undo (*com, TEST_CMD);
215 
216  CHECK (res);
217  CHECK (command1::check_ == 0);
218  }
219 
220 
224  void
225  useCustomHandler (PCommandImpl com)
226  {
227  CustomHandler specialHandler;
228 
229  CHECK (com->canExec());
230  CHECK (not specialHandler.invokedExec());
231 
232  specialHandler.exec (*com, TEST_CMD);
233  CHECK ( specialHandler.invokedExec());
234  CHECK (not specialHandler.invokedUndo());
235 
236  specialHandler.undo (*com, TEST_CMD);
237  CHECK ( specialHandler.invokedExec());
238  }
239  };
240 
241 
243  LAUNCHER (HandlingPatternBasics_test, "function controller");
244 
245 
246 }}} // namespace steam::control::test
typename BuildTupleType< TYPES >::Type Tuple
Build a std::tuple from types given as type sequence.
Support for verifying the occurrence of events from unit tests.
Definition: run.hpp:49
Implementation helper to bind Steam-Layer commands with arbitrary argument tuples.
shared_ptr< CommandImpl > newCommandImpl(function< SIG_OPER > &operFunctor, function< SIG_CAPT > &captFunctor, function< SIG_UNDO > &undoFunctor)
set up a new command implementation frame
Top level of the command implementation.
Helper to log and verify the occurrence of events.
Definition: event-log.hpp:284
Managing command definitions and the storage of individual command objects.
Registry managing command implementation objects (Singleton).
Steam-Layer implementation namespace root.
PCommandImpl buildTestCommand(CommandRegistry &registry)
create a command implementation frame usable for tests.
Simple test class runner.
Result (Status) of command execution.
EventLog & call(string target, string function)
Log occurrence of a function call with no arguments.
Definition: event-log.cpp:699
Steam-Layer command frontend.
Pre-defined command execution skeletons.
Some dummy command functions used for building unit test cases.
Steam-Layer Command implementation.
static HandlingPattern const & get(ID id)
retrieve the pre-configured pattern
Interface: Operation Skeleton how to invoke or undo a command.
static lib::Depend< CommandRegistry > instance
storage for the singleton factory used to access CommandRegistry