1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-07-21 18:24:14 -04:00

Handle commands with min 0 args

This commit is contained in:
James Booth 2012-11-18 00:38:37 +00:00
parent 191ab83c9b
commit 312d362eaa
3 changed files with 39 additions and 0 deletions

View File

@ -75,6 +75,12 @@ parse_args(const char * const inp, int min, int max, int *num)
*num = 0;
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;
return args;
// otherwise return args array
} else {
gchar **args = malloc((*num + 1) * sizeof(*args));
@ -151,6 +157,12 @@ parse_args_with_freetext(const char * const inp, int min, int max, int *num)
*num = 0;
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;
return args;
// otherwise return args array
} else {
gchar **args = malloc((*num + 1) * sizeof(*args));

View File

@ -26,5 +26,6 @@
#include <glib.h>
gchar** parse_args(const char * const inp, int min, int max, int *num);
gchar** parse_args_with_freetext(const char * const inp, int min, int max, int *num);
#endif

View File

@ -172,6 +172,30 @@ parse_cmd_two_args_with_freetext(void)
g_strfreev(result);
}
void
parse_cmd_min_zero(void)
{
char *inp = "/cmd";
int num = 0;
gchar **result = parse_args(inp, 0, 2, &num);
assert_int_equals(0, num);
assert_is_null(result[0]);
g_strfreev(result);
}
void
parse_cmd_min_zero_with_freetext(void)
{
char *inp = "/cmd";
int num = 0;
gchar **result = parse_args_with_freetext(inp, 0, 2, &num);
assert_int_equals(0, num);
assert_is_null(result[0]);
g_strfreev(result);
}
void
register_parser_tests(void)
{
@ -190,4 +214,6 @@ register_parser_tests(void)
TEST(parse_cmd_two_args_with_freetext);
TEST(parse_cmd_with_too_few_returns_null);
TEST(parse_cmd_with_too_many_returns_null);
TEST(parse_cmd_min_zero);
TEST(parse_cmd_min_zero_with_freetext);
}