reapack

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

action.cpp (2152B)


      1 #include "helper.hpp"
      2 
      3 #include <action.hpp>
      4 #include <reaper_plugin_functions.h>
      5 
      6 static const char *M = "[action]";
      7 
      8 using namespace std::string_literals;
      9 
     10 TEST_CASE("registering command ID", M) {
     11   static const char *commandName = nullptr;
     12 
     13   plugin_register = [](const char *what, void *data) {
     14     if(!strcmp(what, "command_id")) {
     15       commandName = static_cast<const char *>(data);
     16       return 1234;
     17     }
     18 
     19     return 0;
     20   };
     21 
     22   const Action action("HELLO", "Hello World", {});
     23   REQUIRE(action.id() == 1234);
     24   REQUIRE(commandName == "HELLO"s);
     25 }
     26 
     27 TEST_CASE("registering action in REAPER's Action List", M) {
     28   static gaccel_register_t *reg = nullptr;
     29 
     30   plugin_register = [](const char *what, void *data) {
     31     if(!strcmp(what, "command_id"))
     32       return 4321;
     33     else if(!strcmp(what, "gaccel"))
     34       reg = static_cast<gaccel_register_t *>(data);
     35 
     36     return 0;
     37   };
     38 
     39   const Action action("HELLO", "Hello World", {});
     40   CHECK(reg != nullptr);
     41   REQUIRE(reg->accel.cmd == 4321);
     42   REQUIRE(reg->desc == "Hello World"s);
     43 }
     44 
     45 TEST_CASE("commands & actions are unregistered on destruction", M) {
     46   static std::vector<std::string> reglog;
     47   static std::vector<void *> datalog;
     48 
     49   plugin_register = [](const char *what, void *data) {
     50     reglog.push_back(what);
     51     datalog.push_back(data);
     52     return 0;
     53   };
     54 
     55   {
     56     const Action action("HELLO", "Hello World", {});
     57     REQUIRE(reglog == std::vector<std::string>{"command_id", "gaccel"});
     58   }
     59 
     60   REQUIRE(reglog == std::vector<std::string>{
     61     "command_id", "gaccel", "-gaccel", "-command_id"});
     62   REQUIRE(datalog[0] == datalog[3]);
     63   REQUIRE(datalog[1] == datalog[2]);
     64 }
     65 
     66 TEST_CASE("run action", M) {
     67   static int commandId = 0;
     68 
     69   plugin_register = [](const char *what, void *data) {
     70     if(!strcmp(what, "command_id"))
     71       return ++commandId;
     72     return 0;
     73   };
     74 
     75   int a = 0, b = 0;
     76   ActionList list;
     77   list.add("Action1", "First action", [&a] { ++a; });
     78   list.add("Action2", "Second action", [&b] { ++b; });
     79 
     80   REQUIRE(!list.run(0));
     81 
     82   CHECK(a == 0);
     83   REQUIRE(list.run(1));
     84   REQUIRE(a == 1);
     85 
     86   CHECK(b == 0);
     87   REQUIRE(list.run(2));
     88   REQUIRE(b == 1);
     89 
     90   REQUIRE(!list.run(3));
     91 }