7 load_data (const gchar *data,
13 keyfile = g_key_file_new ();
14 g_key_file_load_from_data (keyfile, data, -1, flags, &error);
17 g_print ("Could not load data: %s\n", error->message);
25 check_error (GError **error,
31 g_print ("Missing an error\n");
35 if ((*error)->domain != domain)
37 g_print ("Wrong error domain: got %s, expected %s\n",
38 g_quark_to_string ((*error)->domain),
39 g_quark_to_string (domain));
43 if ((*error)->code != code)
45 g_print ("Wrong error code: got %d, expected %d\n",
46 (*error)->code, code);
50 g_error_free (*error);
55 check_no_error (GError **error)
59 g_print ("Unexpected error: (%s, %d) %s\n",
60 g_quark_to_string ((*error)->domain),
61 (*error)->code, (*error)->message);
67 check_string_value (GKeyFile *keyfile,
70 const gchar *expected)
75 value = g_key_file_get_string (keyfile, group, key, &error);
76 check_no_error (&error);
77 g_assert (value != NULL);
79 if (strcmp (value, expected) != 0)
81 g_print ("Group %s key %s: "
82 "expected string value '%s', actual value '%s'\n",
83 group, key, expected, value);
91 check_locale_string_value (GKeyFile *keyfile,
95 const gchar *expected)
100 value = g_key_file_get_locale_string (keyfile, group, key, locale, &error);
101 check_no_error (&error);
102 g_assert (value != NULL);
104 if (strcmp (value, expected) != 0)
106 g_print ("Group %s key %s locale %s: "
107 "expected string value '%s', actual value '%s'\n",
108 group, key, locale, expected, value);
116 check_string_list_value (GKeyFile *keyfile,
125 GError *error = NULL;
127 value = g_key_file_get_string_list (keyfile, group, key, &len, &error);
128 check_no_error (&error);
129 g_assert (value != NULL);
131 va_start (args, key);
133 v = va_arg (args, gchar*);
136 if (value[i] == NULL)
138 g_print ("Group %s key %s: list too short (%d)\n",
142 if (strcmp (v, value[i]) != 0)
144 g_print ("Group %s key %s: mismatch at %d, expected %s, got %s\n",
145 group, key, i, v, value[i]);
150 v = va_arg (args, gchar*);
159 check_integer_list_value (GKeyFile *keyfile,
168 GError *error = NULL;
170 value = g_key_file_get_integer_list (keyfile, group, key, &len, &error);
171 check_no_error (&error);
172 g_assert (value != NULL);
174 va_start (args, key);
176 v = va_arg (args, gint);
181 g_print ("Group %s key %s: list too short (%d)\n",
187 g_print ("Group %s key %s: mismatch at %d, expected %d, got %d\n",
188 group, key, i, v, value[i]);
193 v = va_arg (args, gint);
202 check_boolean_list_value (GKeyFile *keyfile,
211 GError *error = NULL;
213 value = g_key_file_get_boolean_list (keyfile, group, key, &len, &error);
214 check_no_error (&error);
215 g_assert (value != NULL);
217 va_start (args, key);
219 v = va_arg (args, gboolean);
224 g_print ("Group %s key %s: list too short (%d)\n",
230 g_print ("Group %s key %s: mismatch at %d, expected %d, got %d\n",
231 group, key, i, v, value[i]);
236 v = va_arg (args, gboolean);
245 check_boolean_value (GKeyFile *keyfile,
250 GError *error = NULL;
253 value = g_key_file_get_boolean (keyfile, group, key, &error);
254 check_no_error (&error);
256 if (value != expected)
258 g_print ("Group %s key %s: "
259 "expected boolean value '%s', actual value '%s'\n",
261 expected ? "true" : "false",
262 value ? "true" : "false");
268 check_integer_value (GKeyFile *keyfile,
273 GError *error = NULL;
276 value = g_key_file_get_integer (keyfile, group, key, &error);
277 check_no_error (&error);
279 if (value != expected)
281 g_print ("Group %s key %s: "
282 "expected integer value %d, actual value %d\n",
283 group, key, expected, value);
289 check_name (const gchar *what,
291 const gchar *expected,
294 if (!value || strcmp (expected, value) != 0)
296 g_print ("Wrong %s returned: got '%s' at %d, expected '%s'\n",
297 what, value, position, expected);
303 check_length (const gchar *what,
308 if (n_items != length || length != expected)
310 g_print ("Wrong number of %s returned: got %d items, length %d, expected %d\n",
311 what, n_items, length, expected);
317 /* check that both \n and \r\n are accepted as line ends,
318 * and that stray \r are passed through
321 test_line_ends (void)
333 keyfile = load_data (data, 0);
335 check_string_value (keyfile, "group1", "key1", "value1");
336 check_string_value (keyfile, "group1", "key2", "value2");
337 check_string_value (keyfile, "group2", "key3", "value3\r");
338 check_string_value (keyfile, "group2", "key4", "value4");
340 g_key_file_free (keyfile);
343 /* check handling of whitespace
346 test_whitespace (void)
359 keyfile = load_data (data, 0);
361 check_string_value (keyfile, "group1", "key1", "value1");
362 check_string_value (keyfile, "group1", "key2", "value2");
363 check_string_value (keyfile, " group2 ", "key3", "value3 ");
364 check_string_value (keyfile, " group2 ", "key4", "value \t4");
365 check_string_value (keyfile, " group2 ", "key5", "value5");
367 g_key_file_free (keyfile);
370 /* check handling of comments
378 GError *error = NULL;
383 "# top comment, continued\n"
387 "# key comment, continued\n"
389 "# line end check\r\n"
393 "# group comment, continued\n"
396 const gchar *top_comment= " top comment\n top comment, continued\n";
397 const gchar *group_comment= " group comment\n group comment, continued\n";
398 const gchar *key_comment= " key comment\n key comment, continued\n";
400 keyfile = load_data (data, 0);
402 check_string_value (keyfile, "group1", "key1", "value1");
403 check_string_value (keyfile, "group1", "key2", "value2");
404 check_string_value (keyfile, "group1", "key3", "value3");
405 check_string_value (keyfile, "group1", "key4", "value4");
407 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
408 check_no_error (&error);
410 check_length ("keys", g_strv_length (names), len, 4);
411 check_name ("key", names[0], "key1", 0);
412 check_name ("key", names[1], "key2", 1);
413 check_name ("key", names[2], "key3", 2);
414 check_name ("key", names[3], "key4", 3);
418 g_key_file_free (keyfile);
420 keyfile = load_data (data, G_KEY_FILE_KEEP_COMMENTS);
422 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
423 check_no_error (&error);
425 check_length ("keys", g_strv_length (names), len, 4);
426 check_name ("key", names[0], "key1", 0);
427 check_name ("key", names[1], "key2", 1);
428 check_name ("key", names[2], "key3", 2);
429 check_name ("key", names[3], "key4", 3);
433 comment = g_key_file_get_comment (keyfile, NULL, NULL, &error);
434 check_no_error (&error);
435 check_name ("top comment", comment, top_comment, 0);
438 comment = g_key_file_get_comment (keyfile, "group1", "key2", &error);
439 check_no_error (&error);
440 check_name ("key comment", comment, key_comment, 0);
443 comment = g_key_file_get_comment (keyfile, "group2", NULL, &error);
444 check_no_error (&error);
445 check_name ("group comment", comment, group_comment, 0);
448 g_key_file_free (keyfile);
452 /* check key and group listing */
460 GError *error = NULL;
470 keyfile = load_data (data, 0);
472 names = g_key_file_get_groups (keyfile, &len);
475 g_print ("Error listing groups\n");
479 check_length ("groups", g_strv_length (names), len, 2);
480 check_name ("group name", names[0], "group1", 0);
481 check_name ("group name", names[1], "group2", 1);
485 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
486 check_no_error (&error);
488 check_length ("keys", g_strv_length (names), len, 2);
489 check_name ("key", names[0], "key1", 0);
490 check_name ("key", names[1], "key2", 1);
494 names = g_key_file_get_keys (keyfile, "no-such-group", &len, &error);
495 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
499 if (!g_key_file_has_group (keyfile, "group1") ||
500 !g_key_file_has_group (keyfile, "group2") ||
501 g_key_file_has_group (keyfile, "group10") ||
502 g_key_file_has_group (keyfile, "group2 "))
504 g_print ("Group finding trouble\n");
508 start = g_key_file_get_start_group (keyfile);
509 if (!start || strcmp (start, "group1") != 0)
511 g_print ("Start group finding trouble\n");
516 if (!g_key_file_has_key (keyfile, "group1", "key1", &error) ||
517 !g_key_file_has_key (keyfile, "group2", "key3", &error) ||
518 g_key_file_has_key (keyfile, "group2", "no-such-key", &error))
520 g_print ("Key finding trouble\n");
523 check_no_error (&error);
525 g_key_file_has_key (keyfile, "no-such-group", "key", &error);
526 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
528 g_key_file_free (keyfile);
531 /* check parsing of string values */
536 GError *error = NULL;
541 "key1=\\s\\n\\t\\r\\\\\n"
544 "key4=\xe2\x89\xa0\xe2\x89\xa0\n"
546 "key1=\\a\\b\\0800xff\n"
549 keyfile = load_data (data, 0);
551 check_string_value (keyfile, "valid", "key1", " \n\t\r\\");
552 check_string_value (keyfile, "valid", "key2", "\"quoted\"");
553 check_string_value (keyfile, "valid", "key3", "'quoted'");
554 check_string_value (keyfile, "valid", "key4", "\xe2\x89\xa0\xe2\x89\xa0");
556 value = g_key_file_get_string (keyfile, "invalid", "key1", &error);
557 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
560 value = g_key_file_get_string (keyfile, "invalid", "key2", &error);
561 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
564 g_key_file_free (keyfile);
567 /* check parsing of boolean values */
572 GError *error = NULL;
586 keyfile = load_data (data, 0);
588 check_boolean_value (keyfile, "valid", "key1", TRUE);
589 check_boolean_value (keyfile, "valid", "key2", FALSE);
590 check_boolean_value (keyfile, "valid", "key3", TRUE);
591 check_boolean_value (keyfile, "valid", "key4", FALSE);
593 g_key_file_get_boolean (keyfile, "invalid", "key1", &error);
594 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
596 g_key_file_get_boolean (keyfile, "invalid", "key2", &error);
597 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
599 g_key_file_get_boolean (keyfile, "invalid", "key3", &error);
600 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
602 g_key_file_get_boolean (keyfile, "invalid", "key4", &error);
603 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
605 g_key_file_free (keyfile);
608 /* check parsing of integer values */
613 GError *error = NULL;
629 keyfile = load_data (data, 0);
631 check_integer_value (keyfile, "valid", "key1", 0);
632 check_integer_value (keyfile, "valid", "key2", 1);
633 check_integer_value (keyfile, "valid", "key3", -1);
634 check_integer_value (keyfile, "valid", "key4", 2324431);
635 check_integer_value (keyfile, "valid", "key5", -2324431);
636 check_integer_value (keyfile, "valid", "key6", 111);
638 g_key_file_get_integer (keyfile, "invalid", "key1", &error);
639 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
641 g_key_file_get_integer (keyfile, "invalid", "key2", &error);
642 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
644 g_key_file_get_integer (keyfile, "invalid", "key3", &error);
645 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
647 g_key_file_get_integer (keyfile, "invalid", "key4", &error);
648 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
650 g_key_file_free (keyfile);
653 /* check handling of translated strings */
655 test_locale_string (void)
658 GError *error = NULL;
665 "key1[de_DE]=v1-de_DE\n"
666 "key1[de_DE.UTF8]=v1-de_DE.UTF8\n"
674 keyfile = load_data (data, G_KEY_FILE_KEEP_TRANSLATIONS);
676 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
677 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
678 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de_DE");
679 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de_DE.UTF8");
680 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1-fr");
681 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1-fr");
682 check_locale_string_value (keyfile, "valid", "key1", "en", "v1-en");
684 value = g_key_file_get_locale_string (keyfile, "invalid", "key1", "de", &error);
685 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
688 value = g_key_file_get_locale_string (keyfile, "invalid", "key1", "fr", &error);
689 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
692 value = g_key_file_get_locale_string (keyfile, "invalid", "key1", "en", &error);
693 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
696 g_key_file_free (keyfile);
698 /* now test that translations are thrown away */
700 g_setenv ("LANGUAGE", "de", TRUE);
701 setlocale (LC_ALL, "");
703 keyfile = load_data (data, 0);
705 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
706 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
707 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de");
708 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de");
709 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1");
710 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1");
711 check_locale_string_value (keyfile, "valid", "key1", "en", "v1");
713 g_key_file_free (keyfile);
729 "key7= 1 ; 0 ; -1 \n"
732 keyfile = load_data (data, 0);
734 check_string_list_value (keyfile, "valid", "key1", "v1", "v2", NULL);
735 check_string_list_value (keyfile, "valid", "key2", "v1", "v2", NULL);
736 check_string_list_value (keyfile, "valid", "key3", "v1,v2", NULL);
737 check_string_list_value (keyfile, "valid", "key4", "v1;v2", NULL);
738 check_boolean_list_value (keyfile, "valid", "key5", TRUE, FALSE, -100);
739 check_integer_list_value (keyfile, "valid", "key6", 1, 0, -1, -100);
740 /* maybe these should be valid */
741 /* check_integer_list_value (keyfile, "valid", "key7", 1, 0, -1, -100);*/
742 /* check_string_list_value (keyfile, "valid", "key8", "v1\\,v2", NULL);*/
744 g_key_file_free (keyfile);
746 /* Now check an alternate separator */
748 keyfile = load_data (data, 0);
749 g_key_file_set_list_separator (keyfile, ',');
751 check_string_list_value (keyfile, "valid", "key1", "v1;v2", NULL);
752 check_string_list_value (keyfile, "valid", "key2", "v1;v2;", NULL);
753 check_string_list_value (keyfile, "valid", "key3", "v1", "v2", NULL);
755 g_key_file_free (keyfile);
758 /* http://bugzilla.gnome.org/show_bug.cgi?id=165887 */
760 test_group_remove (void)
765 GError *error = NULL;
776 keyfile = load_data (data, 0);
778 names = g_key_file_get_groups (keyfile, &len);
781 g_print ("Error listing groups\n");
785 check_length ("groups", g_strv_length (names), len, 3);
786 check_name ("group name", names[0], "group1", 0);
787 check_name ("group name", names[1], "group2", 1);
788 check_name ("group name", names[2], "group3", 2);
790 g_key_file_remove_group (keyfile, "group1", &error);
791 check_no_error (&error);
795 names = g_key_file_get_groups (keyfile, &len);
798 g_print ("Error listing groups\n");
802 check_length ("groups", g_strv_length (names), len, 2);
803 check_name ("group name", names[0], "group2", 0);
804 check_name ("group name", names[1], "group3", 1);
806 g_key_file_remove_group (keyfile, "group2", &error);
807 check_no_error (&error);
811 names = g_key_file_get_groups (keyfile, &len);
814 g_print ("Error listing groups\n");
818 check_length ("groups", g_strv_length (names), len, 1);
819 check_name ("group name", names[0], "group3", 0);
821 g_key_file_remove_group (keyfile, "no such group", &error);
822 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
826 g_key_file_free (keyfile);
829 /* http://bugzilla.gnome.org/show_bug.cgi?id=165980 */
831 test_key_remove (void)
835 GError *error = NULL;
842 keyfile = load_data (data, 0);
844 check_string_value (keyfile, "group1", "key1", "bla");
846 g_key_file_remove_key (keyfile, "group1", "key1", &error);
847 check_no_error (&error);
849 value = g_key_file_get_string (keyfile, "group1", "key1", &error);
850 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
853 g_key_file_remove_key (keyfile, "group1", "key1", &error);
854 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
856 g_key_file_remove_key (keyfile, "no such group", "key1", &error);
857 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
859 g_key_file_free (keyfile);
863 /* http://bugzilla.gnome.org/show_bug.cgi?id=316309 */
875 keyfile = load_data (data, 0);
877 check_string_value (keyfile, "1", "key1", "123");
878 check_string_value (keyfile, "2", "key2", "123");
880 g_key_file_free (keyfile);
885 main (int argc, char *argv[])
894 test_locale_string ();
896 test_group_remove ();