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;
457 keyfile = load_data (data, 0);
459 names = g_key_file_get_groups (keyfile, &len);
460 g_assert (names != NULL);
462 check_length ("groups", g_strv_length (names), len, 2);
463 check_name ("group name", names[0], "group1", 0);
464 check_name ("group name", names[1], "group2", 1);
468 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
469 check_no_error (&error);
471 check_length ("keys", g_strv_length (names), len, 2);
472 check_name ("key", names[0], "key1", 0);
473 check_name ("key", names[1], "key2", 1);
477 names = g_key_file_get_keys (keyfile, "no-such-group", &len, &error);
478 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
482 g_assert (g_key_file_has_group (keyfile, "group1"));
483 g_assert (g_key_file_has_group (keyfile, "group2"));
484 g_assert (!g_key_file_has_group (keyfile, "group10"));
485 g_assert (!g_key_file_has_group (keyfile, "group20"));
487 start = g_key_file_get_start_group (keyfile);
488 g_assert_cmpstr (start, ==, "group1");
491 g_assert (g_key_file_has_key (keyfile, "group1", "key1", &error));
492 check_no_error (&error);
493 g_assert (g_key_file_has_key (keyfile, "group2", "key3", &error));
494 check_no_error (&error);
495 g_assert (!g_key_file_has_key (keyfile, "group2", "no-such-key", NULL));
497 g_key_file_has_key (keyfile, "no-such-group", "key", &error);
498 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
500 g_assert (g_key_file_has_key_full (keyfile, "group1", "key1", &has_key, &error));
501 check_no_error (&error);
504 g_assert (g_key_file_has_key_full (keyfile, "group2", "key3", &has_key, &error));
505 check_no_error (&error);
508 g_assert (g_key_file_has_key_full (keyfile, "group2", "no-such-key", &has_key, &error));
510 check_no_error (&error);
512 g_assert (!g_key_file_has_key_full (keyfile, "no-such-group", "key", &has_key, &error));
513 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
515 g_key_file_free (keyfile);
518 /* check parsing of string values */
523 GError *error = NULL;
528 "key1=\\s\\n\\t\\r\\\\\n"
531 "key4=\xe2\x89\xa0\xe2\x89\xa0\n"
533 "key1=\\a\\b\\0800xff\n"
536 keyfile = load_data (data, 0);
538 check_string_value (keyfile, "valid", "key1", " \n\t\r\\");
539 check_string_value (keyfile, "valid", "key2", "\"quoted\"");
540 check_string_value (keyfile, "valid", "key3", "'quoted'");
541 check_string_value (keyfile, "valid", "key4", "\xe2\x89\xa0\xe2\x89\xa0");
543 value = g_key_file_get_string (keyfile, "invalid", "key1", &error);
544 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
547 value = g_key_file_get_string (keyfile, "invalid", "key2", &error);
548 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
551 g_key_file_free (keyfile);
554 /* check parsing of boolean values */
559 GError *error = NULL;
573 keyfile = load_data (data, 0);
575 check_boolean_value (keyfile, "valid", "key1", TRUE);
576 check_boolean_value (keyfile, "valid", "key2", FALSE);
577 check_boolean_value (keyfile, "valid", "key3", TRUE);
578 check_boolean_value (keyfile, "valid", "key4", FALSE);
580 g_key_file_get_boolean (keyfile, "invalid", "key1", &error);
581 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
583 g_key_file_get_boolean (keyfile, "invalid", "key2", &error);
584 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
586 g_key_file_get_boolean (keyfile, "invalid", "key3", &error);
587 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
589 g_key_file_get_boolean (keyfile, "invalid", "key4", &error);
590 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
592 g_key_file_free (keyfile);
595 /* check parsing of integer and double values */
600 GError *error = NULL;
623 keyfile = load_data (data, 0);
625 check_integer_value (keyfile, "valid", "key1", 0);
626 check_integer_value (keyfile, "valid", "key2", 1);
627 check_integer_value (keyfile, "valid", "key3", -1);
628 check_integer_value (keyfile, "valid", "key4", 2324431);
629 check_integer_value (keyfile, "valid", "key5", -2324431);
630 check_integer_value (keyfile, "valid", "key6", 111);
631 check_double_value (keyfile, "valid", "dkey1", 111.0);
632 check_double_value (keyfile, "valid", "dkey2", 145.45);
633 check_double_value (keyfile, "valid", "dkey3", -3453.7);
635 g_key_file_get_integer (keyfile, "invalid", "key1", &error);
636 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
638 g_key_file_get_integer (keyfile, "invalid", "key2", &error);
639 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
641 g_key_file_get_integer (keyfile, "invalid", "key3", &error);
642 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
644 g_key_file_get_integer (keyfile, "invalid", "key4", &error);
645 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
647 g_key_file_get_double (keyfile, "invalid", "key5", &error);
648 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
650 g_key_file_get_double (keyfile, "invalid", "key6", &error);
651 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
653 g_key_file_get_double (keyfile, "invalid", "key7", &error);
654 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
656 g_key_file_get_double (keyfile, "invalid", "key8", &error);
657 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
659 g_key_file_free (keyfile);
662 /* check handling of translated strings */
664 test_locale_string (void)
672 "key1[de_DE]=v1-de_DE\n"
673 "key1[de_DE.UTF8]=v1-de_DE.UTF8\n"
676 "key1[sr@Latn]=v1-sr\n";
678 keyfile = load_data (data, G_KEY_FILE_KEEP_TRANSLATIONS);
680 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
681 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
682 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de_DE");
683 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de_DE.UTF8");
684 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1-fr");
685 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1-fr");
686 check_locale_string_value (keyfile, "valid", "key1", "en", "v1-en");
687 check_locale_string_value (keyfile, "valid", "key1", "sr@Latn", "v1-sr");
689 g_key_file_free (keyfile);
691 /* now test that translations are thrown away */
693 g_setenv ("LANGUAGE", "de", TRUE);
694 setlocale (LC_ALL, "");
696 keyfile = load_data (data, 0);
698 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
699 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
700 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de");
701 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de");
702 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1");
703 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1");
704 check_locale_string_value (keyfile, "valid", "key1", "en", "v1");
706 g_key_file_free (keyfile);
722 "key7= 1 ; 0 ; -1 \n"
724 "key9=0;1.3456;-76532.456\n";
726 keyfile = load_data (data, 0);
728 check_string_list_value (keyfile, "valid", "key1", "v1", "v2", NULL);
729 check_string_list_value (keyfile, "valid", "key2", "v1", "v2", NULL);
730 check_string_list_value (keyfile, "valid", "key3", "v1,v2", NULL);
731 check_string_list_value (keyfile, "valid", "key4", "v1;v2", NULL);
732 check_boolean_list_value (keyfile, "valid", "key5", TRUE, FALSE, -100);
733 check_integer_list_value (keyfile, "valid", "key6", 1, 0, -1, -100);
734 check_double_list_value (keyfile, "valid", "key9", 0.0, 1.3456, -76532.456, -100.0);
735 /* maybe these should be valid */
736 /* check_integer_list_value (keyfile, "valid", "key7", 1, 0, -1, -100);*/
737 /* check_string_list_value (keyfile, "valid", "key8", "v1\\,v2", NULL);*/
739 g_key_file_free (keyfile);
741 /* Now check an alternate separator */
743 keyfile = load_data (data, 0);
744 g_key_file_set_list_separator (keyfile, ',');
746 check_string_list_value (keyfile, "valid", "key1", "v1;v2", NULL);
747 check_string_list_value (keyfile, "valid", "key2", "v1;v2;", NULL);
748 check_string_list_value (keyfile, "valid", "key3", "v1", "v2", NULL);
750 g_key_file_free (keyfile);
754 test_lists_set_get (void)
757 static const char * const strings[] = { "v1", "v2" };
758 static const char * const locale_strings[] = { "v1-l", "v2-l" };
759 static int integers[] = { 1, -1, 2 };
760 static gdouble doubles[] = { 3.14, 2.71 };
762 keyfile = g_key_file_new ();
763 g_key_file_set_string_list (keyfile, "group0", "key1", strings, G_N_ELEMENTS (strings));
764 g_key_file_set_locale_string_list (keyfile, "group0", "key1", "de", locale_strings, G_N_ELEMENTS (locale_strings));
765 g_key_file_set_integer_list (keyfile, "group0", "key2", integers, G_N_ELEMENTS (integers));
766 g_key_file_set_double_list (keyfile, "group0", "key3", doubles, G_N_ELEMENTS (doubles));
768 check_string_list_value (keyfile, "group0", "key1", strings[0], strings[1], NULL);
769 check_locale_string_list_value (keyfile, "group0", "key1", "de", locale_strings[0], locale_strings[1], NULL);
770 check_integer_list_value (keyfile, "group0", "key2", integers[0], integers[1], -100);
771 check_double_list_value (keyfile, "group0", "key3", doubles[0], doubles[1], -100.0);
772 g_key_file_free (keyfile);
774 /* and again with a different list separator */
775 keyfile = g_key_file_new ();
776 g_key_file_set_list_separator (keyfile, ',');
777 g_key_file_set_string_list (keyfile, "group0", "key1", strings, G_N_ELEMENTS (strings));
778 g_key_file_set_locale_string_list (keyfile, "group0", "key1", "de", locale_strings, G_N_ELEMENTS (locale_strings));
779 g_key_file_set_integer_list (keyfile, "group0", "key2", integers, G_N_ELEMENTS (integers));
780 g_key_file_set_double_list (keyfile, "group0", "key3", doubles, G_N_ELEMENTS (doubles));
782 check_string_list_value (keyfile, "group0", "key1", strings[0], strings[1], NULL);
783 check_locale_string_list_value (keyfile, "group0", "key1", "de", locale_strings[0], locale_strings[1], NULL);
784 check_integer_list_value (keyfile, "group0", "key2", integers[0], integers[1], -100);
785 check_double_list_value (keyfile, "group0", "key3", doubles[0], doubles[1], -100.0);
786 g_key_file_free (keyfile);
790 test_group_remove (void)
795 GError *error = NULL;
806 g_test_bug ("165887");
808 keyfile = load_data (data, 0);
810 names = g_key_file_get_groups (keyfile, &len);
811 g_assert (names != NULL);
813 check_length ("groups", g_strv_length (names), len, 3);
814 check_name ("group name", names[0], "group1", 0);
815 check_name ("group name", names[1], "group2", 1);
816 check_name ("group name", names[2], "group3", 2);
818 g_key_file_remove_group (keyfile, "group1", &error);
819 check_no_error (&error);
823 names = g_key_file_get_groups (keyfile, &len);
824 g_assert (names != NULL);
826 check_length ("groups", g_strv_length (names), len, 2);
827 check_name ("group name", names[0], "group2", 0);
828 check_name ("group name", names[1], "group3", 1);
830 g_key_file_remove_group (keyfile, "group2", &error);
831 check_no_error (&error);
835 names = g_key_file_get_groups (keyfile, &len);
836 g_assert (names != NULL);
838 check_length ("groups", g_strv_length (names), len, 1);
839 check_name ("group name", names[0], "group3", 0);
841 g_key_file_remove_group (keyfile, "no such group", &error);
842 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
846 g_key_file_free (keyfile);
850 test_key_remove (void)
854 GError *error = NULL;
861 g_test_bug ("165980");
863 keyfile = load_data (data, 0);
865 check_string_value (keyfile, "group1", "key1", "bla");
867 g_key_file_remove_key (keyfile, "group1", "key1", &error);
868 check_no_error (&error);
870 value = g_key_file_get_string (keyfile, "group1", "key1", &error);
871 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
874 g_key_file_remove_key (keyfile, "group1", "key1", &error);
875 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
877 g_key_file_remove_key (keyfile, "no such group", "key1", &error);
878 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
880 g_key_file_free (keyfile);
895 g_test_bug ("316309");
897 keyfile = load_data (data, 0);
899 check_string_value (keyfile, "1", "key1", "123");
900 check_string_value (keyfile, "2", "key2", "123");
902 g_key_file_free (keyfile);
906 test_group_names (void)
909 GError *error = NULL;
913 /* [ in group name */
916 keyfile = g_key_file_new ();
917 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
918 g_key_file_free (keyfile);
921 G_KEY_FILE_ERROR_PARSE);
923 /* ] in group name */
926 keyfile = g_key_file_new ();
927 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
928 g_key_file_free (keyfile);
931 G_KEY_FILE_ERROR_PARSE);
933 /* control char in group name */
936 keyfile = g_key_file_new ();
937 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
938 g_key_file_free (keyfile);
941 G_KEY_FILE_ERROR_PARSE);
943 /* empty group name */
946 keyfile = g_key_file_new ();
947 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
948 g_key_file_free (keyfile);
951 G_KEY_FILE_ERROR_PARSE);
953 /* Unicode in group name */
954 data = "[\xc2\xbd]\n"
956 keyfile = g_key_file_new ();
957 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
958 g_key_file_free (keyfile);
959 check_no_error (&error);
961 keyfile = g_key_file_new ();
962 /*g_key_file_set_string (keyfile, "a[b", "key1", "123");*/
963 value = g_key_file_get_string (keyfile, "a[b", "key1", &error);
966 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
967 g_assert (value == NULL);
968 g_key_file_free (keyfile);
970 keyfile = g_key_file_new ();
971 /*g_key_file_set_string (keyfile, "a]b", "key1", "123");*/
972 value = g_key_file_get_string (keyfile, "a]b", "key1", &error);
975 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
976 g_assert (value == NULL);
977 g_key_file_free (keyfile);
979 keyfile = g_key_file_new ();
980 /*g_key_file_set_string (keyfile, "a\tb", "key1", "123");*/
981 value = g_key_file_get_string (keyfile, "a\tb", "key1", &error);
984 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
985 g_assert (value == NULL);
986 g_key_file_free (keyfile);
988 keyfile = g_key_file_new ();
989 g_key_file_set_string (keyfile, "\xc2\xbd", "key1", "123");
990 check_string_value (keyfile, "\xc2\xbd", "key1", "123");
991 g_key_file_free (keyfile);
995 test_key_names (void)
998 GError *error = NULL;
1005 keyfile = g_key_file_new ();
1006 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1007 g_key_file_free (keyfile);
1008 check_error (&error,
1010 G_KEY_FILE_ERROR_PARSE);
1012 /* empty key name */
1015 keyfile = g_key_file_new ();
1016 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1017 g_key_file_free (keyfile);
1018 check_error (&error,
1020 G_KEY_FILE_ERROR_PARSE);
1022 /* empty key name */
1025 keyfile = g_key_file_new ();
1026 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1027 g_key_file_free (keyfile);
1028 check_error (&error,
1030 G_KEY_FILE_ERROR_PARSE);
1032 /* bad locale suffix */
1035 keyfile = g_key_file_new ();
1036 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1037 g_key_file_free (keyfile);
1038 check_error (&error,
1040 G_KEY_FILE_ERROR_PARSE);
1045 keyfile = g_key_file_new ();
1046 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1047 check_no_error (&error);
1048 check_string_value (keyfile, "a", "foo", "123");
1049 g_key_file_free (keyfile);
1054 keyfile = g_key_file_new ();
1055 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1056 check_no_error (&error);
1057 check_string_value (keyfile, "a", "foo", "123");
1058 g_key_file_free (keyfile);
1063 keyfile = g_key_file_new ();
1064 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1065 check_no_error (&error);
1066 check_string_value (keyfile, "a", "foo bar", "123");
1067 g_key_file_free (keyfile);
1072 keyfile = g_key_file_new ();
1073 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1074 check_error (&error,
1076 G_KEY_FILE_ERROR_PARSE);
1077 g_key_file_free (keyfile);
1079 /* control char in key name */
1082 keyfile = g_key_file_new ();
1083 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1084 g_key_file_free (keyfile);
1085 check_no_error (&error);
1087 /* Unicode in key name */
1090 keyfile = g_key_file_new ();
1091 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1092 g_key_file_free (keyfile);
1093 check_no_error (&error);
1095 keyfile = g_key_file_new ();
1096 g_key_file_set_string (keyfile, "a", "x", "123");
1097 /*g_key_file_set_string (keyfile, "a", "key=", "123");*/
1098 value = g_key_file_get_string (keyfile, "a", "key=", &error);
1099 check_error (&error,
1101 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1102 g_key_file_free (keyfile);
1104 keyfile = g_key_file_new ();
1105 g_key_file_set_string (keyfile, "a", "x", "123");
1106 /*g_key_file_set_string (keyfile, "a", "key[", "123");*/
1107 value = g_key_file_get_string (keyfile, "a", "key[", &error);
1108 check_error (&error,
1110 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1111 g_key_file_free (keyfile);
1113 keyfile = g_key_file_new ();
1114 g_key_file_set_string (keyfile, "a", "x", "123");
1115 g_key_file_set_string (keyfile, "a", "key\tfoo", "123");
1116 value = g_key_file_get_string (keyfile, "a", "key\tfoo", &error);
1117 check_no_error (&error);
1119 g_key_file_free (keyfile);
1121 keyfile = g_key_file_new ();
1122 g_key_file_set_string (keyfile, "a", "x", "123");
1123 /*g_key_file_set_string (keyfile, "a", " key", "123");*/
1124 value = g_key_file_get_string (keyfile, "a", " key", &error);
1125 check_error (&error,
1127 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1128 g_key_file_free (keyfile);
1130 keyfile = g_key_file_new ();
1131 g_key_file_set_string (keyfile, "a", "x", "123");
1134 g_key_file_set_string (keyfile, "a", "\xc2\xbd", "123");
1135 check_string_value (keyfile, "a", "\xc2\xbd", "123");
1137 /* Keys with / + . (as used by the gnome-vfs mime cache) */
1138 g_key_file_set_string (keyfile, "a", "foo/bar", "/");
1139 check_string_value (keyfile, "a", "foo/bar", "/");
1140 g_key_file_set_string (keyfile, "a", "foo+bar", "+");
1141 check_string_value (keyfile, "a", "foo+bar", "+");
1142 g_key_file_set_string (keyfile, "a", "foo.bar", ".");
1143 check_string_value (keyfile, "a", "foo.bar", ".");
1145 g_key_file_free (keyfile);
1149 test_duplicate_keys (void)
1157 keyfile = load_data (data, 0);
1158 check_string_value (keyfile, "1", "key1", "345");
1160 g_key_file_free (keyfile);
1164 test_duplicate_groups (void)
1173 g_test_bug ("157877");
1175 keyfile = load_data (data, 0);
1176 check_string_value (keyfile, "Desktop Entry", "key1", "123");
1177 check_string_value (keyfile, "Desktop Entry", "key2", "123");
1179 g_key_file_free (keyfile);
1183 test_duplicate_groups2 (void)
1194 g_test_bug ("385910");
1196 keyfile = load_data (data, 0);
1197 check_string_value (keyfile, "A", "foo", "bang");
1198 check_string_value (keyfile, "B", "foo", "baz");
1200 g_key_file_free (keyfile);
1204 test_reload_idempotency (void)
1206 static const gchar *original_data=""
1212 "# A random comment in the first group\n"
1213 "anotherkey=anothervalue\n"
1214 "# Second comment - one line\n"
1216 "# Third comment - two lines\n"
1217 "# Third comment - two lines\n"
1226 GError *error = NULL;
1227 gchar *data1, *data2;
1230 g_test_bug ("420686");
1232 /* check that we only insert a single new line between groups */
1233 keyfile = g_key_file_new ();
1234 g_key_file_load_from_data (keyfile,
1235 original_data, strlen(original_data),
1236 G_KEY_FILE_KEEP_COMMENTS,
1238 check_no_error (&error);
1240 data1 = g_key_file_to_data (keyfile, &len1, &error);
1241 g_assert (data1 != NULL);
1242 g_key_file_free (keyfile);
1244 keyfile = g_key_file_new ();
1245 g_key_file_load_from_data (keyfile,
1247 G_KEY_FILE_KEEP_COMMENTS,
1249 check_no_error (&error);
1251 data2 = g_key_file_to_data (keyfile, &len2, &error);
1252 g_assert (data2 != NULL);
1253 g_key_file_free (keyfile);
1255 g_assert_cmpstr (data1, ==, data2);
1261 static const char int64_data[] =
1265 "c=123456789123456789\n"
1266 "d=-123456789123456789\n";
1277 g_test_bug ("614864");
1279 file = g_key_file_new ();
1281 ok = g_key_file_load_from_data (file, int64_data, strlen (int64_data),
1285 c = g_key_file_get_uint64 (file, "bees", "c", NULL);
1286 g_assert (c == G_GUINT64_CONSTANT (123456789123456789));
1288 d = g_key_file_get_int64 (file, "bees", "d", NULL);
1289 g_assert (d == G_GINT64_CONSTANT (-123456789123456789));
1291 g_key_file_set_uint64 (file, "bees", "c",
1292 G_GUINT64_CONSTANT (987654321987654321));
1293 value = g_key_file_get_value (file, "bees", "c", NULL);
1294 g_assert_cmpstr (value, ==, "987654321987654321");
1297 g_key_file_set_int64 (file, "bees", "d",
1298 G_GINT64_CONSTANT (-987654321987654321));
1299 value = g_key_file_get_value (file, "bees", "d", NULL);
1300 g_assert_cmpstr (value, ==, "-987654321987654321");
1303 g_key_file_free (file);
1311 gboolean bools[2] = { TRUE, FALSE };
1313 file = g_key_file_new ();
1315 g_assert (g_key_file_load_from_data_dirs (file, "keyfiletest.ini", NULL, 0, &error));
1316 g_assert_no_error (error);
1318 g_key_file_set_locale_string (file, "test", "key4", "de", "Vierter Schlüssel");
1319 g_key_file_set_boolean_list (file, "test", "key5", bools, 2);
1320 g_key_file_set_integer (file, "test", "key6", 22);
1321 g_key_file_set_double (file, "test", "key7", 2.5);
1322 g_key_file_set_comment (file, "test", "key7", "some float", NULL);
1323 g_key_file_set_comment (file, "test", NULL, "the test group", NULL);
1324 g_key_file_set_comment (file, NULL, NULL, "top comment", NULL);
1326 g_key_file_free (file);
1330 test_non_utf8 (void)
1333 static const char data[] =
1336 "b=a;b;\230\230\230;\n"
1343 file = g_key_file_new ();
1345 ok = g_key_file_load_from_data (file, data, strlen (data), 0, NULL);
1349 s = g_key_file_get_string (file, "group", "a", &error);
1350 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_UNKNOWN_ENCODING);
1351 g_assert (s == NULL);
1353 g_clear_error (&error);
1354 l = g_key_file_get_string_list (file, "group", "b", NULL, &error);
1355 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_UNKNOWN_ENCODING);
1356 g_assert (l == NULL);
1358 g_clear_error (&error);
1359 l = g_key_file_get_string_list (file, "group", "c", NULL, &error);
1360 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
1361 g_assert (l == NULL);
1363 g_clear_error (&error);
1365 g_key_file_free (file);
1373 test_page_boundary (void)
1379 #define GROUP "main_section"
1380 #define KEY_PREFIX "fill_abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvw_"
1381 #define FIRST_KEY 10
1385 g_test_bug ("640695");
1387 file = g_key_file_new ();
1390 g_key_file_load_from_file (file, SRCDIR "/pages.ini", G_KEY_FILE_NONE, &error);
1391 g_assert_no_error (error);
1393 for (i = FIRST_KEY; i <= LAST_KEY; i++)
1398 key = g_strdup_printf (KEY_PREFIX "%d", i);
1399 val = g_key_file_get_integer (file, GROUP, key, &error);
1401 g_assert_no_error (error);
1402 g_assert_cmpint (val, ==, VALUE);
1408 main (int argc, char *argv[])
1410 g_setenv ("XDG_DATA_HOME", SRCDIR, TRUE);
1412 g_test_init (&argc, &argv, NULL);
1413 g_test_bug_base ("http://bugzilla.gnome.org/");
1415 g_test_add_func ("/keyfile/line-ends", test_line_ends);
1416 g_test_add_func ("/keyfile/whitespace", test_whitespace);
1417 g_test_add_func ("/keyfile/comments", test_comments);
1418 g_test_add_func ("/keyfile/listing", test_listing);
1419 g_test_add_func ("/keyfile/string", test_string);
1420 g_test_add_func ("/keyfile/boolean", test_boolean);
1421 g_test_add_func ("/keyfile/number", test_number);
1422 g_test_add_func ("/keyfile/locale-string", test_locale_string);
1423 g_test_add_func ("/keyfile/lists", test_lists);
1424 g_test_add_func ("/keyfile/lists-set-get", test_lists_set_get);
1425 g_test_add_func ("/keyfile/group-remove", test_group_remove);
1426 g_test_add_func ("/keyfile/key-remove", test_key_remove);
1427 g_test_add_func ("/keyfile/groups", test_groups);
1428 g_test_add_func ("/keyfile/duplicate-keys", test_duplicate_keys);
1429 g_test_add_func ("/keyfile/duplicate-groups", test_duplicate_groups);
1430 g_test_add_func ("/keyfile/duplicate-groups2", test_duplicate_groups2);
1431 g_test_add_func ("/keyfile/group-names", test_group_names);
1432 g_test_add_func ("/keyfile/key-names", test_key_names);
1433 g_test_add_func ("/keyfile/reload", test_reload_idempotency);
1434 g_test_add_func ("/keyfile/int64", test_int64);
1435 g_test_add_func ("/keyfile/load", test_load);
1436 g_test_add_func ("/keyfile/non-utf8", test_non_utf8);
1437 g_test_add_func ("/keyfile/page-boundary", test_page_boundary);
1439 return g_test_run ();