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_double_list_value (GKeyFile *keyfile,
211 GError *error = NULL;
213 value = g_key_file_get_double_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, gdouble);
224 g_print ("Group %s key %s: list too short (%d)\n",
230 g_print ("Group %s key %s: mismatch at %d, expected %e, got %e\n",
231 group, key, i, v, value[i]);
236 v = va_arg (args, gdouble);
245 check_boolean_list_value (GKeyFile *keyfile,
254 GError *error = NULL;
256 value = g_key_file_get_boolean_list (keyfile, group, key, &len, &error);
257 check_no_error (&error);
258 g_assert (value != NULL);
260 va_start (args, key);
262 v = va_arg (args, gboolean);
267 g_print ("Group %s key %s: list too short (%d)\n",
273 g_print ("Group %s key %s: mismatch at %d, expected %d, got %d\n",
274 group, key, i, v, value[i]);
279 v = va_arg (args, gboolean);
288 check_boolean_value (GKeyFile *keyfile,
293 GError *error = NULL;
296 value = g_key_file_get_boolean (keyfile, group, key, &error);
297 check_no_error (&error);
299 if (value != expected)
301 g_print ("Group %s key %s: "
302 "expected boolean value '%s', actual value '%s'\n",
304 expected ? "true" : "false",
305 value ? "true" : "false");
311 check_integer_value (GKeyFile *keyfile,
316 GError *error = NULL;
319 value = g_key_file_get_integer (keyfile, group, key, &error);
320 check_no_error (&error);
322 if (value != expected)
324 g_print ("Group %s key %s: "
325 "expected integer value %d, actual value %d\n",
326 group, key, expected, value);
332 check_double_value (GKeyFile *keyfile,
337 GError *error = NULL;
340 value = g_key_file_get_double (keyfile, group, key, &error);
341 check_no_error (&error);
343 if (value != expected)
345 g_print ("Group %s key %s: "
346 "expected integer value %e, actual value %e\n",
347 group, key, expected, value);
353 check_name (const gchar *what,
355 const gchar *expected,
358 if (!value || strcmp (expected, value) != 0)
360 g_print ("Wrong %s returned: got '%s' at %d, expected '%s'\n",
361 what, value, position, expected);
367 check_length (const gchar *what,
372 if (n_items != length || length != expected)
374 g_print ("Wrong number of %s returned: got %d items, length %d, expected %d\n",
375 what, n_items, length, expected);
381 /* check that both \n and \r\n are accepted as line ends,
382 * and that stray \r are passed through
385 test_line_ends (void)
397 keyfile = load_data (data, 0);
399 check_string_value (keyfile, "group1", "key1", "value1");
400 check_string_value (keyfile, "group1", "key2", "value2");
401 check_string_value (keyfile, "group2", "key3", "value3\r");
402 check_string_value (keyfile, "group2", "key4", "value4");
404 g_key_file_free (keyfile);
407 /* check handling of whitespace
410 test_whitespace (void)
423 keyfile = load_data (data, 0);
425 check_string_value (keyfile, "group1", "key1", "value1");
426 check_string_value (keyfile, "group1", "key2", "value2");
427 check_string_value (keyfile, " group2 ", "key3", "value3 ");
428 check_string_value (keyfile, " group2 ", "key4", "value \t4");
429 check_string_value (keyfile, " group2 ", "key5", "value5");
431 g_key_file_free (keyfile);
434 /* check handling of comments
442 GError *error = NULL;
447 "# top comment, continued\n"
451 "# key comment, continued\n"
453 "# line end check\r\n"
457 "# group comment, continued\n"
460 const gchar *top_comment= " top comment\n top comment, continued\n";
461 const gchar *group_comment= " group comment\n group comment, continued\n";
462 const gchar *key_comment= " key comment\n key comment, continued\n";
464 keyfile = load_data (data, 0);
466 check_string_value (keyfile, "group1", "key1", "value1");
467 check_string_value (keyfile, "group1", "key2", "value2");
468 check_string_value (keyfile, "group1", "key3", "value3");
469 check_string_value (keyfile, "group1", "key4", "value4");
471 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
472 check_no_error (&error);
474 check_length ("keys", g_strv_length (names), len, 4);
475 check_name ("key", names[0], "key1", 0);
476 check_name ("key", names[1], "key2", 1);
477 check_name ("key", names[2], "key3", 2);
478 check_name ("key", names[3], "key4", 3);
482 g_key_file_free (keyfile);
484 keyfile = load_data (data, G_KEY_FILE_KEEP_COMMENTS);
486 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
487 check_no_error (&error);
489 check_length ("keys", g_strv_length (names), len, 4);
490 check_name ("key", names[0], "key1", 0);
491 check_name ("key", names[1], "key2", 1);
492 check_name ("key", names[2], "key3", 2);
493 check_name ("key", names[3], "key4", 3);
497 comment = g_key_file_get_comment (keyfile, NULL, NULL, &error);
498 check_no_error (&error);
499 check_name ("top comment", comment, top_comment, 0);
502 comment = g_key_file_get_comment (keyfile, "group1", "key2", &error);
503 check_no_error (&error);
504 check_name ("key comment", comment, key_comment, 0);
507 comment = g_key_file_get_comment (keyfile, "group2", NULL, &error);
508 check_no_error (&error);
509 check_name ("group comment", comment, group_comment, 0);
512 comment = g_key_file_get_comment (keyfile, "group3", NULL, &error);
515 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
516 g_assert (comment == NULL);
518 g_key_file_free (keyfile);
522 /* check key and group listing */
530 GError *error = NULL;
540 keyfile = load_data (data, 0);
542 names = g_key_file_get_groups (keyfile, &len);
545 g_print ("Error listing groups\n");
549 check_length ("groups", g_strv_length (names), len, 2);
550 check_name ("group name", names[0], "group1", 0);
551 check_name ("group name", names[1], "group2", 1);
555 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
556 check_no_error (&error);
558 check_length ("keys", g_strv_length (names), len, 2);
559 check_name ("key", names[0], "key1", 0);
560 check_name ("key", names[1], "key2", 1);
564 names = g_key_file_get_keys (keyfile, "no-such-group", &len, &error);
565 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
569 if (!g_key_file_has_group (keyfile, "group1") ||
570 !g_key_file_has_group (keyfile, "group2") ||
571 g_key_file_has_group (keyfile, "group10") ||
572 g_key_file_has_group (keyfile, "group2 "))
574 g_print ("Group finding trouble\n");
578 start = g_key_file_get_start_group (keyfile);
579 if (!start || strcmp (start, "group1") != 0)
581 g_print ("Start group finding trouble\n");
586 if (!g_key_file_has_key (keyfile, "group1", "key1", &error) ||
587 !g_key_file_has_key (keyfile, "group2", "key3", &error) ||
588 g_key_file_has_key (keyfile, "group2", "no-such-key", &error))
590 g_print ("Key finding trouble\n");
593 check_no_error (&error);
595 g_key_file_has_key (keyfile, "no-such-group", "key", &error);
596 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
598 g_key_file_free (keyfile);
601 /* check parsing of string values */
606 GError *error = NULL;
611 "key1=\\s\\n\\t\\r\\\\\n"
614 "key4=\xe2\x89\xa0\xe2\x89\xa0\n"
616 "key1=\\a\\b\\0800xff\n"
619 keyfile = load_data (data, 0);
621 check_string_value (keyfile, "valid", "key1", " \n\t\r\\");
622 check_string_value (keyfile, "valid", "key2", "\"quoted\"");
623 check_string_value (keyfile, "valid", "key3", "'quoted'");
624 check_string_value (keyfile, "valid", "key4", "\xe2\x89\xa0\xe2\x89\xa0");
626 value = g_key_file_get_string (keyfile, "invalid", "key1", &error);
627 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
630 value = g_key_file_get_string (keyfile, "invalid", "key2", &error);
631 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
634 g_key_file_free (keyfile);
637 /* check parsing of boolean values */
642 GError *error = NULL;
656 keyfile = load_data (data, 0);
658 check_boolean_value (keyfile, "valid", "key1", TRUE);
659 check_boolean_value (keyfile, "valid", "key2", FALSE);
660 check_boolean_value (keyfile, "valid", "key3", TRUE);
661 check_boolean_value (keyfile, "valid", "key4", FALSE);
663 g_key_file_get_boolean (keyfile, "invalid", "key1", &error);
664 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
666 g_key_file_get_boolean (keyfile, "invalid", "key2", &error);
667 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
669 g_key_file_get_boolean (keyfile, "invalid", "key3", &error);
670 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
672 g_key_file_get_boolean (keyfile, "invalid", "key4", &error);
673 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
675 g_key_file_free (keyfile);
678 /* check parsing of integer and double values */
683 GError *error = NULL;
706 keyfile = load_data (data, 0);
708 check_integer_value (keyfile, "valid", "key1", 0);
709 check_integer_value (keyfile, "valid", "key2", 1);
710 check_integer_value (keyfile, "valid", "key3", -1);
711 check_integer_value (keyfile, "valid", "key4", 2324431);
712 check_integer_value (keyfile, "valid", "key5", -2324431);
713 check_integer_value (keyfile, "valid", "key6", 111);
714 check_double_value (keyfile, "valid", "dkey1", 111.0);
715 check_double_value (keyfile, "valid", "dkey2", 145.45);
716 check_double_value (keyfile, "valid", "dkey3", -3453.7);
718 g_key_file_get_integer (keyfile, "invalid", "key1", &error);
719 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
721 g_key_file_get_integer (keyfile, "invalid", "key2", &error);
722 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
724 g_key_file_get_integer (keyfile, "invalid", "key3", &error);
725 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
727 g_key_file_get_integer (keyfile, "invalid", "key4", &error);
728 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
730 g_key_file_get_double (keyfile, "invalid", "key5", &error);
731 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
733 g_key_file_get_double (keyfile, "invalid", "key6", &error);
734 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
736 g_key_file_get_double (keyfile, "invalid", "key7", &error);
737 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
739 g_key_file_get_double (keyfile, "invalid", "key8", &error);
740 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
742 g_key_file_free (keyfile);
745 /* check handling of translated strings */
747 test_locale_string (void)
750 GError *error = NULL;
757 "key1[de_DE]=v1-de_DE\n"
758 "key1[de_DE.UTF8]=v1-de_DE.UTF8\n"
766 keyfile = load_data (data, G_KEY_FILE_KEEP_TRANSLATIONS);
768 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
769 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
770 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de_DE");
771 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de_DE.UTF8");
772 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1-fr");
773 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1-fr");
774 check_locale_string_value (keyfile, "valid", "key1", "en", "v1-en");
776 value = g_key_file_get_locale_string (keyfile, "invalid", "key1", "de", &error);
777 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
780 value = g_key_file_get_locale_string (keyfile, "invalid", "key1", "fr", &error);
781 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
784 value = g_key_file_get_locale_string (keyfile, "invalid", "key1", "en", &error);
785 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
788 g_key_file_free (keyfile);
790 /* now test that translations are thrown away */
792 g_setenv ("LANGUAGE", "de", TRUE);
793 setlocale (LC_ALL, "");
795 keyfile = load_data (data, 0);
797 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
798 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
799 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de");
800 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de");
801 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1");
802 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1");
803 check_locale_string_value (keyfile, "valid", "key1", "en", "v1");
805 g_key_file_free (keyfile);
821 "key7= 1 ; 0 ; -1 \n"
823 "key9=0;1.3456;-76532.456\n";
825 keyfile = load_data (data, 0);
827 check_string_list_value (keyfile, "valid", "key1", "v1", "v2", NULL);
828 check_string_list_value (keyfile, "valid", "key2", "v1", "v2", NULL);
829 check_string_list_value (keyfile, "valid", "key3", "v1,v2", NULL);
830 check_string_list_value (keyfile, "valid", "key4", "v1;v2", NULL);
831 check_boolean_list_value (keyfile, "valid", "key5", TRUE, FALSE, -100);
832 check_integer_list_value (keyfile, "valid", "key6", 1, 0, -1, -100);
833 check_double_list_value (keyfile, "valid", "key9", 0.0, 1.3456, -76532.456, -100.0);
834 /* maybe these should be valid */
835 /* check_integer_list_value (keyfile, "valid", "key7", 1, 0, -1, -100);*/
836 /* check_string_list_value (keyfile, "valid", "key8", "v1\\,v2", NULL);*/
838 g_key_file_free (keyfile);
840 /* Now check an alternate separator */
842 keyfile = load_data (data, 0);
843 g_key_file_set_list_separator (keyfile, ',');
845 check_string_list_value (keyfile, "valid", "key1", "v1;v2", NULL);
846 check_string_list_value (keyfile, "valid", "key2", "v1;v2;", NULL);
847 check_string_list_value (keyfile, "valid", "key3", "v1", "v2", NULL);
849 g_key_file_free (keyfile);
852 /* http://bugzilla.gnome.org/show_bug.cgi?id=165887 */
854 test_group_remove (void)
859 GError *error = NULL;
870 keyfile = load_data (data, 0);
872 names = g_key_file_get_groups (keyfile, &len);
875 g_print ("Error listing groups\n");
879 check_length ("groups", g_strv_length (names), len, 3);
880 check_name ("group name", names[0], "group1", 0);
881 check_name ("group name", names[1], "group2", 1);
882 check_name ("group name", names[2], "group3", 2);
884 g_key_file_remove_group (keyfile, "group1", &error);
885 check_no_error (&error);
889 names = g_key_file_get_groups (keyfile, &len);
892 g_print ("Error listing groups\n");
896 check_length ("groups", g_strv_length (names), len, 2);
897 check_name ("group name", names[0], "group2", 0);
898 check_name ("group name", names[1], "group3", 1);
900 g_key_file_remove_group (keyfile, "group2", &error);
901 check_no_error (&error);
905 names = g_key_file_get_groups (keyfile, &len);
908 g_print ("Error listing groups\n");
912 check_length ("groups", g_strv_length (names), len, 1);
913 check_name ("group name", names[0], "group3", 0);
915 g_key_file_remove_group (keyfile, "no such group", &error);
916 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
920 g_key_file_free (keyfile);
923 /* http://bugzilla.gnome.org/show_bug.cgi?id=165980 */
925 test_key_remove (void)
929 GError *error = NULL;
936 keyfile = load_data (data, 0);
938 check_string_value (keyfile, "group1", "key1", "bla");
940 g_key_file_remove_key (keyfile, "group1", "key1", &error);
941 check_no_error (&error);
943 value = g_key_file_get_string (keyfile, "group1", "key1", &error);
944 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
947 g_key_file_remove_key (keyfile, "group1", "key1", &error);
948 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
950 g_key_file_remove_key (keyfile, "no such group", "key1", &error);
951 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
953 g_key_file_free (keyfile);
957 /* http://bugzilla.gnome.org/show_bug.cgi?id=316309 */
969 keyfile = load_data (data, 0);
971 check_string_value (keyfile, "1", "key1", "123");
972 check_string_value (keyfile, "2", "key2", "123");
974 g_key_file_free (keyfile);
978 test_duplicate_keys (void)
986 keyfile = load_data (data, 0);
987 check_string_value (keyfile, "1", "key1", "345");
989 g_key_file_free (keyfile);
992 /* http://bugzilla.gnome.org/show_bug.cgi?id=157877 */
994 test_duplicate_groups (void)
1003 keyfile = load_data (data, 0);
1004 check_string_value (keyfile, "Desktop Entry", "key1", "123");
1005 check_string_value (keyfile, "Desktop Entry", "key2", "123");
1007 g_key_file_free (keyfile);
1010 /* http://bugzilla.gnome.org/show_bug.cgi?id=385910 */
1012 test_duplicate_groups2 (void)
1023 keyfile = load_data (data, 0);
1024 check_string_value (keyfile, "A", "foo", "bang");
1025 check_string_value (keyfile, "B", "foo", "baz");
1027 g_key_file_free (keyfile);
1031 main (int argc, char *argv[])
1040 test_locale_string ();
1042 test_group_remove ();
1045 test_duplicate_keys ();
1046 test_duplicate_groups ();
1047 test_duplicate_groups2 ();