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_locale_string_list_value (GKeyFile *keyfile,
111 GError *error = NULL;
113 value = g_key_file_get_locale_string_list (keyfile, group, key, locale, &len, &error);
114 check_no_error (&error);
115 g_assert (value != NULL);
117 va_start (args, locale);
119 v = va_arg (args, gchar*);
122 g_assert (value[i] != NULL);
123 g_assert_cmpstr (v, ==, value[i]);
125 v = va_arg (args, gchar*);
134 check_integer_list_value (GKeyFile *keyfile,
143 GError *error = NULL;
145 value = g_key_file_get_integer_list (keyfile, group, key, &len, &error);
146 check_no_error (&error);
147 g_assert (value != NULL);
149 va_start (args, key);
151 v = va_arg (args, gint);
154 g_assert_cmpint (i, <, len);
155 g_assert_cmpint (value[i], ==, v);
157 v = va_arg (args, gint);
166 check_double_list_value (GKeyFile *keyfile,
175 GError *error = NULL;
177 value = g_key_file_get_double_list (keyfile, group, key, &len, &error);
178 check_no_error (&error);
179 g_assert (value != NULL);
181 va_start (args, key);
183 v = va_arg (args, gdouble);
186 g_assert_cmpint (i, <, len);
187 g_assert_cmpfloat (value[i], ==, v);
189 v = va_arg (args, gdouble);
198 check_boolean_list_value (GKeyFile *keyfile,
207 GError *error = NULL;
209 value = g_key_file_get_boolean_list (keyfile, group, key, &len, &error);
210 check_no_error (&error);
211 g_assert (value != NULL);
213 va_start (args, key);
215 v = va_arg (args, gboolean);
218 g_assert_cmpint (i, <, len);
219 g_assert_cmpint (value[i], ==, v);
221 v = va_arg (args, gboolean);
230 check_boolean_value (GKeyFile *keyfile,
235 GError *error = NULL;
238 value = g_key_file_get_boolean (keyfile, group, key, &error);
239 check_no_error (&error);
240 g_assert_cmpint (value, ==, expected);
244 check_integer_value (GKeyFile *keyfile,
249 GError *error = NULL;
252 value = g_key_file_get_integer (keyfile, group, key, &error);
253 check_no_error (&error);
254 g_assert_cmpint (value, ==, expected);
258 check_double_value (GKeyFile *keyfile,
263 GError *error = NULL;
266 value = g_key_file_get_double (keyfile, group, key, &error);
267 check_no_error (&error);
268 g_assert_cmpfloat (value, ==, expected);
272 check_name (const gchar *what,
274 const gchar *expected,
277 g_assert_cmpstr (value, ==, expected);
281 check_length (const gchar *what,
286 g_assert_cmpint (n_items, ==, length);
287 g_assert_cmpint (n_items, ==, expected);
291 /* check that both \n and \r\n are accepted as line ends,
292 * and that stray \r are passed through
295 test_line_ends (void)
307 keyfile = load_data (data, 0);
309 check_string_value (keyfile, "group1", "key1", "value1");
310 check_string_value (keyfile, "group1", "key2", "value2");
311 check_string_value (keyfile, "group2", "key3", "value3\r");
312 check_string_value (keyfile, "group2", "key4", "value4");
314 g_key_file_free (keyfile);
317 /* check handling of whitespace
320 test_whitespace (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 ");
338 check_string_value (keyfile, " group2 ", "key4", "value \t4");
339 check_string_value (keyfile, " group2 ", "key5", "value5");
341 g_key_file_free (keyfile);
344 /* check handling of comments
352 GError *error = NULL;
357 "# top comment, continued\n"
361 "# key comment, continued\n"
363 "# line end check\r\n"
367 "# group comment, continued\n"
370 const gchar *top_comment= " top comment\n top comment, continued\n";
371 const gchar *group_comment= " group comment\n group comment, continued\n";
372 const gchar *key_comment= " key comment\n key comment, continued\n";
374 keyfile = load_data (data, 0);
376 check_string_value (keyfile, "group1", "key1", "value1");
377 check_string_value (keyfile, "group1", "key2", "value2");
378 check_string_value (keyfile, "group1", "key3", "value3");
379 check_string_value (keyfile, "group1", "key4", "value4");
381 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
382 check_no_error (&error);
384 check_length ("keys", g_strv_length (names), len, 4);
385 check_name ("key", names[0], "key1", 0);
386 check_name ("key", names[1], "key2", 1);
387 check_name ("key", names[2], "key3", 2);
388 check_name ("key", names[3], "key4", 3);
392 g_key_file_free (keyfile);
394 keyfile = load_data (data, G_KEY_FILE_KEEP_COMMENTS);
396 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
397 check_no_error (&error);
399 check_length ("keys", g_strv_length (names), len, 4);
400 check_name ("key", names[0], "key1", 0);
401 check_name ("key", names[1], "key2", 1);
402 check_name ("key", names[2], "key3", 2);
403 check_name ("key", names[3], "key4", 3);
407 comment = g_key_file_get_comment (keyfile, NULL, NULL, &error);
408 check_no_error (&error);
409 check_name ("top comment", comment, top_comment, 0);
412 comment = g_key_file_get_comment (keyfile, "group1", "key2", &error);
413 check_no_error (&error);
414 check_name ("key comment", comment, key_comment, 0);
417 g_key_file_remove_comment (keyfile, "group1", "key2", &error);
418 check_no_error (&error);
419 comment = g_key_file_get_comment (keyfile, "group1", "key2", &error);
420 check_no_error (&error);
421 g_assert (comment == NULL);
423 comment = g_key_file_get_comment (keyfile, "group2", NULL, &error);
424 check_no_error (&error);
425 check_name ("group comment", comment, group_comment, 0);
428 comment = g_key_file_get_comment (keyfile, "group3", NULL, &error);
431 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
432 g_assert (comment == NULL);
434 g_key_file_free (keyfile);
438 /* check key and group listing */
446 GError *error = NULL;
456 keyfile = load_data (data, 0);
458 names = g_key_file_get_groups (keyfile, &len);
459 g_assert (names != NULL);
461 check_length ("groups", g_strv_length (names), len, 2);
462 check_name ("group name", names[0], "group1", 0);
463 check_name ("group name", names[1], "group2", 1);
467 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
468 check_no_error (&error);
470 check_length ("keys", g_strv_length (names), len, 2);
471 check_name ("key", names[0], "key1", 0);
472 check_name ("key", names[1], "key2", 1);
476 names = g_key_file_get_keys (keyfile, "no-such-group", &len, &error);
477 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
481 g_assert (g_key_file_has_group (keyfile, "group1"));
482 g_assert (g_key_file_has_group (keyfile, "group2"));
483 g_assert (!g_key_file_has_group (keyfile, "group10"));
484 g_assert (!g_key_file_has_group (keyfile, "group20"));
486 start = g_key_file_get_start_group (keyfile);
487 g_assert_cmpstr (start, ==, "group1");
490 g_assert (g_key_file_has_key (keyfile, "group1", "key1", &error));
491 check_no_error (&error);
492 g_assert (g_key_file_has_key (keyfile, "group2", "key3", &error));
493 check_no_error (&error);
494 g_assert (!g_key_file_has_key (keyfile, "group2", "no-such-key", NULL));
496 g_key_file_has_key (keyfile, "no-such-group", "key", &error);
497 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
499 g_key_file_free (keyfile);
502 /* check parsing of string values */
507 GError *error = NULL;
512 "key1=\\s\\n\\t\\r\\\\\n"
515 "key4=\xe2\x89\xa0\xe2\x89\xa0\n"
517 "key1=\\a\\b\\0800xff\n"
520 keyfile = load_data (data, 0);
522 check_string_value (keyfile, "valid", "key1", " \n\t\r\\");
523 check_string_value (keyfile, "valid", "key2", "\"quoted\"");
524 check_string_value (keyfile, "valid", "key3", "'quoted'");
525 check_string_value (keyfile, "valid", "key4", "\xe2\x89\xa0\xe2\x89\xa0");
527 value = g_key_file_get_string (keyfile, "invalid", "key1", &error);
528 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
531 value = g_key_file_get_string (keyfile, "invalid", "key2", &error);
532 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
535 g_key_file_free (keyfile);
538 /* check parsing of boolean values */
543 GError *error = NULL;
557 keyfile = load_data (data, 0);
559 check_boolean_value (keyfile, "valid", "key1", TRUE);
560 check_boolean_value (keyfile, "valid", "key2", FALSE);
561 check_boolean_value (keyfile, "valid", "key3", TRUE);
562 check_boolean_value (keyfile, "valid", "key4", FALSE);
564 g_key_file_get_boolean (keyfile, "invalid", "key1", &error);
565 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
567 g_key_file_get_boolean (keyfile, "invalid", "key2", &error);
568 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
570 g_key_file_get_boolean (keyfile, "invalid", "key3", &error);
571 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
573 g_key_file_get_boolean (keyfile, "invalid", "key4", &error);
574 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
576 g_key_file_free (keyfile);
579 /* check parsing of integer and double values */
584 GError *error = NULL;
607 keyfile = load_data (data, 0);
609 check_integer_value (keyfile, "valid", "key1", 0);
610 check_integer_value (keyfile, "valid", "key2", 1);
611 check_integer_value (keyfile, "valid", "key3", -1);
612 check_integer_value (keyfile, "valid", "key4", 2324431);
613 check_integer_value (keyfile, "valid", "key5", -2324431);
614 check_integer_value (keyfile, "valid", "key6", 111);
615 check_double_value (keyfile, "valid", "dkey1", 111.0);
616 check_double_value (keyfile, "valid", "dkey2", 145.45);
617 check_double_value (keyfile, "valid", "dkey3", -3453.7);
619 g_key_file_get_integer (keyfile, "invalid", "key1", &error);
620 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
622 g_key_file_get_integer (keyfile, "invalid", "key2", &error);
623 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
625 g_key_file_get_integer (keyfile, "invalid", "key3", &error);
626 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
628 g_key_file_get_integer (keyfile, "invalid", "key4", &error);
629 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
631 g_key_file_get_double (keyfile, "invalid", "key5", &error);
632 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
634 g_key_file_get_double (keyfile, "invalid", "key6", &error);
635 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
637 g_key_file_get_double (keyfile, "invalid", "key7", &error);
638 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
640 g_key_file_get_double (keyfile, "invalid", "key8", &error);
641 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
643 g_key_file_free (keyfile);
646 /* check handling of translated strings */
648 test_locale_string (void)
656 "key1[de_DE]=v1-de_DE\n"
657 "key1[de_DE.UTF8]=v1-de_DE.UTF8\n"
660 "key1[sr@Latn]=v1-sr\n";
662 keyfile = load_data (data, G_KEY_FILE_KEEP_TRANSLATIONS);
664 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
665 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
666 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de_DE");
667 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de_DE.UTF8");
668 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1-fr");
669 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1-fr");
670 check_locale_string_value (keyfile, "valid", "key1", "en", "v1-en");
671 check_locale_string_value (keyfile, "valid", "key1", "sr@Latn", "v1-sr");
673 g_key_file_free (keyfile);
675 /* now test that translations are thrown away */
677 g_setenv ("LANGUAGE", "de", TRUE);
678 setlocale (LC_ALL, "");
680 keyfile = load_data (data, 0);
682 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
683 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
684 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de");
685 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de");
686 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1");
687 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1");
688 check_locale_string_value (keyfile, "valid", "key1", "en", "v1");
690 g_key_file_free (keyfile);
706 "key7= 1 ; 0 ; -1 \n"
708 "key9=0;1.3456;-76532.456\n";
710 keyfile = load_data (data, 0);
712 check_string_list_value (keyfile, "valid", "key1", "v1", "v2", NULL);
713 check_string_list_value (keyfile, "valid", "key2", "v1", "v2", NULL);
714 check_string_list_value (keyfile, "valid", "key3", "v1,v2", NULL);
715 check_string_list_value (keyfile, "valid", "key4", "v1;v2", NULL);
716 check_boolean_list_value (keyfile, "valid", "key5", TRUE, FALSE, -100);
717 check_integer_list_value (keyfile, "valid", "key6", 1, 0, -1, -100);
718 check_double_list_value (keyfile, "valid", "key9", 0.0, 1.3456, -76532.456, -100.0);
719 /* maybe these should be valid */
720 /* check_integer_list_value (keyfile, "valid", "key7", 1, 0, -1, -100);*/
721 /* check_string_list_value (keyfile, "valid", "key8", "v1\\,v2", NULL);*/
723 g_key_file_free (keyfile);
725 /* Now check an alternate separator */
727 keyfile = load_data (data, 0);
728 g_key_file_set_list_separator (keyfile, ',');
730 check_string_list_value (keyfile, "valid", "key1", "v1;v2", NULL);
731 check_string_list_value (keyfile, "valid", "key2", "v1;v2;", NULL);
732 check_string_list_value (keyfile, "valid", "key3", "v1", "v2", NULL);
734 g_key_file_free (keyfile);
738 test_lists_set_get (void)
741 static const char * const strings[] = { "v1", "v2" };
742 static const char * const locale_strings[] = { "v1-l", "v2-l" };
743 static int integers[] = { 1, -1, 2 };
744 static gdouble doubles[] = { 3.14, 2.71 };
746 keyfile = g_key_file_new ();
747 g_key_file_set_string_list (keyfile, "group0", "key1", strings, G_N_ELEMENTS (strings));
748 g_key_file_set_locale_string_list (keyfile, "group0", "key1", "de", locale_strings, G_N_ELEMENTS (locale_strings));
749 g_key_file_set_integer_list (keyfile, "group0", "key2", integers, G_N_ELEMENTS (integers));
750 g_key_file_set_double_list (keyfile, "group0", "key3", doubles, G_N_ELEMENTS (doubles));
752 check_string_list_value (keyfile, "group0", "key1", strings[0], strings[1], NULL);
753 check_locale_string_list_value (keyfile, "group0", "key1", "de", locale_strings[0], locale_strings[1], NULL);
754 check_integer_list_value (keyfile, "group0", "key2", integers[0], integers[1], -100);
755 check_double_list_value (keyfile, "group0", "key3", doubles[0], doubles[1], -100.0);
756 g_key_file_free (keyfile);
758 /* and again with a different list separator */
759 keyfile = g_key_file_new ();
760 g_key_file_set_list_separator (keyfile, ',');
761 g_key_file_set_string_list (keyfile, "group0", "key1", strings, G_N_ELEMENTS (strings));
762 g_key_file_set_locale_string_list (keyfile, "group0", "key1", "de", locale_strings, G_N_ELEMENTS (locale_strings));
763 g_key_file_set_integer_list (keyfile, "group0", "key2", integers, G_N_ELEMENTS (integers));
764 g_key_file_set_double_list (keyfile, "group0", "key3", doubles, G_N_ELEMENTS (doubles));
766 check_string_list_value (keyfile, "group0", "key1", strings[0], strings[1], NULL);
767 check_locale_string_list_value (keyfile, "group0", "key1", "de", locale_strings[0], locale_strings[1], NULL);
768 check_integer_list_value (keyfile, "group0", "key2", integers[0], integers[1], -100);
769 check_double_list_value (keyfile, "group0", "key3", doubles[0], doubles[1], -100.0);
770 g_key_file_free (keyfile);
774 test_group_remove (void)
779 GError *error = NULL;
790 g_test_bug ("165887");
792 keyfile = load_data (data, 0);
794 names = g_key_file_get_groups (keyfile, &len);
795 g_assert (names != NULL);
797 check_length ("groups", g_strv_length (names), len, 3);
798 check_name ("group name", names[0], "group1", 0);
799 check_name ("group name", names[1], "group2", 1);
800 check_name ("group name", names[2], "group3", 2);
802 g_key_file_remove_group (keyfile, "group1", &error);
803 check_no_error (&error);
807 names = g_key_file_get_groups (keyfile, &len);
808 g_assert (names != NULL);
810 check_length ("groups", g_strv_length (names), len, 2);
811 check_name ("group name", names[0], "group2", 0);
812 check_name ("group name", names[1], "group3", 1);
814 g_key_file_remove_group (keyfile, "group2", &error);
815 check_no_error (&error);
819 names = g_key_file_get_groups (keyfile, &len);
820 g_assert (names != NULL);
822 check_length ("groups", g_strv_length (names), len, 1);
823 check_name ("group name", names[0], "group3", 0);
825 g_key_file_remove_group (keyfile, "no such group", &error);
826 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
830 g_key_file_free (keyfile);
834 test_key_remove (void)
838 GError *error = NULL;
845 g_test_bug ("165980");
847 keyfile = load_data (data, 0);
849 check_string_value (keyfile, "group1", "key1", "bla");
851 g_key_file_remove_key (keyfile, "group1", "key1", &error);
852 check_no_error (&error);
854 value = g_key_file_get_string (keyfile, "group1", "key1", &error);
855 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
858 g_key_file_remove_key (keyfile, "group1", "key1", &error);
859 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
861 g_key_file_remove_key (keyfile, "no such group", "key1", &error);
862 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
864 g_key_file_free (keyfile);
879 g_test_bug ("316309");
881 keyfile = load_data (data, 0);
883 check_string_value (keyfile, "1", "key1", "123");
884 check_string_value (keyfile, "2", "key2", "123");
886 g_key_file_free (keyfile);
890 test_group_names (void)
893 GError *error = NULL;
897 /* [ in group name */
900 keyfile = g_key_file_new ();
901 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
902 g_key_file_free (keyfile);
905 G_KEY_FILE_ERROR_PARSE);
907 /* ] in group name */
910 keyfile = g_key_file_new ();
911 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
912 g_key_file_free (keyfile);
915 G_KEY_FILE_ERROR_PARSE);
917 /* control char in group name */
920 keyfile = g_key_file_new ();
921 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
922 g_key_file_free (keyfile);
925 G_KEY_FILE_ERROR_PARSE);
927 /* empty group name */
930 keyfile = g_key_file_new ();
931 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
932 g_key_file_free (keyfile);
935 G_KEY_FILE_ERROR_PARSE);
937 /* Unicode in group name */
938 data = "[\xc2\xbd]\n"
940 keyfile = g_key_file_new ();
941 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
942 g_key_file_free (keyfile);
943 check_no_error (&error);
945 keyfile = g_key_file_new ();
946 /*g_key_file_set_string (keyfile, "a[b", "key1", "123");*/
947 value = g_key_file_get_string (keyfile, "a[b", "key1", &error);
950 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
951 g_assert (value == NULL);
952 g_key_file_free (keyfile);
954 keyfile = g_key_file_new ();
955 /*g_key_file_set_string (keyfile, "a]b", "key1", "123");*/
956 value = g_key_file_get_string (keyfile, "a]b", "key1", &error);
959 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
960 g_assert (value == NULL);
961 g_key_file_free (keyfile);
963 keyfile = g_key_file_new ();
964 /*g_key_file_set_string (keyfile, "a\tb", "key1", "123");*/
965 value = g_key_file_get_string (keyfile, "a\tb", "key1", &error);
968 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
969 g_assert (value == NULL);
970 g_key_file_free (keyfile);
972 keyfile = g_key_file_new ();
973 g_key_file_set_string (keyfile, "\xc2\xbd", "key1", "123");
974 check_string_value (keyfile, "\xc2\xbd", "key1", "123");
975 g_key_file_free (keyfile);
979 test_key_names (void)
982 GError *error = NULL;
989 keyfile = g_key_file_new ();
990 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
991 g_key_file_free (keyfile);
994 G_KEY_FILE_ERROR_PARSE);
999 keyfile = g_key_file_new ();
1000 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1001 g_key_file_free (keyfile);
1002 check_error (&error,
1004 G_KEY_FILE_ERROR_PARSE);
1006 /* empty key name */
1009 keyfile = g_key_file_new ();
1010 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1011 g_key_file_free (keyfile);
1012 check_error (&error,
1014 G_KEY_FILE_ERROR_PARSE);
1016 /* bad locale suffix */
1019 keyfile = g_key_file_new ();
1020 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1021 g_key_file_free (keyfile);
1022 check_error (&error,
1024 G_KEY_FILE_ERROR_PARSE);
1029 keyfile = g_key_file_new ();
1030 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1031 check_no_error (&error);
1032 check_string_value (keyfile, "a", "foo", "123");
1033 g_key_file_free (keyfile);
1038 keyfile = g_key_file_new ();
1039 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1040 check_no_error (&error);
1041 check_string_value (keyfile, "a", "foo", "123");
1042 g_key_file_free (keyfile);
1047 keyfile = g_key_file_new ();
1048 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1049 check_no_error (&error);
1050 check_string_value (keyfile, "a", "foo bar", "123");
1051 g_key_file_free (keyfile);
1056 keyfile = g_key_file_new ();
1057 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1058 check_error (&error,
1060 G_KEY_FILE_ERROR_PARSE);
1061 g_key_file_free (keyfile);
1063 /* control char in key name */
1066 keyfile = g_key_file_new ();
1067 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1068 g_key_file_free (keyfile);
1069 check_no_error (&error);
1071 /* Unicode in key name */
1074 keyfile = g_key_file_new ();
1075 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1076 g_key_file_free (keyfile);
1077 check_no_error (&error);
1079 keyfile = g_key_file_new ();
1080 g_key_file_set_string (keyfile, "a", "x", "123");
1081 /*g_key_file_set_string (keyfile, "a", "key=", "123");*/
1082 value = g_key_file_get_string (keyfile, "a", "key=", &error);
1083 check_error (&error,
1085 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1086 g_key_file_free (keyfile);
1088 keyfile = g_key_file_new ();
1089 g_key_file_set_string (keyfile, "a", "x", "123");
1090 /*g_key_file_set_string (keyfile, "a", "key[", "123");*/
1091 value = g_key_file_get_string (keyfile, "a", "key[", &error);
1092 check_error (&error,
1094 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1095 g_key_file_free (keyfile);
1097 keyfile = g_key_file_new ();
1098 g_key_file_set_string (keyfile, "a", "x", "123");
1099 g_key_file_set_string (keyfile, "a", "key\tfoo", "123");
1100 value = g_key_file_get_string (keyfile, "a", "key\tfoo", &error);
1101 check_no_error (&error);
1103 g_key_file_free (keyfile);
1105 keyfile = g_key_file_new ();
1106 g_key_file_set_string (keyfile, "a", "x", "123");
1107 /*g_key_file_set_string (keyfile, "a", " key", "123");*/
1108 value = g_key_file_get_string (keyfile, "a", " key", &error);
1109 check_error (&error,
1111 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1112 g_key_file_free (keyfile);
1114 keyfile = g_key_file_new ();
1115 g_key_file_set_string (keyfile, "a", "x", "123");
1118 g_key_file_set_string (keyfile, "a", "\xc2\xbd", "123");
1119 check_string_value (keyfile, "a", "\xc2\xbd", "123");
1121 /* Keys with / + . (as used by the gnome-vfs mime cache) */
1122 g_key_file_set_string (keyfile, "a", "foo/bar", "/");
1123 check_string_value (keyfile, "a", "foo/bar", "/");
1124 g_key_file_set_string (keyfile, "a", "foo+bar", "+");
1125 check_string_value (keyfile, "a", "foo+bar", "+");
1126 g_key_file_set_string (keyfile, "a", "foo.bar", ".");
1127 check_string_value (keyfile, "a", "foo.bar", ".");
1129 g_key_file_free (keyfile);
1133 test_duplicate_keys (void)
1141 keyfile = load_data (data, 0);
1142 check_string_value (keyfile, "1", "key1", "345");
1144 g_key_file_free (keyfile);
1148 test_duplicate_groups (void)
1157 g_test_bug ("157877");
1159 keyfile = load_data (data, 0);
1160 check_string_value (keyfile, "Desktop Entry", "key1", "123");
1161 check_string_value (keyfile, "Desktop Entry", "key2", "123");
1163 g_key_file_free (keyfile);
1167 test_duplicate_groups2 (void)
1178 g_test_bug ("385910");
1180 keyfile = load_data (data, 0);
1181 check_string_value (keyfile, "A", "foo", "bang");
1182 check_string_value (keyfile, "B", "foo", "baz");
1184 g_key_file_free (keyfile);
1188 test_reload_idempotency (void)
1190 static const gchar *original_data=""
1196 "# A random comment in the first group\n"
1197 "anotherkey=anothervalue\n"
1198 "# Second comment - one line\n"
1200 "# Third comment - two lines\n"
1201 "# Third comment - two lines\n"
1210 GError *error = NULL;
1211 gchar *data1, *data2;
1214 g_test_bug ("420686");
1216 /* check that we only insert a single new line between groups */
1217 keyfile = g_key_file_new ();
1218 g_key_file_load_from_data (keyfile,
1219 original_data, strlen(original_data),
1220 G_KEY_FILE_KEEP_COMMENTS,
1222 check_no_error (&error);
1224 data1 = g_key_file_to_data (keyfile, &len1, &error);
1225 g_assert (data1 != NULL);
1226 g_key_file_free (keyfile);
1228 keyfile = g_key_file_new ();
1229 g_key_file_load_from_data (keyfile,
1231 G_KEY_FILE_KEEP_COMMENTS,
1233 check_no_error (&error);
1235 data2 = g_key_file_to_data (keyfile, &len2, &error);
1236 g_assert (data2 != NULL);
1237 g_key_file_free (keyfile);
1239 g_assert_cmpstr (data1, ==, data2);
1245 static const char int64_data[] =
1249 "c=123456789123456789\n"
1250 "d=-123456789123456789\n";
1261 g_test_bug ("614864");
1263 file = g_key_file_new ();
1265 ok = g_key_file_load_from_data (file, int64_data, strlen (int64_data),
1269 c = g_key_file_get_uint64 (file, "bees", "c", NULL);
1270 g_assert (c == G_GUINT64_CONSTANT (123456789123456789));
1272 d = g_key_file_get_int64 (file, "bees", "d", NULL);
1273 g_assert (d == G_GINT64_CONSTANT (-123456789123456789));
1275 g_key_file_set_uint64 (file, "bees", "c",
1276 G_GUINT64_CONSTANT (987654321987654321));
1277 value = g_key_file_get_value (file, "bees", "c", NULL);
1278 g_assert_cmpstr (value, ==, "987654321987654321");
1281 g_key_file_set_int64 (file, "bees", "d",
1282 G_GINT64_CONSTANT (-987654321987654321));
1283 value = g_key_file_get_value (file, "bees", "d", NULL);
1284 g_assert_cmpstr (value, ==, "-987654321987654321");
1287 g_key_file_free (file);
1295 gboolean bools[2] = { TRUE, FALSE };
1297 file = g_key_file_new ();
1299 g_assert (g_key_file_load_from_data_dirs (file, "keyfiletest.ini", NULL, 0, &error));
1300 g_assert_no_error (error);
1302 g_key_file_set_locale_string (file, "test", "key4", "de", "Vierter Schlüssel");
1303 g_key_file_set_boolean_list (file, "test", "key5", bools, 2);
1304 g_key_file_set_integer (file, "test", "key6", 22);
1305 g_key_file_set_double (file, "test", "key7", 2.5);
1306 g_key_file_set_comment (file, "test", "key7", "some float", NULL);
1307 g_key_file_set_comment (file, "test", NULL, "the test group", NULL);
1308 g_key_file_set_comment (file, NULL, NULL, "top comment", NULL);
1310 g_key_file_free (file);
1314 test_non_utf8 (void)
1317 static const char data[] =
1320 "b=a;b;\230\230\230;\n"
1327 file = g_key_file_new ();
1329 ok = g_key_file_load_from_data (file, data, strlen (data), 0, NULL);
1333 s = g_key_file_get_string (file, "group", "a", &error);
1334 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_UNKNOWN_ENCODING);
1335 g_assert (s == NULL);
1337 g_clear_error (&error);
1338 l = g_key_file_get_string_list (file, "group", "b", NULL, &error);
1339 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_UNKNOWN_ENCODING);
1340 g_assert (l == NULL);
1342 g_clear_error (&error);
1343 l = g_key_file_get_string_list (file, "group", "c", NULL, &error);
1344 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
1345 g_assert (l == NULL);
1347 g_clear_error (&error);
1349 g_key_file_free (file);
1357 test_page_boundary (void)
1363 #define GROUP "main_section"
1364 #define KEY_PREFIX "fill_abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvw_"
1365 #define FIRST_KEY 10
1369 g_test_bug ("640695");
1371 file = g_key_file_new ();
1374 g_key_file_load_from_file (file, SRCDIR "/pages.ini", G_KEY_FILE_NONE, &error);
1375 g_assert_no_error (error);
1377 for (i = FIRST_KEY; i <= LAST_KEY; i++)
1382 key = g_strdup_printf (KEY_PREFIX "%d", i);
1383 val = g_key_file_get_integer (file, GROUP, key, &error);
1385 g_assert_no_error (error);
1386 g_assert_cmpint (val, ==, VALUE);
1392 main (int argc, char *argv[])
1394 g_setenv ("XDG_DATA_HOME", SRCDIR, TRUE);
1396 g_test_init (&argc, &argv, NULL);
1397 g_test_bug_base ("http://bugzilla.gnome.org/");
1399 g_test_add_func ("/keyfile/line-ends", test_line_ends);
1400 g_test_add_func ("/keyfile/whitespace", test_whitespace);
1401 g_test_add_func ("/keyfile/comments", test_comments);
1402 g_test_add_func ("/keyfile/listing", test_listing);
1403 g_test_add_func ("/keyfile/string", test_string);
1404 g_test_add_func ("/keyfile/boolean", test_boolean);
1405 g_test_add_func ("/keyfile/number", test_number);
1406 g_test_add_func ("/keyfile/locale-string", test_locale_string);
1407 g_test_add_func ("/keyfile/lists", test_lists);
1408 g_test_add_func ("/keyfile/lists-set-get", test_lists_set_get);
1409 g_test_add_func ("/keyfile/group-remove", test_group_remove);
1410 g_test_add_func ("/keyfile/key-remove", test_key_remove);
1411 g_test_add_func ("/keyfile/groups", test_groups);
1412 g_test_add_func ("/keyfile/duplicate-keys", test_duplicate_keys);
1413 g_test_add_func ("/keyfile/duplicate-groups", test_duplicate_groups);
1414 g_test_add_func ("/keyfile/duplicate-groups2", test_duplicate_groups2);
1415 g_test_add_func ("/keyfile/group-names", test_group_names);
1416 g_test_add_func ("/keyfile/key-names", test_key_names);
1417 g_test_add_func ("/keyfile/reload", test_reload_idempotency);
1418 g_test_add_func ("/keyfile/int64", test_int64);
1419 g_test_add_func ("/keyfile/load", test_load);
1420 g_test_add_func ("/keyfile/non-utf8", test_non_utf8);
1421 g_test_add_func ("/keyfile/page-boundary", test_page_boundary);
1423 return g_test_run ();