1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#include <glib.h>
#include "celluloid-option-parser.h"
static void
test_option_with_no_value(void)
{
const gchar *str = "--foo";
gchar *key = NULL;
gchar *val = NULL;
str = parse_option(str, &key, &val);
g_assert_true(str && !*str);
g_assert_true(g_strcmp0(key, "foo") == 0);
g_assert_true(!val);
g_free(key);
g_free(val);
}
static void
test_option_with_value(void)
{
const gchar *str = "--foo=bar";
gchar *key = NULL;
gchar *val = NULL;
str = parse_option(str, &key, &val);
g_assert_true(str && !*str);
g_assert_true(g_strcmp0(key, "foo") == 0);
g_assert_true(g_strcmp0(val, "bar") == 0);
g_free(key);
g_free(val);
}
static void
test_option_with_hyphen(void)
{
const gchar *str = "--foo-bar=baz";
gchar *key = NULL;
gchar *val = NULL;
str = parse_option(str, &key, &val);
g_assert_true(str && !*str);
g_assert_true(g_strcmp0(key, "foo-bar") == 0);
g_assert_true(g_strcmp0(val, "baz") == 0);
g_free(key);
g_free(val);
}
static void
test_option_with_underscore(void)
{
const gchar *str = "--foo_bar=baz";
gchar *key = NULL;
gchar *val = NULL;
str = parse_option(str, &key, &val);
g_assert_true(str && !*str);
g_assert_true(g_strcmp0(key, "foo_bar") == 0);
g_assert_true(g_strcmp0(val, "baz") == 0);
g_free(key);
g_free(val);
}
static void
test_multiple_options(void)
{
const gchar *str = "--foo --foo-bar=baz --bar --baz=foo";
gchar *key = NULL;
gchar *val = NULL;
str = parse_option(str, &key, &val);
g_assert_true(str);
g_assert_true(g_strcmp0(key, "foo") == 0);
g_assert_true(!val);
g_clear_pointer(&key, g_free);
g_clear_pointer(&val, g_free);
str = parse_option(str, &key, &val);
g_assert_true(str);
g_assert_true(g_strcmp0(key, "foo-bar") == 0);
g_assert_true(g_strcmp0(val, "baz") == 0);
g_clear_pointer(&key, g_free);
g_clear_pointer(&val, g_free);
str = parse_option(str, &key, &val);
g_assert_true(str);
g_assert_true(g_strcmp0(key, "bar") == 0);
g_assert_true(!val);
g_clear_pointer(&key, g_free);
g_clear_pointer(&val, g_free);
str = parse_option(str, &key, &val);
g_assert_true(str && !*str);
g_assert_true(g_strcmp0(key, "baz") == 0);
g_assert_true(g_strcmp0(val, "foo") == 0);
g_clear_pointer(&key, g_free);
g_clear_pointer(&val, g_free);
}
static void
test_option_with_no_prefix(void)
{
const gchar *str = "foo=bar";
gchar *key = NULL;
gchar *val = NULL;
str = parse_option(str, &key, &val);
g_assert_true(str && !*str);
g_assert_true(g_strcmp0(key, "foo") == 0);
g_assert_true(g_strcmp0(val, "bar") == 0);
g_free(key);
g_free(val);
}
static void
test_quoted_value(void)
{
const gchar *str = "--foo='bar baz'";
gchar *key = NULL;
gchar *val = NULL;
str = parse_option(str, &key, &val);
g_assert_true(str && !*str);
g_assert_true(g_strcmp0(key, "foo") == 0);
g_assert_true(g_strcmp0(val, "bar baz") == 0);
g_free(key);
g_free(val);
}
static void
test_double_quoted_value(void)
{
const gchar *str = "--foo=\"bar baz\"";
gchar *key = NULL;
gchar *val = NULL;
str = parse_option(str, &key, &val);
g_assert_true(str && !*str);
g_assert_true(g_strcmp0(key, "foo") == 0);
g_assert_true(g_strcmp0(val, "bar baz") == 0);
g_free(key);
g_free(val);
}
int
main(gint argc, gchar **argv)
{
g_test_init(&argc, &argv, NULL);
g_test_set_nonfatal_assertions();
g_test_add_func("/test-option-with-no-value",
test_option_with_no_value );
g_test_add_func("/test-option-with-value",
test_option_with_value );
g_test_add_func("/test-option-with-hyphen",
test_option_with_hyphen );
g_test_add_func("/test-option-with-underscore",
test_option_with_underscore );
g_test_add_func("/test-multiple-options",
test_multiple_options );
g_test_add_func("/test-option-with-no-prefix",
test_option_with_no_prefix );
g_test_add_func("/test-quoted-value",
test_quoted_value );
g_test_add_func("/test-double-quoted-value",
test_double_quoted_value );
return g_test_run();
}
|