mirror of
https://github.com/profanity-im/profanity.git
synced 2025-01-03 14:57:42 -05:00
Added parse_options
This commit is contained in:
parent
cc68fc5e9a
commit
8d77930ece
@ -372,3 +372,43 @@ get_start(char *string, int tokens)
|
||||
|
||||
return result_str;
|
||||
}
|
||||
|
||||
GHashTable *
|
||||
parse_options(gchar **args, int start, GList *keys, gboolean *res)
|
||||
{
|
||||
GHashTable *options = NULL;
|
||||
|
||||
// no options found, success
|
||||
if (args[start] == NULL) {
|
||||
options = g_hash_table_new(g_str_hash, g_str_equal);
|
||||
*res = TRUE;
|
||||
return options;
|
||||
}
|
||||
|
||||
// check each option is valid and has value, failure if not
|
||||
int curr;
|
||||
for (curr = start; curr < g_strv_length(args); curr+= 2) {
|
||||
if ((g_list_find(keys, args[curr]) == NULL) || (args[curr+1] == NULL)) {
|
||||
*res = FALSE;
|
||||
return options;
|
||||
}
|
||||
}
|
||||
|
||||
// create map
|
||||
options = g_hash_table_new(g_str_hash, g_str_equal);
|
||||
*res = TRUE;
|
||||
for (curr = start; curr < g_strv_length(args); curr+=2) {
|
||||
g_hash_table_insert(options, args[curr], args[curr+1]);
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
void
|
||||
options_destroy(GHashTable *options)
|
||||
{
|
||||
if (options != NULL) {
|
||||
g_hash_table_destroy(options);
|
||||
options = NULL;
|
||||
}
|
||||
}
|
@ -29,5 +29,7 @@ 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);
|
||||
GHashTable* parse_options(gchar **args, int start, GList *keys, gboolean *res);
|
||||
void options_destroy(GHashTable *options);
|
||||
|
||||
#endif
|
||||
#endif
|
@ -502,4 +502,169 @@ get_first_two_of_three_first_and_second_quoted(void **state)
|
||||
char *result = get_start(inp, 3);
|
||||
|
||||
assert_string_equal("\"one\" \"two\" ", result);
|
||||
}
|
||||
|
||||
void
|
||||
parse_options_when_none_returns_empty_hasmap(void **state)
|
||||
{
|
||||
gchar *args[] = { "cmd1", "cmd2", NULL };
|
||||
|
||||
GList *keys = NULL;
|
||||
keys = g_list_append(keys, "opt1");
|
||||
|
||||
gboolean res = FALSE;
|
||||
|
||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
||||
|
||||
assert_true(options != NULL);
|
||||
assert_int_equal(0, g_hash_table_size(options));
|
||||
assert_true(res);
|
||||
|
||||
options_destroy(options);
|
||||
}
|
||||
|
||||
void
|
||||
parse_options_when_opt1_no_val_sets_error(void **state)
|
||||
{
|
||||
gchar *args[] = { "cmd1", "cmd2", "opt1", NULL };
|
||||
|
||||
GList *keys = NULL;
|
||||
keys = g_list_append(keys, "opt1");
|
||||
|
||||
gboolean res = TRUE;
|
||||
|
||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
||||
|
||||
assert_null(options);
|
||||
assert_false(res);
|
||||
|
||||
options_destroy(options);
|
||||
}
|
||||
|
||||
void
|
||||
parse_options_when_one_returns_map(void **state)
|
||||
{
|
||||
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", NULL };
|
||||
|
||||
GList *keys = NULL;
|
||||
keys = g_list_append(keys, "opt1");
|
||||
|
||||
gboolean res = FALSE;
|
||||
|
||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
||||
|
||||
assert_int_equal(1, g_hash_table_size(options));
|
||||
assert_true(g_hash_table_contains(options, "opt1"));
|
||||
assert_string_equal("val1", g_hash_table_lookup(options, "opt1"));
|
||||
assert_true(res);
|
||||
|
||||
options_destroy(options);
|
||||
}
|
||||
|
||||
void
|
||||
parse_options_when_opt2_no_val_sets_error(void **state)
|
||||
{
|
||||
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", NULL };
|
||||
|
||||
GList *keys = NULL;
|
||||
keys = g_list_append(keys, "opt1");
|
||||
keys = g_list_append(keys, "opt2");
|
||||
|
||||
gboolean res = TRUE;
|
||||
|
||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
||||
|
||||
assert_null(options);
|
||||
assert_false(res);
|
||||
|
||||
options_destroy(options);
|
||||
}
|
||||
|
||||
void
|
||||
parse_options_when_two_returns_map(void **state)
|
||||
{
|
||||
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", "val2", NULL };
|
||||
|
||||
GList *keys = NULL;
|
||||
keys = g_list_append(keys, "opt1");
|
||||
keys = g_list_append(keys, "opt2");
|
||||
|
||||
gboolean res = FALSE;
|
||||
|
||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
||||
|
||||
assert_int_equal(2, g_hash_table_size(options));
|
||||
assert_true(g_hash_table_contains(options, "opt1"));
|
||||
assert_true(g_hash_table_contains(options, "opt2"));
|
||||
assert_string_equal("val1", g_hash_table_lookup(options, "opt1"));
|
||||
assert_string_equal("val2", g_hash_table_lookup(options, "opt2"));
|
||||
assert_true(res);
|
||||
|
||||
options_destroy(options);
|
||||
}
|
||||
|
||||
void
|
||||
parse_options_when_opt3_no_val_sets_error(void **state)
|
||||
{
|
||||
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", "val2", "opt3", NULL };
|
||||
|
||||
GList *keys = NULL;
|
||||
keys = g_list_append(keys, "opt1");
|
||||
keys = g_list_append(keys, "opt2");
|
||||
keys = g_list_append(keys, "opt3");
|
||||
|
||||
gboolean res = TRUE;
|
||||
|
||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
||||
|
||||
assert_null(options);
|
||||
assert_false(res);
|
||||
|
||||
options_destroy(options);
|
||||
}
|
||||
|
||||
void
|
||||
parse_options_when_three_returns_map(void **state)
|
||||
{
|
||||
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", "val2", "opt3", "val3", NULL };
|
||||
|
||||
GList *keys = NULL;
|
||||
keys = g_list_append(keys, "opt1");
|
||||
keys = g_list_append(keys, "opt2");
|
||||
keys = g_list_append(keys, "opt3");
|
||||
|
||||
gboolean res = FALSE;
|
||||
|
||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
||||
|
||||
assert_int_equal(3, g_hash_table_size(options));
|
||||
assert_true(g_hash_table_contains(options, "opt1"));
|
||||
assert_true(g_hash_table_contains(options, "opt2"));
|
||||
assert_true(g_hash_table_contains(options, "opt3"));
|
||||
assert_string_equal("val1", g_hash_table_lookup(options, "opt1"));
|
||||
assert_string_equal("val2", g_hash_table_lookup(options, "opt2"));
|
||||
assert_string_equal("val3", g_hash_table_lookup(options, "opt3"));
|
||||
assert_true(res);
|
||||
|
||||
options_destroy(options);
|
||||
}
|
||||
|
||||
void
|
||||
parse_options_when_unknown_opt_sets_error(void **state)
|
||||
{
|
||||
gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "oops", "val2", "opt3", "val3", NULL };
|
||||
|
||||
GList *keys = NULL;
|
||||
keys = g_list_append(keys, "opt1");
|
||||
keys = g_list_append(keys, "opt2");
|
||||
keys = g_list_append(keys, "opt3");
|
||||
|
||||
gboolean res = TRUE;
|
||||
|
||||
GHashTable *options = parse_options(args, 2, keys, &res);
|
||||
|
||||
assert_null(options);
|
||||
assert_false(res);
|
||||
|
||||
options_destroy(options);
|
||||
}
|
@ -39,3 +39,11 @@ void get_first_two_of_three(void **state);
|
||||
void get_first_two_of_three_first_quoted(void **state);
|
||||
void get_first_two_of_three_second_quoted(void **state);
|
||||
void get_first_two_of_three_first_and_second_quoted(void **state);
|
||||
void parse_options_when_none_returns_empty_hasmap(void **state);
|
||||
void parse_options_when_opt1_no_val_sets_error(void **state);
|
||||
void parse_options_when_one_returns_map(void **state);
|
||||
void parse_options_when_opt2_no_val_sets_error(void **state);
|
||||
void parse_options_when_two_returns_map(void **state);
|
||||
void parse_options_when_opt3_no_val_sets_error(void **state);
|
||||
void parse_options_when_three_returns_map(void **state);
|
||||
void parse_options_when_unknown_opt_sets_error(void **state);
|
@ -165,6 +165,14 @@ int main(int argc, char* argv[]) {
|
||||
unit_test(get_first_two_of_three_first_quoted),
|
||||
unit_test(get_first_two_of_three_second_quoted),
|
||||
unit_test(get_first_two_of_three_first_and_second_quoted),
|
||||
unit_test(parse_options_when_none_returns_empty_hasmap),
|
||||
unit_test(parse_options_when_opt1_no_val_sets_error),
|
||||
unit_test(parse_options_when_one_returns_map),
|
||||
unit_test(parse_options_when_opt2_no_val_sets_error),
|
||||
unit_test(parse_options_when_two_returns_map),
|
||||
unit_test(parse_options_when_opt3_no_val_sets_error),
|
||||
unit_test(parse_options_when_three_returns_map),
|
||||
unit_test(parse_options_when_unknown_opt_sets_error),
|
||||
|
||||
unit_test(empty_list_when_none_added),
|
||||
unit_test(contains_one_element),
|
||||
|
Loading…
Reference in New Issue
Block a user