Add unit test for the deferred virtual call implementation in SizeNotifiable.
Fix the build as necessary. Change DeferredRegister() to take no construction parameters.
This commit is contained in:
parent
0a3897f60a
commit
10f830809b
11 changed files with 164 additions and 16 deletions
|
@ -43,7 +43,7 @@ namespace cloonel {
|
|||
Placeable(float2(0.0f)),
|
||||
Drawable(parSize),
|
||||
m_bottomBar(float2(0.0f), parSize.x()),
|
||||
m_screenRatio(parMain, DeferredRegister(&m_screenRatio)),
|
||||
m_screenRatio(parMain, DeferredRegister()),
|
||||
m_bounceCallback(&DoNothing),
|
||||
m_texture(new Texture(parPath, parMain, false))
|
||||
#if defined(WITH_DEBUG_VISUALS)
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
#include "tiledwallpaper.hpp"
|
||||
#include "texture.hpp"
|
||||
#include "platformspawner.hpp"
|
||||
#include "CloonelJumpConfig.h"
|
||||
#include "CloonelJumpProjectConfig.h"
|
||||
#include <algorithm>
|
||||
#include <SDL2/SDL_scancode.h>
|
||||
#include <ciso646>
|
||||
|
|
|
@ -28,7 +28,7 @@ namespace cloonel {
|
|||
///--------------------------------------------------------------------------
|
||||
Platform::Platform (SDLMain* parSdlMain, const float2& parPos, Texture* parTexture, const float2& parSize) :
|
||||
Placeable(parPos),
|
||||
m_screenRatio(parSdlMain, &m_screenRatio),
|
||||
m_screenRatio(parSdlMain, DeferredRegister()),
|
||||
m_size(parSize),
|
||||
m_collisionTop(new HorzCollisionBar(parPos, parSize.x())),
|
||||
m_surface(parTexture)
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
|
||||
#include "platformset.hpp"
|
||||
#include "platform.hpp"
|
||||
#include "CloonelJumpConfig.h"
|
||||
#include "CloonelJumpProjectConfig.h"
|
||||
#include "casts.hpp"
|
||||
#include <ciso646>
|
||||
#include <algorithm>
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
|
||||
#include "platformspawner.hpp"
|
||||
#include "platform.hpp"
|
||||
#include "CloonelJumpConfig.h"
|
||||
#include "CloonelJumpProjectConfig.h"
|
||||
#include "texture.hpp"
|
||||
#include "gameplayscene.hpp"
|
||||
#include "mover.hpp"
|
||||
|
|
|
@ -67,7 +67,7 @@ namespace cloonel {
|
|||
///--------------------------------------------------------------------------
|
||||
DeferredRegister::~DeferredRegister() {
|
||||
assert(m_call);
|
||||
if (not std::uncaught_exception())
|
||||
if (m_call and not std::uncaught_exception())
|
||||
m_call();
|
||||
}
|
||||
} //namespace cloonel
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright 2014 Michele "King_DuckZ" Santullo
|
||||
Copyright 2014-2016 Michele "King_DuckZ" Santullo
|
||||
|
||||
This file is part of CloonelJump.
|
||||
|
||||
|
@ -25,6 +25,7 @@
|
|||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <cassert>
|
||||
#include <ciso646>
|
||||
|
||||
namespace cloonel {
|
||||
class SizeRatio;
|
||||
|
@ -75,12 +76,14 @@ namespace cloonel {
|
|||
|
||||
class DeferredRegister {
|
||||
public:
|
||||
template <typename R>
|
||||
DeferredRegister ( SizeNotifiable<R>* parObject ) noexcept;
|
||||
DeferredRegister ( void ) noexcept = default;
|
||||
~DeferredRegister ( void );
|
||||
|
||||
template <typename R>
|
||||
void SetThis ( SizeNotifiable<R>* parObject ) const noexcept;
|
||||
|
||||
private:
|
||||
std::function<void()> m_call;
|
||||
mutable std::function<void()> m_call;
|
||||
};
|
||||
|
||||
class SizeNotifiableBase {
|
||||
|
@ -112,7 +115,7 @@ namespace cloonel {
|
|||
{
|
||||
}
|
||||
explicit SizeNotifiable ( const DeferredRegister& parRegisterFunctor ) {
|
||||
static_cast<void>(parRegisterFunctor);
|
||||
parRegisterFunctor.SetThis(this);
|
||||
}
|
||||
virtual ~SizeNotifiable ( void ) noexcept {
|
||||
this->Unregister();
|
||||
|
@ -133,7 +136,7 @@ namespace cloonel {
|
|||
SizeNotifiable ( SDLMain* parSdlMain, const DeferredRegister& parRegisterFunctor ) :
|
||||
RegisterBehaviour(parSdlMain)
|
||||
{
|
||||
static_cast<void>(parRegisterFunctor);
|
||||
parRegisterFunctor.SetThis(this);
|
||||
}
|
||||
virtual ~SizeNotifiable ( void ) noexcept {
|
||||
this->Unregister();
|
||||
|
@ -141,9 +144,9 @@ namespace cloonel {
|
|||
};
|
||||
|
||||
template <typename R>
|
||||
DeferredRegister::DeferredRegister (SizeNotifiable<R>* parObject) noexcept :
|
||||
m_call(std::bind(&R::Register, static_cast<R*>(parObject), parObject))
|
||||
{
|
||||
void DeferredRegister::SetThis (SizeNotifiable<R>* parObject) const noexcept {
|
||||
assert(not m_call);
|
||||
m_call = std::bind(&R::Register, static_cast<R*>(parObject), parObject);
|
||||
assert(m_call);
|
||||
}
|
||||
} //namespace cloonel
|
||||
|
|
|
@ -98,7 +98,7 @@ namespace cloonel {
|
|||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
TiledWallpaper::TileCountNotifiable::TileCountNotifiable (SDLMain* parMain, const ushort2& parTileSize) :
|
||||
BaseClass(parMain, this),
|
||||
BaseClass(parMain, DeferredRegister()),
|
||||
m_tileCount(CountTilesInScreen(parMain->WidthHeight(), parTileSize)),
|
||||
m_tileSize(vector_cast<float2>(parTileSize))
|
||||
#if defined(WITH_DEBUG_VISUALS)
|
||||
|
|
|
@ -2,6 +2,7 @@ project(clooneltest CXX)
|
|||
|
||||
add_executable(${PROJECT_NAME}
|
||||
main.cpp
|
||||
deferred_virtual_call.cpp
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
|
|
125
test/unit/deferred_virtual_call.cpp
Normal file
125
test/unit/deferred_virtual_call.cpp
Normal file
|
@ -0,0 +1,125 @@
|
|||
/*
|
||||
Copyright 2014-2016 Michele "King_DuckZ" Santullo
|
||||
|
||||
This file is part of CloonelJump.
|
||||
|
||||
CloonelJump 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 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
CloonelJump 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 CloonelJump. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "catch.hpp"
|
||||
#include "sizenotifiable.hpp"
|
||||
#include "sizeratio.hpp"
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
|
||||
class DummyRegister {
|
||||
public:
|
||||
enum { SDLMAIN_NEEDED = false };
|
||||
|
||||
DummyRegister() = default;
|
||||
void Register (cloonel::SizeNotifiableBase* parNotifiable) {
|
||||
using cloonel::SizeRatio;
|
||||
using cloonel::float2;
|
||||
|
||||
float2 orig(1024.0f, 768.0f);
|
||||
float2 size(123.0f, 456.0f);
|
||||
parNotifiable->NotifyResChanged(SizeRatio(orig, size));
|
||||
}
|
||||
|
||||
void Unregister() {}
|
||||
};
|
||||
|
||||
class NotifiableA : public cloonel::SizeNotifiable<::DummyRegister> {
|
||||
public:
|
||||
typedef cloonel::SizeNotifiable<::DummyRegister> BaseClass;
|
||||
|
||||
NotifiableA (bool parThrow, bool* parOut, const cloonel::DeferredRegister& parDeferred) :
|
||||
BaseClass(parDeferred),
|
||||
notif_a_called(false),
|
||||
m_out_a(parOut)
|
||||
{
|
||||
if (parThrow)
|
||||
throw std::runtime_error("Throwing exception because parThrow=true");
|
||||
}
|
||||
|
||||
bool notif_a_called;
|
||||
|
||||
private:
|
||||
bool* m_out_a;
|
||||
|
||||
virtual void OnResChanged (const cloonel::SizeRatio&) override {
|
||||
notif_a_called = true;
|
||||
if (m_out_a)
|
||||
*m_out_a = true;
|
||||
}
|
||||
};
|
||||
|
||||
class NotifiableB : public NotifiableA {
|
||||
public:
|
||||
NotifiableB (bool parThrow, bool* parOutA, bool* parOutB, const cloonel::DeferredRegister& parDeferred) :
|
||||
NotifiableA(parThrow, parOutA, parDeferred),
|
||||
notif_b_called(false),
|
||||
m_out_b(parOutB)
|
||||
{}
|
||||
|
||||
bool notif_b_called;
|
||||
|
||||
private:
|
||||
bool* m_out_b;
|
||||
|
||||
virtual void OnResChanged (const cloonel::SizeRatio&) override {
|
||||
notif_b_called = true;
|
||||
if (m_out_b)
|
||||
*m_out_b = true;
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE ("Check the deferred virtual call mechanism in SizeNotifiable", "[core]") {
|
||||
using cloonel::DeferredRegister;
|
||||
|
||||
{
|
||||
NotifiableB b(false, nullptr, nullptr, DeferredRegister());
|
||||
REQUIRE( b.notif_b_called );
|
||||
REQUIRE( !b.notif_a_called );
|
||||
}
|
||||
{
|
||||
NotifiableA a(false, nullptr, DeferredRegister());
|
||||
REQUIRE( a.notif_a_called );
|
||||
}
|
||||
|
||||
{
|
||||
bool notif_a = false;
|
||||
bool notif_b = false;
|
||||
|
||||
//Check that the test mechanism works (sets the bool pointers)
|
||||
{
|
||||
NotifiableB b(false, ¬if_a, ¬if_b, DeferredRegister());
|
||||
REQUIRE( b.notif_b_called );
|
||||
REQUIRE( not notif_a );
|
||||
REQUIRE( notif_b );
|
||||
}
|
||||
|
||||
notif_a = notif_b = false;
|
||||
std::unique_ptr<NotifiableB> notifiable;
|
||||
try {
|
||||
notifiable.reset(new NotifiableB(true, ¬if_a, ¬if_b, DeferredRegister()));
|
||||
REQUIRE( false ); //not reached, constructor should throw
|
||||
}
|
||||
catch (const std::runtime_error& e) {
|
||||
REQUIRE( e.what() == std::string("Throwing exception because parThrow=true") );
|
||||
}
|
||||
REQUIRE( not notif_a );
|
||||
REQUIRE( not notif_b );
|
||||
}
|
||||
}
|
|
@ -1,2 +1,21 @@
|
|||
/*
|
||||
Copyright 2014-2016 Michele "King_DuckZ" Santullo
|
||||
|
||||
This file is part of CloonelJump.
|
||||
|
||||
CloonelJump 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 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
CloonelJump 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 CloonelJump. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#include "catch.hpp"
|
||||
|
|
Loading…
Reference in a new issue