about summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-04-14 22:48:18 +0100
committerJames Booth <boothj5@gmail.com>2014-04-14 22:48:18 +0100
commit8d77930eceba6841673605f7df5083657a0ecd28 (patch)
tree1dd4a3f4b0821cffa2609286e848e4aa0cae23cf /tests
parentcc68fc5e9a7346d501c8c7df8444acc1f538ef58 (diff)
downloadprofani-tty-8d77930eceba6841673605f7df5083657a0ecd28.tar.gz
Added parse_options
Diffstat (limited to 'tests')
-rw-r--r--tests/test_parser.c165
-rw-r--r--tests/test_parser.h8
-rw-r--r--tests/testsuite.c8
3 files changed, 181 insertions, 0 deletions
diff --git a/tests/test_parser.c b/tests/test_parser.c
index d05d17c3..1309e8e1 100644
--- a/tests/test_parser.c
+++ b/tests/test_parser.c
@@ -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);
 }
\ No newline at end of file
diff --git a/tests/test_parser.h b/tests/test_parser.h
index 7b178c3d..70b94b50 100644
--- a/tests/test_parser.h
+++ b/tests/test_parser.h
@@ -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);
\ No newline at end of file
diff --git a/tests/testsuite.c b/tests/testsuite.c
index cdcd61ee..c6577410 100644
--- a/tests/testsuite.c
+++ b/tests/testsuite.c
@@ -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),