168 lines
5.2 KiB
C
168 lines
5.2 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "client_cmd.h"
|
|
#include "plugins.h"
|
|
|
|
static int tests_passed = 0;
|
|
static int tests_failed = 0;
|
|
|
|
#define ASSERT_EQ(expected, actual, msg) do { \
|
|
if ((expected) != (actual)) { \
|
|
fprintf(stderr, "FAIL: %s (expected %d, got %d)\n", msg, (int)(expected), (int)(actual)); \
|
|
tests_failed++; \
|
|
} else { \
|
|
printf("PASS: %s\n", msg); \
|
|
tests_passed++; \
|
|
} \
|
|
} while(0)
|
|
|
|
#define ASSERT_STR_EQ(expected, actual, msg) do { \
|
|
if (strcmp((expected), (actual)) != 0) { \
|
|
fprintf(stderr, "FAIL: %s (expected \"%s\", got \"%s\")\n", msg, (expected), (actual)); \
|
|
tests_failed++; \
|
|
} else { \
|
|
printf("PASS: %s\n", msg); \
|
|
tests_passed++; \
|
|
} \
|
|
} while(0)
|
|
|
|
/* Test from command */
|
|
static void test_from_store(void)
|
|
{
|
|
int ret = handle_client_command("from looper:out_0", NULL);
|
|
ASSERT_EQ(0, ret, "handle_client_command('from looper:out_0', NULL) returns 0");
|
|
const char *stored = get_stored_from();
|
|
ASSERT_STR_EQ("looper:out_0", stored, "get_stored_from() returns 'looper:out_0'");
|
|
}
|
|
|
|
/* Test to command */
|
|
static void test_to_store(void)
|
|
{
|
|
int ret = handle_client_command("to plugin:in", NULL);
|
|
ASSERT_EQ(0, ret, "handle_client_command('to plugin:in', NULL) returns 0");
|
|
const char *stored = get_stored_to();
|
|
ASSERT_STR_EQ("plugin:in", stored, "get_stored_to() returns 'plugin:in'");
|
|
}
|
|
|
|
/* Test connect using stored from/to (should call plugin_connect with those ports, fail because no plugin) */
|
|
static void test_connect_uses_stored(void)
|
|
{
|
|
/* Ensure stored from and to are set */
|
|
handle_client_command("from looper:out_0", NULL);
|
|
handle_client_command("to plugin:in", NULL);
|
|
int id = -1;
|
|
int ret = handle_client_command("connect", &id);
|
|
/* Should return -1 because plugin_connect fails (no plugin loaded), but not -1 from missing args */
|
|
ASSERT_EQ(-1, ret, "handle_client_command('connect', ...) returns -1 when plugin_connect fails (no JACK)");
|
|
}
|
|
|
|
/* Test disconnect using stored from/to */
|
|
static void test_disconnect_uses_stored(void)
|
|
{
|
|
handle_client_command("from looper:out_0", NULL);
|
|
handle_client_command("to plugin:in", NULL);
|
|
int id = -1;
|
|
int ret = handle_client_command("disconnect", &id);
|
|
/* plugin_disconnect returns 0 even without JACK, so we expect 0 */
|
|
ASSERT_EQ(0, ret, "handle_client_command('disconnect', ...) returns 0 (safe stub)");
|
|
}
|
|
|
|
/* Test rack/grid commands return 0 */
|
|
static void test_rack_grid_commands(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("rack", &id);
|
|
ASSERT_EQ(0, ret, "handle_client_command('rack', ...) returns 0");
|
|
ret = handle_client_command("grid", &id);
|
|
ASSERT_EQ(0, ret, "handle_client_command('grid', ...) returns 0");
|
|
}
|
|
|
|
/* Test invalid commands */
|
|
static void test_unknown_command(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("unknown_command", &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command('unknown_command', ...) returns -1");
|
|
}
|
|
|
|
static void test_empty_input(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("", &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command('', ...) returns -1");
|
|
}
|
|
|
|
static void test_null_input(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command(NULL, &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command(NULL, ...) returns -1");
|
|
}
|
|
|
|
/* Test addplugin command */
|
|
static void test_addplugin_no_path(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("addplugin", &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command('addplugin', ...) returns -1 (no path)");
|
|
}
|
|
|
|
static void test_addplugin_empty_path(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("addplugin ", &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command('addplugin ', ...) returns -1 (empty path)");
|
|
}
|
|
|
|
static void test_addplugin_valid(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("addplugin /does/not/exist.so", &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command('addplugin /does/not/exist.so', ...) returns -1 (no such file)");
|
|
}
|
|
|
|
/* Test connect command */
|
|
static void test_connect_no_args(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("connect", &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command('connect', ...) returns -1 (no args)");
|
|
}
|
|
|
|
static void test_connect_missing_to(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("connect plugin:out_1", &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command('connect plugin:out_1', ...) returns -1 (missing 'to')");
|
|
}
|
|
|
|
static void test_connect_invalid_id(void)
|
|
{
|
|
int id = -1;
|
|
int ret = handle_client_command("connect plugin:out looper:in", &id);
|
|
ASSERT_EQ(-1, ret, "handle_client_command('connect plugin:out looper:in', ...) returns -1 (stub)");
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
printf("=== Client command parser unit tests ===\n");
|
|
|
|
test_unknown_command();
|
|
test_empty_input();
|
|
test_null_input();
|
|
test_addplugin_no_path();
|
|
test_addplugin_empty_path();
|
|
test_addplugin_valid();
|
|
test_connect_no_args();
|
|
test_connect_missing_to();
|
|
test_connect_invalid_id();
|
|
test_from_store();
|
|
test_to_store();
|
|
test_connect_uses_stored();
|
|
test_disconnect_uses_stored();
|
|
test_rack_grid_commands();
|
|
|
|
printf("\nResults: %d passed, %d failed\n", tests_passed, tests_failed);
|
|
return tests_failed > 0 ? 1 : 0;
|
|
}
|