1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-14 10:12:59 +02:00
vcmi/test/events/EventBusTest.cpp
Alexander Wilms 73019c204d Replace redundant types with auto for the lvalues of template factory functions for smart pointers
grep -r --include \*.h --include \*.cpp "= std::" * | grep -v auto | grep -Po ".*[^ ]+ [^ ]+ [^ ]*[ ]*=.*;" | grep -v "auto\|int\|char\|bool\|float|\double\|for\|if\|googletest\|fuzzylite\|size_t\|using\|return" | grep -v double | grep -v si64 | grep -v si32 | grep -v ui32 | grep \< | grep -v float | tr -d '\t' | grep -v assert > redundant_types.txt

import re

with open("redundant_types.txt") as f:
    for line in f:
        line = line.strip()
        path = line.split(":", 1)[0]
        original_code = ":".join(line.split(":")[1:]).strip()

        print()
        print(path)
        print(original_code)
        prefix = "auto "
        if original_code.startswith("static"):
            static = True
        else:
            static = False

        cpp_type = " ".join(original_code.split("=")[0].strip().split(" ")[0:-1])
        print(cpp_type)

        if static:
            new_code = "static auto "+ " ".join(original_code.split(" ")[2:])
        else:
            new_code = "auto "+ " ".join(original_code.split(" ")[1:])
        print(new_code)

        if True:
            with open(path, "r") as f:
                filedata = f.read()

            filedata = filedata.replace(original_code, new_code)

            with open(path, "w") as f:
                f.write(filedata)
2024-01-17 12:50:00 +00:00

158 lines
5.1 KiB
C++

