2020-12-12 16:55:27 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <gmock/gmock.h>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <mm/engine.hpp>
|
|
|
|
#include <mm/update_strategies/update_strategy.hpp>
|
|
|
|
|
|
|
|
#include <entt/core/hashed_string.hpp>
|
|
|
|
|
|
|
|
using ::testing::Return;
|
|
|
|
using ::testing::_;
|
|
|
|
|
2020-12-12 18:21:45 +01:00
|
|
|
using namespace entt::literals;
|
|
|
|
|
2020-12-12 16:55:27 +01:00
|
|
|
class MockUpdateStrategy : public MM::UpdateStrategies::UpdateStrategy {
|
|
|
|
public:
|
2021-02-16 20:59:47 +01:00
|
|
|
const char* name(void) override { return "MockUpdateStrategy"; }
|
|
|
|
|
2020-12-12 16:55:27 +01:00
|
|
|
// protected:
|
|
|
|
MOCK_METHOD(void, doUpdate, (MM::Engine& engine), (override));
|
|
|
|
|
2021-04-28 19:38:25 +02:00
|
|
|
MOCK_METHOD(bool, enableService, (const entt::id_type s_id, std::vector<MM::UpdateStrategies::TaskInfo>&& task_array), (override));
|
2020-12-12 16:55:27 +01:00
|
|
|
MOCK_METHOD(bool, disableService, (const entt::id_type s_id), (override));
|
|
|
|
|
|
|
|
// public:
|
2021-04-24 02:03:11 +02:00
|
|
|
MOCK_METHOD(void, addDeferred, (std::function<void(MM::Engine&)> function), (override));
|
|
|
|
MOCK_METHOD(void, addAsync, (std::function<void(MM::Engine&)> function), (override));
|
2021-04-28 19:38:25 +02:00
|
|
|
|
|
|
|
MOCK_METHOD(void, forEachTask, (std::function<bool(MM::UpdateStrategies::TaskInfo&)> function), (override));
|
2020-12-12 16:55:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class MockService : public MM::Services::Service {
|
|
|
|
public:
|
|
|
|
const char* name(void) override { return "MockService"; }
|
|
|
|
|
2021-04-28 19:38:25 +02:00
|
|
|
MOCK_METHOD(bool, enable, (MM::Engine& engine, std::vector<MM::UpdateStrategies::TaskInfo>& task_array), (override));
|
2020-12-12 16:55:27 +01:00
|
|
|
MOCK_METHOD(void, disable, (MM::Engine& engine), (override));
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(engine_mock, update_strategy_run) {
|
|
|
|
auto mock = std::make_unique<MockUpdateStrategy>();
|
|
|
|
|
|
|
|
EXPECT_CALL(*mock, doUpdate(_))
|
|
|
|
.Times(1);
|
|
|
|
|
|
|
|
MM::Engine engine(std::move(mock));
|
|
|
|
|
|
|
|
engine.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(engine_mock, service_update_strategy) {
|
|
|
|
auto mock = std::make_unique<MockUpdateStrategy>();
|
|
|
|
|
2021-04-28 19:38:25 +02:00
|
|
|
EXPECT_CALL(*mock, enableService(_, _))
|
2020-12-12 16:55:27 +01:00
|
|
|
.Times(1);
|
|
|
|
EXPECT_CALL(*mock, disableService(_))
|
|
|
|
.Times(1);
|
|
|
|
|
|
|
|
class TmpMockService : public MockService {
|
|
|
|
public:
|
|
|
|
TmpMockService(void) {
|
2021-04-28 19:38:25 +02:00
|
|
|
EXPECT_CALL(*this, enable(_, _))
|
2020-12-12 16:55:27 +01:00
|
|
|
.Times(1);
|
2021-04-28 19:38:25 +02:00
|
|
|
ON_CALL(*this, enable(_, _))
|
2020-12-12 16:55:27 +01:00
|
|
|
.WillByDefault(Return(true));
|
|
|
|
|
|
|
|
EXPECT_CALL(*this, disable(_))
|
|
|
|
.Times(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
MM::Engine engine(std::move(mock));
|
|
|
|
|
|
|
|
engine.addService<TmpMockService>();
|
|
|
|
|
|
|
|
ASSERT_TRUE(engine.enableService<TmpMockService>());
|
|
|
|
engine.disableService<TmpMockService>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(engine_mock, service_update_strategy_run_1) {
|
|
|
|
auto mock = std::make_unique<MockUpdateStrategy>();
|
|
|
|
|
2021-04-28 19:38:25 +02:00
|
|
|
EXPECT_CALL(*mock, enableService(_, _))
|
2020-12-12 16:55:27 +01:00
|
|
|
.Times(1);
|
|
|
|
EXPECT_CALL(*mock, disableService(_))
|
|
|
|
.Times(1);
|
|
|
|
|
|
|
|
class TmpMockService : public MockService {
|
|
|
|
public:
|
|
|
|
explicit TmpMockService(void) {
|
2021-04-28 19:38:25 +02:00
|
|
|
EXPECT_CALL(*this, enable)
|
2020-12-12 16:55:27 +01:00
|
|
|
.Times(1);
|
2021-04-28 19:38:25 +02:00
|
|
|
ON_CALL(*this, enable)
|
|
|
|
.WillByDefault([](MM::Engine&, std::vector<MM::UpdateStrategies::TaskInfo>& task_array) -> bool {
|
|
|
|
using MM::UpdateStrategies::TaskInfo;
|
2020-12-12 16:55:27 +01:00
|
|
|
|
2021-04-28 19:38:25 +02:00
|
|
|
task_array.push_back(
|
|
|
|
TaskInfo{"TmpMockService"}
|
|
|
|
//.precede("PreviousTask")
|
|
|
|
.fn([](MM::Engine& engine) { (void)engine; })
|
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2020-12-12 16:55:27 +01:00
|
|
|
|
|
|
|
EXPECT_CALL(*this, disable(_))
|
|
|
|
.Times(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
MM::Engine engine(std::move(mock));
|
|
|
|
|
|
|
|
engine.addService<TmpMockService>();
|
|
|
|
|
|
|
|
ASSERT_TRUE(engine.enableService<TmpMockService>());
|
|
|
|
engine.disableService<TmpMockService>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|