1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Merge branch 'master' into plugins

Conflicts:
	src/command/command.c
This commit is contained in:
James Booth 2014-04-09 22:48:01 +01:00
commit 45feaefa71
12 changed files with 294 additions and 200 deletions

View File

@ -1254,22 +1254,12 @@ gboolean
cmd_execute(const char * const command, const char * const inp) cmd_execute(const char * const command, const char * const inp)
{ {
Command *cmd = g_hash_table_lookup(commands, command); Command *cmd = g_hash_table_lookup(commands, command);
gboolean result = FALSE;
if (cmd != NULL) { if (cmd != NULL) {
gchar **args = cmd->parser(inp, cmd->min_args, cmd->max_args); gchar **args = cmd->parser(inp, cmd->min_args, cmd->max_args, &result);
if ((args == NULL) && (cmd->setting_func != NULL)) { if (result == FALSE) {
cons_show(""); ui_invalid_command_usage(cmd->help.usage, cmd->setting_func);
(*cmd->setting_func)();
cons_show("Usage: %s", cmd->help.usage);
return TRUE;
} else if (args == NULL) {
cons_show("");
cons_show("Usage: %s", cmd->help.usage);
if (ui_current_win_type() == WIN_CHAT) {
char usage[strlen(cmd->help.usage) + 8];
sprintf(usage, "Usage: %s", cmd->help.usage);
ui_current_print_line(usage);
}
return TRUE; return TRUE;
} else { } else {
gboolean result = cmd->func(args, cmd->help); gboolean result = cmd->func(args, cmd->help);
@ -1405,7 +1395,7 @@ cmd_execute_default(const char * const inp)
break; break;
case WIN_CONSOLE: case WIN_CONSOLE:
cons_show("Unknown command: %s", inp); ui_unknown_command(inp);
break; break;
case WIN_DUCK: case WIN_DUCK:
@ -1840,12 +1830,13 @@ _alias_autocomplete(char *input, int *size)
static char * static char *
_connect_autocomplete(char *input, int *size) _connect_autocomplete(char *input, int *size)
{ {
char *result = NULL; char *found = NULL;
gboolean result = FALSE;
input[*size] = '\0'; input[*size] = '\0';
gchar **args = parse_args(input, 2, 4); gchar **args = parse_args(input, 2, 4, &result);
if ((strncmp(input, "/connect", 8) == 0) && (args != NULL)) { if ((strncmp(input, "/connect", 8) == 0) && (result == TRUE)) {
GString *beginning = g_string_new("/connect "); GString *beginning = g_string_new("/connect ");
g_string_append(beginning, args[0]); g_string_append(beginning, args[0]);
if (args[1] != NULL && args[2] != NULL) { if (args[1] != NULL && args[2] != NULL) {
@ -1854,16 +1845,16 @@ _connect_autocomplete(char *input, int *size)
g_string_append(beginning, " "); g_string_append(beginning, " ");
g_string_append(beginning, args[2]); g_string_append(beginning, args[2]);
} }
result = autocomplete_param_with_ac(input, size, beginning->str, connect_property_ac); found = autocomplete_param_with_ac(input, size, beginning->str, connect_property_ac);
g_string_free(beginning, TRUE); g_string_free(beginning, TRUE);
if (result != NULL) { if (found != NULL) {
return result; return found;
} }
} }
result = autocomplete_param_with_func(input, size, "/connect", accounts_find_enabled); found = autocomplete_param_with_func(input, size, "/connect", accounts_find_enabled);
if (result != NULL) { if (found != NULL) {
return result; return found;
} }
return NULL; return NULL;
@ -1872,12 +1863,13 @@ _connect_autocomplete(char *input, int *size)
static char * static char *
_join_autocomplete(char *input, int *size) _join_autocomplete(char *input, int *size)
{ {
char *result = NULL; char *found = NULL;
gboolean result = FALSE;
input[*size] = '\0'; input[*size] = '\0';
gchar **args = parse_args(input, 2, 4); gchar **args = parse_args(input, 2, 4, &result);
if ((strncmp(input, "/join", 5) == 0) && (args != NULL)) { if ((strncmp(input, "/join", 5) == 0) && (result == TRUE)) {
GString *beginning = g_string_new("/join "); GString *beginning = g_string_new("/join ");
g_string_append(beginning, args[0]); g_string_append(beginning, args[0]);
if (args[1] != NULL && args[2] != NULL) { if (args[1] != NULL && args[2] != NULL) {
@ -1886,10 +1878,10 @@ _join_autocomplete(char *input, int *size)
g_string_append(beginning, " "); g_string_append(beginning, " ");
g_string_append(beginning, args[2]); g_string_append(beginning, args[2]);
} }
result = autocomplete_param_with_ac(input, size, beginning->str, join_property_ac); found = autocomplete_param_with_ac(input, size, beginning->str, join_property_ac);
g_string_free(beginning, TRUE); g_string_free(beginning, TRUE);
if (result != NULL) { if (found != NULL) {
return result; return found;
} }
} }
@ -1899,28 +1891,29 @@ _join_autocomplete(char *input, int *size)
static char * static char *
_account_autocomplete(char *input, int *size) _account_autocomplete(char *input, int *size)
{ {
char *result = NULL; char *found = NULL;
gboolean result = FALSE;
input[*size] = '\0'; input[*size] = '\0';
gchar **args = parse_args(input, 3, 3); gchar **args = parse_args(input, 3, 3, &result);
if ((strncmp(input, "/account set", 12) == 0) && (args != NULL)) { if ((strncmp(input, "/account set", 12) == 0) && (result == TRUE)) {
GString *beginning = g_string_new("/account set "); GString *beginning = g_string_new("/account set ");
g_string_append(beginning, args[1]); g_string_append(beginning, args[1]);
result = autocomplete_param_with_ac(input, size, beginning->str, account_set_ac); found = autocomplete_param_with_ac(input, size, beginning->str, account_set_ac);
g_string_free(beginning, TRUE); g_string_free(beginning, TRUE);
if (result != NULL) { if (found != NULL) {
return result; return found;
} }
} }
if ((strncmp(input, "/account clear", 14) == 0) && (args != NULL)) { if ((strncmp(input, "/account clear", 14) == 0) && (result == TRUE)) {
GString *beginning = g_string_new("/account clear "); GString *beginning = g_string_new("/account clear ");
g_string_append(beginning, args[1]); g_string_append(beginning, args[1]);
result = autocomplete_param_with_ac(input, size, beginning->str, account_clear_ac); found = autocomplete_param_with_ac(input, size, beginning->str, account_clear_ac);
g_string_free(beginning, TRUE); g_string_free(beginning, TRUE);
if (result != NULL) { if (found != NULL) {
return result; return found;
} }
} }
@ -1931,17 +1924,13 @@ _account_autocomplete(char *input, int *size)
"/account disable", "/account rename", "/account clear" }; "/account disable", "/account rename", "/account clear" };
for (i = 0; i < ARRAY_SIZE(account_choice); i++) { for (i = 0; i < ARRAY_SIZE(account_choice); i++) {
result = autocomplete_param_with_func(input, size, account_choice[i], found = autocomplete_param_with_func(input, size, account_choice[i],
accounts_find_all); accounts_find_all);
if (result != NULL) { if (found != NULL) {
return result; return found;
} }
} }
result = autocomplete_param_with_ac(input, size, "/account", account_ac); found = autocomplete_param_with_ac(input, size, "/account", account_ac);
if (result != NULL) { return found;
return result;
}
return NULL;
} }

View File

@ -66,7 +66,7 @@ cmd_connect(gchar **args, struct cmd_help_t help)
jabber_conn_status_t conn_status = jabber_get_connection_status(); jabber_conn_status_t conn_status = jabber_get_connection_status();
if ((conn_status != JABBER_DISCONNECTED) && (conn_status != JABBER_STARTED)) { if ((conn_status != JABBER_DISCONNECTED) && (conn_status != JABBER_STARTED)) {
cons_show("You are either connected already, or a login is in process."); ui_already_connected();
result = TRUE; result = TRUE;
} else { } else {
char *user = args[0]; char *user = args[0];

View File

@ -43,7 +43,7 @@ typedef struct cmd_help_t {
typedef struct cmd_t { typedef struct cmd_t {
gchar *cmd; gchar *cmd;
gboolean (*func)(gchar **args, struct cmd_help_t help); gboolean (*func)(gchar **args, struct cmd_help_t help);
gchar** (*parser)(const char * const inp, int min, int max); gchar** (*parser)(const char * const inp, int min, int max, gboolean *result);
int min_args; int min_args;
int max_args; int max_args;
void (**setting_func)(void); void (**setting_func)(void);

View File

@ -76,15 +76,10 @@ plugins_run_command(const char * const input)
while (p_command != NULL) { while (p_command != NULL) {
PluginCommand *command = p_command->data; PluginCommand *command = p_command->data;
if (g_strcmp0(split[0], command->command_name) == 0) { if (g_strcmp0(split[0], command->command_name) == 0) {
gchar **args = parse_args(input, command->min_args, command->max_args); gboolean result;
if (args == NULL) { gchar **args = parse_args(input, command->min_args, command->max_args, &result);
cons_show(""); if (result == FALSE) {
cons_show("Usage: %s", command->usage); ui_invalid_command_usage(command->usage, NULL);
if (ui_current_win_type() == WIN_CHAT) {
char usage[strlen(command->usage) + 8];
sprintf(usage, "Usage: %s", command->usage);
ui_current_print_line(usage);
}
return TRUE; return TRUE;
} else { } else {
command->callback_func(command, args); command->callback_func(command, args);

View File

@ -48,9 +48,10 @@
* *
*/ */
gchar ** gchar **
parse_args(const char * const inp, int min, int max) parse_args(const char * const inp, int min, int max, gboolean *result)
{ {
if (inp == NULL) { if (inp == NULL) {
*result = FALSE;
return NULL; return NULL;
} }
@ -122,6 +123,7 @@ parse_args(const char * const inp, int min, int max)
if ((num < min) || (num > max)) { if ((num < min) || (num > max)) {
g_slist_free_full(tokens, free); g_slist_free_full(tokens, free);
g_free(copy); g_free(copy);
*result = FALSE;
return NULL; return NULL;
// if min allowed is 0 and 0 found, return empty char* array // if min allowed is 0 and 0 found, return empty char* array
@ -130,6 +132,7 @@ parse_args(const char * const inp, int min, int max)
gchar **args = malloc((num + 1) * sizeof(*args)); gchar **args = malloc((num + 1) * sizeof(*args));
args[0] = NULL; args[0] = NULL;
g_free(copy); g_free(copy);
*result = TRUE;
return args; return args;
// otherwise return args array // otherwise return args array
@ -147,7 +150,7 @@ parse_args(const char * const inp, int min, int max)
args[arg_count] = NULL; args[arg_count] = NULL;
g_slist_free_full(tokens, free); g_slist_free_full(tokens, free);
g_free(copy); g_free(copy);
*result = TRUE;
return args; return args;
} }
} }
@ -179,9 +182,10 @@ parse_args(const char * const inp, int min, int max)
* *
*/ */
gchar ** gchar **
parse_args_with_freetext(const char * const inp, int min, int max) parse_args_with_freetext(const char * const inp, int min, int max, gboolean *result)
{ {
if (inp == NULL) { if (inp == NULL) {
*result = FALSE;
return NULL; return NULL;
} }
@ -267,12 +271,14 @@ parse_args_with_freetext(const char * const inp, int min, int max)
// if num args not valid return NULL // if num args not valid return NULL
if ((num < min) || (num > max)) { if ((num < min) || (num > max)) {
g_slist_free_full(tokens, free); g_slist_free_full(tokens, free);
*result = FALSE;
return NULL; return NULL;
// if min allowed is 0 and 0 found, return empty char* array // if min allowed is 0 and 0 found, return empty char* array
} else if (min == 0 && num == 0) { } else if (min == 0 && num == 0) {
gchar **args = malloc((num + 1) * sizeof(*args)); gchar **args = malloc((num + 1) * sizeof(*args));
args[0] = NULL; args[0] = NULL;
*result = TRUE;
return args; return args;
// otherwise return args array // otherwise return args array
@ -289,7 +295,7 @@ parse_args_with_freetext(const char * const inp, int min, int max)
args[arg_count] = NULL; args[arg_count] = NULL;
g_slist_free_full(tokens, free); g_slist_free_full(tokens, free);
*result = TRUE;
return args; return args;
} }
} }

View File

@ -25,8 +25,8 @@
#include <glib.h> #include <glib.h>
gchar** parse_args(const char * const inp, int min, int max); gchar** parse_args(const char * const inp, int min, int max, gboolean *result);
gchar** parse_args_with_freetext(const char * const inp, int min, int max); gchar** parse_args_with_freetext(const char * const inp, int min, int max, gboolean *result);
int count_tokens(char *string); int count_tokens(char *string);
char* get_start(char *string, int tokens); char* get_start(char *string, int tokens);

View File

@ -523,6 +523,36 @@ _ui_handle_error(const char * const err_msg)
g_string_free(msg, TRUE); g_string_free(msg, TRUE);
} }
static void
_ui_invalid_command_usage(const char * const usage, void (**setting_func)(void))
{
if (setting_func != NULL) {
cons_show("");
(*setting_func)();
cons_show("Usage: %s", usage);
} else {
cons_show("");
cons_show("Usage: %s", usage);
if (ui_current_win_type() == WIN_CHAT) {
char usage_cpy[strlen(usage) + 8];
sprintf(usage_cpy, "Usage: %s", usage);
ui_current_print_line(usage_cpy);
}
}
}
static void
_ui_unknown_command(const char * const input)
{
cons_show("Unknown command: %s", input);
}
static void
_ui_already_connected(void)
{
cons_show("You are either connected already, or a login is in process.");
}
static void static void
_ui_disconnected(void) _ui_disconnected(void)
{ {
@ -1901,4 +1931,7 @@ ui_init_module(void)
ui_input_clear = _ui_input_clear; ui_input_clear = _ui_input_clear;
ui_input_nonblocking = _ui_input_nonblocking; ui_input_nonblocking = _ui_input_nonblocking;
ui_replace_input = _ui_replace_input; ui_replace_input = _ui_replace_input;
ui_invalid_command_usage = _ui_invalid_command_usage;
ui_unknown_command = _ui_unknown_command;
ui_already_connected = _ui_already_connected;
} }

View File

@ -159,6 +159,10 @@ void (*ui_input_clear)(void);
void (*ui_input_nonblocking)(void); void (*ui_input_nonblocking)(void);
void (*ui_replace_input)(char *input, const char * const new_input, int *size); void (*ui_replace_input)(char *input, const char * const new_input, int *size);
void (*ui_invalid_command_usage)(const char * const usage, void (**setting_func)(void));
void (*ui_unknown_command)(const char * const input);
void (*ui_already_connected)(void);
// console window actions // console window actions
void (*cons_show)(const char * const msg, ...); void (*cons_show)(const char * const msg, ...);
void (*cons_about)(void); void (*cons_about)(void);

View File

@ -19,13 +19,11 @@
static void test_with_connection_status(jabber_conn_status_t status) static void test_with_connection_status(jabber_conn_status_t status)
{ {
mock_cons_show(); stub_ui_already_connected();
CommandHelp *help = malloc(sizeof(CommandHelp)); CommandHelp *help = malloc(sizeof(CommandHelp));
mock_connection_status(status); mock_connection_status(status);
expect_cons_show("You are either connected already, or a login is in process.");
gboolean result = cmd_connect(NULL, *help); gboolean result = cmd_connect(NULL, *help);
assert_true(result); assert_true(result);

View File

@ -10,325 +10,381 @@ void
parse_null_returns_null(void **state) parse_null_returns_null(void **state)
{ {
char *inp = NULL; char *inp = NULL;
gchar **result = parse_args(inp, 1, 2); gboolean result = TRUE;
gchar **args = parse_args(inp, 1, 2, &result);
assert_null(result); assert_false(result);
g_strfreev(result); assert_null(args);
g_strfreev(args);
} }
void void
parse_empty_returns_null(void **state) parse_empty_returns_null(void **state)
{ {
char *inp = ""; char *inp = "";
gchar **result = parse_args(inp, 1, 2); gboolean result = TRUE;
gchar **args = parse_args(inp, 1, 2, &result);
assert_null(result); assert_false(result);
g_strfreev(result); assert_null(args);
g_strfreev(args);
} }
void void
parse_space_returns_null(void **state) parse_space_returns_null(void **state)
{ {
char *inp = " "; char *inp = " ";
gchar **result = parse_args(inp, 1, 2); gboolean result = TRUE;
gchar **args = parse_args(inp, 1, 2, &result);
assert_null(result); assert_false(result);
g_strfreev(result); assert_null(args);
g_strfreev(args);
} }
void void
parse_cmd_no_args_returns_null(void **state) parse_cmd_no_args_returns_null(void **state)
{ {
char *inp = "/cmd"; char *inp = "/cmd";
gchar **result = parse_args(inp, 1, 2); gboolean result = TRUE;
gchar **args = parse_args(inp, 1, 2, &result);
assert_null(result); assert_false(result);
g_strfreev(result); assert_null(args);
g_strfreev(args);
} }
void void
parse_cmd_with_space_returns_null(void **state) parse_cmd_with_space_returns_null(void **state)
{ {
char *inp = "/cmd "; char *inp = "/cmd ";
gchar **result = parse_args(inp, 1, 2); gboolean result = TRUE;
gchar **args = parse_args(inp, 1, 2, &result);
assert_null(result); assert_false(result);
g_strfreev(result); assert_null(args);
g_strfreev(args);
} }
void void
parse_cmd_with_too_few_returns_null(void **state) parse_cmd_with_too_few_returns_null(void **state)
{ {
char *inp = "/cmd arg1"; char *inp = "/cmd arg1";
gchar **result = parse_args(inp, 2, 3); gboolean result = TRUE;
gchar **args = parse_args(inp, 2, 3, &result);
assert_null(result); assert_false(result);
g_strfreev(result); assert_null(args);
g_strfreev(args);
} }
void void
parse_cmd_with_too_many_returns_null(void **state) parse_cmd_with_too_many_returns_null(void **state)
{ {
char *inp = "/cmd arg1 arg2 arg3 arg4"; char *inp = "/cmd arg1 arg2 arg3 arg4";
gchar **result = parse_args(inp, 1, 3); gboolean result = TRUE;
gchar **args = parse_args(inp, 1, 3, &result);
assert_null(result); assert_false(result);
g_strfreev(result); assert_null(args);
g_strfreev(args);
} }
void void
parse_cmd_one_arg(void **state) parse_cmd_one_arg(void **state)
{ {
char *inp = "/cmd arg1"; char *inp = "/cmd arg1";
gchar **result = parse_args(inp, 1, 2); gboolean result = FALSE;
gchar **args = parse_args(inp, 1, 2, &result);
assert_int_equal(1, g_strv_length(result)); assert_true(result);
assert_string_equal("arg1", result[0]); assert_int_equal(1, g_strv_length(args));
g_strfreev(result); assert_string_equal("arg1", args[0]);
g_strfreev(args);
} }
void void
parse_cmd_two_args(void **state) parse_cmd_two_args(void **state)
{ {
char *inp = "/cmd arg1 arg2"; char *inp = "/cmd arg1 arg2";
gchar **result = parse_args(inp, 1, 2); gboolean result = FALSE;
gchar **args = parse_args(inp, 1, 2, &result);
assert_int_equal(2, g_strv_length(result)); assert_true(result);
assert_string_equal("arg1", result[0]); assert_int_equal(2, g_strv_length(args));
assert_string_equal("arg2", result[1]); assert_string_equal("arg1", args[0]);
g_strfreev(result); assert_string_equal("arg2", args[1]);
g_strfreev(args);
} }
void void
parse_cmd_three_args(void **state) parse_cmd_three_args(void **state)
{ {
char *inp = "/cmd arg1 arg2 arg3"; char *inp = "/cmd arg1 arg2 arg3";
gchar **result = parse_args(inp, 3, 3); gboolean result = FALSE;
gchar **args = parse_args(inp, 3, 3, &result);
assert_int_equal(3, g_strv_length(result)); assert_true(result);
assert_string_equal("arg1", result[0]); assert_int_equal(3, g_strv_length(args));
assert_string_equal("arg2", result[1]); assert_string_equal("arg1", args[0]);
assert_string_equal("arg3", result[2]); assert_string_equal("arg2", args[1]);
g_strfreev(result); assert_string_equal("arg3", args[2]);
g_strfreev(args);
} }
void void
parse_cmd_three_args_with_spaces(void **state) parse_cmd_three_args_with_spaces(void **state)
{ {
char *inp = " /cmd arg1 arg2 arg3 "; char *inp = " /cmd arg1 arg2 arg3 ";
gchar **result = parse_args(inp, 3, 3); gboolean result = FALSE;
gchar **args = parse_args(inp, 3, 3, &result);
assert_int_equal(3, g_strv_length(result)); assert_true(result);
assert_string_equal("arg1", result[0]); assert_int_equal(3, g_strv_length(args));
assert_string_equal("arg2", result[1]); assert_string_equal("arg1", args[0]);
assert_string_equal("arg3", result[2]); assert_string_equal("arg2", args[1]);
g_strfreev(result); assert_string_equal("arg3", args[2]);
g_strfreev(args);
} }
void void
parse_cmd_with_freetext(void **state) parse_cmd_with_freetext(void **state)
{ {
char *inp = "/cmd this is some free text"; char *inp = "/cmd this is some free text";
gchar **result = parse_args_with_freetext(inp, 1, 1); gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 1, 1, &result);
assert_int_equal(1, g_strv_length(result)); assert_true(result);
assert_string_equal("this is some free text", result[0]); assert_int_equal(1, g_strv_length(args));
g_strfreev(result); assert_string_equal("this is some free text", args[0]);
g_strfreev(args);
} }
void void
parse_cmd_one_arg_with_freetext(void **state) parse_cmd_one_arg_with_freetext(void **state)
{ {
char *inp = "/cmd arg1 this is some free text"; char *inp = "/cmd arg1 this is some free text";
gchar **result = parse_args_with_freetext(inp, 1, 2); gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 1, 2, &result);
assert_int_equal(2, g_strv_length(result)); assert_true(result);
assert_string_equal("arg1", result[0]); assert_int_equal(2, g_strv_length(args));
assert_string_equal("this is some free text", result[1]); assert_string_equal("arg1", args[0]);
g_strfreev(result); assert_string_equal("this is some free text", args[1]);
g_strfreev(args);
} }
void void
parse_cmd_two_args_with_freetext(void **state) parse_cmd_two_args_with_freetext(void **state)
{ {
char *inp = "/cmd arg1 arg2 this is some free text"; char *inp = "/cmd arg1 arg2 this is some free text";
gchar **result = parse_args_with_freetext(inp, 1, 3); gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 1, 3, &result);
assert_int_equal(3, g_strv_length(result)); assert_true(result);
assert_string_equal("arg1", result[0]); assert_int_equal(3, g_strv_length(args));
assert_string_equal("arg2", result[1]); assert_string_equal("arg1", args[0]);
assert_string_equal("this is some free text", result[2]); assert_string_equal("arg2", args[1]);
g_strfreev(result); assert_string_equal("this is some free text", args[2]);
g_strfreev(args);
} }
void void
parse_cmd_min_zero(void **state) parse_cmd_min_zero(void **state)
{ {
char *inp = "/cmd"; char *inp = "/cmd";
gchar **result = parse_args(inp, 0, 2); gboolean result = FALSE;
gchar **args = parse_args(inp, 0, 2, &result);
assert_int_equal(0, g_strv_length(result)); assert_true(result);
assert_null(result[0]); assert_int_equal(0, g_strv_length(args));
g_strfreev(result); assert_null(args[0]);
g_strfreev(args);
} }
void void
parse_cmd_min_zero_with_freetext(void **state) parse_cmd_min_zero_with_freetext(void **state)
{ {
char *inp = "/cmd"; char *inp = "/cmd";
gchar **result = parse_args_with_freetext(inp, 0, 2); gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 0, 2, &result);
assert_int_equal(0, g_strv_length(result)); assert_true(result);
assert_null(result[0]); assert_int_equal(0, g_strv_length(args));
g_strfreev(result); assert_null(args[0]);
g_strfreev(args);
} }
void void
parse_cmd_with_quoted(void **state) parse_cmd_with_quoted(void **state)
{ {
char *inp = "/cmd \"arg1\" arg2"; char *inp = "/cmd \"arg1\" arg2";
gchar **result = parse_args(inp, 2, 2); gboolean result = FALSE;
gchar **args = parse_args(inp, 2, 2, &result);
assert_int_equal(2, g_strv_length(result)); assert_true(result);
assert_string_equal("arg1", result[0]); assert_int_equal(2, g_strv_length(args));
assert_string_equal("arg2", result[1]); assert_string_equal("arg1", args[0]);
g_strfreev(result); assert_string_equal("arg2", args[1]);
g_strfreev(args);
} }
void void
parse_cmd_with_quoted_and_space(void **state) parse_cmd_with_quoted_and_space(void **state)
{ {
char *inp = "/cmd \"the arg1\" arg2"; char *inp = "/cmd \"the arg1\" arg2";
gchar **result = parse_args(inp, 2, 2); gboolean result = FALSE;
gchar **args = parse_args(inp, 2, 2, &result);
assert_int_equal(2, g_strv_length(result)); assert_true(result);
assert_string_equal("the arg1", result[0]); assert_int_equal(2, g_strv_length(args));
assert_string_equal("arg2", result[1]); assert_string_equal("the arg1", args[0]);
g_strfreev(result); assert_string_equal("arg2", args[1]);
g_strfreev(args);
} }
void void
parse_cmd_with_quoted_and_many_spaces(void **state) parse_cmd_with_quoted_and_many_spaces(void **state)
{ {
char *inp = "/cmd \"the arg1 is here\" arg2"; char *inp = "/cmd \"the arg1 is here\" arg2";
gchar **result = parse_args(inp, 2, 2); gboolean result = FALSE;
gchar **args = parse_args(inp, 2, 2, &result);
assert_int_equal(2, g_strv_length(result)); assert_true(result);
assert_string_equal("the arg1 is here", result[0]); assert_int_equal(2, g_strv_length(args));
assert_string_equal("arg2", result[1]); assert_string_equal("the arg1 is here", args[0]);
g_strfreev(result); assert_string_equal("arg2", args[1]);
g_strfreev(args);
} }
void void
parse_cmd_with_many_quoted_and_many_spaces(void **state) parse_cmd_with_many_quoted_and_many_spaces(void **state)
{ {
char *inp = "/cmd \"the arg1 is here\" \"and arg2 is right here\""; char *inp = "/cmd \"the arg1 is here\" \"and arg2 is right here\"";
gchar **result = parse_args(inp, 2, 2); gboolean result = FALSE;
gchar **args = parse_args(inp, 2, 2, &result);
assert_int_equal(2, g_strv_length(result)); assert_true(result);
assert_string_equal("the arg1 is here", result[0]); assert_int_equal(2, g_strv_length(args));
assert_string_equal("and arg2 is right here", result[1]); assert_string_equal("the arg1 is here", args[0]);
g_strfreev(result); assert_string_equal("and arg2 is right here", args[1]);
g_strfreev(args);
} }
void void
parse_cmd_freetext_with_quoted(void **state) parse_cmd_freetext_with_quoted(void **state)
{ {
char *inp = "/cmd \"arg1\" arg2 hello there whats up"; char *inp = "/cmd \"arg1\" arg2 hello there whats up";
gchar **result = parse_args_with_freetext(inp, 3, 3); gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 3, 3, &result);
assert_int_equal(3, g_strv_length(result)); assert_true(result);
assert_string_equal("arg1", result[0]); assert_int_equal(3, g_strv_length(args));
assert_string_equal("arg2", result[1]); assert_string_equal("arg1", args[0]);
assert_string_equal("hello there whats up", result[2]); assert_string_equal("arg2", args[1]);
g_strfreev(result); assert_string_equal("hello there whats up", args[2]);
g_strfreev(args);
} }
void void
parse_cmd_freetext_with_quoted_and_space(void **state) parse_cmd_freetext_with_quoted_and_space(void **state)
{ {
char *inp = "/cmd \"the arg1\" arg2 another bit of freetext"; char *inp = "/cmd \"the arg1\" arg2 another bit of freetext";
gchar **result = parse_args_with_freetext(inp, 3, 3); gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 3, 3, &result);
assert_int_equal(3, g_strv_length(result)); assert_true(result);
assert_string_equal("the arg1", result[0]); assert_int_equal(3, g_strv_length(args));
assert_string_equal("arg2", result[1]); assert_string_equal("the arg1", args[0]);
assert_string_equal("another bit of freetext", result[2]); assert_string_equal("arg2", args[1]);
g_strfreev(result); assert_string_equal("another bit of freetext", args[2]);
g_strfreev(args);
} }
void void
parse_cmd_freetext_with_quoted_and_many_spaces(void **state) parse_cmd_freetext_with_quoted_and_many_spaces(void **state)
{ {
char *inp = "/cmd \"the arg1 is here\" arg2 some more freetext"; char *inp = "/cmd \"the arg1 is here\" arg2 some more freetext";
gchar **result = parse_args_with_freetext(inp, 3, 3); gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 3, 3, &result);
assert_int_equal(3, g_strv_length(result)); assert_true(result);
assert_string_equal("the arg1 is here", result[0]); assert_int_equal(3, g_strv_length(args));
assert_string_equal("arg2", result[1]); assert_string_equal("the arg1 is here", args[0]);
assert_string_equal("some more freetext", result[2]); assert_string_equal("arg2", args[1]);
g_strfreev(result); assert_string_equal("some more freetext", args[2]);
g_strfreev(args);
} }
void void
parse_cmd_freetext_with_many_quoted_and_many_spaces(void **state) parse_cmd_freetext_with_many_quoted_and_many_spaces(void **state)
{ {
char *inp = "/cmd \"the arg1 is here\" \"and arg2 is right here\" and heres the free text"; char *inp = "/cmd \"the arg1 is here\" \"and arg2 is right here\" and heres the free text";
gchar **result = parse_args_with_freetext(inp, 3, 3); gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 3, 3, &result);
assert_int_equal(3, g_strv_length(result)); assert_true(result);
assert_string_equal("the arg1 is here", result[0]); assert_int_equal(3, g_strv_length(args));
assert_string_equal("and arg2 is right here", result[1]); assert_string_equal("the arg1 is here", args[0]);
assert_string_equal("and heres the free text", result[2]); assert_string_equal("and arg2 is right here", args[1]);
g_strfreev(result); assert_string_equal("and heres the free text", args[2]);
g_strfreev(args);
} }
void void
parse_cmd_with_quoted_freetext(void **state) parse_cmd_with_quoted_freetext(void **state)
{ {
char *inp = "/cmd arg1 here is \"some\" quoted freetext"; char *inp = "/cmd arg1 here is \"some\" quoted freetext";
gchar **result = parse_args_with_freetext(inp, 1, 2); gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 1, 2, &result);
assert_int_equal(2, g_strv_length(result)); assert_true(result);
assert_string_equal("arg1", result[0]); assert_int_equal(2, g_strv_length(args));
assert_string_equal("here is \"some\" quoted freetext", result[1]); assert_string_equal("arg1", args[0]);
g_strfreev(result); assert_string_equal("here is \"some\" quoted freetext", args[1]);
g_strfreev(args);
} }
void void
parse_cmd_with_third_arg_quoted_0_min_3_max(void **state) parse_cmd_with_third_arg_quoted_0_min_3_max(void **state)
{ {
char *inp = "/group add friends \"The User\""; char *inp = "/group add friends \"The User\"";
gchar **result = parse_args_with_freetext(inp, 0, 3); gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 0, 3, &result);
assert_int_equal(3, g_strv_length(result)); assert_true(result);
assert_string_equal("add", result[0]); assert_int_equal(3, g_strv_length(args));
assert_string_equal("friends", result[1]); assert_string_equal("add", args[0]);
assert_string_equal("The User", result[2]); assert_string_equal("friends", args[1]);
assert_string_equal("The User", args[2]);
} }
void void
parse_cmd_with_second_arg_quoted_0_min_3_max(void **state) parse_cmd_with_second_arg_quoted_0_min_3_max(void **state)
{ {
char *inp = "/group add \"The Group\" friend"; char *inp = "/group add \"The Group\" friend";
gchar **result = parse_args_with_freetext(inp, 0, 3); gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 0, 3, &result);
assert_int_equal(3, g_strv_length(result)); assert_true(result);
assert_string_equal("add", result[0]); assert_int_equal(3, g_strv_length(args));
assert_string_equal("The Group", result[1]); assert_string_equal("add", args[0]);
assert_string_equal("friend", result[2]); assert_string_equal("The Group", args[1]);
assert_string_equal("friend", args[2]);
} }
void void
parse_cmd_with_second_and_third_arg_quoted_0_min_3_max(void **state) parse_cmd_with_second_and_third_arg_quoted_0_min_3_max(void **state)
{ {
char *inp = "/group add \"The Group\" \"The User\""; char *inp = "/group add \"The Group\" \"The User\"";
gchar **result = parse_args_with_freetext(inp, 0, 3); gboolean result = FALSE;
gchar **args = parse_args_with_freetext(inp, 0, 3, &result);
assert_int_equal(3, g_strv_length(result)); assert_true(result);
assert_string_equal("add", result[0]); assert_int_equal(3, g_strv_length(args));
assert_string_equal("The Group", result[1]); assert_string_equal("add", args[0]);
assert_string_equal("The User", result[2]); assert_string_equal("The Group", args[1]);
assert_string_equal("The User", args[2]);
} }
void void

View File

@ -82,6 +82,11 @@ char * _stub_ui_ask_password(void)
return NULL; return NULL;
} }
static
void _stub_ui_already_connected(void)
{
}
static static
win_type_t _mock_ui_current_win_type(void) win_type_t _mock_ui_current_win_type(void)
{ {
@ -258,6 +263,12 @@ stub_cons_show(void)
cons_show = _stub_cons_show; cons_show = _stub_cons_show;
} }
void
stub_ui_already_connected(void)
{
ui_already_connected = _stub_ui_already_connected;
}
void void
stub_ui_handle_recipient_not_found(void) stub_ui_handle_recipient_not_found(void)
{ {

View File

@ -66,4 +66,6 @@ void cons_show_roster_expect(GSList *list);
void ui_switch_win_expect_and_return(int given_i, gboolean result); void ui_switch_win_expect_and_return(int given_i, gboolean result);
void stub_ui_already_connected(void);
#endif #endif