libpurple/tests/test_command.c

Sun, 10 Aug 2025 23:44:08 +0800

author
Gong Zhile <gongzl@stu.hebust.edu.cn>
date
Sun, 10 Aug 2025 23:44:08 +0800
branch
purple_conversation_find_message_by_id
changeset 43309
099e1dfb856b
parent 42967
9da19bb7e207
permissions
-rw-r--r--

Add Purple.Conversation.find_message_by_id

The method was added so that a protocol or plugin could easily lookup
for the reference for a message. This will be especially useful when a
protocol received a quoted message but only with an id.

/*
 * Purple - Internet Messaging Library
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib.h>

#include <purple.h>

/******************************************************************************
 * Tests
 *****************************************************************************/
static void
test_purple_command_new(void) {
	PurpleCommand *command = NULL;

	command = purple_command_new("name", "source", 100);
	g_assert_true(PURPLE_IS_COMMAND(command));

	g_assert_finalize_object(command);
}

static void
test_purple_command_properties(void) {
	PurpleCommand *command = NULL;
	PurpleTags *tags = NULL;
	GDateTime *last_used = NULL;
	GDateTime *last_used1 = NULL;
	char *icon_name = NULL;
	char *name = NULL;
	char *source = NULL;
	char *summary = NULL;
	int priority = 0;
	guint use_count = 0;

	last_used = g_date_time_new_now_utc();

	command = g_object_new(
		PURPLE_TYPE_COMMAND,
		"icon-name", "icon",
		"last-used", last_used,
		"name", "name",
		"priority", 100,
		"source", "source",
		"summary", "summary",
		"use-count", 1337,
		NULL);

	g_assert_true(PURPLE_IS_COMMAND(command));

	g_object_get(
		G_OBJECT(command),
		"icon-name", &icon_name,
		"last-used", &last_used1,
		"name", &name,
		"priority", &priority,
		"source", &source,
		"summary", &summary,
		"tags", &tags,
		"use-count", &use_count,
		NULL);

	g_assert_cmpstr(icon_name, ==, "icon");
	g_clear_pointer(&icon_name, g_free);

	g_assert_true(birb_date_time_equal(last_used1, last_used));
	birb_date_time_clear(&last_used1);

	g_assert_cmpstr(name, ==, "name");
	g_clear_pointer(&name, g_free);

	g_assert_cmpint(priority, ==, 100);

	g_assert_cmpstr(source, ==, "source");
	g_clear_pointer(&source, g_free);

	g_assert_cmpstr(summary, ==, "summary");
	g_clear_pointer(&summary, g_free);

	g_assert_true(PURPLE_IS_TAGS(tags));
	g_clear_object(&tags);

	g_assert_cmpuint(use_count, ==, 1337);

	g_assert_finalize_object(command);

	birb_date_time_clear(&last_used);
}

/******************************************************************************
 * Execute Tests
 *****************************************************************************/
static void
test_purple_command_executed_counter_cb(PurpleCommand *command,
                                        G_GNUC_UNUSED PurpleConversation *conversation,
                                        G_GNUC_UNUSED GStrv params,
                                        gpointer data)
{
	guint *counter = data;

	g_assert_true(PURPLE_IS_COMMAND(command));

	*counter = *counter + 1;
}

static void
test_purple_command_executed_params_cb(PurpleCommand *command,
                                       G_GNUC_UNUSED PurpleConversation *conversation,
                                       GStrv params,
                                       gpointer data)
{
	g_assert_true(PURPLE_IS_COMMAND(command));

	g_assert_cmpstrv(params, data);
}

