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)
5  2009, 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 
19 #include "lib/test/run.hpp"
25 #include "lib/test/event-log.hpp"
26 
28 
29 
30 namespace steam {
31 namespace control{
32 namespace test {
33 
34  using std::function;
35 
36  namespace { // test fixture...
37 
38  string TEST_CMD = "test.command1.handling";
39  HandlingPattern::ID TEST_PATTERN = HandlingPattern::DUMMY;
40 
41 
43  : public HandlingPattern
44  {
45  mutable
46  lib::test::EventLog log_{"custom command handler"};
47 
48 
49  /* ==== HandlingPattern - Interface ==== */
50 
51  void
52  performExec (CommandImpl& command) const override
53  {
54  log_.call (TEST_CMD, "exec");
55  command.invokeCapture();
56  command.invokeOperation();
57  }
58 
59  void
60  performUndo (CommandImpl& command) const override
61  {
62  log_.call (TEST_CMD, "undo");
63  command.invokeUndo();
64  }
65 
66  bool
67  isValid() const override
68  {
69  return true;
70  }
71 
72 
73  public:
74  bool
75  invokedExec()
76  {
77  return log_.verifyCall("exec").on(TEST_CMD);
78  }
79 
80  bool
81  invokedUndo()
82  {
83  return log_.verifyCall("undo").on(TEST_CMD)
84  .afterCall("exec");
85  }
86  };
87 
88  }//(End) test fixture
89 
91  typedef HandlingPattern const& HaPatt;
92 
93 
94 
95 
96 
97 
98  /******************************************************************************/
108  class HandlingPatternBasics_test : public Test
109  {
110 
111  uint cnt_inst;
112 
113 
114  virtual void
115  run (Arg)
116  {
117  seedRand();
119  CHECK (&registry);
120 
121  cnt_inst = registry.instance_count();
122 
123  {
124  PCommandImpl pCom = buildTestCommand(registry);
125  checkExec (pCom);
126  checkUndo (pCom);
127 
128  useCustomHandler (pCom);
129  }
130 
131  CHECK (cnt_inst == registry.instance_count());
132  }
133 
134 
140  PCommandImpl
142  {
143 
144  typedef void Sig_oper(int);
145  typedef long Sig_capt(int);
146  typedef void Sig_undo(int,long);
147 
148  function<Sig_oper> o_Fun (command1::operate);
149  function<Sig_capt> c_Fun (command1::capture);
150  function<Sig_undo> u_Fun (command1::undoIt);
151 
152  CHECK (o_Fun && c_Fun && u_Fun);
153 
154  // when the CommandDef is complete, it issues the
155  // allocation call to the registry behind the scenes....
156 
157  PCommandImpl pImpl = registry.newCommandImpl(o_Fun,c_Fun,u_Fun);
158  CHECK (pImpl);
159  CHECK (*pImpl);
160  return pImpl;
161  }
162 
163 
165  void
166  checkExec (PCommandImpl com)
167  {
168  CHECK (com);
169  CHECK (!com->canExec());
170 
171  typedef Types<int> ArgType;
172  const int ARGR{1 + rani (1000)};
173  Tuple<ArgType> tuple(ARGR);
174  TypedArguments<Tuple<ArgType>> arg(tuple);
175  com->setArguments(arg);
176 
177  CHECK (com->canExec());
178  CHECK (!com->canUndo());
179  command1::check_ = 0;
180 
181  HaPatt patt = HandlingPattern::get(TEST_PATTERN);
182  ExecResult res = patt.exec (*com, TEST_CMD);
183 
184  CHECK (res);
185  CHECK (ARGR == command1::check_);
186  CHECK (com->canUndo());
187  }
188 
189 
191  void
192  checkUndo (PCommandImpl com)
193  {
194  CHECK (com);
195  CHECK (com->canExec());
196  CHECK (com->canUndo());
197 
198  CHECK (command1::check_ > 0);
199 
200  HaPatt ePatt = HandlingPattern::get(TEST_PATTERN);
201  ExecResult res = ePatt.undo (*com, TEST_CMD);
202 
203  CHECK (res);
204  CHECK (command1::check_ == 0);
205  }
206 
207 
211  void
212  useCustomHandler (PCommandImpl com)
213  {
214  CustomHandler specialHandler;
215 
216  CHECK (com->canExec());
217  CHECK (not specialHandler.invokedExec());
218 
219  specialHandler.exec (*com, TEST_CMD);
220  CHECK ( specialHandler.invokedExec());
221  CHECK (not specialHandler.invokedUndo());
222 
223  specialHandler.undo (*com, TEST_CMD);
224  CHECK ( specialHandler.invokedExec());
225  }
226  };
227 
228 
230  LAUNCHER (HandlingPatternBasics_test, "function controller");
231 
232 
233 }}} // 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:40
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:275
Managing command definitions and the storage of individual command objects.
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
Registry managing command implementation objects (Singleton).
Steam-Layer implementation namespace root.
PCommandImpl buildTestCommand(CommandRegistry &registry)
create a command implementation frame usable for tests.
Simplistic 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:690
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