diff --git a/src/command/command.c b/src/command/command.c index b664914b..8b681778 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -1254,22 +1254,12 @@ gboolean cmd_execute(const char * const command, const char * const inp) { Command *cmd = g_hash_table_lookup(commands, command); + gboolean result = FALSE; if (cmd != NULL) { - gchar **args = cmd->parser(inp, cmd->min_args, cmd->max_args); - if ((args == NULL) && (cmd->setting_func != NULL)) { - cons_show(""); - (*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); - } + gchar **args = cmd->parser(inp, cmd->min_args, cmd->max_args, &result); + if (result == FALSE) { + ui_invalid_command_usage(cmd->help.usage, cmd->setting_func); return TRUE; } else { gboolean result = cmd->func(args, cmd->help); @@ -1405,7 +1395,7 @@ cmd_execute_default(const char * const inp) break; case WIN_CONSOLE: - cons_show("Unknown command: %s", inp); + ui_unknown_command(inp); break; case WIN_DUCK: @@ -1840,12 +1830,13 @@ _alias_autocomplete(char *input, int *size) static char * _connect_autocomplete(char *input, int *size) { - char *result = NULL; + char *found = NULL; + gboolean result = FALSE; 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 "); g_string_append(beginning, args[0]); 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, 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); - if (result != NULL) { - return result; + if (found != NULL) { + return found; } } - result = autocomplete_param_with_func(input, size, "/connect", accounts_find_enabled); - if (result != NULL) { - return result; + found = autocomplete_param_with_func(input, size, "/connect", accounts_find_enabled); + if (found != NULL) { + return found; } return NULL; @@ -1872,12 +1863,13 @@ _connect_autocomplete(char *input, int *size) static char * _join_autocomplete(char *input, int *size) { - char *result = NULL; + char *found = NULL; + gboolean result = FALSE; 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 "); g_string_append(beginning, args[0]); 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, 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); - if (result != NULL) { - return result; + if (found != NULL) { + return found; } } @@ -1899,28 +1891,29 @@ _join_autocomplete(char *input, int *size) static char * _account_autocomplete(char *input, int *size) { - char *result = NULL; + char *found = NULL; + gboolean result = FALSE; 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 "); 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); - if (result != NULL) { - return result; + if (found != NULL) { + 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 "); 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); - if (result != NULL) { - return result; + if (found != NULL) { + return found; } } @@ -1931,17 +1924,13 @@ _account_autocomplete(char *input, int *size) "/account disable", "/account rename", "/account clear" }; 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); - if (result != NULL) { - return result; + if (found != NULL) { + return found; } } - result = autocomplete_param_with_ac(input, size, "/account", account_ac); - if (result != NULL) { - return result; - } - - return NULL; + found = autocomplete_param_with_ac(input, size, "/account", account_ac); + return found; } diff --git a/src/command/commands.c b/src/command/commands.c index 6d31de99..ee80ab2d 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -66,7 +66,7 @@ cmd_connect(gchar **args, struct cmd_help_t help) jabber_conn_status_t conn_status = jabber_get_connection_status(); 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; } else { char *user = args[0]; diff --git a/src/command/commands.h b/src/command/commands.h index d96211bb..57a5da14 100644 --- a/src/command/commands.h +++ b/src/command/commands.h @@ -43,7 +43,7 @@ typedef struct cmd_help_t { typedef struct cmd_t { gchar *cmd; 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 max_args; void (**setting_func)(void); diff --git a/src/plugins/callbacks.c b/src/plugins/callbacks.c index 8b1f3e98..2d97798b 100644 --- a/src/plugins/callbacks.c +++ b/src/plugins/callbacks.c @@ -76,15 +76,10 @@ plugins_run_command(const char * const input) while (p_command != NULL) { PluginCommand *command = p_command->data; if (g_strcmp0(split[0], command->command_name) == 0) { - gchar **args = parse_args(input, command->min_args, command->max_args); - if (args == NULL) { - cons_show(""); - cons_show("Usage: %s", command->usage); - if (ui_current_win_type() == WIN_CHAT) { - char usage[strlen(command->usage) + 8]; - sprintf(usage, "Usage: %s", command->usage); - ui_current_print_line(usage); - } + gboolean result; + gchar **args = parse_args(input, command->min_args, command->max_args, &result); + if (result == FALSE) { + ui_invalid_command_usage(command->usage, NULL); return TRUE; } else { command->callback_func(command, args); diff --git a/src/tools/parser.c b/src/tools/parser.c index 4e746557..932fd9aa 100644 --- a/src/tools/parser.c +++ b/src/tools/parser.c @@ -48,9 +48,10 @@ * */ 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) { + *result = FALSE; return NULL; } @@ -122,6 +123,7 @@ parse_args(const char * const inp, int min, int max) if ((num < min) || (num > max)) { g_slist_free_full(tokens, free); g_free(copy); + *result = FALSE; return NULL; // 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)); args[0] = NULL; g_free(copy); + *result = TRUE; return args; // otherwise return args array @@ -147,7 +150,7 @@ parse_args(const char * const inp, int min, int max) args[arg_count] = NULL; g_slist_free_full(tokens, free); g_free(copy); - + *result = TRUE; return args; } } @@ -179,9 +182,10 @@ parse_args(const char * const inp, int min, int max) * */ 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) { + *result = FALSE; 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 < min) || (num > max)) { g_slist_free_full(tokens, free); + *result = FALSE; return NULL; // if min allowed is 0 and 0 found, return empty char* array } else if (min == 0 && num == 0) { gchar **args = malloc((num + 1) * sizeof(*args)); args[0] = NULL; + *result = TRUE; return args; // otherwise return args array @@ -289,7 +295,7 @@ parse_args_with_freetext(const char * const inp, int min, int max) args[arg_count] = NULL; g_slist_free_full(tokens, free); - + *result = TRUE; return args; } } diff --git a/src/tools/parser.h b/src/tools/parser.h index c82199d4..f03fca81 100644 --- a/src/tools/parser.h +++ b/src/tools/parser.h @@ -25,8 +25,8 @@ #include -gchar** parse_args(const char * const inp, int min, int max); -gchar** parse_args_with_freetext(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, gboolean *result); int count_tokens(char *string); char* get_start(char *string, int tokens); diff --git a/src/ui/core.c b/src/ui/core.c index 38b6ee3f..2a8d1438 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -523,6 +523,36 @@ _ui_handle_error(const char * const err_msg) 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 _ui_disconnected(void) { @@ -1901,4 +1931,7 @@ ui_init_module(void) ui_input_clear = _ui_input_clear; ui_input_nonblocking = _ui_input_nonblocking; 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; } diff --git a/src/ui/ui.h b/src/ui/ui.h index 204636a1..6039ea80 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -159,6 +159,10 @@ void (*ui_input_clear)(void); void (*ui_input_nonblocking)(void); 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 void (*cons_show)(const char * const msg, ...); void (*cons_about)(void); diff --git a/tests/test_cmd_connect.c b/tests/test_cmd_connect.c index 33f49164..76493115 100644 --- a/tests/test_cmd_connect.c +++ b/tests/test_cmd_connect.c @@ -19,13 +19,11 @@ static void test_with_connection_status(jabber_conn_status_t status) { - mock_cons_show(); + stub_ui_already_connected(); CommandHelp *help = malloc(sizeof(CommandHelp)); mock_connection_status(status); - expect_cons_show("You are either connected already, or a login is in process."); - gboolean result = cmd_connect(NULL, *help); assert_true(result); diff --git a/tests/test_parser.c b/tests/test_parser.c index 49fc4354..d05d17c3 100644 --- a/tests/test_parser.c +++ b/tests/test_parser.c @@ -10,325 +10,381 @@ void parse_null_returns_null(void **state) { char *inp = NULL; - gchar **result = parse_args(inp, 1, 2); + gboolean result = TRUE; + gchar **args = parse_args(inp, 1, 2, &result); - assert_null(result); - g_strfreev(result); + assert_false(result); + assert_null(args); + g_strfreev(args); } void parse_empty_returns_null(void **state) { char *inp = ""; - gchar **result = parse_args(inp, 1, 2); + gboolean result = TRUE; + gchar **args = parse_args(inp, 1, 2, &result); - assert_null(result); - g_strfreev(result); + assert_false(result); + assert_null(args); + g_strfreev(args); } void parse_space_returns_null(void **state) { char *inp = " "; - gchar **result = parse_args(inp, 1, 2); + gboolean result = TRUE; + gchar **args = parse_args(inp, 1, 2, &result); - assert_null(result); - g_strfreev(result); + assert_false(result); + assert_null(args); + g_strfreev(args); } void parse_cmd_no_args_returns_null(void **state) { char *inp = "/cmd"; - gchar **result = parse_args(inp, 1, 2); + gboolean result = TRUE; + gchar **args = parse_args(inp, 1, 2, &result); - assert_null(result); - g_strfreev(result); + assert_false(result); + assert_null(args); + g_strfreev(args); } void parse_cmd_with_space_returns_null(void **state) { char *inp = "/cmd "; - gchar **result = parse_args(inp, 1, 2); + gboolean result = TRUE; + gchar **args = parse_args(inp, 1, 2, &result); - assert_null(result); - g_strfreev(result); + assert_false(result); + assert_null(args); + g_strfreev(args); } void parse_cmd_with_too_few_returns_null(void **state) { 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); - g_strfreev(result); + assert_false(result); + assert_null(args); + g_strfreev(args); } void parse_cmd_with_too_many_returns_null(void **state) { 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); - g_strfreev(result); + assert_false(result); + assert_null(args); + g_strfreev(args); } void parse_cmd_one_arg(void **state) { 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_string_equal("arg1", result[0]); - g_strfreev(result); + assert_true(result); + assert_int_equal(1, g_strv_length(args)); + assert_string_equal("arg1", args[0]); + g_strfreev(args); } void parse_cmd_two_args(void **state) { 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_string_equal("arg1", result[0]); - assert_string_equal("arg2", result[1]); - g_strfreev(result); + assert_true(result); + assert_int_equal(2, g_strv_length(args)); + assert_string_equal("arg1", args[0]); + assert_string_equal("arg2", args[1]); + g_strfreev(args); } void parse_cmd_three_args(void **state) { 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_string_equal("arg1", result[0]); - assert_string_equal("arg2", result[1]); - assert_string_equal("arg3", result[2]); - g_strfreev(result); + assert_true(result); + assert_int_equal(3, g_strv_length(args)); + assert_string_equal("arg1", args[0]); + assert_string_equal("arg2", args[1]); + assert_string_equal("arg3", args[2]); + g_strfreev(args); } void parse_cmd_three_args_with_spaces(void **state) { 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_string_equal("arg1", result[0]); - assert_string_equal("arg2", result[1]); - assert_string_equal("arg3", result[2]); - g_strfreev(result); + assert_true(result); + assert_int_equal(3, g_strv_length(args)); + assert_string_equal("arg1", args[0]); + assert_string_equal("arg2", args[1]); + assert_string_equal("arg3", args[2]); + g_strfreev(args); } void parse_cmd_with_freetext(void **state) { 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_string_equal("this is some free text", result[0]); - g_strfreev(result); + assert_true(result); + assert_int_equal(1, g_strv_length(args)); + assert_string_equal("this is some free text", args[0]); + g_strfreev(args); } void parse_cmd_one_arg_with_freetext(void **state) { 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_string_equal("arg1", result[0]); - assert_string_equal("this is some free text", result[1]); - g_strfreev(result); + assert_true(result); + assert_int_equal(2, g_strv_length(args)); + assert_string_equal("arg1", args[0]); + assert_string_equal("this is some free text", args[1]); + g_strfreev(args); } void parse_cmd_two_args_with_freetext(void **state) { 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_string_equal("arg1", result[0]); - assert_string_equal("arg2", result[1]); - assert_string_equal("this is some free text", result[2]); - g_strfreev(result); + assert_true(result); + assert_int_equal(3, g_strv_length(args)); + assert_string_equal("arg1", args[0]); + assert_string_equal("arg2", args[1]); + assert_string_equal("this is some free text", args[2]); + g_strfreev(args); } void parse_cmd_min_zero(void **state) { 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_null(result[0]); - g_strfreev(result); + assert_true(result); + assert_int_equal(0, g_strv_length(args)); + assert_null(args[0]); + g_strfreev(args); } void parse_cmd_min_zero_with_freetext(void **state) { 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_null(result[0]); - g_strfreev(result); + assert_true(result); + assert_int_equal(0, g_strv_length(args)); + assert_null(args[0]); + g_strfreev(args); } void parse_cmd_with_quoted(void **state) { 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_string_equal("arg1", result[0]); - assert_string_equal("arg2", result[1]); - g_strfreev(result); + assert_true(result); + assert_int_equal(2, g_strv_length(args)); + assert_string_equal("arg1", args[0]); + assert_string_equal("arg2", args[1]); + g_strfreev(args); } void parse_cmd_with_quoted_and_space(void **state) { 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_string_equal("the arg1", result[0]); - assert_string_equal("arg2", result[1]); - g_strfreev(result); + assert_true(result); + assert_int_equal(2, g_strv_length(args)); + assert_string_equal("the arg1", args[0]); + assert_string_equal("arg2", args[1]); + g_strfreev(args); } void parse_cmd_with_quoted_and_many_spaces(void **state) { 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_string_equal("the arg1 is here", result[0]); - assert_string_equal("arg2", result[1]); - g_strfreev(result); + assert_true(result); + assert_int_equal(2, g_strv_length(args)); + assert_string_equal("the arg1 is here", args[0]); + assert_string_equal("arg2", args[1]); + g_strfreev(args); } void parse_cmd_with_many_quoted_and_many_spaces(void **state) { 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_string_equal("the arg1 is here", result[0]); - assert_string_equal("and arg2 is right here", result[1]); - g_strfreev(result); + assert_true(result); + assert_int_equal(2, g_strv_length(args)); + assert_string_equal("the arg1 is here", args[0]); + assert_string_equal("and arg2 is right here", args[1]); + g_strfreev(args); } void parse_cmd_freetext_with_quoted(void **state) { 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_string_equal("arg1", result[0]); - assert_string_equal("arg2", result[1]); - assert_string_equal("hello there whats up", result[2]); - g_strfreev(result); + assert_true(result); + assert_int_equal(3, g_strv_length(args)); + assert_string_equal("arg1", args[0]); + assert_string_equal("arg2", args[1]); + assert_string_equal("hello there whats up", args[2]); + g_strfreev(args); } void parse_cmd_freetext_with_quoted_and_space(void **state) { 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_string_equal("the arg1", result[0]); - assert_string_equal("arg2", result[1]); - assert_string_equal("another bit of freetext", result[2]); - g_strfreev(result); + assert_true(result); + assert_int_equal(3, g_strv_length(args)); + assert_string_equal("the arg1", args[0]); + assert_string_equal("arg2", args[1]); + assert_string_equal("another bit of freetext", args[2]); + g_strfreev(args); } void parse_cmd_freetext_with_quoted_and_many_spaces(void **state) { 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_string_equal("the arg1 is here", result[0]); - assert_string_equal("arg2", result[1]); - assert_string_equal("some more freetext", result[2]); - g_strfreev(result); + assert_true(result); + assert_int_equal(3, g_strv_length(args)); + assert_string_equal("the arg1 is here", args[0]); + assert_string_equal("arg2", args[1]); + assert_string_equal("some more freetext", args[2]); + g_strfreev(args); } void 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"; - 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_string_equal("the arg1 is here", result[0]); - assert_string_equal("and arg2 is right here", result[1]); - assert_string_equal("and heres the free text", result[2]); - g_strfreev(result); + assert_true(result); + assert_int_equal(3, g_strv_length(args)); + assert_string_equal("the arg1 is here", args[0]); + assert_string_equal("and arg2 is right here", args[1]); + assert_string_equal("and heres the free text", args[2]); + g_strfreev(args); } void parse_cmd_with_quoted_freetext(void **state) { 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_string_equal("arg1", result[0]); - assert_string_equal("here is \"some\" quoted freetext", result[1]); - g_strfreev(result); + assert_true(result); + assert_int_equal(2, g_strv_length(args)); + assert_string_equal("arg1", args[0]); + assert_string_equal("here is \"some\" quoted freetext", args[1]); + g_strfreev(args); } void parse_cmd_with_third_arg_quoted_0_min_3_max(void **state) { 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_string_equal("add", result[0]); - assert_string_equal("friends", result[1]); - assert_string_equal("The User", result[2]); + assert_true(result); + assert_int_equal(3, g_strv_length(args)); + assert_string_equal("add", args[0]); + assert_string_equal("friends", args[1]); + assert_string_equal("The User", args[2]); } void parse_cmd_with_second_arg_quoted_0_min_3_max(void **state) { 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_string_equal("add", result[0]); - assert_string_equal("The Group", result[1]); - assert_string_equal("friend", result[2]); + assert_true(result); + assert_int_equal(3, g_strv_length(args)); + assert_string_equal("add", args[0]); + assert_string_equal("The Group", args[1]); + assert_string_equal("friend", args[2]); } void parse_cmd_with_second_and_third_arg_quoted_0_min_3_max(void **state) { 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_string_equal("add", result[0]); - assert_string_equal("The Group", result[1]); - assert_string_equal("The User", result[2]); + assert_true(result); + assert_int_equal(3, g_strv_length(args)); + assert_string_equal("add", args[0]); + assert_string_equal("The Group", args[1]); + assert_string_equal("The User", args[2]); } void @@ -446,4 +502,4 @@ get_first_two_of_three_first_and_second_quoted(void **state) char *result = get_start(inp, 3); assert_string_equal("\"one\" \"two\" ", result); -} +} \ No newline at end of file diff --git a/tests/ui/mock_ui.c b/tests/ui/mock_ui.c index 94f8e3c3..0e01be25 100644 --- a/tests/ui/mock_ui.c +++ b/tests/ui/mock_ui.c @@ -82,6 +82,11 @@ char * _stub_ui_ask_password(void) return NULL; } +static +void _stub_ui_already_connected(void) +{ +} + static win_type_t _mock_ui_current_win_type(void) { @@ -258,6 +263,12 @@ stub_cons_show(void) cons_show = _stub_cons_show; } +void +stub_ui_already_connected(void) +{ + ui_already_connected = _stub_ui_already_connected; +} + void stub_ui_handle_recipient_not_found(void) { diff --git a/tests/ui/mock_ui.h b/tests/ui/mock_ui.h index 5b984615..70c9f3b3 100644 --- a/tests/ui/mock_ui.h +++ b/tests/ui/mock_ui.h @@ -66,4 +66,6 @@ void cons_show_roster_expect(GSList *list); void ui_switch_win_expect_and_return(int given_i, gboolean result); -#endif +void stub_ui_already_connected(void); + +#endif \ No newline at end of file