static void
test_purple_command_execute_null_params(void) {
	PurpleCommand *command = NULL;
	GDateTime *last_used = NULL;
	guint counter = 0;
	guint use_count = 0;

	command = purple_command_new("command", "test", 0);
	g_signal_connect(command, "executed",
	                 G_CALLBACK(test_purple_command_executed_counter_cb),
	                 &counter);
	g_signal_connect(command, "executed",
	                 G_CALLBACK(test_purple_command_executed_params_cb),
	                 NULL);

	purple_command_execute(command, NULL, NULL);
	g_assert_cmpuint(counter, ==, 1);

	last_used = purple_command_get_last_used(command);
	g_assert_nonnull(last_used);

	use_count = purple_command_get_use_count(command);
	g_assert_cmpuint(use_count, ==, 1);

	g_assert_finalize_object(command);
}

static void
test_purple_command_execute_empty_params(void) {
	PurpleCommand *command = NULL;
	GDateTime *last_used = NULL;
	guint counter = 0;
	guint use_count = 0;
	const char * const expected[] = {NULL};

	command = purple_command_new("command", "test", 0);
	g_signal_connect(command, "executed",
	                 G_CALLBACK(test_purple_command_executed_counter_cb),
	                 &counter);
	g_signal_connect(command, "executed",
	                 G_CALLBACK(test_purple_command_executed_params_cb),
	                 (gpointer)expected);

	purple_command_execute(command, NULL, "");
	g_assert_cmpuint(counter, ==, 1);

	last_used = purple_command_get_last_used(command);
	g_assert_nonnull(last_used);

	use_count = purple_command_get_use_count(command);
	g_assert_cmpuint(use_count, ==, 1);

	g_assert_finalize_object(command);
}

static void
test_purple_command_execute_single_param(void) {
	PurpleCommand *command = NULL;
	GDateTime *last_used = NULL;
	guint counter = 0;
	guint use_count = 0;
	const char * const expected[] = {"param1", NULL};

	command = purple_command_new("command", "test", 0);
	g_signal_connect(command, "executed",
	                 G_CALLBACK(test_purple_command_executed_counter_cb),
	                 &counter);
	g_signal_connect(command, "executed",
	                 G_CALLBACK(test_purple_command_executed_params_cb),
	                 (gpointer)expected);

	purple_command_execute(command, NULL, "param1");
	g_assert_cmpuint(counter, ==, 1);

	last_used = purple_command_get_last_used(command);
	g_assert_nonnull(last_used);

	use_count = purple_command_get_use_count(command);
	g_assert_cmpuint(use_count, ==, 1);

	g_assert_finalize_object(command);
}

static void
test_purple_command_execute_multiple_params(void) {
	PurpleCommand *command = NULL;
	GDateTime *last_used = NULL;
	guint counter = 0;
	guint use_count = 0;
	const char * const expected[] = {"param1", "param2", "param3", NULL};

	command = purple_command_new("command", "test", 0);
	g_signal_connect(command, "executed",
	                 G_CALLBACK(test_purple_command_executed_counter_cb),
	                 &counter);
	g_signal_connect(command, "executed",
	                 G_CALLBACK(test_purple_command_executed_params_cb),
	                 (gpointer)expected);

	purple_command_execute(command, NULL, "param1 param2 param3");
	g_assert_cmpuint(counter, ==, 1);

	last_used = purple_command_get_last_used(command);
	g_assert_nonnull(last_used);

	use_count = purple_command_get_use_count(command);
	g_assert_cmpuint(use_count, ==, 1);

	g_assert_finalize_object(command);
}

static void
test_purple_command_executev_null_params(void) {
	PurpleCommand *command = NULL;
	GDateTime *last_used = NULL;
	guint counter = 0;
	guint use_count = 0;

	command = purple_command_new("command", "test", 0);
	g_signal_connect(command, "executed",
	                 G_CALLBACK(test_purple_command_executed_counter_cb),
	                 &counter);
	g_signal_connect(command, "executed",
	                 G_CALLBACK(test_purple_command_executed_params_cb),
	                 NULL);

	purple_command_executev(command, NULL, NULL);
	g_assert_cmpuint(counter, ==, 1);

	last_used = purple_command_get_last_used(command);
	g_assert_nonnull(last_used);

	use_count = purple_command_get_use_count(command);
	g_assert_cmpuint(use_count, ==, 1);

	g_assert_finalize_object(command);
}

