pixtool/tests/CommandRunner.cpp

167 lines
3.9 KiB
C++

#include <catch2/catch.hpp>
#include "model/Command.hpp"
#include "model/CommandRunner.hpp"
#include "model/PixTool.hpp"
#include "model/Shortcut.hpp"
using namespace pt::model;
class CommandRunnerTest
{
public:
explicit CommandRunnerTest() {}
virtual ~CommandRunnerTest() {}
protected:
PixTool m_pixtool;
CommandRunner m_runner {m_pixtool};
};
struct CommandMock: public Command
{
int exec_count = 0;
explicit CommandMock(): Command("mock") {}
virtual void execute(PixTool&) override
{
exec_count++;
}
};
TEST_CASE_METHOD(CommandRunnerTest, "CommandRunner_one_key")
{
auto cmd = std::make_shared<CommandMock>();
m_runner.bind(Shortcut {{KeyMod {"a"}}}, cmd);
REQUIRE(cmd->exec_count == 0);
m_runner.update(KeyMod {"b"});
REQUIRE(cmd->exec_count == 0);
m_runner.update(KeyMod {"a"});
REQUIRE(cmd->exec_count == 1);
m_runner.update(KeyMod {"b"});
REQUIRE(cmd->exec_count == 1);
m_runner.update(KeyMod {"a"});
REQUIRE(cmd->exec_count == 2);
}
TEST_CASE_METHOD(CommandRunnerTest, "CommandRunner_three_keys")
{
auto cmd = std::make_shared<CommandMock>();
m_runner.bind(Shortcut {{KeyMod {"a"},
KeyMod {"b", {PT_CONTROL}},
KeyMod {"c", {PT_ALT}}}}, cmd);
REQUIRE(cmd->exec_count == 0);
m_runner.update(KeyMod {"a"});
REQUIRE(cmd->exec_count == 0);
m_runner.update(KeyMod {"b", {PT_CONTROL}});
REQUIRE(cmd->exec_count == 0);
m_runner.update(KeyMod {"c", {PT_ALT}});
REQUIRE(cmd->exec_count == 1);
m_runner.update(KeyMod {"a"});
REQUIRE(cmd->exec_count == 1);
m_runner.update(KeyMod {"b"});
REQUIRE(cmd->exec_count == 1);
m_runner.update(KeyMod {"c", {PT_ALT}});
REQUIRE(cmd->exec_count == 1);
m_runner.update(KeyMod {"a"});
REQUIRE(cmd->exec_count == 1);
m_runner.update(KeyMod {"b", {PT_CONTROL}});
REQUIRE(cmd->exec_count == 1);
m_runner.update(KeyMod {"a"});
REQUIRE(cmd->exec_count == 1);
m_runner.update(KeyMod {"a"});
REQUIRE(cmd->exec_count == 1);
m_runner.update(KeyMod {"a"});
REQUIRE(cmd->exec_count == 1);
m_runner.update(KeyMod {"b", {PT_CONTROL}});
REQUIRE(cmd->exec_count == 1);
m_runner.update(KeyMod {"c", {PT_ALT}});
REQUIRE(cmd->exec_count == 2);
}
TEST_CASE_METHOD(CommandRunnerTest, "CommandRunner_two_commands")
{
auto cmd0 = std::make_shared<CommandMock>();
auto cmd1 = std::make_shared<CommandMock>();
m_runner.bind(Shortcut {{KeyMod {"a"},
KeyMod {"b"},
KeyMod {"c"}}}, cmd0);
m_runner.bind(Shortcut {{KeyMod {"c"},
KeyMod {"d"},
KeyMod {"e"}}}, cmd1);
m_runner.update(KeyMod {"a"});
REQUIRE(cmd0->exec_count == 0);
REQUIRE(cmd1->exec_count == 0);
m_runner.update(KeyMod {"b"});
REQUIRE(cmd0->exec_count == 0);
REQUIRE(cmd1->exec_count == 0);
m_runner.update(KeyMod {"c"});
REQUIRE(cmd0->exec_count == 1);
REQUIRE(cmd1->exec_count == 0);
m_runner.update(KeyMod {"d"});
REQUIRE(cmd0->exec_count == 1);
REQUIRE(cmd1->exec_count == 0);
m_runner.update(KeyMod {"e"});
REQUIRE(cmd0->exec_count == 1);
REQUIRE(cmd1->exec_count == 0);
m_runner.update(KeyMod {"c"});
REQUIRE(cmd0->exec_count == 1);
REQUIRE(cmd1->exec_count == 0);
m_runner.update(KeyMod {"d"});
REQUIRE(cmd0->exec_count == 1);
REQUIRE(cmd1->exec_count == 0);
m_runner.update(KeyMod {"e"});
REQUIRE(cmd0->exec_count == 1);
REQUIRE(cmd1->exec_count == 1);
}
TEST_CASE_METHOD(CommandRunnerTest, "CommandRunner_wrong_key")
{
auto cmd = std::make_shared<CommandMock>();
m_runner.bind(Shortcut {{KeyMod {"C", {PT_CONTROL}},
KeyMod {"X", {PT_CONTROL}}}},
cmd);
REQUIRE(cmd->exec_count == 0);
m_runner.update(KeyMod {"C", {PT_CONTROL}});
m_runner.update(KeyMod {"V", {PT_CONTROL}});
m_runner.update(KeyMod {"X", {PT_CONTROL}});
REQUIRE(cmd->exec_count == 0);
}