pywiq/tests/Executor.cpp

148 lines
3.7 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/View.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"); }
};
struct MockView : public View {
virtual void clear_screen() override {}
virtual void draw(UI_Node const&) override { }
virtual std::pair<int, int> size() const override { return {0, 0}; }
};
class ExecutorTest
{
public:
explicit ExecutorTest() {}
virtual ~ExecutorTest() {}
protected:
std::shared_ptr<Executor> exec;
MockController m_ctrl;
MockView m_view;
Pywiq m_pywiq = Pywiq(m_ctrl, m_view);
Mode m_mode = Mode("executor_test",
m_pywiq,
std::make_shared<UI_Node>(nullptr),
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);
}