mirror of
https://git.jabber.space/devs/cproof.git
synced 2026-07-22 09:56:21 +00:00
Added /alias command, writing aliases to [alias] group in profrc
This commit is contained in:
150
tests/test_cmd_alias.c
Normal file
150
tests/test_cmd_alias.c
Normal file
@@ -0,0 +1,150 @@
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <glib.h>
|
||||
|
||||
#include "xmpp/xmpp.h"
|
||||
#include "xmpp/mock_xmpp.h"
|
||||
|
||||
#include "ui/ui.h"
|
||||
#include "ui/mock_ui.h"
|
||||
|
||||
#include "config/preferences.h"
|
||||
|
||||
#include "command/commands.h"
|
||||
|
||||
void cmd_alias_add_shows_usage_when_no_args(void **state)
|
||||
{
|
||||
mock_cons_show();
|
||||
CommandHelp *help = malloc(sizeof(CommandHelp));
|
||||
help->usage = "some usage";
|
||||
gchar *args[] = { "add", NULL };
|
||||
|
||||
expect_cons_show("Usage: some usage");
|
||||
|
||||
gboolean result = cmd_alias(args, *help);
|
||||
assert_true(result);
|
||||
|
||||
free(help);
|
||||
}
|
||||
|
||||
void cmd_alias_add_shows_usage_when_no_value(void **state)
|
||||
{
|
||||
mock_cons_show();
|
||||
CommandHelp *help = malloc(sizeof(CommandHelp));
|
||||
help->usage = "some usage";
|
||||
gchar *args[] = { "add", "alias", NULL };
|
||||
|
||||
expect_cons_show("Usage: some usage");
|
||||
|
||||
gboolean result = cmd_alias(args, *help);
|
||||
assert_true(result);
|
||||
|
||||
free(help);
|
||||
}
|
||||
|
||||
void cmd_alias_remove_shows_usage_when_no_args(void **state)
|
||||
{
|
||||
mock_cons_show();
|
||||
CommandHelp *help = malloc(sizeof(CommandHelp));
|
||||
help->usage = "some usage";
|
||||
gchar *args[] = { "remove", NULL };
|
||||
|
||||
expect_cons_show("Usage: some usage");
|
||||
|
||||
gboolean result = cmd_alias(args, *help);
|
||||
assert_true(result);
|
||||
|
||||
free(help);
|
||||
}
|
||||
|
||||
void cmd_alias_show_usage_when_invalid_subcmd(void **state)
|
||||
{
|
||||
mock_cons_show();
|
||||
CommandHelp *help = malloc(sizeof(CommandHelp));
|
||||
help->usage = "some usage";
|
||||
gchar *args[] = { "blah", NULL };
|
||||
|
||||
expect_cons_show("Usage: some usage");
|
||||
|
||||
gboolean result = cmd_alias(args, *help);
|
||||
assert_true(result);
|
||||
|
||||
free(help);
|
||||
}
|
||||
|
||||
void cmd_alias_add_adds_alias(void **state)
|
||||
{
|
||||
mock_cons_show();
|
||||
CommandHelp *help = malloc(sizeof(CommandHelp));
|
||||
gchar *args[] = { "add", "hc", "/help commands", NULL };
|
||||
|
||||
expect_cons_show("Command alias added /hc -> /help commands");
|
||||
|
||||
gboolean result = cmd_alias(args, *help);
|
||||
|
||||
char *returned_val = prefs_get_alias("hc");
|
||||
|
||||
assert_true(result);
|
||||
assert_string_equal("/help commands", returned_val);
|
||||
|
||||
free(help);
|
||||
}
|
||||
|
||||
void cmd_alias_remove_removes_alias(void **state)
|
||||
{
|
||||
mock_cons_show();
|
||||
CommandHelp *help = malloc(sizeof(CommandHelp));
|
||||
gchar *args[] = { "remove", "hn", NULL };
|
||||
|
||||
prefs_add_alias("hn", "/help navigation");
|
||||
|
||||
expect_cons_show("Command alias removed -> /hn");
|
||||
|
||||
gboolean result = cmd_alias(args, *help);
|
||||
|
||||
char *returned_val = prefs_get_alias("hn");
|
||||
|
||||
assert_true(result);
|
||||
assert_null(returned_val);
|
||||
|
||||
free(help);
|
||||
}
|
||||
|
||||
void cmd_alias_remove_shows_message_when_no_alias(void **state)
|
||||
{
|
||||
mock_cons_show();
|
||||
CommandHelp *help = malloc(sizeof(CommandHelp));
|
||||
gchar *args[] = { "remove", "hn", NULL };
|
||||
|
||||
expect_cons_show("No such command alias /hn");
|
||||
|
||||
gboolean result = cmd_alias(args, *help);
|
||||
assert_true(result);
|
||||
|
||||
free(help);
|
||||
}
|
||||
|
||||
void cmd_alias_list_shows_all_aliases(void **state)
|
||||
{
|
||||
mock_cons_show_aliases();
|
||||
CommandHelp *help = malloc(sizeof(CommandHelp));
|
||||
gchar *args[] = { "list", NULL };
|
||||
|
||||
prefs_add_alias("vy", "/vercheck on");
|
||||
prefs_add_alias("q", "/quit");
|
||||
prefs_add_alias("hn", "/help navigation");
|
||||
prefs_add_alias("hc", "/help commands");
|
||||
prefs_add_alias("vn", "/vercheck off");
|
||||
|
||||
// write a custom checker to check the correct list is passed
|
||||
expect_cons_show_aliases();
|
||||
|
||||
gboolean result = cmd_alias(args, *help);
|
||||
assert_true(result);
|
||||
|
||||
free(help);
|
||||
}
|
||||
8
tests/test_cmd_alias.h
Normal file
8
tests/test_cmd_alias.h
Normal file
@@ -0,0 +1,8 @@
|
||||
void cmd_alias_add_shows_usage_when_no_args(void **state);
|
||||
void cmd_alias_add_shows_usage_when_no_value(void **state);
|
||||
void cmd_alias_remove_shows_usage_when_no_args(void **state);
|
||||
void cmd_alias_show_usage_when_invalid_subcmd(void **state);
|
||||
void cmd_alias_add_adds_alias(void **state);
|
||||
void cmd_alias_remove_removes_alias(void **state);
|
||||
void cmd_alias_remove_shows_message_when_no_alias(void **state);
|
||||
void cmd_alias_list_shows_all_aliases(void **state);
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "test_roster_list.h"
|
||||
#include "test_preferences.h"
|
||||
#include "test_server_events.h"
|
||||
#include "test_cmd_alias.h"
|
||||
|
||||
#define PROF_RUN_TESTS(name) fprintf(stderr, "\n-> Running %s\n", #name); \
|
||||
fflush(stderr); \
|
||||
@@ -390,6 +391,26 @@ int main(int argc, char* argv[]) {
|
||||
// delete_config_file),
|
||||
};
|
||||
|
||||
const UnitTest cmd_alias_tests[] = {
|
||||
unit_test(cmd_alias_add_shows_usage_when_no_args),
|
||||
unit_test(cmd_alias_add_shows_usage_when_no_value),
|
||||
unit_test(cmd_alias_remove_shows_usage_when_no_args),
|
||||
unit_test(cmd_alias_show_usage_when_invalid_subcmd),
|
||||
unit_test_setup_teardown(cmd_alias_add_adds_alias,
|
||||
create_config_file,
|
||||
delete_config_file),
|
||||
unit_test_setup_teardown(cmd_alias_remove_removes_alias,
|
||||
create_config_file,
|
||||
delete_config_file),
|
||||
unit_test_setup_teardown(cmd_alias_remove_shows_message_when_no_alias,
|
||||
create_config_file,
|
||||
delete_config_file),
|
||||
unit_test_setup_teardown(cmd_alias_list_shows_all_aliases,
|
||||
create_config_file,
|
||||
delete_config_file),
|
||||
};
|
||||
|
||||
|
||||
int bak, new;
|
||||
fflush(stdout);
|
||||
bak = dup(1);
|
||||
@@ -413,6 +434,7 @@ int main(int argc, char* argv[]) {
|
||||
PROF_RUN_TESTS(cmd_statuses_tests);
|
||||
PROF_RUN_TESTS(preferences_tests);
|
||||
PROF_RUN_TESTS(server_events_tests);
|
||||
PROF_RUN_TESTS(cmd_alias_tests);
|
||||
|
||||
fflush(stdout);
|
||||
dup2(bak, 1);
|
||||
|
||||
@@ -68,6 +68,12 @@ void _mock_cons_show_account(ProfAccount *account)
|
||||
check_expected(account);
|
||||
}
|
||||
|
||||
static
|
||||
void _mock_cons_show_aliases(GList *aliases)
|
||||
{
|
||||
check_expected(aliases);
|
||||
}
|
||||
|
||||
static
|
||||
void _mock_cons_show_account_list(gchar **accounts)
|
||||
{
|
||||
@@ -122,6 +128,12 @@ mock_cons_show_account(void)
|
||||
cons_show_account = _mock_cons_show_account;
|
||||
}
|
||||
|
||||
void
|
||||
mock_cons_show_aliases(void)
|
||||
{
|
||||
cons_show_aliases = _mock_cons_show_aliases;
|
||||
}
|
||||
|
||||
void
|
||||
mock_cons_show_account_list(void)
|
||||
{
|
||||
@@ -189,6 +201,13 @@ expect_cons_show_contact_online(PContact contact, Resource *resource, GDateTime
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
expect_cons_show_aliases()
|
||||
{
|
||||
// write a custom checker for the list
|
||||
expect_any(_mock_cons_show_aliases, aliases);
|
||||
}
|
||||
|
||||
void
|
||||
mock_ui_ask_password_returns(char *password)
|
||||
{
|
||||
|
||||
@@ -21,6 +21,9 @@ void expect_cons_show_error(char *output);
|
||||
void mock_cons_show_account(void);
|
||||
void expect_cons_show_account(ProfAccount *account);
|
||||
|
||||
void mock_cons_show_aliases(void);
|
||||
void expect_cons_show_aliases(void);
|
||||
|
||||
void mock_cons_show_account_list(void);
|
||||
void expect_cons_show_account_list(gchar **accounts);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user