pidgin/pidgin

Add some unit tests for key value pairs

2020-05-01, Gary Kramlich
79cbdc5c5989
Parents 72271baf92bc
Children 7281920d8a66
Add some unit tests for key value pairs
--- a/libpurple/purplekeyvaluepair.c Thu Apr 30 23:43:54 2020 -0500
+++ b/libpurple/purplekeyvaluepair.c Fri May 01 00:10:57 2020 -0500
@@ -35,7 +35,7 @@
{
PurpleKeyValuePair *kvp;
- kvp = g_new0(PurpleKeyValuePair, 1);
+ kvp = g_new(PurpleKeyValuePair, 1);
kvp->key = g_strdup(key);
kvp->value = value;
kvp->value_destroy_func = value_destroy_func;
@@ -59,7 +59,7 @@
PurpleKeyValuePair *
purple_key_value_pair_copy(PurpleKeyValuePair *kvp) {
g_return_val_if_fail(kvp != NULL, NULL);
+ g_return_val_if_fail(kvp->value_destroy_func == NULL, NULL);
- return purple_key_value_pair_new_full(kvp->key, kvp->value,
- kvp->value_destroy_func);
+ return purple_key_value_pair_new(kvp->key, kvp->value);
}
--- a/libpurple/purplekeyvaluepair.h Thu Apr 30 23:43:54 2020 -0500
+++ b/libpurple/purplekeyvaluepair.h Fri May 01 00:10:57 2020 -0500
@@ -110,6 +110,9 @@
*
* Creates a copy of @kvp.
*
+ * If @kvp has a %value_destroy_func, %NULL will be returned as we this
+ * function has no way to know how to allocate a new copy of the value.
+ *
* Returns: (transfer full): A new copy of @kvp.
*/
PurpleKeyValuePair *purple_key_value_pair_copy(PurpleKeyValuePair *kvp);
--- a/libpurple/tests/meson.build Thu Apr 30 23:43:54 2020 -0500
+++ b/libpurple/tests/meson.build Fri May 01 00:10:57 2020 -0500
@@ -3,6 +3,7 @@
'attention_type',
'circular_buffer',
'image',
+ 'keyvaluepair',
'protocol_action',
'protocol_attention',
'protocol_xfer',
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/libpurple/tests/test_keyvaluepair.c Fri May 01 00:10:57 2020 -0500
@@ -0,0 +1,116 @@
+/*
+ * purple
+ *
+ * Purple is the legal property of its developers, whose names are too numerous
+ * to list here. Please refer to the COPYRIGHT file distributed with this
+ * source distribution.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <glib.h>
+
+#include <purple.h>
+
+/******************************************************************************
+ * Tests
+ *****************************************************************************/
+static void
+test_key_value_pair_new(void) {
+ PurpleKeyValuePair *kvp = NULL;
+
+ kvp = purple_key_value_pair_new("abc", "123");
+ g_assert_nonnull(kvp);
+
+ g_assert_cmpstr(kvp->key, ==, "abc");
+ g_assert_cmpstr(kvp->value, ==, "123");
+ g_assert_null(kvp->value_destroy_func);
+
+ purple_key_value_pair_free(kvp);
+}
+
+static void
+test_key_value_pair_new_full(void) {
+ PurpleKeyValuePair *kvp = NULL;
+
+ kvp = purple_key_value_pair_new_full("abc", g_strdup("123"), g_free);
+ g_assert_nonnull(kvp);
+
+ g_assert_cmpstr(kvp->key, ==, "abc");
+ g_assert_cmpstr(kvp->value, ==, "123");
+ g_assert_true(kvp->value_destroy_func == g_free);
+
+ purple_key_value_pair_free(kvp);
+}
+
+static void
+test_key_value_pair_copy(void) {
+ PurpleKeyValuePair *kvp1 = NULL, *kvp2 = NULL;
+
+ kvp1 = purple_key_value_pair_new("abc", "123");
+ g_assert_nonnull(kvp1);
+
+ kvp2 = purple_key_value_pair_copy(kvp1);
+ g_assert_nonnull(kvp2);
+
+ g_assert_cmpstr(kvp1->key, ==, kvp2->key);
+ g_assert_cmpstr(kvp1->value, ==, kvp2->value);;
+ g_assert_true(kvp1->value_destroy_func == kvp2->value_destroy_func);
+
+ purple_key_value_pair_free(kvp1);
+ purple_key_value_pair_free(kvp2);
+}
+
+static void
+test_key_value_pair_copy_allocated_subprocess(void) {
+ PurpleKeyValuePair *kvp1 = NULL, *kvp2 = NULL;
+
+ kvp1 = purple_key_value_pair_new_full("abc", g_strdup("123"), g_free);
+ g_assert_nonnull(kvp1);
+
+ kvp2 = purple_key_value_pair_copy(kvp1);
+ g_assert_null(kvp2);
+
+ purple_key_value_pair_free(kvp1);
+}
+
+static void
+test_key_value_pair_copy_allocated(void) {
+ g_test_trap_subprocess("/key-value-pair/copy-allocated/subprocess", 0, 0);
+
+ g_test_trap_assert_failed();
+}
+
+/******************************************************************************
+ * Main
+ *****************************************************************************/
+gint
+main(gint argc, gchar **argv) {
+ g_test_init(&argc, &argv, NULL);
+
+ g_test_set_nonfatal_assertions();
+
+ g_test_add_func("/key-value-pair/new",
+ test_key_value_pair_new);
+ g_test_add_func("/key-value-pair/new-full",
+ test_key_value_pair_new_full);
+ g_test_add_func("/key-value-pair/copy",
+ test_key_value_pair_copy);
+ g_test_add_func("/key-value-pair/copy-allocated",
+ test_key_value_pair_copy_allocated);
+ g_test_add_func("/key-value-pair/copy-allocated/subprocess",
+ test_key_value_pair_copy_allocated_subprocess);
+
+ return g_test_run();
+}