26be83f893
* module: Implement proof of concept action router Action implementation inside module becomes much cleaner because each module just registers action names together with a callback (pointer to member function) and the action router does the rest. * Make input function final This forces all modules to use the action router * modules: Catch exceptions in action handlers * Use action router for all modules * Use action_ prefix for function names The mpd module's 'stop' action overwrote the base module's stop function which caused difficult to debug behavior. To prevent this in the future we now prefix each function that is responsible for an action with 'action_' * Cleanup * actions: Throw exception when re-registering action Action names are unique inside modules. Unfortunately there is no way to ensure this statically, the next best thing is to crash the module and let the user know that this is a bug. * Formatting * actions: Ignore data for actions without data This is the same behavior as before. * action_router: Write tests
49 lines
1.2 KiB
C++
49 lines
1.2 KiB
C++
#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);
|
|
}
|
|
|
|
action_router<MockModule> router(&m);
|
|
router.register_action("action1", &MockModule::action1);
|
|
router.register_action_with_data("action2", &MockModule::action2);
|
|
router.invoke("action1", "");
|
|
router.invoke("action2", "foo");
|
|
}
|
|
|
|
TEST(ActionRouterTest, HasAction) {
|
|
MockModule m;
|
|
action_router<MockModule> router(&m);
|
|
|
|
router.register_action("foo", &MockModule::action1);
|
|
|
|
EXPECT_TRUE(router.has_action("foo"));
|
|
EXPECT_FALSE(router.has_action("bar"));
|
|
}
|
|
|
|
TEST(ActionRouterTest, ThrowsOnDuplicate) {
|
|
MockModule m;
|
|
action_router<MockModule> router(&m);
|
|
|
|
router.register_action("foo", &MockModule::action1);
|
|
EXPECT_THROW(router.register_action("foo", &MockModule::action1), std::invalid_argument);
|
|
EXPECT_THROW(router.register_action_with_data("foo", &MockModule::action2), std::invalid_argument);
|
|
}
|