pywiq/tests/Executor.cpp

137 lines
3.3 KiB
C++
Raw Normal View History

#include <catch2/catch.hpp>
#include "../src/Mode.hpp"
#include "../src/Action.hpp"
#include "../src/Binding.hpp"
#include "../src/Executor.hpp"
#include "../src/Controller.hpp"
#include "../src/Pywiq.hpp"
using namespace pwq;
struct MockController : public Controller {
virtual KeyMod wait_keymod() override { return KeyMod(L"a"); }
};
class ExecutorTest
{
public:
explicit ExecutorTest() {}
virtual ~ExecutorTest() {}
protected:
std::shared_ptr<Executor> exec;
MockController m_ctrl;
Pywiq m_pywiq = Pywiq(m_ctrl);
Mode m_mode = Mode("executor_test", m_pywiq, exec);
};
struct ActionMock: public Action
{
int execute_counter = 0;
ActionMock(Mode& mode)
: Action(mode, "mock_action", "mock_doc")
{
}
void execute() override
{
execute_counter++;
}
};
TEST_CASE_METHOD(ExecutorTest, "Executor_one_action")
{
auto action = std::make_shared<ActionMock>(m_mode);
Executor exec;
exec.bind(Binding(std::vector<KeyMod>{KeyMod(L"C-a"),
KeyMod(L"C-c")},
action));
REQUIRE(0 == action->execute_counter);
exec.update(KeyMod(L"C-a"));
REQUIRE(0 == action->execute_counter);
exec.update(KeyMod(L"C-c"));
REQUIRE(1 == action->execute_counter);
exec.update(KeyMod(L"C-c"));
REQUIRE(1 == action->execute_counter);
exec.update(KeyMod(L"C-a"));
REQUIRE(1 == action->execute_counter);
exec.update(KeyMod(L"C-c"));
REQUIRE(2 == action->execute_counter);
}
TEST_CASE_METHOD(ExecutorTest, "Executor_two_actions")
{
auto first_action = std::make_shared<ActionMock>(m_mode);
auto second_action = std::make_shared<ActionMock>(m_mode);
Executor exec;
exec.bind(Binding(std::vector<KeyMod>{KeyMod(L"A-g"),
KeyMod(L"p")},
first_action));
exec.bind(Binding(std::vector<KeyMod>{KeyMod(L"z"),
KeyMod(L"z")},
second_action));
REQUIRE(0 == first_action->execute_counter);
REQUIRE(0 == second_action->execute_counter);
exec.update(KeyMod(L"A-g"));
REQUIRE(0 == first_action->execute_counter);
REQUIRE(0 == second_action->execute_counter);
exec.update(KeyMod(L"z"));
REQUIRE(0 == first_action->execute_counter);
REQUIRE(0 == second_action->execute_counter);
exec.update(KeyMod(L"z"));
REQUIRE(0 == first_action->execute_counter);
REQUIRE(1 == second_action->execute_counter);
exec.update(KeyMod(L"p"));
REQUIRE(0 == first_action->execute_counter);
REQUIRE(1 == second_action->execute_counter);
exec.update(KeyMod(L"A-g"));
REQUIRE(0 == first_action->execute_counter);
REQUIRE(1 == second_action->execute_counter);
exec.update(KeyMod(L"p"));
REQUIRE(1 == first_action->execute_counter);
REQUIRE(1 == second_action->execute_counter);
}
TEST_CASE_METHOD(ExecutorTest, "Executor_failed_but_start_another")
{
auto action = std::make_shared<ActionMock>(m_mode);
Executor exec;
exec.bind(Binding(std::vector<KeyMod>{KeyMod(L"a"),
KeyMod(L"b"),
KeyMod(L"c")},
action));
REQUIRE(0 == action->execute_counter);
exec.update(KeyMod(L"a"));
REQUIRE(0 == action->execute_counter);
exec.update(KeyMod(L"a"));
REQUIRE(0 == action->execute_counter);
exec.update(KeyMod(L"b"));
REQUIRE(0 == action->execute_counter);
exec.update(KeyMod(L"c"));
REQUIRE(1 == action->execute_counter);
}