2017-03-31 21:12:12 +02:00
|
|
|
/*
|
|
|
|
|
CommandInstanceManager(Test) - verify helper for setup of actual command definitions
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2017, Hermann Vosseler <Ichthyostega@web.de>
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
/** @file command-instance-manager-test.cpp
|
|
|
|
|
** unit test \ref CommandInstanceManager_test
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
2017-03-31 23:15:29 +02:00
|
|
|
#include "proc/control/test-dummy-commands.hpp"
|
2017-03-31 21:12:12 +02:00
|
|
|
#include "proc/control/command-instance-manager.hpp"
|
|
|
|
|
#include "lib/format-string.hpp"
|
|
|
|
|
#include "lib/format-cout.hpp"
|
2017-03-31 23:31:24 +02:00
|
|
|
#include "lib/iter-stack.hpp"
|
2017-03-31 21:12:12 +02:00
|
|
|
#include "lib/util.hpp"
|
|
|
|
|
|
2017-03-31 23:31:24 +02:00
|
|
|
#include <algorithm>
|
2017-04-06 19:58:45 +02:00
|
|
|
#include <cstdlib>
|
2017-03-31 23:31:24 +02:00
|
|
|
#include <utility>
|
2017-03-31 21:12:12 +02:00
|
|
|
#include <string>
|
2017-03-31 23:31:24 +02:00
|
|
|
#include <deque>
|
2017-03-31 21:12:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace proc {
|
|
|
|
|
namespace control {
|
|
|
|
|
namespace test {
|
|
|
|
|
|
|
|
|
|
using lib::Literal;
|
|
|
|
|
using std::string;
|
|
|
|
|
using util::_Fmt;
|
2017-03-31 23:31:24 +02:00
|
|
|
using std::move;
|
2017-04-06 19:58:45 +02:00
|
|
|
using std::rand;
|
2017-03-31 21:12:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace { // Test fixture....
|
|
|
|
|
|
2017-03-31 23:15:29 +02:00
|
|
|
const Symbol COMMAND_PROTOTYPE = test_Dummy_command1;
|
|
|
|
|
const string INVOCATION_ID = "CommandInstanceManager_test";
|
|
|
|
|
|
|
|
|
|
class Fixture
|
|
|
|
|
: public CommandDispatch
|
2017-03-31 21:12:12 +02:00
|
|
|
{
|
2017-03-31 23:31:24 +02:00
|
|
|
std::deque<Command> queue_;
|
2017-03-31 21:12:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/* == Interface: CommandDispatch == */
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
clear() override
|
|
|
|
|
{
|
2017-03-31 23:31:24 +02:00
|
|
|
queue_.clear();
|
2017-03-31 21:12:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2017-03-31 23:31:24 +02:00
|
|
|
enqueue (Command cmd) override
|
2017-03-31 21:12:12 +02:00
|
|
|
{
|
2017-03-31 23:31:24 +02:00
|
|
|
queue_.emplace_front(move (cmd));
|
2017-03-31 21:12:12 +02:00
|
|
|
}
|
2017-03-31 23:15:29 +02:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
bool
|
2017-04-01 18:39:53 +02:00
|
|
|
contains (Command const& ref)
|
2017-03-31 23:15:29 +02:00
|
|
|
{
|
2017-03-31 23:31:24 +02:00
|
|
|
return queue_.end()!= std::find_if (queue_.begin()
|
|
|
|
|
,queue_.end()
|
|
|
|
|
,[=](Command const& elm)
|
|
|
|
|
{
|
2017-04-01 18:39:53 +02:00
|
|
|
return elm == ref;
|
2017-03-31 23:31:24 +02:00
|
|
|
});
|
2017-03-31 23:15:29 +02:00
|
|
|
}
|
2017-03-31 21:12:12 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************//**
|
|
|
|
|
* @test CommandInstanceManager is responsible for providing individual
|
|
|
|
|
* clone copies from a basic command definition, to be bound with
|
|
|
|
|
* actual arguments and finally handed over to the ProcDispatcher
|
|
|
|
|
* for invocation.
|
|
|
|
|
*
|
|
|
|
|
* @see proc::control::CommandInstanceManager
|
|
|
|
|
*/
|
|
|
|
|
class CommandInstanceManager_test : public Test
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
run (Arg)
|
|
|
|
|
{
|
|
|
|
|
verify_standardUsage();
|
2017-04-06 19:58:45 +02:00
|
|
|
verify_instanceIdentity();
|
|
|
|
|
verify_duplicates();
|
|
|
|
|
verify_lifecycle();
|
2017-03-31 21:12:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-04-06 19:58:45 +02:00
|
|
|
/** @test demonstrate the command instance standard usage pattern.*/
|
2017-03-31 21:12:12 +02:00
|
|
|
void
|
|
|
|
|
verify_standardUsage()
|
|
|
|
|
{
|
|
|
|
|
Fixture fixture;
|
|
|
|
|
CommandInstanceManager iManager{fixture};
|
2017-04-06 19:58:45 +02:00
|
|
|
Symbol instanceID = iManager.newInstance (COMMAND_PROTOTYPE, INVOCATION_ID);
|
2017-03-31 21:12:12 +02:00
|
|
|
|
2017-04-06 19:58:45 +02:00
|
|
|
Command cmd = iManager.getInstance (instanceID);
|
2017-04-01 00:37:12 +02:00
|
|
|
CHECK (cmd);
|
|
|
|
|
CHECK (not cmd.canExec());
|
|
|
|
|
|
2017-03-31 23:31:24 +02:00
|
|
|
cmd.bind(42);
|
2017-04-01 00:37:12 +02:00
|
|
|
CHECK (cmd.canExec());
|
2017-03-31 21:12:12 +02:00
|
|
|
|
|
|
|
|
iManager.dispatch (instanceID);
|
2017-04-01 18:39:53 +02:00
|
|
|
CHECK (fixture.contains (cmd));
|
2017-03-31 21:12:12 +02:00
|
|
|
CHECK (not iManager.contains (instanceID));
|
2017-04-01 00:37:12 +02:00
|
|
|
VERIFY_ERROR (INVALID_COMMAND, iManager.getInstance (instanceID));
|
2017-03-31 21:12:12 +02:00
|
|
|
}
|
2017-04-06 19:58:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test relation of command, instanceID and concrete instance
|
|
|
|
|
* The CommandInstanceManager provides the notion of a _current instance,_
|
|
|
|
|
* which can then be used to bind arguments. When done, it will be _dispatched,_
|
|
|
|
|
* and then go through the ProcDispatcher's CommandQueue (in this test, we use
|
|
|
|
|
* just a dummy Fixture, which only enqueues the dispatched commands.
|
|
|
|
|
*
|
|
|
|
|
* The following notions need to be kept apart
|
|
|
|
|
* - a *command* is the operation _definition_. It is registered with a commandID.
|
|
|
|
|
* - the *instance ID* is a decorated commandID and serves to keep different
|
|
|
|
|
* usage contexts of the same command (prototype) apart. For each instanceID
|
|
|
|
|
* there is at any given time maximally _one_ concrete instance "opened"
|
|
|
|
|
* - the *concrete command instance* is what can be bound and executed.
|
|
|
|
|
* It retains it's own identity, even after being handed over for dispatch.
|
|
|
|
|
* Consequently, a given instance can sit in the dispatcher queue to await
|
|
|
|
|
* invocation, while the next instance for the _same instance ID_ is already
|
|
|
|
|
* opened in the CommandInstanceManager for binding arguments.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
verify_instanceIdentity()
|
|
|
|
|
{
|
|
|
|
|
Fixture fixture;
|
|
|
|
|
CommandInstanceManager iManager{fixture};
|
|
|
|
|
Symbol i1 = iManager.newInstance (COMMAND_PROTOTYPE, "i1");
|
|
|
|
|
Symbol i2 = iManager.newInstance (COMMAND_PROTOTYPE, "i2");
|
|
|
|
|
|
|
|
|
|
Command c11 = iManager.getInstance (i1);
|
|
|
|
|
Command c12 = iManager.getInstance (i1);
|
|
|
|
|
CHECK (c11 == c12);
|
|
|
|
|
CHECK (c11.isValid());
|
|
|
|
|
CHECK (not c11.canExec());
|
|
|
|
|
|
|
|
|
|
int r1{rand()}, r2{rand()}, r3{rand()};
|
|
|
|
|
command1::check_ = 0; // commands will add to this on invocation
|
|
|
|
|
|
|
|
|
|
c11.bind (r1);
|
|
|
|
|
CHECK (c12.canExec());
|
|
|
|
|
CHECK (c11.canExec());
|
|
|
|
|
|
|
|
|
|
Command c2 = iManager.getInstance (i2);
|
|
|
|
|
CHECK (c2 != c11);
|
|
|
|
|
CHECK (c2 != c12);
|
|
|
|
|
c2.bind (r2);
|
|
|
|
|
|
|
|
|
|
CHECK (iManager.contains (i1));
|
|
|
|
|
CHECK (iManager.contains (i2));
|
|
|
|
|
CHECK (not fixture.contains (c11));
|
|
|
|
|
CHECK (not fixture.contains (c12));
|
|
|
|
|
CHECK (not fixture.contains (c2));
|
|
|
|
|
|
|
|
|
|
iManager.dispatch (i1);
|
|
|
|
|
CHECK (not iManager.contains (i1));
|
|
|
|
|
CHECK ( iManager.contains (i2));
|
|
|
|
|
CHECK ( fixture.contains (c11));
|
|
|
|
|
CHECK ( fixture.contains (c12));
|
|
|
|
|
CHECK (not fixture.contains (c2));
|
|
|
|
|
|
|
|
|
|
CHECK (command1::check_ == 0);
|
|
|
|
|
|
|
|
|
|
Symbol i11 = iManager.newInstance (COMMAND_PROTOTYPE, "i1");
|
|
|
|
|
CHECK (i11 == i1);
|
|
|
|
|
CHECK ((const char*)i11 == (const char*) i1);
|
|
|
|
|
|
|
|
|
|
// but the instances themselves are disjoint
|
|
|
|
|
Command c13 = iManager.getInstance (i1);
|
|
|
|
|
CHECK (c13 != c11);
|
|
|
|
|
CHECK (c13 != c12);
|
|
|
|
|
CHECK (c11.canExec());
|
|
|
|
|
CHECK (not c13.canExec());
|
|
|
|
|
|
|
|
|
|
c13.bind (r3);
|
|
|
|
|
CHECK (c13.canExec());
|
|
|
|
|
|
|
|
|
|
CHECK (command1::check_ == 0);
|
|
|
|
|
c12();
|
|
|
|
|
CHECK (command1::check_ == 0+r1);
|
|
|
|
|
|
|
|
|
|
// even a command still in instance manager can be invoked
|
|
|
|
|
c2();
|
|
|
|
|
CHECK (command1::check_ == 0+r1+r2);
|
|
|
|
|
|
|
|
|
|
CHECK ( iManager.contains (i1));
|
|
|
|
|
CHECK ( iManager.contains (i2));
|
|
|
|
|
CHECK ( fixture.contains (c11));
|
|
|
|
|
CHECK ( fixture.contains (c12));
|
|
|
|
|
CHECK (not fixture.contains (c2));
|
|
|
|
|
|
|
|
|
|
iManager.dispatch (i2);
|
|
|
|
|
iManager.dispatch (i11);
|
|
|
|
|
CHECK (not iManager.contains (i1));
|
|
|
|
|
CHECK (not iManager.contains (i2));
|
|
|
|
|
CHECK ( fixture.contains (c11));
|
|
|
|
|
CHECK ( fixture.contains (c12));
|
|
|
|
|
CHECK ( fixture.contains (c13));
|
|
|
|
|
CHECK ( fixture.contains (c2));
|
|
|
|
|
|
|
|
|
|
// if we continue to hold onto an instance,
|
|
|
|
|
// we can do anything with it. Like re-binding arguments.
|
|
|
|
|
c2.bind (47);
|
|
|
|
|
c2();
|
|
|
|
|
c13();
|
|
|
|
|
c13();
|
|
|
|
|
CHECK (command1::check_ == 0+r1+r2+47+r3+r3);
|
|
|
|
|
|
|
|
|
|
c11.undo();
|
|
|
|
|
CHECK (command1::check_ == 0);
|
|
|
|
|
c2.undo();
|
|
|
|
|
CHECK (command1::check_ == 0+r1);
|
|
|
|
|
c11.undo();
|
|
|
|
|
CHECK (command1::check_ == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-04-06 20:12:31 +02:00
|
|
|
/** @test there can be only one active "opened" instance
|
|
|
|
|
* The CommandInstanceManager opens (creates) a new instance by cloning from the prototype.
|
|
|
|
|
* Unless this instance is dispatched, it does not allow to open a further instance
|
|
|
|
|
* (for the same instanceID). But of course it allows to open a different instance from
|
|
|
|
|
* the same prototype, but with a different invocationID and hence a different instanceID
|
|
|
|
|
*/
|
2017-04-06 19:58:45 +02:00
|
|
|
void
|
|
|
|
|
verify_duplicates()
|
|
|
|
|
{
|
2017-04-06 20:12:31 +02:00
|
|
|
Fixture fixture;
|
|
|
|
|
CommandInstanceManager iManager{fixture};
|
|
|
|
|
Symbol i1 = iManager.newInstance (COMMAND_PROTOTYPE, "i1");
|
|
|
|
|
Symbol i2 = iManager.newInstance (COMMAND_PROTOTYPE, "i2");
|
|
|
|
|
|
|
|
|
|
VERIFY_ERROR (DUPLICATE_COMMAND, iManager.newInstance (COMMAND_PROTOTYPE, "i1"));
|
|
|
|
|
VERIFY_ERROR (DUPLICATE_COMMAND, iManager.newInstance (COMMAND_PROTOTYPE, "i2"));
|
|
|
|
|
|
|
|
|
|
Command c11 = iManager.getInstance (i1);
|
|
|
|
|
c11.bind(-1);
|
|
|
|
|
iManager.dispatch (i1);
|
|
|
|
|
|
|
|
|
|
iManager.newInstance (COMMAND_PROTOTYPE, "i1");
|
|
|
|
|
VERIFY_ERROR (DUPLICATE_COMMAND, iManager.newInstance (COMMAND_PROTOTYPE, "i2"));
|
|
|
|
|
|
|
|
|
|
CHECK (iManager.getInstance (i1));
|
|
|
|
|
CHECK (iManager.getInstance (i2));
|
2017-04-06 19:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
void
|
|
|
|
|
verify_lifecycle()
|
|
|
|
|
{
|
|
|
|
|
UNIMPLEMENTED ("lifecycle sanity");
|
|
|
|
|
}
|
2017-03-31 21:12:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
LAUNCHER (CommandInstanceManager_test, "unit controller");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}}} // namespace proc::control::test
|