2007-09-23 16:50:05 +02:00
|
|
|
/*
|
|
|
|
|
SingletonTestMock(Test) - using Singleton for injecting Test-Mocks
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2008-03-10 04:25:03 +01:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2007-09-23 16:50:05 +02:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
2010-12-17 23:28:49 +01:00
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
2007-09-23 16:50:05 +02:00
|
|
|
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.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2007-09-23 16:50:05 +02:00
|
|
|
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.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2007-09-23 16:50:05 +02:00
|
|
|
* *****************************************************/
|
|
|
|
|
|
2017-02-22 01:54:20 +01:00
|
|
|
/** @file singleton-testmock-test.cpp
|
2017-02-22 03:17:18 +01:00
|
|
|
** unit test \ref SingletonTestMock_test
|
2016-11-03 18:20:10 +01:00
|
|
|
*/
|
|
|
|
|
|
2007-09-23 16:50:05 +02:00
|
|
|
|
|
|
|
|
|
2008-12-18 04:47:41 +01:00
|
|
|
#include "lib/test/run.hpp"
|
2018-03-26 07:47:59 +02:00
|
|
|
#include "lib/depend-inject.hpp"
|
2008-12-18 04:47:41 +01:00
|
|
|
#include "lib/util.hpp"
|
2007-09-23 16:50:05 +02:00
|
|
|
|
2016-01-07 03:58:29 +01:00
|
|
|
#include "lib/format-cout.hpp"
|
|
|
|
|
#include "lib/format-string.hpp"
|
2007-09-23 16:50:05 +02:00
|
|
|
|
2016-01-07 03:58:29 +01:00
|
|
|
using util::_Fmt;
|
2007-09-23 16:50:05 +02:00
|
|
|
using util::isnil;
|
|
|
|
|
|
|
|
|
|
|
2009-09-30 00:27:14 +02:00
|
|
|
namespace lib {
|
|
|
|
|
namespace test{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Client Class normally to be instantiated as Singleton.
|
|
|
|
|
* But for tests, this class should be replaced by a Mock....
|
|
|
|
|
*/
|
2018-03-26 07:47:59 +02:00
|
|
|
class TestSingO
|
2007-09-23 16:50:05 +02:00
|
|
|
{
|
2013-10-14 01:18:56 +02:00
|
|
|
int callCnt_;
|
|
|
|
|
Symbol typid_;
|
2016-01-07 03:58:29 +01:00
|
|
|
_Fmt msg_;
|
2009-09-30 00:27:14 +02:00
|
|
|
|
|
|
|
|
public:
|
2018-03-26 07:47:59 +02:00
|
|
|
TestSingO(Symbol ty="TestSingO")
|
2013-10-14 01:18:56 +02:00
|
|
|
: callCnt_(0)
|
|
|
|
|
, typid_(ty)
|
|
|
|
|
, msg_("%s::doIt() call=%d\n")
|
|
|
|
|
{
|
|
|
|
|
TRACE (test, "ctor %s", typid_.c());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual
|
2018-03-26 07:47:59 +02:00
|
|
|
~TestSingO()
|
2013-10-14 01:18:56 +02:00
|
|
|
{
|
|
|
|
|
TRACE (test, "dtor %s", typid_.c());
|
|
|
|
|
}
|
2009-09-30 00:27:14 +02:00
|
|
|
|
|
|
|
|
void doIt ()
|
2013-10-14 01:18:56 +02:00
|
|
|
{
|
|
|
|
|
++callCnt_;
|
|
|
|
|
cout << msg_ % typid_ % callCnt_;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-30 00:27:14 +02:00
|
|
|
int getCnt ()
|
2013-10-14 01:18:56 +02:00
|
|
|
{
|
|
|
|
|
return callCnt_;
|
|
|
|
|
}
|
2009-09-30 00:27:14 +02:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mock-1 to replace the Client Class...
|
|
|
|
|
*/
|
2018-03-26 07:47:59 +02:00
|
|
|
struct Mock_1 : TestSingO
|
2009-09-30 00:27:14 +02:00
|
|
|
{
|
2018-03-26 07:47:59 +02:00
|
|
|
Mock_1() : TestSingO("Mock_1") { };
|
2009-09-30 00:27:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mock-2 to replace the Client Class...
|
2018-03-26 07:47:59 +02:00
|
|
|
* @note no default ctor
|
2009-09-30 00:27:14 +02:00
|
|
|
*/
|
2018-03-26 07:47:59 +02:00
|
|
|
struct Mock_2 : TestSingO
|
2009-09-30 00:27:14 +02:00
|
|
|
{
|
2018-03-26 07:47:59 +02:00
|
|
|
int id;
|
|
|
|
|
|
|
|
|
|
Mock_2(Literal specialID, int i)
|
|
|
|
|
: TestSingO{Symbol (_Fmt{"%s_%d"} % specialID % i)}
|
|
|
|
|
, id{i}
|
|
|
|
|
{ };
|
2009-09-30 00:27:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
/***************************************************************//**
|
2009-09-30 00:27:14 +02:00
|
|
|
* @test inject a Mock object into the Singleton Factory,
|
|
|
|
|
* to be returned and used in place of the original object.
|
2013-10-20 03:19:36 +02:00
|
|
|
* This test covers the full usage cycle: first access the
|
|
|
|
|
* Client Object, then replace it by two different mocks,
|
|
|
|
|
* and finally restore the original Client Object.
|
|
|
|
|
* @see lib::Depend
|
2018-03-26 07:47:59 +02:00
|
|
|
* @see depend-inject.hpp
|
2013-10-20 03:19:36 +02:00
|
|
|
* @see DependencyFactory_test
|
2009-09-30 00:27:14 +02:00
|
|
|
*/
|
|
|
|
|
class SingletonTestMock_test : public Test
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
void
|
2015-08-15 18:24:35 +02:00
|
|
|
run (Arg)
|
2007-09-24 16:20:41 +02:00
|
|
|
{
|
2018-03-26 07:47:59 +02:00
|
|
|
Depend<TestSingO> sing;
|
2009-09-30 00:27:14 +02:00
|
|
|
|
2012-01-20 00:05:12 +01:00
|
|
|
sing().doIt();
|
|
|
|
|
sing().doIt();
|
|
|
|
|
CHECK (sing().getCnt() == 2);
|
2009-09-30 00:27:14 +02:00
|
|
|
|
2018-03-26 07:47:59 +02:00
|
|
|
{
|
|
|
|
|
// shadow by local Mock instance
|
|
|
|
|
DependInject<TestSingO>::Local<Mock_1> mock_1;
|
|
|
|
|
sing().doIt();
|
|
|
|
|
sing().doIt();
|
|
|
|
|
sing().doIt();
|
|
|
|
|
sing().doIt();
|
|
|
|
|
sing().doIt();
|
|
|
|
|
CHECK (sing().getCnt() == 5);
|
|
|
|
|
|
|
|
|
|
// shadow again by different local Mock, this time with special ctor call
|
|
|
|
|
int instanceID = 0;
|
|
|
|
|
DependInject<TestSingO>::Local<Mock_2> mock_2 ([&]{ return new Mock_2{"Mock", instanceID}; });
|
|
|
|
|
|
|
|
|
|
// NOTE: the ctor call for the Mock really happens delayed...
|
2024-11-13 02:23:23 +01:00
|
|
|
instanceID = rani(10);
|
2018-03-26 07:47:59 +02:00
|
|
|
sing().doIt(); // ctor invoked on first access
|
|
|
|
|
CHECK (sing().getCnt() == 1);
|
|
|
|
|
|
|
|
|
|
// can access the Mock for instrumentation
|
|
|
|
|
CHECK (instanceID == mock_2->id);
|
|
|
|
|
|
|
|
|
|
}// original instance automatically un-shadowed here
|
2009-09-30 00:27:14 +02:00
|
|
|
|
2012-01-20 00:05:12 +01:00
|
|
|
CHECK (sing().getCnt() == 2);
|
|
|
|
|
sing().doIt();
|
|
|
|
|
CHECK (sing().getCnt() == 3);
|
2007-09-24 16:20:41 +02:00
|
|
|
}
|
2009-09-30 00:27:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
LAUNCHER (SingletonTestMock_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|