diff options
author | Gaël Bonithon <gael@xfce.org> | 2023-04-14 19:40:30 +0200 |
---|---|---|
committer | Gaël Bonithon <gael@xfce.org> | 2023-04-20 16:59:04 +0200 |
commit | 86b4b0453ea3a814167d4a5f7a4031d467543716 (patch) | |
tree | bdb2d4b026c5c75233d91e15c4fbb10775210100 /glib/tests/keyfile.c | |
parent | f74589f53041abff29d538a5d9884c3202f2c00a (diff) | |
download | glib-86b4b0453ea3a814167d4a5f7a4031d467543716.tar.gz |
gkeyfile: Fix group comment management
This removes the `comment` member of the GKeyFileGroup structure, which
seemed intended to distinguish comments just above a group from comments
above them, separated by one or more blank lines. Indeed:
* This does not seem to match any specification in the documentation,
where blank lines and lines starting with `#` are indiscriminately
considered comments. In particular, no distinction is made between the
comment above the first group and the comment at the beginning of the
file.
* This distinction was only half implemented, resulting in confusion
between comment above a group and comment at the end of the preceding
group.
Instead, the same logic is used for groups as for keys: the comment
above a group is simply the sequence of key-value pairs of the preceding
group where the key is null, starting from the bottom.
The addition of a blank line above groups when writing, involved in
bugs #104 and #2927, is kept, but:
* It is now added as a comment as soon as the group is added (since a
blank line is considered a comment), so that
`g_key_file_get_comment()` returns the correct result right away.
* It is always added if comments are not kept.
* Otherwise it is only added if the group is newly created (not present
in the original data), in order to really keep comments (existing and
not existing).
Closes: #104, #2927
Diffstat (limited to 'glib/tests/keyfile.c')
-rw-r--r-- | glib/tests/keyfile.c | 75 |
1 files changed, 73 insertions, 2 deletions
diff --git a/glib/tests/keyfile.c b/glib/tests/keyfile.c index 3d72d9670..d3eed2984 100644 --- a/glib/tests/keyfile.c +++ b/glib/tests/keyfile.c @@ -382,7 +382,9 @@ test_comments (void) "key4 = value4\n" "# group comment\n" "# group comment, continued\n" - "[group2]\n"; + "[group2]\n\n" + "[group3]\n" + "[group4]\n"; const gchar *top_comment = " top comment\n top comment, continued"; const gchar *group_comment = " group comment\n group comment, continued"; @@ -427,6 +429,12 @@ test_comments (void) check_name ("top comment", comment, top_comment, 0); g_free (comment); + g_key_file_remove_comment (keyfile, NULL, NULL, &error); + check_no_error (&error); + comment = g_key_file_get_comment (keyfile, NULL, NULL, &error); + check_no_error (&error); + g_assert_null (comment); + comment = g_key_file_get_comment (keyfile, "group1", "key2", &error); check_no_error (&error); check_name ("key comment", comment, key_comment, 0); @@ -448,7 +456,25 @@ test_comments (void) check_name ("group comment", comment, group_comment, 0); g_free (comment); + g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/104"); + + /* check if comments above another group than the first one are properly removed */ + g_key_file_remove_comment (keyfile, "group2", NULL, &error); + check_no_error (&error); + comment = g_key_file_get_comment (keyfile, "group2", NULL, &error); + check_no_error (&error); + g_assert_null (comment); + comment = g_key_file_get_comment (keyfile, "group3", NULL, &error); + check_no_error (&error); + check_name ("group comment", comment, "", 0); + g_free (comment); + + comment = g_key_file_get_comment (keyfile, "group4", NULL, &error); + check_no_error (&error); + g_assert_null (comment); + + comment = g_key_file_get_comment (keyfile, "group5", NULL, &error); check_error (&error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND); @@ -1321,9 +1347,16 @@ test_reload_idempotency (void) "[fifth]\n"; GKeyFile *keyfile; GError *error = NULL; - gchar *data1, *data2; + gchar *data1, *data2, *comment; gsize len1, len2; + const gchar *key_comment = " A random comment in the first group"; + const gchar *top_comment = " Top comment\n\n First comment"; + const gchar *group_comment_1 = top_comment; + const gchar *group_comment_2 = " Second comment - one line"; + const gchar *group_comment_3 = " Third comment - two lines\n Third comment - two lines"; + const gchar *group_comment_4 = "\n"; + g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=420686"); /* check that we only insert a single new line between groups */ @@ -1347,6 +1380,44 @@ test_reload_idempotency (void) data2 = g_key_file_to_data (keyfile, &len2, &error); g_assert_nonnull (data2); + + g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2927"); + + /* check if comments are preserved on reload */ + comment = g_key_file_get_comment (keyfile, "first", "anotherkey", &error); + check_no_error (&error); + g_assert_cmpstr (comment, ==, key_comment); + g_free (comment); + + comment = g_key_file_get_comment (keyfile, NULL, NULL, &error); + check_no_error (&error); + g_assert_cmpstr (comment, ==, top_comment); + g_free (comment); + + comment = g_key_file_get_comment (keyfile, "first", NULL, &error); + check_no_error (&error); + g_assert_cmpstr (comment, ==, group_comment_1); + g_free (comment); + + comment = g_key_file_get_comment (keyfile, "second", NULL, &error); + check_no_error (&error); + g_assert_cmpstr (comment, ==, group_comment_2); + g_free (comment); + + comment = g_key_file_get_comment (keyfile, "third", NULL, &error); + check_no_error (&error); + g_assert_cmpstr (comment, ==, group_comment_3); + g_free (comment); + + comment = g_key_file_get_comment (keyfile, "fourth", NULL, &error); + check_no_error (&error); + g_assert_cmpstr (comment, ==, group_comment_4); + g_free (comment); + + comment = g_key_file_get_comment (keyfile, "fifth", NULL, &error); + check_no_error (&error); + g_assert_null (comment); + g_key_file_free (keyfile); g_assert_cmpstr (data1, ==, data2); |