7 load_data (const gchar *data,
13 keyfile = g_key_file_new ();
14 g_key_file_load_from_data (keyfile, data, -1, flags, &error);
15 g_assert_no_error (error);
20 check_error (GError **error,
24 g_assert_error (*error, domain, code);
25 g_error_free (*error);
30 check_no_error (GError **error)
32 g_assert_no_error (*error);
36 check_string_value (GKeyFile *keyfile,
39 const gchar *expected)
44 value = g_key_file_get_string (keyfile, group, key, &error);
45 check_no_error (&error);
46 g_assert (value != NULL);
47 g_assert_cmpstr (value, ==, expected);
52 check_locale_string_value (GKeyFile *keyfile,
56 const gchar *expected)
61 value = g_key_file_get_locale_string (keyfile, group, key, locale, &error);
62 check_no_error (&error);
63 g_assert (value != NULL);
64 g_assert_cmpstr (value, ==, expected);
69 check_string_list_value (GKeyFile *keyfile,
80 value = g_key_file_get_string_list (keyfile, group, key, &len, &error);
81 check_no_error (&error);
82 g_assert (value != NULL);
86 v = va_arg (args, gchar*);
89 g_assert (value[i] != NULL);
90 g_assert_cmpstr (v, ==, value[i]);
92 v = va_arg (args, gchar*);
101 check_integer_list_value (GKeyFile *keyfile,
110 GError *error = NULL;
112 value = g_key_file_get_integer_list (keyfile, group, key, &len, &error);
113 check_no_error (&error);
114 g_assert (value != NULL);
116 va_start (args, key);
118 v = va_arg (args, gint);
121 g_assert_cmpint (i, <, len);
122 g_assert_cmpint (value[i], ==, v);
124 v = va_arg (args, gint);
133 check_double_list_value (GKeyFile *keyfile,
142 GError *error = NULL;
144 value = g_key_file_get_double_list (keyfile, group, key, &len, &error);
145 check_no_error (&error);
146 g_assert (value != NULL);
148 va_start (args, key);
150 v = va_arg (args, gdouble);
153 g_assert_cmpint (i, <, len);
154 g_assert_cmpfloat (value[i], ==, v);
156 v = va_arg (args, gdouble);
165 check_boolean_list_value (GKeyFile *keyfile,
174 GError *error = NULL;
176 value = g_key_file_get_boolean_list (keyfile, group, key, &len, &error);
177 check_no_error (&error);
178 g_assert (value != NULL);
180 va_start (args, key);
182 v = va_arg (args, gboolean);
185 g_assert_cmpint (i, <, len);
186 g_assert_cmpint (value[i], ==, v);
188 v = va_arg (args, gboolean);
197 check_boolean_value (GKeyFile *keyfile,
202 GError *error = NULL;
205 value = g_key_file_get_boolean (keyfile, group, key, &error);
206 check_no_error (&error);
207 g_assert_cmpint (value, ==, expected);
211 check_integer_value (GKeyFile *keyfile,
216 GError *error = NULL;
219 value = g_key_file_get_integer (keyfile, group, key, &error);
220 check_no_error (&error);
221 g_assert_cmpint (value, ==, expected);
225 check_double_value (GKeyFile *keyfile,
230 GError *error = NULL;
233 value = g_key_file_get_double (keyfile, group, key, &error);
234 check_no_error (&error);
235 g_assert_cmpfloat (value, ==, expected);
239 check_name (const gchar *what,
241 const gchar *expected,
244 g_assert_cmpstr (value, ==, expected);
248 check_length (const gchar *what,
253 g_assert_cmpint (n_items, ==, length);
254 g_assert_cmpint (n_items, ==, expected);
258 /* check that both \n and \r\n are accepted as line ends,
259 * and that stray \r are passed through
262 test_line_ends (void)
274 keyfile = load_data (data, 0);
276 check_string_value (keyfile, "group1", "key1", "value1");
277 check_string_value (keyfile, "group1", "key2", "value2");
278 check_string_value (keyfile, "group2", "key3", "value3\r");
279 check_string_value (keyfile, "group2", "key4", "value4");
281 g_key_file_free (keyfile);
284 /* check handling of whitespace
287 test_whitespace (void)
300 keyfile = load_data (data, 0);
302 check_string_value (keyfile, "group1", "key1", "value1");
303 check_string_value (keyfile, "group1", "key2", "value2");
304 check_string_value (keyfile, " group2 ", "key3", "value3 ");
305 check_string_value (keyfile, " group2 ", "key4", "value \t4");
306 check_string_value (keyfile, " group2 ", "key5", "value5");
308 g_key_file_free (keyfile);
311 /* check handling of comments
319 GError *error = NULL;
324 "# top comment, continued\n"
328 "# key comment, continued\n"
330 "# line end check\r\n"
334 "# group comment, continued\n"
337 const gchar *top_comment= " top comment\n top comment, continued\n";
338 const gchar *group_comment= " group comment\n group comment, continued\n";
339 const gchar *key_comment= " key comment\n key comment, continued\n";
341 keyfile = load_data (data, 0);
343 check_string_value (keyfile, "group1", "key1", "value1");
344 check_string_value (keyfile, "group1", "key2", "value2");
345 check_string_value (keyfile, "group1", "key3", "value3");
346 check_string_value (keyfile, "group1", "key4", "value4");
348 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
349 check_no_error (&error);
351 check_length ("keys", g_strv_length (names), len, 4);
352 check_name ("key", names[0], "key1", 0);
353 check_name ("key", names[1], "key2", 1);
354 check_name ("key", names[2], "key3", 2);
355 check_name ("key", names[3], "key4", 3);
359 g_key_file_free (keyfile);
361 keyfile = load_data (data, G_KEY_FILE_KEEP_COMMENTS);
363 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
364 check_no_error (&error);
366 check_length ("keys", g_strv_length (names), len, 4);
367 check_name ("key", names[0], "key1", 0);
368 check_name ("key", names[1], "key2", 1);
369 check_name ("key", names[2], "key3", 2);
370 check_name ("key", names[3], "key4", 3);
374 comment = g_key_file_get_comment (keyfile, NULL, NULL, &error);
375 check_no_error (&error);
376 check_name ("top comment", comment, top_comment, 0);
379 comment = g_key_file_get_comment (keyfile, "group1", "key2", &error);
380 check_no_error (&error);
381 check_name ("key comment", comment, key_comment, 0);
384 comment = g_key_file_get_comment (keyfile, "group2", NULL, &error);
385 check_no_error (&error);
386 check_name ("group comment", comment, group_comment, 0);
389 comment = g_key_file_get_comment (keyfile, "group3", NULL, &error);
392 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
393 g_assert (comment == NULL);
395 g_key_file_free (keyfile);
399 /* check key and group listing */
407 GError *error = NULL;
417 keyfile = load_data (data, 0);
419 names = g_key_file_get_groups (keyfile, &len);
420 g_assert (names != NULL);
422 check_length ("groups", g_strv_length (names), len, 2);
423 check_name ("group name", names[0], "group1", 0);
424 check_name ("group name", names[1], "group2", 1);
428 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
429 check_no_error (&error);
431 check_length ("keys", g_strv_length (names), len, 2);
432 check_name ("key", names[0], "key1", 0);
433 check_name ("key", names[1], "key2", 1);
437 names = g_key_file_get_keys (keyfile, "no-such-group", &len, &error);
438 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
442 g_assert (g_key_file_has_group (keyfile, "group1"));
443 g_assert (g_key_file_has_group (keyfile, "group2"));
444 g_assert (!g_key_file_has_group (keyfile, "group10"));
445 g_assert (!g_key_file_has_group (keyfile, "group20"));
447 start = g_key_file_get_start_group (keyfile);
448 g_assert_cmpstr (start, ==, "group1");
451 g_assert (g_key_file_has_key (keyfile, "group1", "key1", &error));
452 check_no_error (&error);
453 g_assert (g_key_file_has_key (keyfile, "group2", "key3", &error));
454 check_no_error (&error);
455 g_assert (!g_key_file_has_key (keyfile, "group2", "no-such-key", NULL));
457 g_key_file_has_key (keyfile, "no-such-group", "key", &error);
458 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
460 g_key_file_free (keyfile);
463 /* check parsing of string values */
468 GError *error = NULL;
473 "key1=\\s\\n\\t\\r\\\\\n"
476 "key4=\xe2\x89\xa0\xe2\x89\xa0\n"
478 "key1=\\a\\b\\0800xff\n"
481 keyfile = load_data (data, 0);
483 check_string_value (keyfile, "valid", "key1", " \n\t\r\\");
484 check_string_value (keyfile, "valid", "key2", "\"quoted\"");
485 check_string_value (keyfile, "valid", "key3", "'quoted'");
486 check_string_value (keyfile, "valid", "key4", "\xe2\x89\xa0\xe2\x89\xa0");
488 value = g_key_file_get_string (keyfile, "invalid", "key1", &error);
489 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
492 value = g_key_file_get_string (keyfile, "invalid", "key2", &error);
493 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
496 g_key_file_free (keyfile);
499 /* check parsing of boolean values */
504 GError *error = NULL;
518 keyfile = load_data (data, 0);
520 check_boolean_value (keyfile, "valid", "key1", TRUE);
521 check_boolean_value (keyfile, "valid", "key2", FALSE);
522 check_boolean_value (keyfile, "valid", "key3", TRUE);
523 check_boolean_value (keyfile, "valid", "key4", FALSE);
525 g_key_file_get_boolean (keyfile, "invalid", "key1", &error);
526 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
528 g_key_file_get_boolean (keyfile, "invalid", "key2", &error);
529 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
531 g_key_file_get_boolean (keyfile, "invalid", "key3", &error);
532 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
534 g_key_file_get_boolean (keyfile, "invalid", "key4", &error);
535 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
537 g_key_file_free (keyfile);
540 /* check parsing of integer and double values */
545 GError *error = NULL;
568 keyfile = load_data (data, 0);
570 check_integer_value (keyfile, "valid", "key1", 0);
571 check_integer_value (keyfile, "valid", "key2", 1);
572 check_integer_value (keyfile, "valid", "key3", -1);
573 check_integer_value (keyfile, "valid", "key4", 2324431);
574 check_integer_value (keyfile, "valid", "key5", -2324431);
575 check_integer_value (keyfile, "valid", "key6", 111);
576 check_double_value (keyfile, "valid", "dkey1", 111.0);
577 check_double_value (keyfile, "valid", "dkey2", 145.45);
578 check_double_value (keyfile, "valid", "dkey3", -3453.7);
580 g_key_file_get_integer (keyfile, "invalid", "key1", &error);
581 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
583 g_key_file_get_integer (keyfile, "invalid", "key2", &error);
584 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
586 g_key_file_get_integer (keyfile, "invalid", "key3", &error);
587 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
589 g_key_file_get_integer (keyfile, "invalid", "key4", &error);
590 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
592 g_key_file_get_double (keyfile, "invalid", "key5", &error);
593 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
595 g_key_file_get_double (keyfile, "invalid", "key6", &error);
596 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
598 g_key_file_get_double (keyfile, "invalid", "key7", &error);
599 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
601 g_key_file_get_double (keyfile, "invalid", "key8", &error);
602 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
604 g_key_file_free (keyfile);
607 /* check handling of translated strings */
609 test_locale_string (void)
617 "key1[de_DE]=v1-de_DE\n"
618 "key1[de_DE.UTF8]=v1-de_DE.UTF8\n"
621 "key1[sr@Latn]=v1-sr\n";
623 keyfile = load_data (data, G_KEY_FILE_KEEP_TRANSLATIONS);
625 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
626 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
627 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de_DE");
628 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de_DE.UTF8");
629 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1-fr");
630 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1-fr");
631 check_locale_string_value (keyfile, "valid", "key1", "en", "v1-en");
632 check_locale_string_value (keyfile, "valid", "key1", "sr@Latn", "v1-sr");
634 g_key_file_free (keyfile);
636 /* now test that translations are thrown away */
638 g_setenv ("LANGUAGE", "de", TRUE);
639 setlocale (LC_ALL, "");
641 keyfile = load_data (data, 0);
643 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
644 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
645 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de");
646 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de");
647 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1");
648 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1");
649 check_locale_string_value (keyfile, "valid", "key1", "en", "v1");
651 g_key_file_free (keyfile);
667 "key7= 1 ; 0 ; -1 \n"
669 "key9=0;1.3456;-76532.456\n";
671 keyfile = load_data (data, 0);
673 check_string_list_value (keyfile, "valid", "key1", "v1", "v2", NULL);
674 check_string_list_value (keyfile, "valid", "key2", "v1", "v2", NULL);
675 check_string_list_value (keyfile, "valid", "key3", "v1,v2", NULL);
676 check_string_list_value (keyfile, "valid", "key4", "v1;v2", NULL);
677 check_boolean_list_value (keyfile, "valid", "key5", TRUE, FALSE, -100);
678 check_integer_list_value (keyfile, "valid", "key6", 1, 0, -1, -100);
679 check_double_list_value (keyfile, "valid", "key9", 0.0, 1.3456, -76532.456, -100.0);
680 /* maybe these should be valid */
681 /* check_integer_list_value (keyfile, "valid", "key7", 1, 0, -1, -100);*/
682 /* check_string_list_value (keyfile, "valid", "key8", "v1\\,v2", NULL);*/
684 g_key_file_free (keyfile);
686 /* Now check an alternate separator */
688 keyfile = load_data (data, 0);
689 g_key_file_set_list_separator (keyfile, ',');
691 check_string_list_value (keyfile, "valid", "key1", "v1;v2", NULL);
692 check_string_list_value (keyfile, "valid", "key2", "v1;v2;", NULL);
693 check_string_list_value (keyfile, "valid", "key3", "v1", "v2", NULL);
695 g_key_file_free (keyfile);
699 test_group_remove (void)
704 GError *error = NULL;
715 g_test_bug ("165887");
717 keyfile = load_data (data, 0);
719 names = g_key_file_get_groups (keyfile, &len);
720 g_assert (names != NULL);
722 check_length ("groups", g_strv_length (names), len, 3);
723 check_name ("group name", names[0], "group1", 0);
724 check_name ("group name", names[1], "group2", 1);
725 check_name ("group name", names[2], "group3", 2);
727 g_key_file_remove_group (keyfile, "group1", &error);
728 check_no_error (&error);
732 names = g_key_file_get_groups (keyfile, &len);
733 g_assert (names != NULL);
735 check_length ("groups", g_strv_length (names), len, 2);
736 check_name ("group name", names[0], "group2", 0);
737 check_name ("group name", names[1], "group3", 1);
739 g_key_file_remove_group (keyfile, "group2", &error);
740 check_no_error (&error);
744 names = g_key_file_get_groups (keyfile, &len);
745 g_assert (names != NULL);
747 check_length ("groups", g_strv_length (names), len, 1);
748 check_name ("group name", names[0], "group3", 0);
750 g_key_file_remove_group (keyfile, "no such group", &error);
751 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
755 g_key_file_free (keyfile);
759 test_key_remove (void)
763 GError *error = NULL;
770 g_test_bug ("165980");
772 keyfile = load_data (data, 0);
774 check_string_value (keyfile, "group1", "key1", "bla");
776 g_key_file_remove_key (keyfile, "group1", "key1", &error);
777 check_no_error (&error);
779 value = g_key_file_get_string (keyfile, "group1", "key1", &error);
780 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
783 g_key_file_remove_key (keyfile, "group1", "key1", &error);
784 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
786 g_key_file_remove_key (keyfile, "no such group", "key1", &error);
787 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
789 g_key_file_free (keyfile);
804 g_test_bug ("316309");
806 keyfile = load_data (data, 0);
808 check_string_value (keyfile, "1", "key1", "123");
809 check_string_value (keyfile, "2", "key2", "123");
811 g_key_file_free (keyfile);
815 test_group_names (void)
818 GError *error = NULL;
822 /* [ in group name */
825 keyfile = g_key_file_new ();
826 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
827 g_key_file_free (keyfile);
830 G_KEY_FILE_ERROR_PARSE);
832 /* ] in group name */
835 keyfile = g_key_file_new ();
836 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
837 g_key_file_free (keyfile);
840 G_KEY_FILE_ERROR_PARSE);
842 /* control char in group name */
845 keyfile = g_key_file_new ();
846 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
847 g_key_file_free (keyfile);
850 G_KEY_FILE_ERROR_PARSE);
852 /* empty group name */
855 keyfile = g_key_file_new ();
856 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
857 g_key_file_free (keyfile);
860 G_KEY_FILE_ERROR_PARSE);
862 /* Unicode in group name */
863 data = "[\xc2\xbd]\n"
865 keyfile = g_key_file_new ();
866 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
867 g_key_file_free (keyfile);
868 check_no_error (&error);
870 keyfile = g_key_file_new ();
871 /*g_key_file_set_string (keyfile, "a[b", "key1", "123");*/
872 value = g_key_file_get_string (keyfile, "a[b", "key1", &error);
875 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
876 g_key_file_free (keyfile);
878 keyfile = g_key_file_new ();
879 /*g_key_file_set_string (keyfile, "a]b", "key1", "123");*/
880 value = g_key_file_get_string (keyfile, "a]b", "key1", &error);
883 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
884 g_key_file_free (keyfile);
886 keyfile = g_key_file_new ();
887 /*g_key_file_set_string (keyfile, "a\tb", "key1", "123");*/
888 value = g_key_file_get_string (keyfile, "a\tb", "key1", &error);
891 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
892 g_key_file_free (keyfile);
894 keyfile = g_key_file_new ();
895 g_key_file_set_string (keyfile, "\xc2\xbd", "key1", "123");
896 check_string_value (keyfile, "\xc2\xbd", "key1", "123");
897 g_key_file_free (keyfile);
901 test_key_names (void)
904 GError *error = NULL;
911 keyfile = g_key_file_new ();
912 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
913 g_key_file_free (keyfile);
916 G_KEY_FILE_ERROR_PARSE);
921 keyfile = g_key_file_new ();
922 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
923 g_key_file_free (keyfile);
926 G_KEY_FILE_ERROR_PARSE);
931 keyfile = g_key_file_new ();
932 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
933 g_key_file_free (keyfile);
936 G_KEY_FILE_ERROR_PARSE);
938 /* bad locale suffix */
941 keyfile = g_key_file_new ();
942 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
943 g_key_file_free (keyfile);
946 G_KEY_FILE_ERROR_PARSE);
951 keyfile = g_key_file_new ();
952 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
953 check_no_error (&error);
954 check_string_value (keyfile, "a", "foo", "123");
955 g_key_file_free (keyfile);
960 keyfile = g_key_file_new ();
961 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
962 check_no_error (&error);
963 check_string_value (keyfile, "a", "foo", "123");
964 g_key_file_free (keyfile);
969 keyfile = g_key_file_new ();
970 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
971 check_no_error (&error);
972 check_string_value (keyfile, "a", "foo bar", "123");
973 g_key_file_free (keyfile);
978 keyfile = g_key_file_new ();
979 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
982 G_KEY_FILE_ERROR_PARSE);
983 g_key_file_free (keyfile);
985 /* control char in key name */
988 keyfile = g_key_file_new ();
989 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
990 g_key_file_free (keyfile);
991 check_no_error (&error);
993 /* Unicode in key name */
996 keyfile = g_key_file_new ();
997 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
998 g_key_file_free (keyfile);
999 check_no_error (&error);
1001 keyfile = g_key_file_new ();
1002 g_key_file_set_string (keyfile, "a", "x", "123");
1003 /*g_key_file_set_string (keyfile, "a", "key=", "123");*/
1004 value = g_key_file_get_string (keyfile, "a", "key=", &error);
1005 check_error (&error,
1007 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1008 g_key_file_free (keyfile);
1010 keyfile = g_key_file_new ();
1011 g_key_file_set_string (keyfile, "a", "x", "123");
1012 /*g_key_file_set_string (keyfile, "a", "key[", "123");*/
1013 value = g_key_file_get_string (keyfile, "a", "key[", &error);
1014 check_error (&error,
1016 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1017 g_key_file_free (keyfile);
1019 keyfile = g_key_file_new ();
1020 g_key_file_set_string (keyfile, "a", "x", "123");
1021 g_key_file_set_string (keyfile, "a", "key\tfoo", "123");
1022 value = g_key_file_get_string (keyfile, "a", "key\tfoo", &error);
1023 check_no_error (&error);
1024 g_key_file_free (keyfile);
1026 keyfile = g_key_file_new ();
1027 g_key_file_set_string (keyfile, "a", "x", "123");
1028 /*g_key_file_set_string (keyfile, "a", " key", "123");*/
1029 value = g_key_file_get_string (keyfile, "a", " key", &error);
1030 check_error (&error,
1032 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1033 g_key_file_free (keyfile);
1035 keyfile = g_key_file_new ();
1036 g_key_file_set_string (keyfile, "a", "x", "123");
1039 g_key_file_set_string (keyfile, "a", "\xc2\xbd", "123");
1040 check_string_value (keyfile, "a", "\xc2\xbd", "123");
1042 /* Keys with / + . (as used by the gnome-vfs mime cache) */
1043 g_key_file_set_string (keyfile, "a", "foo/bar", "/");
1044 check_string_value (keyfile, "a", "foo/bar", "/");
1045 g_key_file_set_string (keyfile, "a", "foo+bar", "+");
1046 check_string_value (keyfile, "a", "foo+bar", "+");
1047 g_key_file_set_string (keyfile, "a", "foo.bar", ".");
1048 check_string_value (keyfile, "a", "foo.bar", ".");
1050 g_key_file_free (keyfile);
1054 test_duplicate_keys (void)
1062 keyfile = load_data (data, 0);
1063 check_string_value (keyfile, "1", "key1", "345");
1065 g_key_file_free (keyfile);
1069 test_duplicate_groups (void)
1078 g_test_bug ("157877");
1080 keyfile = load_data (data, 0);
1081 check_string_value (keyfile, "Desktop Entry", "key1", "123");
1082 check_string_value (keyfile, "Desktop Entry", "key2", "123");
1084 g_key_file_free (keyfile);
1088 test_duplicate_groups2 (void)
1099 g_test_bug ("385910");
1101 keyfile = load_data (data, 0);
1102 check_string_value (keyfile, "A", "foo", "bang");
1103 check_string_value (keyfile, "B", "foo", "baz");
1105 g_key_file_free (keyfile);
1109 test_reload_idempotency (void)
1111 static const gchar *original_data=""
1117 "# A random comment in the first group\n"
1118 "anotherkey=anothervalue\n"
1119 "# Second comment - one line\n"
1121 "# Third comment - two lines\n"
1122 "# Third comment - two lines\n"
1131 GError *error = NULL;
1132 gchar *data1, *data2;
1135 g_test_bug ("420686");
1137 /* check that we only insert a single new line between groups */
1138 keyfile = g_key_file_new ();
1139 g_key_file_load_from_data (keyfile,
1140 original_data, strlen(original_data),
1141 G_KEY_FILE_KEEP_COMMENTS,
1143 check_no_error (&error);
1145 data1 = g_key_file_to_data (keyfile, &len1, &error);
1146 g_assert (data1 != NULL);
1147 g_key_file_free (keyfile);
1149 keyfile = g_key_file_new ();
1150 g_key_file_load_from_data (keyfile,
1152 G_KEY_FILE_KEEP_COMMENTS,
1154 check_no_error (&error);
1156 data2 = g_key_file_to_data (keyfile, &len2, &error);
1157 g_assert (data2 != NULL);
1158 g_key_file_free (keyfile);
1160 g_assert_cmpstr (data1, ==, data2);
1167 main (int argc, char *argv[])
1169 g_test_init (&argc, &argv, NULL);
1170 g_test_bug_base ("http://bugzilla.gnome.org/");
1172 g_test_add_func ("/keyfile/line-ends", test_line_ends);
1173 g_test_add_func ("/keyfile/whitespace", test_whitespace);
1174 g_test_add_func ("/keyfile/comments", test_comments);
1175 g_test_add_func ("/keyfile/listing", test_listing);
1176 g_test_add_func ("/keyfile/string", test_string);
1177 g_test_add_func ("/keyfile/boolean", test_boolean);
1178 g_test_add_func ("/keyfile/number", test_number);
1179 g_test_add_func ("/keyfile/locale-string", test_locale_string);
1180 g_test_add_func ("/keyfile/lists", test_lists);
1181 g_test_add_func ("/keyfile/group-remove", test_group_remove);
1182 g_test_add_func ("/keyfile/key-remove", test_key_remove);
1183 g_test_add_func ("/keyfile/groups", test_groups);
1184 g_test_add_func ("/keyfile/duplicate-keys", test_duplicate_keys);
1185 g_test_add_func ("/keyfile/duplicate-groups", test_duplicate_groups);
1186 g_test_add_func ("/keyfile/duplicate-groups2", test_duplicate_groups2);
1187 g_test_add_func ("/keyfile/group-names", test_group_names);
1188 g_test_add_func ("/keyfile/key-names", test_key_names);
1189 g_test_add_func ("/keyfile/reload", test_reload_idempotency);
1191 return g_test_run ();