mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-23 04:36:17 +01:00
Add some more tests.
2005-01-20 Matthias Clasen <mclasen@redhat.com> * tests/keyfile-test.c: Add some more tests. * glib/gkeyfile.c (g_key_file_get_keys): Return keys in the order found in the file, rather than the opposite. (g_key_file_parse_value_as_string): Fix error reporting.
This commit is contained in:
parent
92a710ba28
commit
9775edea89
@ -1,3 +1,11 @@
|
||||
2005-01-20 Matthias Clasen <mclasen@redhat.com>
|
||||
|
||||
* tests/keyfile-test.c: Add some more tests.
|
||||
|
||||
* glib/gkeyfile.c (g_key_file_get_keys): Return keys in
|
||||
the order found in the file, rather than the opposite.
|
||||
(g_key_file_parse_value_as_string): Fix error reporting.
|
||||
|
||||
2005-01-19 Matthias Clasen <mclasen@redhat.com>
|
||||
|
||||
* glib/gkeyfile.c (g_key_file_parse_data): Accept \r\n as
|
||||
|
@ -1,3 +1,11 @@
|
||||
2005-01-20 Matthias Clasen <mclasen@redhat.com>
|
||||
|
||||
* tests/keyfile-test.c: Add some more tests.
|
||||
|
||||
* glib/gkeyfile.c (g_key_file_get_keys): Return keys in
|
||||
the order found in the file, rather than the opposite.
|
||||
(g_key_file_parse_value_as_string): Fix error reporting.
|
||||
|
||||
2005-01-19 Matthias Clasen <mclasen@redhat.com>
|
||||
|
||||
* glib/gkeyfile.c (g_key_file_parse_data): Accept \r\n as
|
||||
|
@ -1,3 +1,11 @@
|
||||
2005-01-20 Matthias Clasen <mclasen@redhat.com>
|
||||
|
||||
* tests/keyfile-test.c: Add some more tests.
|
||||
|
||||
* glib/gkeyfile.c (g_key_file_get_keys): Return keys in
|
||||
the order found in the file, rather than the opposite.
|
||||
(g_key_file_parse_value_as_string): Fix error reporting.
|
||||
|
||||
2005-01-19 Matthias Clasen <mclasen@redhat.com>
|
||||
|
||||
* glib/gkeyfile.c (g_key_file_parse_data): Accept \r\n as
|
||||
|
@ -1,3 +1,11 @@
|
||||
2005-01-20 Matthias Clasen <mclasen@redhat.com>
|
||||
|
||||
* tests/keyfile-test.c: Add some more tests.
|
||||
|
||||
* glib/gkeyfile.c (g_key_file_get_keys): Return keys in
|
||||
the order found in the file, rather than the opposite.
|
||||
(g_key_file_parse_value_as_string): Fix error reporting.
|
||||
|
||||
2005-01-19 Matthias Clasen <mclasen@redhat.com>
|
||||
|
||||
* glib/gkeyfile.c (g_key_file_parse_data): Accept \r\n as
|
||||
|
@ -1016,16 +1016,16 @@ g_key_file_get_keys (GKeyFile *key_file,
|
||||
keys = (gchar **) g_new0 (gchar **, num_keys + 1);
|
||||
|
||||
tmp = group->key_value_pairs;
|
||||
for (i = 0; i < num_keys; i++)
|
||||
for (i = 1; i <= num_keys; i++)
|
||||
{
|
||||
GKeyFileKeyValuePair *pair;
|
||||
|
||||
pair = (GKeyFileKeyValuePair *) tmp->data;
|
||||
keys[i] = g_strdup (pair->key);
|
||||
keys[num_keys - i] = g_strdup (pair->key);
|
||||
|
||||
tmp = tmp->next;
|
||||
}
|
||||
keys[i] = NULL;
|
||||
keys[num_keys] = NULL;
|
||||
|
||||
if (length)
|
||||
*length = num_keys;
|
||||
@ -2976,7 +2976,6 @@ g_key_file_parse_value_as_string (GKeyFile *key_file,
|
||||
GSList **pieces,
|
||||
GError **error)
|
||||
{
|
||||
GError *parse_error = NULL;
|
||||
gchar *string_value, *p, *q0, *q;
|
||||
|
||||
string_value = g_new0 (gchar, strlen (value) + 1);
|
||||
@ -3019,7 +3018,7 @@ g_key_file_parse_value_as_string (GKeyFile *key_file,
|
||||
*q++ = '\\';
|
||||
*q = *p;
|
||||
|
||||
if (parse_error == NULL)
|
||||
if (*error == NULL)
|
||||
{
|
||||
gchar sequence[3];
|
||||
|
||||
@ -3050,7 +3049,7 @@ g_key_file_parse_value_as_string (GKeyFile *key_file,
|
||||
p++;
|
||||
}
|
||||
|
||||
if (p > value && p[-1] == '\\' && error == NULL)
|
||||
if (p > value && p[-1] == '\\' && q[-1] != '\\' && *error == NULL)
|
||||
g_set_error (error, G_KEY_FILE_ERROR,
|
||||
G_KEY_FILE_ERROR_INVALID_VALUE,
|
||||
_("Key file contains escape character at end of line"));
|
||||
|
@ -19,6 +19,48 @@ load_data (const gchar *data)
|
||||
return keyfile;
|
||||
}
|
||||
|
||||
static void
|
||||
check_error (GError **error,
|
||||
GQuark domain,
|
||||
gint code)
|
||||
{
|
||||
if (*error == NULL)
|
||||
{
|
||||
g_print ("Missing an error\n");
|
||||
exit (1);
|
||||
}
|
||||
|
||||
if ((*error)->domain != domain)
|
||||
{
|
||||
g_print ("Wrong error domain: got %s, expected %s\n",
|
||||
g_quark_to_string ((*error)->domain),
|
||||
g_quark_to_string (domain));
|
||||
exit (1);
|
||||
}
|
||||
|
||||
if ((*error)->code != code)
|
||||
{
|
||||
g_print ("Wrong error code: got %d, expected %d\n",
|
||||
(*error)->code, code);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
g_error_free (*error);
|
||||
*error = NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
check_no_error (GError **error)
|
||||
{
|
||||
if (*error != NULL)
|
||||
{
|
||||
g_print ("Unexpected error: (%s, %d) %s\n",
|
||||
g_quark_to_string ((*error)->domain),
|
||||
(*error)->code, (*error)->message);
|
||||
exit (1);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
check_string_value (GKeyFile *keyfile,
|
||||
const gchar *group,
|
||||
@ -29,23 +71,75 @@ check_string_value (GKeyFile *keyfile,
|
||||
gchar *value;
|
||||
|
||||
value = g_key_file_get_string (keyfile, group, key, &error);
|
||||
if (error)
|
||||
{
|
||||
g_print ("Group %s key %s: %s\n", group, key, error->message);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
check_no_error (&error);
|
||||
g_assert (value != NULL);
|
||||
|
||||
if (strcmp (value, expected) != 0)
|
||||
{
|
||||
g_print ("Group %s key %s: "
|
||||
"expected value '%s', actual value '%s'\n",
|
||||
"expected string value '%s', actual value '%s'\n",
|
||||
group, key, expected, value);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
g_free (value);
|
||||
}
|
||||
|
||||
static void
|
||||
check_boolean_value (GKeyFile *keyfile,
|
||||
const gchar *group,
|
||||
const gchar *key,
|
||||
gboolean expected)
|
||||
{
|
||||
GError *error = NULL;
|
||||
gboolean value;
|
||||
|
||||
value = g_key_file_get_boolean (keyfile, group, key, &error);
|
||||
check_no_error (&error);
|
||||
|
||||
if (value != expected)
|
||||
{
|
||||
g_print ("Group %s key %s: "
|
||||
"expected boolean value '%s', actual value '%s'\n",
|
||||
group, key,
|
||||
expected ? "true" : "false",
|
||||
value ? "true" : "false");
|
||||
exit (1);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
check_name (const gchar *what,
|
||||
const gchar *value,
|
||||
const gchar *expected,
|
||||
gint position)
|
||||
{
|
||||
if (strcmp (expected, value) != 0)
|
||||
{
|
||||
g_print ("Wrong %s returned: got %s at %d, expected %s\n",
|
||||
what, value, position, expected);
|
||||
exit (1);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
check_length (const gchar *what,
|
||||
gint n_items,
|
||||
gint length,
|
||||
gint expected)
|
||||
{
|
||||
if (n_items != length || length != expected)
|
||||
{
|
||||
g_print ("Wrong number of %s returned: got %d items, length %d, expected %d\n",
|
||||
what, n_items, length, expected);
|
||||
exit (1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* check that both \n and \r\n are accepted as line ends,
|
||||
* and that stray \r are passed through
|
||||
*/
|
||||
static void
|
||||
test_line_ends (void)
|
||||
{
|
||||
@ -65,8 +159,12 @@ test_line_ends (void)
|
||||
check_string_value (keyfile, "group1", "key2", "value2");
|
||||
check_string_value (keyfile, "group2", "key3", "value3\r");
|
||||
check_string_value (keyfile, "group2", "key4", "value4");
|
||||
|
||||
g_key_file_free (keyfile);
|
||||
}
|
||||
|
||||
/* check handling of whitespace
|
||||
*/
|
||||
static void
|
||||
test_whitespace (void)
|
||||
{
|
||||
@ -78,7 +176,8 @@ test_whitespace (void)
|
||||
"key2\t=\tvalue2\n"
|
||||
" [ group2 ] \n"
|
||||
"key3 = value3 \n"
|
||||
"key4 = value \t4\n";
|
||||
"key4 = value \t4\n"
|
||||
" key5 = value5\n";
|
||||
|
||||
keyfile = load_data (data);
|
||||
|
||||
@ -86,6 +185,162 @@ test_whitespace (void)
|
||||
check_string_value (keyfile, "group1", "key2", "value2");
|
||||
check_string_value (keyfile, " group2 ", "key3", "value3 ");
|
||||
check_string_value (keyfile, " group2 ", "key4", "value \t4");
|
||||
check_string_value (keyfile, " group2 ", "key5", "value5");
|
||||
|
||||
g_key_file_free (keyfile);
|
||||
}
|
||||
|
||||
/* check key and group listing */
|
||||
static void
|
||||
test_listing (void)
|
||||
{
|
||||
GKeyFile *keyfile;
|
||||
gchar **names;
|
||||
gsize len;
|
||||
gchar *start;
|
||||
GError *error = NULL;
|
||||
|
||||
const gchar *data =
|
||||
"[group1]\n"
|
||||
"key1=value1\n"
|
||||
"key2=value2\n"
|
||||
"[group2]\n"
|
||||
"key3=value3\n"
|
||||
"key4=value4\n";
|
||||
|
||||
keyfile = load_data (data);
|
||||
|
||||
names = g_key_file_get_groups (keyfile, &len);
|
||||
if (names == NULL)
|
||||
{
|
||||
g_print ("Error listing groups\n");
|
||||
exit (1);
|
||||
}
|
||||
|
||||
check_length ("groups", g_strv_length (names), len, 2);
|
||||
check_name ("group name", names[0], "group1", 0);
|
||||
check_name ("group name", names[1], "group2", 1);
|
||||
|
||||
g_strfreev (names);
|
||||
|
||||
names = g_key_file_get_keys (keyfile, "group1", &len, &error);
|
||||
check_no_error (&error);
|
||||
|
||||
check_length ("keys", g_strv_length (names), len, 2);
|
||||
check_name ("key", names[0], "key1", 0);
|
||||
check_name ("key", names[1], "key2", 1);
|
||||
|
||||
g_strfreev (names);
|
||||
|
||||
names = g_key_file_get_keys (keyfile, "no-such-group", &len, &error);
|
||||
check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
|
||||
|
||||
g_strfreev (names);
|
||||
|
||||
if (!g_key_file_has_group (keyfile, "group1") ||
|
||||
!g_key_file_has_group (keyfile, "group2") ||
|
||||
g_key_file_has_group (keyfile, "group10") ||
|
||||
g_key_file_has_group (keyfile, "group2 "))
|
||||
{
|
||||
g_print ("Group finding trouble\n");
|
||||
exit (1);
|
||||
}
|
||||
|
||||
start = g_key_file_get_start_group (keyfile);
|
||||
if (!start || strcmp (start, "group1") != 0)
|
||||
{
|
||||
g_print ("Start group finding trouble\n");
|
||||
exit (1);
|
||||
}
|
||||
g_free (start);
|
||||
|
||||
if (!g_key_file_has_key (keyfile, "group1", "key1", &error) ||
|
||||
!g_key_file_has_key (keyfile, "group2", "key3", &error) ||
|
||||
g_key_file_has_key (keyfile, "group2", "no-such-key", &error))
|
||||
{
|
||||
g_print ("Key finding trouble\n");
|
||||
exit (1);
|
||||
}
|
||||
check_no_error (&error);
|
||||
|
||||
g_key_file_has_key (keyfile, "no-such-group", "key", &error);
|
||||
check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
|
||||
|
||||
g_key_file_free (keyfile);
|
||||
}
|
||||
|
||||
/* check parsing of string values */
|
||||
static void
|
||||
test_string (void)
|
||||
{
|
||||
GKeyFile *keyfile;
|
||||
GError *error = NULL;
|
||||
gchar *value;
|
||||
|
||||
const gchar *data =
|
||||
"[valid]\n"
|
||||
"key1=\\s\\n\\t\\r\\\\\n"
|
||||
"key2=\"quoted\"\n"
|
||||
"key3='quoted'\n"
|
||||
"key4=\xe2\x89\xa0\xe2\x89\xa0\n"
|
||||
"[invalid]\n"
|
||||
"key1=\\a\\b\\0800xff\n"
|
||||
"key2=blabla\\\n";
|
||||
|
||||
keyfile = load_data (data);
|
||||
|
||||
check_string_value (keyfile, "valid", "key1", " \n\t\r\\");
|
||||
check_string_value (keyfile, "valid", "key2", "\"quoted\"");
|
||||
check_string_value (keyfile, "valid", "key3", "'quoted'");
|
||||
check_string_value (keyfile, "valid", "key4", "\xe2\x89\xa0\xe2\x89\xa0");
|
||||
|
||||
value = g_key_file_get_string (keyfile, "invalid", "key1", &error);
|
||||
check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
|
||||
g_free (value);
|
||||
|
||||
value = g_key_file_get_string (keyfile, "invalid", "key2", &error);
|
||||
check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
|
||||
g_free (value);
|
||||
|
||||
g_key_file_free (keyfile);
|
||||
}
|
||||
|
||||
/* check parsing of boolean values */
|
||||
static void
|
||||
test_boolean (void)
|
||||
{
|
||||
GKeyFile *keyfile;
|
||||
GError *error = NULL;
|
||||
|
||||
const gchar *data =
|
||||
"[valid]\n"
|
||||
"key1=true\n"
|
||||
"key2=false\n"
|
||||
"key3=1\n"
|
||||
"key4=0\n"
|
||||
"[invalid]\n"
|
||||
"key1=t\n"
|
||||
"key2=f\n"
|
||||
"key3=yes\n"
|
||||
"key4=no\n";
|
||||
|
||||
keyfile = load_data (data);
|
||||
check_boolean_value (keyfile, "valid", "key1", TRUE);
|
||||
check_boolean_value (keyfile, "valid", "key2", FALSE);
|
||||
check_boolean_value (keyfile, "valid", "key3", TRUE);
|
||||
check_boolean_value (keyfile, "valid", "key4", FALSE);
|
||||
|
||||
g_key_file_get_boolean (keyfile, "invalid", "key1", &error);
|
||||
check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
|
||||
|
||||
g_key_file_get_boolean (keyfile, "invalid", "key2", &error);
|
||||
check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
|
||||
|
||||
g_key_file_get_boolean (keyfile, "invalid", "key3", &error);
|
||||
check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
|
||||
|
||||
g_key_file_get_boolean (keyfile, "invalid", "key4", &error);
|
||||
check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
|
||||
}
|
||||
|
||||
int
|
||||
@ -93,6 +348,9 @@ main (int argc, char *argv[])
|
||||
{
|
||||
test_line_ends ();
|
||||
test_whitespace ();
|
||||
test_listing ();
|
||||
test_string ();
|
||||
test_boolean ();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user