reapack

Package manager for REAPER
Log | Files | Refs | Submodules | README | LICENSE

commit 9f61e092ff58fe743fd4f55f88f3bb405b094fc7
parent 7aa6bfaffb56158165931abab45fb542644b1fe8
Author: cfillion <cfillion@users.noreply.github.com>
Date:   Tue, 25 Sep 2018 02:01:52 -0400

add tests for the new action registration code

Diffstat:
Msrc/action.hpp | 4+++-
Atest/action.cpp | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 94 insertions(+), 1 deletion(-)

diff --git a/src/action.hpp b/src/action.hpp @@ -46,13 +46,15 @@ public: template<class... Args> Action *add(Args&&... args) { auto action = std::make_unique<Action>(args...); m_list.emplace(action->id(), std::move(action)); + return action.get(); } - Action *find(Action::CommandID id) const; bool run(Action::CommandID id) const; private: + Action *find(Action::CommandID id) const; + std::map<Action::CommandID, std::unique_ptr<Action>> m_list; }; diff --git a/test/action.cpp b/test/action.cpp @@ -0,0 +1,91 @@ +#include "helper.hpp" + +#include <action.hpp> +#include <reaper_plugin_functions.h> + +static const char *M = "[action]"; + +using namespace std::string_literals; + +TEST_CASE("registering command ID", M) { + static const char *commandName = nullptr; + + plugin_register = [](const char *what, void *data) { + if(!strcmp(what, "command_id")) { + commandName = static_cast<const char *>(data); + return 1234; + } + + return 0; + }; + + const Action action("HELLO", "Hello World", {}); + REQUIRE(action.id() == 1234); + REQUIRE(commandName == "HELLO"s); +} + +TEST_CASE("registering action in REAPER's Action List", M) { + static gaccel_register_t *reg = nullptr; + + plugin_register = [](const char *what, void *data) { + if(!strcmp(what, "command_id")) + return 4321; + else if(!strcmp(what, "gaccel")) + reg = static_cast<gaccel_register_t *>(data); + + return 0; + }; + + const Action action("HELLO", "Hello World", {}); + CHECK(reg != nullptr); + REQUIRE(reg->accel.cmd == 4321); + REQUIRE(reg->desc == "Hello World"s); +} + +TEST_CASE("commands & actions are unregistered on destruction", M) { + static std::vector<std::string> reglog; + static std::vector<void *> datalog; + + plugin_register = [](const char *what, void *data) { + reglog.push_back(what); + datalog.push_back(data); + return 0; + }; + + { + const Action action("HELLO", "Hello World", {}); + REQUIRE(reglog == std::vector<std::string>{"command_id", "gaccel"}); + } + + REQUIRE(reglog == std::vector<std::string>{ + "command_id", "gaccel", "-gaccel", "-command_id"}); + REQUIRE(datalog[0] == datalog[3]); + REQUIRE(datalog[1] == datalog[2]); +} + +TEST_CASE("run action", M) { + static int commandId = 0; + + plugin_register = [](const char *what, void *data) { + if(!strcmp(what, "command_id")) + return ++commandId; + return 0; + }; + + int a = 0, b = 0; + ActionList list; + list.add("Action1", "First action", [&a] { ++a; }); + list.add("Action2", "Second action", [&b] { ++b; }); + + REQUIRE(!list.run(0)); + + CHECK(a == 0); + REQUIRE(list.run(1)); + REQUIRE(a == 1); + + CHECK(b == 0); + REQUIRE(list.run(2)); + REQUIRE(b == 1); + + REQUIRE(!list.run(3)); +}