/*
* EventBusTest.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include <vcmi/events/Event.h>
#include <vcmi/events/EventBus.h>
namespace test
{
using namespace ::testing;
using namespace ::events;
class EventExample : public Event
{
public:
using PreHandler = SubscriptionRegistry<EventExample>::PreHandler;
using PostHandler = SubscriptionRegistry<EventExample>::PostHandler;
using ExecHandler = SubscriptionRegistry<EventExample>::ExecHandler;
MOCK_CONST_METHOD0(isEnabled, bool());
public:
static SubscriptionRegistry<EventExample> * getRegistry()
{
static auto Instance = std::make_unique<SubscriptionRegistry<EventExample>>();
return Instance.get();
}
friend class SubscriptionRegistry<EventExample>;
};
class ListenerMock
{
public:
MOCK_METHOD1(beforeEvent, void(EventExample &));
MOCK_METHOD1(onEvent, void(EventExample &));
MOCK_METHOD1(afterEvent, void(const EventExample &));
};
class EventBusTest : public Test
{
public:
EventExample event1;
EventExample event2;
EventBus subject1;
EventBus subject2;
StrictMock<ListenerMock> listener;
StrictMock<ListenerMock> listener1;
StrictMock<ListenerMock> listener2;
};
TEST_F(EventBusTest, ExecuteNoListeners)
{
EXPECT_CALL(event1, isEnabled()).WillRepeatedly(Return(true));
subject1.executeEvent(event1);
}
TEST_F(EventBusTest, ExecuteNoListenersWithHandler)
{
EXPECT_CALL(event1, isEnabled()).WillRepeatedly(Return(true));
EXPECT_CALL(listener, onEvent(Ref(event1))).Times(1);
subject1.executeEvent(event1, std::bind(&ListenerMock::onEvent, &listener, _1));
}
TEST_F(EventBusTest, ExecuteIgnoredSubscription)
{
EXPECT_CALL(event1, isEnabled()).WillRepeatedly(Return(true));
subject1.subscribeBefore<EventExample>(std::bind(&ListenerMock::beforeEvent, &listener, _1));
subject1.subscribeAfter<EventExample>(std::bind(&ListenerMock::afterEvent, &listener, _1));
EXPECT_CALL(listener, beforeEvent(_)).Times(0);
EXPECT_CALL(listener, afterEvent(_)).Times(0);
subject1.executeEvent(event1);
}
TEST_F(EventBusTest, ExecuteSequence)
{
EXPECT_CALL(event1, isEnabled()).WillRepeatedly(Return(true));
auto subscription1 = subject1.subscribeBefore<EventExample>(std::bind(&ListenerMock::beforeEvent, &listener1, _1));
auto subscription2 = subject1.subscribeAfter<EventExample>(std::bind(&ListenerMock::afterEvent, &listener1, _1));
auto subscription3 = subject1.subscribeBefore<EventExample>(std::bind(&ListenerMock::beforeEvent, &listener2, _1));
auto subscription4 = subject1.subscribeAfter<EventExample>(std::bind(&ListenerMock::afterEvent, &listener2, _1));
{
InSequence sequence;
EXPECT_CALL(listener1, beforeEvent(Ref(event1))).Times(1);
EXPECT_CALL(listener2, beforeEvent(Ref(event1))).Times(1);
EXPECT_CALL(listener, onEvent(Ref(event1))).Times(1);
EXPECT_CALL(listener1, afterEvent(Ref(event1))).Times(1);
EXPECT_CALL(listener2, afterEvent(Ref(event1))).Times(1);
}
subject1.executeEvent(event1, std::bind(&ListenerMock::onEvent, &listener, _1));
}
TEST_F(EventBusTest, BusesAreIndependent)
{
EXPECT_CALL(event1, isEnabled()).WillRepeatedly(Return(true));
auto subscription1 = subject1.subscribeBefore<EventExample>(std::bind(&ListenerMock::beforeEvent, &listener1, _1));
auto subscription2 = subject1.subscribeAfter<EventExample>(std::bind(&ListenerMock::afterEvent, &listener1, _1));
auto subscription3 = subject2.subscribeBefore<EventExample>(std::bind(&ListenerMock::beforeEvent, &listener2, _1));
auto subscription4 = subject2.subscribeAfter<EventExample>(std::bind(&ListenerMock::afterEvent, &listener2, _1));
EXPECT_CALL(listener1, beforeEvent(_)).Times(1);
EXPECT_CALL(listener2, beforeEvent(_)).Times(0);
EXPECT_CALL(listener1, afterEvent(_)).Times(1);
EXPECT_CALL(listener2, afterEvent(_)).Times(0);
subject1.executeEvent(event1);
}
TEST_F(EventBusTest, DisabledTestDontExecute)
{
EXPECT_CALL(event1, isEnabled()).Times(AtLeast(1)).WillRepeatedly(Return(false));
EXPECT_CALL(listener, onEvent(Ref(event1))).Times(0);
subject1.executeEvent(event1, std::bind(&ListenerMock::onEvent, &listener, _1));
}
TEST_F(EventBusTest, DisabledTestDontExecutePostHandler)
{
EXPECT_CALL(event1, isEnabled()).Times(AtLeast(1)).WillRepeatedly(Return(false));
EXPECT_CALL(listener, onEvent(Ref(event1))).WillRepeatedly(Return());
EXPECT_CALL(listener1, afterEvent(Ref(event1))).Times(0);
auto subscription1 = subject1.subscribeAfter<EventExample>(std::bind(&ListenerMock::afterEvent, &listener1, _1));
subject1.executeEvent(event1, std::bind(&ListenerMock::onEvent, &listener, _1));
}
TEST_F(EventBusTest, DisabledTestExecutePreHandler)
{
EXPECT_CALL(event1, isEnabled()).Times(AtLeast(1)).WillRepeatedly(Return(false));
EXPECT_CALL(listener, onEvent(Ref(event1))).WillRepeatedly(Return());
EXPECT_CALL(listener1, beforeEvent(Ref(event1))).Times(1);
auto subscription1 = subject1.subscribeBefore<EventExample>(std::bind(&ListenerMock::beforeEvent, &listener1, _1));
subject1.executeEvent(event1, std::bind(&ListenerMock::onEvent, &listener, _1));
}
//TODO: test sending event to destroyed event bus
}