static void
test_purple_command_executev_empty_params(void) {
	PurpleCommand *command = NULL;
	GDateTime *last_used = NULL;
	guint counter = 0;
	guint use_count = 0;
	const char * const expected[] = {NULL};

	command = purple_command_new("command", "test", 0);
	g_signal_connect(command, "executed",
	                 G_CALLBACK(test_purple_command_executed_counter_cb),
	                 &counter);
	g_signal_connect(command, "executed",
	                 G_CALLBACK(test_purple_command_executed_params_cb),
	                 (gpointer)expected);

	purple_command_executev(command, NULL, (GStrv)expected);
	g_assert_cmpuint(counter, ==, 1);

	last_used = purple_command_get_last_used(command);
	g_assert_nonnull(last_used);

	use_count = purple_command_get_use_count(command);
	g_assert_cmpuint(use_count, ==, 1);

	g_assert_finalize_object(command);
}

static void
test_purple_command_executev_single_param(void) {
	PurpleCommand *command = NULL;
	GDateTime *last_used = NULL;
	guint counter = 0;
	guint use_count = 0;
	const char * const expected[] = {"param1", NULL};

	command = purple_command_new("command", "test", 0);
	g_signal_connect(command, "executed",
	                 G_CALLBACK(test_purple_command_executed_counter_cb),
	                 &counter);
	g_signal_connect(command, "executed",
	                 G_CALLBACK(test_purple_command_executed_params_cb),
	                 (gpointer)expected);

	purple_command_executev(command, NULL, (GStrv)expected);
	g_assert_cmpuint(counter, ==, 1);

	last_used = purple_command_get_last_used(command);
	g_assert_nonnull(last_used);

	use_count = purple_command_get_use_count(command);
	g_assert_cmpuint(use_count, ==, 1);

	g_assert_finalize_object(command);
}

static void
test_purple_command_executev_multiple_params(void) {
	PurpleCommand *command = NULL;
	GDateTime *last_used = NULL;
	guint counter = 0;
	guint use_count = 0;
	const char * const expected[] = {"param1", "param2", "param3", NULL};

	command = purple_command_new("command", "test", 0);
	g_signal_connect(command, "executed",
	                 G_CALLBACK(test_purple_command_executed_counter_cb),
	                 &counter);
	g_signal_connect(command, "executed",
	                 G_CALLBACK(test_purple_command_executed_params_cb),
	                 (gpointer)expected);

	purple_command_executev(command, NULL, (GStrv)expected);
	g_assert_cmpuint(counter, ==, 1);

	last_used = purple_command_get_last_used(command);
	g_assert_nonnull(last_used);

	use_count = purple_command_get_use_count(command);
	g_assert_cmpuint(use_count, ==, 1);

	g_assert_finalize_object(command);
}

/******************************************************************************
 * Main
 *****************************************************************************/
int
main(int argc, char *argv[]) {
	g_test_init(&argc, &argv, NULL);
	g_test_set_nonfatal_assertions();

	g_test_add_func("/command/new", test_purple_command_new);
	g_test_add_func("/command/properties", test_purple_command_properties);

	g_test_add_func("/command/execute/null-params",
	                test_purple_command_execute_null_params);
	g_test_add_func("/command/execute/empty-params",
	                test_purple_command_execute_empty_params);
	g_test_add_func("/command/execute/single-param",
	                test_purple_command_execute_single_param);
	g_test_add_func("/command/execute/multiple-params",
	                test_purple_command_execute_multiple_params);

	g_test_add_func("/command/executev/null-params",
	                test_purple_command_executev_null_params);
	g_test_add_func("/command/executev/empty-params",
	                test_purple_command_executev_empty_params);
	g_test_add_func("/command/executev/single-param",
	                test_purple_command_executev_single_param);
	g_test_add_func("/command/executev/multiple-params",
	                test_purple_command_executev_multiple_params);

	return g_test_run();
}

mercurial