Add purple_tags_remove_with_value

Sun, 16 Jul 2023 03:24:38 -0500

author
Gary Kramlich <grim@reaperworld.com>
date
Sun, 16 Jul 2023 03:24:38 -0500
changeset 42239
a453c0c955e9
parent 42238
48bb3244b5ea
child 42240
8920a8452aea

Add purple_tags_remove_with_value

This is just a wrapper around purple_tags_remove that builds the tag out of a
name and value.

Testing Done:
Ran the unit tests.

Reviewed at https://reviews.imfreedom.org/r/2514/

libpurple/purpletags.c file | annotate | diff | comparison | revisions
libpurple/purpletags.h file | annotate | diff | comparison | revisions
libpurple/tests/test_tags.c file | annotate | diff | comparison | revisions
--- a/libpurple/purpletags.c	Fri Jul 14 16:21:19 2023 -0500
+++ b/libpurple/purpletags.c	Sun Jul 16 03:24:38 2023 -0500
@@ -161,6 +161,25 @@
 	return FALSE;
 }
 
+gboolean
+purple_tags_remove_with_value(PurpleTags *tags, const char *name, const char *value) {
+	gboolean ret = FALSE;
+
+	g_return_val_if_fail(PURPLE_IS_TAGS(tags), FALSE);
+	g_return_val_if_fail(name != NULL, FALSE);
+
+	if(value == NULL) {
+		ret = purple_tags_remove(tags, name);
+	} else {
+		char *tag = g_strdup_printf("%s:%s", name, value);
+
+		ret = purple_tags_remove(tags, tag);
+		g_free(tag);
+	}
+
+	return ret;
+}
+
 guint
 purple_tags_get_count(PurpleTags *tags) {
 	g_return_val_if_fail(PURPLE_IS_TAGS(tags), 0);
--- a/libpurple/purpletags.h	Fri Jul 14 16:21:19 2023 -0500
+++ b/libpurple/purpletags.h	Sun Jul 16 03:24:38 2023 -0500
@@ -120,8 +120,8 @@
  * @tags: The instance.
  * @tag: The tag data.
  *
- * Removes the first occurrence of @tag from @tags. Note that this is the tag
- * name and value not just the name.
+ * Removes @tag from @tags. Note that this is the tag name and value not just
+ * the name.
  *
  * Returns: %TRUE if @tag was found and removed, otherwise %FALSE.
  *
@@ -130,6 +130,21 @@
 gboolean purple_tags_remove(PurpleTags *tags, const gchar *tag);
 
 /**
+ * purple_tags_remove_with_value:
+ * @tags: The instance.
+ * @name: The tag name.
+ * @value: (nullable): The tag value.
+ *
+ * A helper function around [method@Tags.remove] that builds the tag variable
+ * from @name and @value.
+ *
+ * Returns: %TRUE if a tag was found and removed, otherwise %FALSE.
+ *
+ * Since: 3.0.0
+ */
+gboolean purple_tags_remove_with_value(PurpleTags *tags, const char *name, const char *value);
+
+/**
  * purple_tags_get_count:
  * @tags: The instance.
  *
--- a/libpurple/tests/test_tags.c	Fri Jul 14 16:21:19 2023 -0500
+++ b/libpurple/tests/test_tags.c	Sun Jul 16 03:24:38 2023 -0500
@@ -94,8 +94,40 @@
 static void
 test_purple_tags_remove_non_existent_bare(void) {
 	PurpleTags *tags = purple_tags_new();
+	gboolean ret = FALSE;
 
-	purple_tags_remove(tags, "tag1");
+	ret = purple_tags_remove(tags, "tag1");
+	g_assert_false(ret);
+	g_assert_cmpuint(purple_tags_get_count(tags), ==, 0);
+
+	g_clear_object(&tags);
+}
+
+static void
+test_purple_tags_add_remove(void) {
+	PurpleTags *tags = purple_tags_new();
+	gboolean ret = FALSE;
+
+	purple_tags_add(tags, "tag1:purple");
+	g_assert_cmpuint(purple_tags_get_count(tags), ==, 1);
+
+	ret = purple_tags_remove(tags, "tag1:purple");
+	g_assert_true(ret);
+	g_assert_cmpuint(purple_tags_get_count(tags), ==, 0);
+
+	g_clear_object(&tags);
+}
+
+static void
+test_purple_tags_add_remove_with_null_value(void) {
+	PurpleTags *tags = purple_tags_new();
+	gboolean ret = FALSE;
+
+	purple_tags_add_with_value(tags, "tag1", NULL);
+	g_assert_cmpuint(purple_tags_get_count(tags), ==, 1);
+
+	ret = purple_tags_remove_with_value(tags, "tag1", NULL);
+	g_assert_true(ret);
 	g_assert_cmpuint(purple_tags_get_count(tags), ==, 0);
 
 	g_clear_object(&tags);
@@ -104,11 +136,13 @@
 static void
 test_purple_tags_add_remove_with_value(void) {
 	PurpleTags *tags = purple_tags_new();
+	gboolean ret = FALSE;
 
-	purple_tags_add(tags, "tag1:purple");
+	purple_tags_add_with_value(tags, "tag1", "purple");
 	g_assert_cmpuint(purple_tags_get_count(tags), ==, 1);
 
-	purple_tags_remove(tags, "tag1:purple");
+	ret = purple_tags_remove_with_value(tags, "tag1", "purple");
+	g_assert_true(ret);
 	g_assert_cmpuint(purple_tags_get_count(tags), ==, 0);
 
 	g_clear_object(&tags);
@@ -381,6 +415,10 @@
 	g_test_add_func("/tags/add-with-value-null",
 	                test_purple_tags_add_with_value_null);
 
+	g_test_add_func("/tags/add-remove",
+	                test_purple_tags_add_remove);
+	g_test_add_func("/tags/add-remove-with-null-value",
+	                test_purple_tags_add_remove_with_null_value);
 	g_test_add_func("/tags/add-remove-with-value",
 	                test_purple_tags_add_remove_with_value);
 	g_test_add_func("/tags/add-duplicate-with-value",

mercurial