3 #include <glib/gstdio.h>
9 load_data (const gchar *data,
15 keyfile = g_key_file_new ();
16 g_key_file_load_from_data (keyfile, data, -1, flags, &error);
17 g_assert_no_error (error);
22 check_error (GError **error,
26 g_assert_error (*error, domain, code);
27 g_error_free (*error);
32 check_no_error (GError **error)
34 g_assert_no_error (*error);
38 check_string_value (GKeyFile *keyfile,
41 const gchar *expected)
46 value = g_key_file_get_string (keyfile, group, key, &error);
47 check_no_error (&error);
48 g_assert_nonnull (value);
49 g_assert_cmpstr (value, ==, expected);
54 check_locale_string_value (GKeyFile *keyfile,
58 const gchar *expected)
63 value = g_key_file_get_locale_string (keyfile, group, key, locale, &error);
64 check_no_error (&error);
65 g_assert_nonnull (value);
66 g_assert_cmpstr (value, ==, expected);
71 check_string_locale_value (GKeyFile *keyfile,
75 const gchar *expected)
79 value = g_key_file_get_locale_for_key (keyfile, group, key, locale);
80 g_assert_cmpstr (value, ==, expected);
85 check_string_list_value (GKeyFile *keyfile,
96 value = g_key_file_get_string_list (keyfile, group, key, &len, &error);
97 check_no_error (&error);
98 g_assert_nonnull (value);
100 va_start (args, key);
102 v = va_arg (args, gchar*);
105 g_assert_nonnull (value[i]);
106 g_assert_cmpstr (v, ==, value[i]);
108 v = va_arg (args, gchar*);
117 check_locale_string_list_value (GKeyFile *keyfile,
127 GError *error = NULL;
129 value = g_key_file_get_locale_string_list (keyfile, group, key, locale, &len, &error);
130 check_no_error (&error);
131 g_assert_nonnull (value);
133 va_start (args, locale);
135 v = va_arg (args, gchar*);
138 g_assert_nonnull (value[i]);
139 g_assert_cmpstr (v, ==, value[i]);
141 v = va_arg (args, gchar*);
150 check_integer_list_value (GKeyFile *keyfile,
159 GError *error = NULL;
161 value = g_key_file_get_integer_list (keyfile, group, key, &len, &error);
162 check_no_error (&error);
163 g_assert_nonnull (value);
165 va_start (args, key);
167 v = va_arg (args, gint);
170 g_assert_cmpint (i, <, len);
171 g_assert_cmpint (value[i], ==, v);
173 v = va_arg (args, gint);
182 check_double_list_value (GKeyFile *keyfile,
191 GError *error = NULL;
193 value = g_key_file_get_double_list (keyfile, group, key, &len, &error);
194 check_no_error (&error);
195 g_assert_nonnull (value);
197 va_start (args, key);
199 v = va_arg (args, gdouble);
202 g_assert_cmpint (i, <, len);
203 g_assert_cmpfloat (value[i], ==, v);
205 v = va_arg (args, gdouble);
214 check_boolean_list_value (GKeyFile *keyfile,
223 GError *error = NULL;
225 value = g_key_file_get_boolean_list (keyfile, group, key, &len, &error);
226 check_no_error (&error);
227 g_assert_nonnull (value);
229 va_start (args, key);
231 v = va_arg (args, gboolean);
234 g_assert_cmpint (i, <, len);
235 g_assert_cmpint (value[i], ==, v);
237 v = va_arg (args, gboolean);
246 check_boolean_value (GKeyFile *keyfile,
251 GError *error = NULL;
254 value = g_key_file_get_boolean (keyfile, group, key, &error);
255 check_no_error (&error);
256 g_assert_cmpint (value, ==, expected);
260 check_integer_value (GKeyFile *keyfile,
265 GError *error = NULL;
268 value = g_key_file_get_integer (keyfile, group, key, &error);
269 check_no_error (&error);
270 g_assert_cmpint (value, ==, expected);
274 check_double_value (GKeyFile *keyfile,
279 GError *error = NULL;
282 value = g_key_file_get_double (keyfile, group, key, &error);
283 check_no_error (&error);
284 g_assert_cmpfloat (value, ==, expected);
288 check_name (const gchar *what,
290 const gchar *expected,
293 g_assert_cmpstr (value, ==, expected);
297 check_length (const gchar *what,
302 g_assert_cmpint (n_items, ==, length);
303 g_assert_cmpint (n_items, ==, expected);
307 /* check that both \n and \r\n are accepted as line ends,
308 * and that stray \r are passed through
311 test_line_ends (void)
323 keyfile = load_data (data, 0);
325 check_string_value (keyfile, "group1", "key1", "value1");
326 check_string_value (keyfile, "group1", "key2", "value2");
327 check_string_value (keyfile, "group2", "key3", "value3\r");
328 check_string_value (keyfile, "group2", "key4", "value4");
330 g_key_file_free (keyfile);
333 /* check handling of whitespace
336 test_whitespace (void)
349 keyfile = load_data (data, 0);
351 check_string_value (keyfile, "group1", "key1", "value1");
352 check_string_value (keyfile, "group1", "key2", "value2");
353 check_string_value (keyfile, " group2 ", "key3", "value3 ");
354 check_string_value (keyfile, " group2 ", "key4", "value \t4");
355 check_string_value (keyfile, " group2 ", "key5", "value5");
357 g_key_file_free (keyfile);
360 /* check handling of comments
368 GError *error = NULL;
373 "# top comment, continued\n"
377 "# key comment, continued\n"
379 "# line end check\r\n"
381 "# single line comment\n"
384 "# group comment, continued\n"
389 const gchar *top_comment = " top comment\n top comment, continued";
390 const gchar *group_comment = " group comment\n group comment, continued";
391 const gchar *key_comment = " key comment\n key comment, continued";
392 const gchar *key4_comment = " single line comment";
394 keyfile = load_data (data, 0);
396 check_string_value (keyfile, "group1", "key1", "value1");
397 check_string_value (keyfile, "group1", "key2", "value2");
398 check_string_value (keyfile, "group1", "key3", "value3");
399 check_string_value (keyfile, "group1", "key4", "value4");
401 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
402 check_no_error (&error);
404 check_length ("keys", g_strv_length (names), len, 4);
405 check_name ("key", names[0], "key1", 0);
406 check_name ("key", names[1], "key2", 1);
407 check_name ("key", names[2], "key3", 2);
408 check_name ("key", names[3], "key4", 3);
412 g_key_file_free (keyfile);
414 keyfile = load_data (data, G_KEY_FILE_KEEP_COMMENTS);
416 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
417 check_no_error (&error);
419 check_length ("keys", g_strv_length (names), len, 4);
420 check_name ("key", names[0], "key1", 0);
421 check_name ("key", names[1], "key2", 1);
422 check_name ("key", names[2], "key3", 2);
423 check_name ("key", names[3], "key4", 3);
427 comment = g_key_file_get_comment (keyfile, NULL, NULL, &error);
428 check_no_error (&error);
429 check_name ("top comment", comment, top_comment, 0);
432 g_key_file_remove_comment (keyfile, NULL, NULL, &error);
433 check_no_error (&error);
434 comment = g_key_file_get_comment (keyfile, NULL, NULL, &error);
435 check_no_error (&error);
436 g_assert_null (comment);
438 comment = g_key_file_get_comment (keyfile, "group1", "key2", &error);
439 check_no_error (&error);
440 check_name ("key comment", comment, key_comment, 0);
443 g_key_file_remove_comment (keyfile, "group1", "key2", &error);
444 check_no_error (&error);
445 comment = g_key_file_get_comment (keyfile, "group1", "key2", &error);
446 check_no_error (&error);
447 g_assert_null (comment);
449 comment = g_key_file_get_comment (keyfile, "group1", "key4", &error);
450 check_no_error (&error);
451 check_name ("key comment", comment, key4_comment, 0);
454 comment = g_key_file_get_comment (keyfile, "group2", NULL, &error);
455 check_no_error (&error);
456 check_name ("group comment", comment, group_comment, 0);
459 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/3047");
461 /* check if adding a key to group N preserve group comment of group N+1 */
462 g_key_file_set_string (keyfile, "group1", "key5", "value5");
463 comment = g_key_file_get_comment (keyfile, "group2", NULL, &error);
464 check_no_error (&error);
465 check_name ("group comment", comment, group_comment, 0);
468 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/104");
470 /* check if comments above another group than the first one are properly removed */
471 g_key_file_remove_comment (keyfile, "group2", NULL, &error);
472 check_no_error (&error);
473 comment = g_key_file_get_comment (keyfile, "group2", NULL, &error);
474 check_no_error (&error);
475 g_assert_null (comment);
477 comment = g_key_file_get_comment (keyfile, "group3", NULL, &error);
478 check_no_error (&error);
479 check_name ("group comment", comment, "", 0);
482 comment = g_key_file_get_comment (keyfile, "group4", NULL, &error);
483 check_no_error (&error);
484 g_assert_null (comment);
486 comment = g_key_file_get_comment (keyfile, "group5", NULL, &error);
489 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
490 g_assert_null (comment);
492 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/3047");
494 /* check if we don't add a blank line above new group if last value of preceding
495 * group was added via g_key_file_set_value() and contains line breaks */
496 g_key_file_set_value (keyfile, "group4", "key1", "value1\n\n# group comment");
497 g_key_file_set_string (keyfile, "group5", "key1", "value1");
498 comment = g_key_file_get_comment (keyfile, "group5", NULL, &error);
499 check_no_error (&error);
500 g_assert_null (comment);
502 g_key_file_free (keyfile);
506 /* check key and group listing */
514 GError *error = NULL;
524 keyfile = load_data (data, 0);
526 names = g_key_file_get_groups (keyfile, &len);
527 g_assert_nonnull (names);
529 check_length ("groups", g_strv_length (names), len, 2);
530 check_name ("group name", names[0], "group1", 0);
531 check_name ("group name", names[1], "group2", 1);
535 names = g_key_file_get_keys (keyfile, "group1", &len, &error);
536 check_no_error (&error);
538 check_length ("keys", g_strv_length (names), len, 2);
539 check_name ("key", names[0], "key1", 0);
540 check_name ("key", names[1], "key2", 1);
544 names = g_key_file_get_keys (keyfile, "no-such-group", &len, &error);
545 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
549 g_assert_true (g_key_file_has_group (keyfile, "group1"));
550 g_assert_true (g_key_file_has_group (keyfile, "group2"));
551 g_assert_false (g_key_file_has_group (keyfile, "group10"));
552 g_assert_false (g_key_file_has_group (keyfile, "group20"));
554 start = g_key_file_get_start_group (keyfile);
555 g_assert_cmpstr (start, ==, "group1");
558 g_assert_true (g_key_file_has_key (keyfile, "group1", "key1", &error));
559 check_no_error (&error);
560 g_assert_true (g_key_file_has_key (keyfile, "group2", "key3", &error));
561 check_no_error (&error);
562 g_assert_false (g_key_file_has_key (keyfile, "group2", "no-such-key", NULL));
564 g_key_file_has_key (keyfile, "no-such-group", "key", &error);
565 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
567 g_key_file_free (keyfile);
570 /* check parsing of string values */
575 GError *error = NULL;
577 const gchar * const list[3] = {
584 "key1=\\s\\n\\t\\r\\\\\n"
587 "key4=\xe2\x89\xa0\xe2\x89\xa0\n"
588 "key5= leading space\n"
589 "key6=trailing space \n"
591 "key1=\\a\\b\\0800xff\n"
592 "key2=blabla\\\n" /* escape at end of line */
593 "key3=\\ifoo\n" /* invalid escape */
594 "key4=\\i\\hfoo\n"; /* invalid escape with multiple stacked errors */
596 keyfile = load_data (data, 0);
598 check_string_value (keyfile, "valid", "key1", " \n\t\r\\");
599 check_string_value (keyfile, "valid", "key2", "\"quoted\"");
600 check_string_value (keyfile, "valid", "key3", "'quoted'");
601 check_string_value (keyfile, "valid", "key4", "\xe2\x89\xa0\xe2\x89\xa0");
602 check_string_value (keyfile, "valid", "key5", "leading space");
603 check_string_value (keyfile, "valid", "key6", "trailing space ");
605 value = g_key_file_get_string (keyfile, "invalid", "key1", &error);
606 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
609 value = g_key_file_get_string (keyfile, "invalid", "key2", &error);
610 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
613 value = g_key_file_get_string (keyfile, "invalid", "key3", &error);
614 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
617 value = g_key_file_get_string (keyfile, "invalid", "key4", &error);
618 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
621 g_key_file_set_string (keyfile, "inserted", "key1", "simple");
622 g_key_file_set_string (keyfile, "inserted", "key2", " leading space");
623 g_key_file_set_string (keyfile, "inserted", "key3", "\tleading tab");
624 g_key_file_set_string (keyfile, "inserted", "key4", "new\nline");
625 g_key_file_set_string (keyfile, "inserted", "key5", "carriage\rreturn");
626 g_key_file_set_string (keyfile, "inserted", "key6", "slash\\yay!");
627 g_key_file_set_string_list (keyfile, "inserted", "key7", list, 3);
629 check_string_value (keyfile, "inserted", "key1", "simple");
630 check_string_value (keyfile, "inserted", "key2", " leading space");
631 check_string_value (keyfile, "inserted", "key3", "\tleading tab");
632 check_string_value (keyfile, "inserted", "key4", "new\nline");
633 check_string_value (keyfile, "inserted", "key5", "carriage\rreturn");
634 check_string_value (keyfile, "inserted", "key6", "slash\\yay!");
635 check_string_list_value (keyfile, "inserted", "key7", "one", "two;andahalf", "3", NULL);
637 g_key_file_free (keyfile);
640 /* check parsing of boolean values */
645 GError *error = NULL;
661 keyfile = load_data (data, 0);
663 check_boolean_value (keyfile, "valid", "key1", TRUE);
664 check_boolean_value (keyfile, "valid", "key2", FALSE);
665 check_boolean_value (keyfile, "valid", "key3", TRUE);
666 check_boolean_value (keyfile, "valid", "key4", FALSE);
667 check_boolean_value (keyfile, "valid", "key5", TRUE);
668 check_boolean_value (keyfile, "valid", "key6", TRUE);
670 g_key_file_get_boolean (keyfile, "invalid", "key1", &error);
671 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
673 g_key_file_get_boolean (keyfile, "invalid", "key2", &error);
674 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
676 g_key_file_get_boolean (keyfile, "invalid", "key3", &error);
677 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
679 g_key_file_get_boolean (keyfile, "invalid", "key4", &error);
680 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
682 g_key_file_set_boolean (keyfile, "valid", "key1", FALSE);
683 check_boolean_value (keyfile, "valid", "key1", FALSE);
685 g_key_file_free (keyfile);
688 /* check parsing of integer and double values */
693 GError *error = NULL;
719 keyfile = load_data (data, 0);
721 check_integer_value (keyfile, "valid", "key1", 0);
722 check_integer_value (keyfile, "valid", "key2", 1);
723 check_integer_value (keyfile, "valid", "key3", -1);
724 check_integer_value (keyfile, "valid", "key4", 2324431);
725 check_integer_value (keyfile, "valid", "key5", -2324431);
726 check_integer_value (keyfile, "valid", "key6", 111);
727 check_integer_value (keyfile, "valid", "key7", 1);
728 check_integer_value (keyfile, "valid", "key8", 1);
729 check_double_value (keyfile, "valid", "dkey1", 111.0);
730 check_double_value (keyfile, "valid", "dkey2", 145.45);
731 check_double_value (keyfile, "valid", "dkey3", -3453.7);
733 g_key_file_get_integer (keyfile, "invalid", "key1", &error);
734 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
736 g_key_file_get_integer (keyfile, "invalid", "key2", &error);
737 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
739 g_key_file_get_integer (keyfile, "invalid", "key3", &error);
740 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
742 g_key_file_get_integer (keyfile, "invalid", "key4", &error);
743 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
745 dval = g_key_file_get_double (keyfile, "invalid", "key5", &error);
746 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
747 g_assert_cmpfloat (dval, ==, 0.0);
749 dval = g_key_file_get_double (keyfile, "invalid", "key6", &error);
750 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
751 g_assert_cmpfloat (dval, ==, 0.0);
753 dval = g_key_file_get_double (keyfile, "invalid", "key7", &error);
754 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
755 g_assert_cmpfloat (dval, ==, 0.0);
757 dval = g_key_file_get_double (keyfile, "invalid", "key8", &error);
758 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
759 g_assert_cmpfloat (dval, ==, 0.0);
761 g_key_file_free (keyfile);
764 /* check handling of translated strings */
766 test_locale_string (void)
775 "key1[de_DE]=v1-de_DE\n"
776 "key1[de_DE.UTF8]=v1-de_DE.UTF8\n"
779 "key1[sr@Latn]=v1-sr\n";
781 keyfile = load_data (data, G_KEY_FILE_KEEP_TRANSLATIONS);
783 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
784 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
785 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de_DE");
786 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de_DE.UTF8");
787 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1-fr");
788 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1-fr");
789 check_locale_string_value (keyfile, "valid", "key1", "en", "v1-en");
790 check_locale_string_value (keyfile, "valid", "key1", "sr@Latn", "v1-sr");
792 g_key_file_free (keyfile);
794 /* now test that translations are thrown away */
796 old_locale = g_strdup (setlocale (LC_ALL, NULL));
797 g_setenv ("LANGUAGE", "de", TRUE);
798 setlocale (LC_ALL, "");
800 keyfile = load_data (data, 0);
802 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
803 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
804 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de");
805 check_locale_string_value (keyfile, "valid", "key1", "de_DE.UTF8", "v1-de");
806 check_locale_string_value (keyfile, "valid", "key1", "fr", "v1");
807 check_locale_string_value (keyfile, "valid", "key1", "fr_FR", "v1");
808 check_locale_string_value (keyfile, "valid", "key1", "en", "v1");
810 g_key_file_free (keyfile);
812 setlocale (LC_ALL, old_locale);
817 test_locale_string_multiple_loads (void)
819 GKeyFile *keyfile = NULL;
820 GError *local_error = NULL;
821 gchar *old_locale = NULL;
827 "key1[de_DE]=v1-de_DE\n"
828 "key1[de_DE.UTF8]=v1-de_DE.UTF8\n"
831 "key1[sr@Latn]=v1-sr\n";
833 g_test_summary ("Check that loading with translations multiple times works");
834 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2361");
836 old_locale = g_strdup (setlocale (LC_ALL, NULL));
837 g_setenv ("LANGUAGE", "de", TRUE);
838 setlocale (LC_ALL, "");
840 keyfile = g_key_file_new ();
842 for (i = 0; i < 3; i++)
844 g_key_file_load_from_data (keyfile, data, -1, G_KEY_FILE_NONE, &local_error);
845 g_assert_no_error (local_error);
847 check_locale_string_value (keyfile, "valid", "key1", "it", "v1");
848 check_locale_string_value (keyfile, "valid", "key1", "de", "v1-de");
849 check_locale_string_value (keyfile, "valid", "key1", "de_DE", "v1-de");
852 g_key_file_free (keyfile);
854 setlocale (LC_ALL, old_locale);
871 "key7= 1 ; 0 ; -1 \n"
873 "key9=0;1.3456;-76532.456\n";
875 keyfile = load_data (data, 0);
877 check_string_list_value (keyfile, "valid", "key1", "v1", "v2", NULL);
878 check_string_list_value (keyfile, "valid", "key2", "v1", "v2", NULL);
879 check_string_list_value (keyfile, "valid", "key3", "v1,v2", NULL);
880 check_string_list_value (keyfile, "valid", "key4", "v1;v2", NULL);
881 check_boolean_list_value (keyfile, "valid", "key5", TRUE, FALSE, -100);
882 check_integer_list_value (keyfile, "valid", "key6", 1, 0, -1, -100);
883 check_double_list_value (keyfile, "valid", "key9", 0.0, 1.3456, -76532.456, -100.0);
884 /* maybe these should be valid */
885 /* check_integer_list_value (keyfile, "valid", "key7", 1, 0, -1, -100);*/
886 /* check_string_list_value (keyfile, "valid", "key8", "v1\\,v2", NULL);*/
888 g_key_file_free (keyfile);
890 /* Now check an alternate separator */
892 keyfile = load_data (data, 0);
893 g_key_file_set_list_separator (keyfile, ',');
895 check_string_list_value (keyfile, "valid", "key1", "v1;v2", NULL);
896 check_string_list_value (keyfile, "valid", "key2", "v1;v2;", NULL);
897 check_string_list_value (keyfile, "valid", "key3", "v1", "v2", NULL);
899 g_key_file_free (keyfile);
903 test_lists_set_get (void)
906 static const char * const strings[] = { "v1", "v2" };
907 static const char * const locale_strings[] = { "v1-l", "v2-l" };
908 static int integers[] = { 1, -1, 2 };
909 static gdouble doubles[] = { 3.14, 2.71 };
911 keyfile = g_key_file_new ();
912 g_key_file_set_string_list (keyfile, "group0", "key1", strings, G_N_ELEMENTS (strings));
913 g_key_file_set_locale_string_list (keyfile, "group0", "key1", "de", locale_strings, G_N_ELEMENTS (locale_strings));
914 g_key_file_set_integer_list (keyfile, "group0", "key2", integers, G_N_ELEMENTS (integers));
915 g_key_file_set_double_list (keyfile, "group0", "key3", doubles, G_N_ELEMENTS (doubles));
917 check_string_list_value (keyfile, "group0", "key1", strings[0], strings[1], NULL);
918 check_locale_string_list_value (keyfile, "group0", "key1", "de", locale_strings[0], locale_strings[1], NULL);
919 check_integer_list_value (keyfile, "group0", "key2", integers[0], integers[1], -100);
920 check_double_list_value (keyfile, "group0", "key3", doubles[0], doubles[1], -100.0);
921 g_key_file_free (keyfile);
923 /* and again with a different list separator */
924 keyfile = g_key_file_new ();
925 g_key_file_set_list_separator (keyfile, ',');
926 g_key_file_set_string_list (keyfile, "group0", "key1", strings, G_N_ELEMENTS (strings));
927 g_key_file_set_locale_string_list (keyfile, "group0", "key1", "de", locale_strings, G_N_ELEMENTS (locale_strings));
928 g_key_file_set_integer_list (keyfile, "group0", "key2", integers, G_N_ELEMENTS (integers));
929 g_key_file_set_double_list (keyfile, "group0", "key3", doubles, G_N_ELEMENTS (doubles));
931 check_string_list_value (keyfile, "group0", "key1", strings[0], strings[1], NULL);
932 check_locale_string_list_value (keyfile, "group0", "key1", "de", locale_strings[0], locale_strings[1], NULL);
933 check_integer_list_value (keyfile, "group0", "key2", integers[0], integers[1], -100);
934 check_double_list_value (keyfile, "group0", "key3", doubles[0], doubles[1], -100.0);
935 g_key_file_free (keyfile);
939 test_group_remove (void)
944 GError *error = NULL;
955 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=165887");
957 keyfile = load_data (data, 0);
959 names = g_key_file_get_groups (keyfile, &len);
960 g_assert_nonnull (names);
962 check_length ("groups", g_strv_length (names), len, 3);
963 check_name ("group name", names[0], "group1", 0);
964 check_name ("group name", names[1], "group2", 1);
965 check_name ("group name", names[2], "group3", 2);
967 g_key_file_remove_group (keyfile, "group1", &error);
968 check_no_error (&error);
972 names = g_key_file_get_groups (keyfile, &len);
973 g_assert_nonnull (names);
975 check_length ("groups", g_strv_length (names), len, 2);
976 check_name ("group name", names[0], "group2", 0);
977 check_name ("group name", names[1], "group3", 1);
979 g_key_file_remove_group (keyfile, "group2", &error);
980 check_no_error (&error);
984 names = g_key_file_get_groups (keyfile, &len);
985 g_assert_nonnull (names);
987 check_length ("groups", g_strv_length (names), len, 1);
988 check_name ("group name", names[0], "group3", 0);
990 g_key_file_remove_group (keyfile, "no such group", &error);
991 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
995 g_key_file_free (keyfile);
999 test_key_remove (void)
1003 GError *error = NULL;
1010 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=165980");
1012 keyfile = load_data (data, 0);
1014 check_string_value (keyfile, "group1", "key1", "bla");
1016 g_key_file_remove_key (keyfile, "group1", "key1", &error);
1017 check_no_error (&error);
1019 value = g_key_file_get_string (keyfile, "group1", "key1", &error);
1020 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1023 g_key_file_remove_key (keyfile, "group1", "key1", &error);
1024 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1026 g_key_file_remove_key (keyfile, "no such group", "key1", &error);
1027 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1029 g_key_file_free (keyfile);
1044 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=316309");
1046 keyfile = load_data (data, 0);
1048 check_string_value (keyfile, "1", "key1", "123");
1049 check_string_value (keyfile, "2", "key2", "123");
1051 g_key_file_free (keyfile);
1055 test_group_names (void)
1058 GError *error = NULL;
1062 /* [ in group name */
1065 keyfile = g_key_file_new ();
1066 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1067 g_key_file_free (keyfile);
1068 check_error (&error,
1070 G_KEY_FILE_ERROR_PARSE);
1072 /* ] in group name */
1075 keyfile = g_key_file_new ();
1076 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1077 g_key_file_free (keyfile);
1078 check_error (&error,
1080 G_KEY_FILE_ERROR_PARSE);
1082 /* control char in group name */
1085 keyfile = g_key_file_new ();
1086 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1087 g_key_file_free (keyfile);
1088 check_error (&error,
1090 G_KEY_FILE_ERROR_PARSE);
1092 /* empty group name */
1095 keyfile = g_key_file_new ();
1096 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1097 g_key_file_free (keyfile);
1098 check_error (&error,
1100 G_KEY_FILE_ERROR_PARSE);
1102 /* Unicode in group name */
1103 data = "[\xc2\xbd]\n"
1105 keyfile = g_key_file_new ();
1106 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1107 g_key_file_free (keyfile);
1108 check_no_error (&error);
1110 keyfile = g_key_file_new ();
1111 /*g_key_file_set_string (keyfile, "a[b", "key1", "123");*/
1112 value = g_key_file_get_string (keyfile, "a[b", "key1", &error);
1113 check_error (&error,
1115 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1116 g_assert_null (value);
1117 g_key_file_free (keyfile);
1119 keyfile = g_key_file_new ();
1120 /*g_key_file_set_string (keyfile, "a]b", "key1", "123");*/
1121 value = g_key_file_get_string (keyfile, "a]b", "key1", &error);
1122 check_error (&error,
1124 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1125 g_assert_null (value);
1126 g_key_file_free (keyfile);
1128 keyfile = g_key_file_new ();
1129 /*g_key_file_set_string (keyfile, "a\tb", "key1", "123");*/
1130 value = g_key_file_get_string (keyfile, "a\tb", "key1", &error);
1131 check_error (&error,
1133 G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1134 g_assert_null (value);
1135 g_key_file_free (keyfile);
1137 keyfile = g_key_file_new ();
1138 g_key_file_set_string (keyfile, "\xc2\xbd", "key1", "123");
1139 check_string_value (keyfile, "\xc2\xbd", "key1", "123");
1140 g_key_file_free (keyfile);
1144 test_key_names (void)
1147 GError *error = NULL;
1154 keyfile = g_key_file_new ();
1155 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1156 g_key_file_free (keyfile);
1157 check_error (&error,
1159 G_KEY_FILE_ERROR_PARSE);
1161 /* empty key name */
1164 keyfile = g_key_file_new ();
1165 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1166 g_key_file_free (keyfile);
1167 check_error (&error,
1169 G_KEY_FILE_ERROR_PARSE);
1171 /* empty key name */
1174 keyfile = g_key_file_new ();
1175 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1176 g_key_file_free (keyfile);
1177 check_error (&error,
1179 G_KEY_FILE_ERROR_PARSE);
1181 /* bad locale suffix */
1184 keyfile = g_key_file_new ();
1185 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1186 g_key_file_free (keyfile);
1187 check_error (&error,
1189 G_KEY_FILE_ERROR_PARSE);
1194 keyfile = g_key_file_new ();
1195 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1196 check_no_error (&error);
1197 check_string_value (keyfile, "a", "foo", "123");
1198 g_key_file_free (keyfile);
1203 keyfile = g_key_file_new ();
1204 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1205 check_no_error (&error);
1206 check_string_value (keyfile, "a", "foo", "123");
1207 g_key_file_free (keyfile);
1212 keyfile = g_key_file_new ();
1213 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1214 check_no_error (&error);
1215 check_string_value (keyfile, "a", "foo bar", "123");
1216 g_key_file_free (keyfile);
1221 keyfile = g_key_file_new ();
1222 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1223 check_error (&error,
1225 G_KEY_FILE_ERROR_PARSE);
1226 g_key_file_free (keyfile);
1228 /* control char in key name */
1231 keyfile = g_key_file_new ();
1232 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1233 g_key_file_free (keyfile);
1234 check_no_error (&error);
1236 /* Unicode in key name */
1239 keyfile = g_key_file_new ();
1240 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1241 g_key_file_free (keyfile);
1242 check_no_error (&error);
1244 keyfile = g_key_file_new ();
1245 g_key_file_set_string (keyfile, "a", "x", "123");
1246 /*g_key_file_set_string (keyfile, "a", "key=", "123");*/
1247 value = g_key_file_get_string (keyfile, "a", "key=", &error);
1248 check_error (&error,
1250 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1251 g_assert_null (value);
1252 g_key_file_free (keyfile);
1254 keyfile = g_key_file_new ();
1255 g_key_file_set_string (keyfile, "a", "x", "123");
1256 /*g_key_file_set_string (keyfile, "a", "key[", "123");*/
1257 value = g_key_file_get_string (keyfile, "a", "key[", &error);
1258 check_error (&error,
1260 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1261 g_assert_null (value);
1262 g_key_file_free (keyfile);
1264 keyfile = g_key_file_new ();
1265 g_key_file_set_string (keyfile, "a", "x", "123");
1266 g_key_file_set_string (keyfile, "a", "key\tfoo", "123");
1267 value = g_key_file_get_string (keyfile, "a", "key\tfoo", &error);
1268 check_no_error (&error);
1270 g_key_file_free (keyfile);
1272 keyfile = g_key_file_new ();
1273 g_key_file_set_string (keyfile, "a", "x", "123");
1274 /*g_key_file_set_string (keyfile, "a", " key", "123");*/
1275 value = g_key_file_get_string (keyfile, "a", " key", &error);
1276 check_error (&error,
1278 G_KEY_FILE_ERROR_KEY_NOT_FOUND);
1279 g_assert_null (value);
1280 g_key_file_free (keyfile);
1282 keyfile = g_key_file_new ();
1283 g_key_file_set_string (keyfile, "a", "x", "123");
1286 g_key_file_set_string (keyfile, "a", "\xc2\xbd", "123");
1287 check_string_value (keyfile, "a", "\xc2\xbd", "123");
1289 /* Keys with / + . (as used by the gnome-vfs mime cache) */
1290 g_key_file_set_string (keyfile, "a", "foo/bar", "/");
1291 check_string_value (keyfile, "a", "foo/bar", "/");
1292 g_key_file_set_string (keyfile, "a", "foo+bar", "+");
1293 check_string_value (keyfile, "a", "foo+bar", "+");
1294 g_key_file_set_string (keyfile, "a", "foo.bar", ".");
1295 check_string_value (keyfile, "a", "foo.bar", ".");
1297 g_key_file_free (keyfile);
1301 test_duplicate_keys (void)
1309 keyfile = load_data (data, 0);
1310 check_string_value (keyfile, "1", "key1", "345");
1312 g_key_file_free (keyfile);
1316 test_duplicate_groups (void)
1325 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=157877");
1327 keyfile = load_data (data, 0);
1328 check_string_value (keyfile, "Desktop Entry", "key1", "123");
1329 check_string_value (keyfile, "Desktop Entry", "key2", "123");
1331 g_key_file_free (keyfile);
1335 test_duplicate_groups2 (void)
1346 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=385910");
1348 keyfile = load_data (data, 0);
1349 check_string_value (keyfile, "A", "foo", "bang");
1350 check_string_value (keyfile, "B", "foo", "baz");
1352 g_key_file_free (keyfile);
1356 test_reload_idempotency (void)
1358 static const gchar *original_data=""
1364 "# A random comment in the first group\n"
1365 "anotherkey=anothervalue\n"
1366 "# Second comment - one line\n"
1368 "# Third comment - two lines\n"
1369 "# Third comment - two lines\n"
1378 GError *error = NULL;
1379 gchar *data1, *data2, *comment;
1382 const gchar *key_comment = " A random comment in the first group";
1383 const gchar *top_comment = " Top comment\n\n First comment";
1384 const gchar *group_comment_1 = top_comment;
1385 const gchar *group_comment_2 = " Second comment - one line";
1386 const gchar *group_comment_3 = " Third comment - two lines\n Third comment - two lines";
1387 const gchar *group_comment_4 = "\n";
1389 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=420686");
1391 /* check that we only insert a single new line between groups */
1392 keyfile = g_key_file_new ();
1393 g_key_file_load_from_data (keyfile,
1394 original_data, strlen(original_data),
1395 G_KEY_FILE_KEEP_COMMENTS,
1397 check_no_error (&error);
1399 data1 = g_key_file_to_data (keyfile, &len1, &error);
1400 g_assert_nonnull (data1);
1401 g_key_file_free (keyfile);
1403 keyfile = g_key_file_new ();
1404 g_key_file_load_from_data (keyfile,
1406 G_KEY_FILE_KEEP_COMMENTS,
1408 check_no_error (&error);
1410 data2 = g_key_file_to_data (keyfile, &len2, &error);
1411 g_assert_nonnull (data2);
1413 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2927");
1415 /* check if comments are preserved on reload */
1416 comment = g_key_file_get_comment (keyfile, "first", "anotherkey", &error);
1417 check_no_error (&error);
1418 g_assert_cmpstr (comment, ==, key_comment);
1421 comment = g_key_file_get_comment (keyfile, NULL, NULL, &error);
1422 check_no_error (&error);
1423 g_assert_cmpstr (comment, ==, top_comment);
1426 comment = g_key_file_get_comment (keyfile, "first", NULL, &error);
1427 check_no_error (&error);
1428 g_assert_cmpstr (comment, ==, group_comment_1);
1431 comment = g_key_file_get_comment (keyfile, "second", NULL, &error);
1432 check_no_error (&error);
1433 g_assert_cmpstr (comment, ==, group_comment_2);
1436 comment = g_key_file_get_comment (keyfile, "third", NULL, &error);
1437 check_no_error (&error);
1438 g_assert_cmpstr (comment, ==, group_comment_3);
1441 comment = g_key_file_get_comment (keyfile, "fourth", NULL, &error);
1442 check_no_error (&error);
1443 g_assert_cmpstr (comment, ==, group_comment_4);
1446 comment = g_key_file_get_comment (keyfile, "fifth", NULL, &error);
1447 check_no_error (&error);
1448 g_assert_null (comment);
1450 g_key_file_free (keyfile);
1452 g_assert_cmpstr (data1, ==, data2);
1458 static const char int64_data[] =
1462 "c=123456789123456789\n"
1463 "d=-123456789123456789\n";
1474 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=614864");
1476 file = g_key_file_new ();
1478 ok = g_key_file_load_from_data (file, int64_data, strlen (int64_data),
1482 c = g_key_file_get_uint64 (file, "bees", "c", NULL);
1483 g_assert_cmpuint (c, ==, G_GUINT64_CONSTANT (123456789123456789));
1485 d = g_key_file_get_int64 (file, "bees", "d", NULL);
1486 g_assert_cmpint (d, ==, G_GINT64_CONSTANT (-123456789123456789));
1488 g_key_file_set_uint64 (file, "bees", "c",
1489 G_GUINT64_CONSTANT (987654321987654321));
1490 value = g_key_file_get_value (file, "bees", "c", NULL);
1491 g_assert_cmpstr (value, ==, "987654321987654321");
1494 g_key_file_set_int64 (file, "bees", "d",
1495 G_GINT64_CONSTANT (-987654321987654321));
1496 value = g_key_file_get_value (file, "bees", "d", NULL);
1497 g_assert_cmpstr (value, ==, "-987654321987654321");
1500 g_key_file_free (file);
1508 gboolean bools[2] = { TRUE, FALSE };
1511 file = g_key_file_new ();
1514 /* Uses the value of $XDG_DATA_HOME we set in main() */
1515 loaded = g_key_file_load_from_data_dirs (file, "keyfiletest.ini", NULL, 0, &error);
1517 loaded = g_key_file_load_from_file (file, g_test_get_filename (G_TEST_DIST, "keyfiletest.ini", NULL), 0, &error);
1519 g_assert_no_error (error);
1520 g_assert_true (loaded);
1522 g_key_file_set_locale_string (file, "test", "key4", "de", "Vierter SchlĆ¼ssel");
1523 g_key_file_set_boolean_list (file, "test", "key5", bools, 2);
1524 g_key_file_set_integer (file, "test", "key6", 22);
1525 g_key_file_set_double (file, "test", "key7", 2.5);
1526 g_key_file_set_comment (file, "test", "key7", "some float", NULL);
1527 g_key_file_set_comment (file, "test", NULL, "the test group", NULL);
1528 g_key_file_set_comment (file, NULL, NULL, "top comment", NULL);
1530 g_key_file_free (file);
1532 file = g_key_file_new ();
1534 g_assert_false (g_key_file_load_from_data_dirs (file, "keyfile-test.ini", NULL, 0, &error));
1535 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_NOT_FOUND);
1536 g_error_free (error);
1537 g_key_file_free (file);
1545 static const char data[] =
1549 "c=123456789123456789\n"
1550 "d=-123456789123456789\n";
1554 GError *error = NULL;
1557 kf = g_key_file_new ();
1558 ok = g_key_file_load_from_data (kf, data, strlen (data), 0, NULL);
1561 file = g_strdup ("key_file_XXXXXX");
1562 fd = g_mkstemp (file);
1563 g_assert_cmpint (fd, !=, -1);
1564 ok = g_close (fd, &error);
1566 g_assert_no_error (error);
1567 ok = g_key_file_save_to_file (kf, file, &error);
1569 g_assert_no_error (error);
1571 kf2 = g_key_file_new ();
1572 ok = g_key_file_load_from_file (kf2, file, 0, &error);
1574 g_assert_no_error (error);
1576 c = g_key_file_get_uint64 (kf2, "bees", "c", NULL);
1577 g_assert_cmpuint (c, ==, G_GUINT64_CONSTANT (123456789123456789));
1581 g_key_file_free (kf);
1582 g_key_file_free (kf2);
1586 test_load_fail (void)
1591 file = g_key_file_new ();
1593 g_assert_false (g_key_file_load_from_file (file, g_test_get_filename (G_TEST_DIST, "keyfile.c", NULL), 0, &error));
1594 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE);
1595 g_clear_error (&error);
1596 g_assert_false (g_key_file_load_from_file (file, "/nosuchfile", 0, &error));
1597 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
1598 g_clear_error (&error);
1600 g_key_file_free (file);
1604 test_non_utf8 (void)
1607 static const char data[] =
1610 "b=a;b;\230\230\230;\n"
1617 file = g_key_file_new ();
1619 ok = g_key_file_load_from_data (file, data, strlen (data), 0, NULL);
1623 s = g_key_file_get_string (file, "group", "a", &error);
1624 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_UNKNOWN_ENCODING);
1627 g_clear_error (&error);
1628 l = g_key_file_get_string_list (file, "group", "b", NULL, &error);
1629 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_UNKNOWN_ENCODING);
1632 g_clear_error (&error);
1633 l = g_key_file_get_string_list (file, "group", "c", NULL, &error);
1634 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE);
1637 g_clear_error (&error);
1639 g_key_file_free (file);
1643 test_page_boundary (void)
1649 #define GROUP "main_section"
1650 #define KEY_PREFIX "fill_abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvw_"
1651 #define FIRST_KEY 10
1655 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=640695");
1657 file = g_key_file_new ();
1660 g_key_file_load_from_file (file, g_test_get_filename (G_TEST_DIST, "pages.ini", NULL), G_KEY_FILE_NONE, &error);
1661 g_assert_no_error (error);
1663 for (i = FIRST_KEY; i <= LAST_KEY; i++)
1668 key = g_strdup_printf (KEY_PREFIX "%d", i);
1669 val = g_key_file_get_integer (file, GROUP, key, &error);
1671 g_assert_no_error (error);
1672 g_assert_cmpint (val, ==, VALUE);
1675 g_key_file_free (file);
1682 static const char data[] =
1687 file = g_key_file_new ();
1689 ok = g_key_file_load_from_data (file, data, strlen (data), 0, NULL);
1691 g_assert_true (g_key_file_has_key (file, "group", "a", NULL));
1692 g_key_file_ref (file);
1693 g_key_file_free (file);
1694 g_key_file_unref (file);
1697 /* https://bugzilla.gnome.org/show_bug.cgi?id=634232 */
1699 test_replace_value (void)
1703 keyfile = g_key_file_new();
1704 g_key_file_set_value(keyfile, "grupo1", "chave1", "1234567890");
1705 g_key_file_set_value(keyfile, "grupo1", "chave1", "123123423423423432432423423");
1706 g_key_file_remove_group(keyfile, "grupo1", NULL);
1707 g_free (g_key_file_to_data (keyfile, NULL, NULL));
1708 g_key_file_unref (keyfile);
1712 test_list_separator (void)
1715 GError *error = NULL;
1721 keyfile = g_key_file_new ();
1722 g_key_file_set_list_separator (keyfile, ',');
1723 g_key_file_load_from_data (keyfile, data, -1, 0, &error);
1725 check_string_list_value (keyfile, "test", "key1", "v1", "v2", NULL);
1726 g_key_file_unref (keyfile);
1730 test_empty_string (void)
1732 GError *error = NULL;
1735 kf = g_key_file_new ();
1737 g_key_file_load_from_data (kf, "", 0, 0, &error);
1738 g_assert_no_error (error);
1740 g_key_file_load_from_data (kf, "", -1, 0, &error);
1741 g_assert_no_error (error);
1743 /* NULL is a fine pointer to use if length is zero */
1744 g_key_file_load_from_data (kf, NULL, 0, 0, &error);
1745 g_assert_no_error (error);
1747 /* should not attempt to access non-NULL pointer if length is zero */
1748 g_key_file_load_from_data (kf, GINT_TO_POINTER (1), 0, 0, &error);
1749 g_assert_no_error (error);
1751 g_key_file_unref (kf);
1758 static const char data[] =
1765 file = g_key_file_new ();
1768 ok = g_key_file_load_from_data (file, data, strlen (data), 0, &error);
1769 g_assert_false (ok);
1770 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1771 g_clear_error (&error);
1772 g_key_file_free (file);
1778 const gchar *invalid_encoding_names[] =
1786 for (i = 0; i < G_N_ELEMENTS (invalid_encoding_names); i++)
1788 GKeyFile *file = NULL;
1791 GError *error = NULL;
1793 g_test_message ("Testing invalid encoding ā%sā", invalid_encoding_names[i]);
1795 file = g_key_file_new ();
1796 data = g_strdup_printf ("[group]\n"
1797 "Encoding=%s\n", invalid_encoding_names[i]);
1798 ok = g_key_file_load_from_data (file, data, strlen (data), 0, &error);
1799 g_assert_false (ok);
1800 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_UNKNOWN_ENCODING);
1801 g_clear_error (&error);
1802 g_key_file_free (file);
1808 test_roundtrip (void)
1811 const gchar orig[] =
1820 kf = load_data (orig, G_KEY_FILE_KEEP_COMMENTS);
1821 g_key_file_set_integer (kf, "Group1", "key2", 0);
1822 g_key_file_remove_key (kf, "Group1", "key2", NULL);
1824 data = g_key_file_to_data (kf, &len, NULL);
1825 g_assert_cmpstr (data, ==, orig);
1828 g_key_file_free (kf);
1834 const gchar data[] =
1841 GKeyFile *kf = g_key_file_new ();
1842 GBytes *bytes = g_bytes_new (data, strlen (data));
1843 GError *error = NULL;
1848 g_key_file_load_from_bytes (kf, bytes, 0, &error);
1850 g_assert_no_error (error);
1852 names = g_key_file_get_groups (kf, &len);
1853 g_assert_nonnull (names);
1855 check_length ("groups", g_strv_length (names), len, 2);
1856 check_name ("group name", names[0], "Group1", 0);
1857 check_name ("group name", names[1], "Group2", 1);
1859 check_string_value (kf, "Group1", "key1", "value1");
1860 check_string_value (kf, "Group2", "key2", "value2");
1863 g_bytes_unref (bytes);
1864 g_key_file_free (kf);
1868 test_get_locale (void)
1872 kf = g_key_file_new ();
1873 g_key_file_load_from_data (kf,
1878 -1, G_KEY_FILE_KEEP_TRANSLATIONS,
1881 check_locale_string_value (kf, "Group", "x", "fr_CA", "a");
1882 check_string_locale_value (kf, "Group", "x", "fr_CA", "fr_CA");
1884 check_locale_string_value (kf, "Group", "x", "fr_CH", "b");
1885 check_string_locale_value (kf, "Group", "x", "fr_CH", "fr");
1887 check_locale_string_value (kf, "Group", "x", "eo", "c");
1888 check_string_locale_value (kf, "Group", "x", "eo", NULL);
1890 g_key_file_free (kf);
1894 test_free_when_not_last_ref (void)
1897 GError *error = NULL;
1902 kf = load_data (data, G_KEY_FILE_NONE);
1903 /* Add a second ref */
1904 g_key_file_ref (kf);
1906 /* Quick coherence check */
1907 g_assert_true (g_key_file_has_group (kf, "Group"));
1908 g_assert_true (g_key_file_has_key (kf, "Group", "Key", &error));
1909 g_assert_no_error (error);
1911 /* Should clear all keys and groups, and remove one ref */
1912 g_key_file_free (kf);
1914 /* kf should still work */
1915 g_assert_false (g_key_file_has_group (kf, "Group"));
1916 g_assert_false (g_key_file_has_key (kf, "Group", "Key", &error));
1917 check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
1918 g_clear_error (&error);
1920 g_key_file_load_from_data (kf, data, -1, G_KEY_FILE_NONE, &error);
1921 g_assert_no_error (error);
1923 g_assert_true (g_key_file_has_group (kf, "Group"));
1924 g_assert_true (g_key_file_has_key (kf, "Group", "Key", &error));
1926 g_key_file_unref (kf);
1930 main (int argc, char *argv[])
1932 g_test_init (&argc, &argv, NULL);
1935 g_setenv ("XDG_DATA_HOME", g_test_get_dir (G_TEST_DIST), TRUE);
1938 g_test_add_func ("/keyfile/line-ends", test_line_ends);
1939 g_test_add_func ("/keyfile/whitespace", test_whitespace);
1940 g_test_add_func ("/keyfile/comments", test_comments);
1941 g_test_add_func ("/keyfile/listing", test_listing);
1942 g_test_add_func ("/keyfile/string", test_string);
1943 g_test_add_func ("/keyfile/boolean", test_boolean);
1944 g_test_add_func ("/keyfile/number", test_number);
1945 g_test_add_func ("/keyfile/locale-string", test_locale_string);
1946 g_test_add_func ("/keyfile/locale-string/multiple-loads", test_locale_string_multiple_loads);
1947 g_test_add_func ("/keyfile/lists", test_lists);
1948 g_test_add_func ("/keyfile/lists-set-get", test_lists_set_get);
1949 g_test_add_func ("/keyfile/group-remove", test_group_remove);
1950 g_test_add_func ("/keyfile/key-remove", test_key_remove);
1951 g_test_add_func ("/keyfile/groups", test_groups);
1952 g_test_add_func ("/keyfile/duplicate-keys", test_duplicate_keys);
1953 g_test_add_func ("/keyfile/duplicate-groups", test_duplicate_groups);
1954 g_test_add_func ("/keyfile/duplicate-groups2", test_duplicate_groups2);
1955 g_test_add_func ("/keyfile/group-names", test_group_names);
1956 g_test_add_func ("/keyfile/key-names", test_key_names);
1957 g_test_add_func ("/keyfile/reload", test_reload_idempotency);
1958 g_test_add_func ("/keyfile/int64", test_int64);
1959 g_test_add_func ("/keyfile/load", test_load);
1960 g_test_add_func ("/keyfile/save", test_save);
1961 g_test_add_func ("/keyfile/load-fail", test_load_fail);
1962 g_test_add_func ("/keyfile/non-utf8", test_non_utf8);
1963 g_test_add_func ("/keyfile/page-boundary", test_page_boundary);
1964 g_test_add_func ("/keyfile/ref", test_ref);
1965 g_test_add_func ("/keyfile/replace-value", test_replace_value);
1966 g_test_add_func ("/keyfile/list-separator", test_list_separator);
1967 g_test_add_func ("/keyfile/empty-string", test_empty_string);
1968 g_test_add_func ("/keyfile/limbo", test_limbo);
1969 g_test_add_func ("/keyfile/utf8", test_utf8);
1970 g_test_add_func ("/keyfile/roundtrip", test_roundtrip);
1971 g_test_add_func ("/keyfile/bytes", test_bytes);
1972 g_test_add_func ("/keyfile/get-locale", test_get_locale);
1973 g_test_add_func ("/keyfile/free-when-not-last-ref", test_free_when_not_last_ref);
1975 return g_test_run ();