2021-01-04 09:25:52 +00:00
|
|
|
#include "utils/action_router.hpp"
|
|
|
|
|
|
|
|
#include "common/test.hpp"
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
|
|
|
|
using namespace polybar;
|
|
|
|
using ::testing::InSequence;
|
|
|
|
|
|
|
|
class MockModule {
|
|
|
|
public:
|
|
|
|
MOCK_METHOD(void, action1, ());
|
|
|
|
MOCK_METHOD(void, action2, (const string&));
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(ActionRouterTest, CallsCorrectFunctions) {
|
|
|
|
MockModule m;
|
|
|
|
|
|
|
|
{
|
|
|
|
InSequence seq;
|
|
|
|
EXPECT_CALL(m, action1()).Times(1);
|
|
|
|
EXPECT_CALL(m, action2("foo")).Times(1);
|
|
|
|
}
|
|
|
|
|
2021-10-03 00:57:49 +00:00
|
|
|
action_router router;
|
|
|
|
router.register_action("action1", [&]() { m.action1(); });
|
|
|
|
router.register_action_with_data("action2", [&](const std::string& data) { m.action2(data); });
|
2021-01-04 09:25:52 +00:00
|
|
|
router.invoke("action1", "");
|
|
|
|
router.invoke("action2", "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ActionRouterTest, HasAction) {
|
|
|
|
MockModule m;
|
2021-10-03 00:57:49 +00:00
|
|
|
action_router router;
|
2021-01-04 09:25:52 +00:00
|
|
|
|
2021-10-03 00:57:49 +00:00
|
|
|
router.register_action("foo", [&]() { m.action1(); });
|
2021-01-04 09:25:52 +00:00
|
|
|
|
|
|
|
EXPECT_TRUE(router.has_action("foo"));
|
|
|
|
EXPECT_FALSE(router.has_action("bar"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ActionRouterTest, ThrowsOnDuplicate) {
|
|
|
|
MockModule m;
|
2021-10-03 00:57:49 +00:00
|
|
|
action_router router;
|
2021-01-04 09:25:52 +00:00
|
|
|
|
2021-10-03 00:57:49 +00:00
|
|
|
router.register_action("foo", [&]() { m.action1(); });
|
|
|
|
EXPECT_THROW(router.register_action("foo", [&]() { m.action1(); }), std::invalid_argument);
|
|
|
|
EXPECT_THROW(router.register_action_with_data("foo", [&](const std::string& data) { m.action2(data); }),
|
|
|
|
std::invalid_argument);
|
2021-01-04 09:25:52 +00:00
|
|
|
}
|