Tue, 24 Sep 2024 21:28:06 -0500
Add a tags property to Purple.Command
Testing Done:
Ran the tests under valgrind and called in the turtles for good measure.
Bugs closed: PIDGIN-17973
Reviewed at https://reviews.imfreedom.org/r/3523/
| 42964 | 1 | /* |
| 2 | * Purple - Internet Messaging Library | |
| 3 | * Copyright (C) Pidgin Developers <devel@pidgin.im> | |
| 4 | * | |
| 5 | * This library is free software; you can redistribute it and/or | |
| 6 | * modify it under the terms of the GNU Lesser General Public | |
| 7 | * License as published by the Free Software Foundation; either | |
| 8 | * version 2 of the License, or (at your option) any later version. | |
| 9 | * | |
| 10 | * This library is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | * Lesser General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU Lesser General Public | |
| 16 | * License along with this library; if not, see <https://www.gnu.org/licenses/>. | |
| 17 | */ | |
| 18 | ||
| 19 | #include <glib.h> | |
| 20 | ||
| 21 | #include <purple.h> | |
| 22 | ||
| 23 | /****************************************************************************** | |
| 24 | * Tests | |
| 25 | *****************************************************************************/ | |
| 26 | static void | |
| 27 | test_purple_command_new(void) { | |
| 28 | PurpleCommand *command = NULL; | |
| 29 | ||
| 30 | command = purple_command_new("name", "source", 100); | |
| 31 | g_assert_true(PURPLE_IS_COMMAND(command)); | |
| 32 | ||
| 33 | g_assert_finalize_object(command); | |
| 34 | } | |
| 35 | ||
| 36 | static void | |
| 37 | test_purple_command_properties(void) { | |
| 38 | PurpleCommand *command = NULL; | |
|
42967
9da19bb7e207
Add a tags property to Purple.Command
Gary Kramlich <grim@reaperworld.com>
parents:
42964
diff
changeset
|
39 | PurpleTags *tags = NULL; |
| 42964 | 40 | GDateTime *last_used = NULL; |
| 41 | GDateTime *last_used1 = NULL; | |
| 42 | char *icon_name = NULL; | |
| 43 | char *name = NULL; | |
| 44 | char *source = NULL; | |
| 45 | char *summary = NULL; | |
| 46 | int priority = 0; | |
| 47 | guint use_count = 0; | |
| 48 | ||
| 49 | last_used = g_date_time_new_now_utc(); | |
| 50 | ||
| 51 | command = g_object_new( | |
| 52 | PURPLE_TYPE_COMMAND, | |
| 53 | "icon-name", "icon", | |
| 54 | "last-used", last_used, | |
| 55 | "name", "name", | |
| 56 | "priority", 100, | |
| 57 | "source", "source", | |
| 58 | "summary", "summary", | |
| 59 | "use-count", 1337, | |
| 60 | NULL); | |
| 61 | ||
| 62 | g_assert_true(PURPLE_IS_COMMAND(command)); | |
| 63 | ||
| 64 | g_object_get( | |
| 65 | G_OBJECT(command), | |
| 66 | "icon-name", &icon_name, | |
| 67 | "last-used", &last_used1, | |
| 68 | "name", &name, | |
| 69 | "priority", &priority, | |
| 70 | "source", &source, | |
| 71 | "summary", &summary, | |
|
42967
9da19bb7e207
Add a tags property to Purple.Command
Gary Kramlich <grim@reaperworld.com>
parents:
42964
diff
changeset
|
72 | "tags", &tags, |
| 42964 | 73 | "use-count", &use_count, |
| 74 | NULL); | |
| 75 | ||
| 76 | g_assert_cmpstr(icon_name, ==, "icon"); | |
| 77 | g_clear_pointer(&icon_name, g_free); | |
| 78 | ||
| 79 | g_assert_true(birb_date_time_equal(last_used1, last_used)); | |
| 80 | birb_date_time_clear(&last_used1); | |
| 81 | ||
| 82 | g_assert_cmpstr(name, ==, "name"); | |
| 83 | g_clear_pointer(&name, g_free); | |
| 84 | ||
| 85 | g_assert_cmpint(priority, ==, 100); | |
| 86 | ||
| 87 | g_assert_cmpstr(source, ==, "source"); | |
| 88 | g_clear_pointer(&source, g_free); | |
| 89 | ||
| 90 | g_assert_cmpstr(summary, ==, "summary"); | |
| 91 | g_clear_pointer(&summary, g_free); | |
| 92 | ||
|
42967
9da19bb7e207
Add a tags property to Purple.Command
Gary Kramlich <grim@reaperworld.com>
parents:
42964
diff
changeset
|
93 | g_assert_true(PURPLE_IS_TAGS(tags)); |
|
9da19bb7e207
Add a tags property to Purple.Command
Gary Kramlich <grim@reaperworld.com>
parents:
42964
diff
changeset
|
94 | g_clear_object(&tags); |
|
9da19bb7e207
Add a tags property to Purple.Command
Gary Kramlich <grim@reaperworld.com>
parents:
42964
diff
changeset
|
95 | |
| 42964 | 96 | g_assert_cmpuint(use_count, ==, 1337); |
| 97 | ||
| 98 | g_assert_finalize_object(command); | |
| 99 | ||
| 100 | birb_date_time_clear(&last_used); | |
| 101 | } | |
| 102 | ||
| 103 | /****************************************************************************** | |
| 104 | * Execute Tests | |
| 105 | *****************************************************************************/ | |
| 106 | static void | |
| 107 | test_purple_command_executed_counter_cb(PurpleCommand *command, | |
| 108 | G_GNUC_UNUSED PurpleConversation *conversation, | |
| 109 | G_GNUC_UNUSED GStrv params, | |
| 110 | gpointer data) | |
| 111 | { | |
| 112 | guint *counter = data; | |
| 113 | ||
| 114 | g_assert_true(PURPLE_IS_COMMAND(command)); | |
| 115 | ||
| 116 | *counter = *counter + 1; | |
| 117 | } | |
| 118 | ||
| 119 | static void | |
| 120 | test_purple_command_executed_params_cb(PurpleCommand *command, | |
| 121 | G_GNUC_UNUSED PurpleConversation *conversation, | |
| 122 | GStrv params, | |
| 123 | gpointer data) | |
| 124 | { | |
| 125 | g_assert_true(PURPLE_IS_COMMAND(command)); | |
| 126 | ||
| 127 | g_assert_cmpstrv(params, data); | |
| 128 | } | |
| 129 | ||
| 130 | static void | |
| 131 | test_purple_command_execute_null_params(void) { | |
| 132 | PurpleCommand *command = NULL; | |
| 133 | GDateTime *last_used = NULL; | |
| 134 | guint counter = 0; | |
| 135 | guint use_count = 0; | |
| 136 | ||
| 137 | command = purple_command_new("command", "test", 0); | |
| 138 | g_signal_connect(command, "executed", | |
| 139 | G_CALLBACK(test_purple_command_executed_counter_cb), | |
| 140 | &counter); | |
| 141 | g_signal_connect(command, "executed", | |
| 142 | G_CALLBACK(test_purple_command_executed_params_cb), | |
| 143 | NULL); | |
| 144 | ||
| 145 | purple_command_execute(command, NULL, NULL); | |
| 146 | g_assert_cmpuint(counter, ==, 1); | |
| 147 | ||
| 148 | last_used = purple_command_get_last_used(command); | |
| 149 | g_assert_nonnull(last_used); | |
| 150 | ||
| 151 | use_count = purple_command_get_use_count(command); | |
| 152 | g_assert_cmpuint(use_count, ==, 1); | |
| 153 | ||
| 154 | g_assert_finalize_object(command); | |
| 155 | } | |
| 156 | ||
| 157 | static void | |
| 158 | test_purple_command_execute_empty_params(void) { | |
| 159 | PurpleCommand *command = NULL; | |
| 160 | GDateTime *last_used = NULL; | |
| 161 | guint counter = 0; | |
| 162 | guint use_count = 0; | |
| 163 | const char * const expected[] = {NULL}; | |
| 164 | ||
| 165 | command = purple_command_new("command", "test", 0); | |
| 166 | g_signal_connect(command, "executed", | |
| 167 | G_CALLBACK(test_purple_command_executed_counter_cb), | |
| 168 | &counter); | |
| 169 | g_signal_connect(command, "executed", | |
| 170 | G_CALLBACK(test_purple_command_executed_params_cb), | |
| 171 | (gpointer)expected); | |
| 172 | ||
| 173 | purple_command_execute(command, NULL, ""); | |
| 174 | g_assert_cmpuint(counter, ==, 1); | |
| 175 | ||
| 176 | last_used = purple_command_get_last_used(command); | |
| 177 | g_assert_nonnull(last_used); | |
| 178 | ||
| 179 | use_count = purple_command_get_use_count(command); | |
| 180 | g_assert_cmpuint(use_count, ==, 1); | |
| 181 | ||
| 182 | g_assert_finalize_object(command); | |
| 183 | } | |
| 184 | ||
| 185 | static void | |
| 186 | test_purple_command_execute_single_param(void) { | |
| 187 | PurpleCommand *command = NULL; | |
| 188 | GDateTime *last_used = NULL; | |
| 189 | guint counter = 0; | |
| 190 | guint use_count = 0; | |
| 191 | const char * const expected[] = {"param1", NULL}; | |
| 192 | ||
| 193 | command = purple_command_new("command", "test", 0); | |
| 194 | g_signal_connect(command, "executed", | |
| 195 | G_CALLBACK(test_purple_command_executed_counter_cb), | |
| 196 | &counter); | |
| 197 | g_signal_connect(command, "executed", | |
| 198 | G_CALLBACK(test_purple_command_executed_params_cb), | |
| 199 | (gpointer)expected); | |
| 200 | ||
| 201 | purple_command_execute(command, NULL, "param1"); | |
| 202 | g_assert_cmpuint(counter, ==, 1); | |
| 203 | ||
| 204 | last_used = purple_command_get_last_used(command); | |
| 205 | g_assert_nonnull(last_used); | |
| 206 | ||
| 207 | use_count = purple_command_get_use_count(command); | |
| 208 | g_assert_cmpuint(use_count, ==, 1); | |
| 209 | ||
| 210 | g_assert_finalize_object(command); | |
| 211 | } | |
| 212 | ||
| 213 | static void | |
| 214 | test_purple_command_execute_multiple_params(void) { | |
| 215 | PurpleCommand *command = NULL; | |
| 216 | GDateTime *last_used = NULL; | |
| 217 | guint counter = 0; | |
| 218 | guint use_count = 0; | |
| 219 | const char * const expected[] = {"param1", "param2", "param3", NULL}; | |
| 220 | ||
| 221 | command = purple_command_new("command", "test", 0); | |
| 222 | g_signal_connect(command, "executed", | |
| 223 | G_CALLBACK(test_purple_command_executed_counter_cb), | |
| 224 | &counter); | |
| 225 | g_signal_connect(command, "executed", | |
| 226 | G_CALLBACK(test_purple_command_executed_params_cb), | |
| 227 | (gpointer)expected); | |
| 228 | ||
| 229 | purple_command_execute(command, NULL, "param1 param2 param3"); | |
| 230 | g_assert_cmpuint(counter, ==, 1); | |
| 231 | ||
| 232 | last_used = purple_command_get_last_used(command); | |
| 233 | g_assert_nonnull(last_used); | |
| 234 | ||
| 235 | use_count = purple_command_get_use_count(command); | |
| 236 | g_assert_cmpuint(use_count, ==, 1); | |
| 237 | ||
| 238 | g_assert_finalize_object(command); | |
| 239 | } | |
| 240 | ||
| 241 | static void | |
| 242 | test_purple_command_executev_null_params(void) { | |
| 243 | PurpleCommand *command = NULL; | |
| 244 | GDateTime *last_used = NULL; | |
| 245 | guint counter = 0; | |
| 246 | guint use_count = 0; | |
| 247 | ||
| 248 | command = purple_command_new("command", "test", 0); | |
| 249 | g_signal_connect(command, "executed", | |
| 250 | G_CALLBACK(test_purple_command_executed_counter_cb), | |
| 251 | &counter); | |
| 252 | g_signal_connect(command, "executed", | |
| 253 | G_CALLBACK(test_purple_command_executed_params_cb), | |
| 254 | NULL); | |
| 255 | ||
| 256 | purple_command_executev(command, NULL, NULL); | |
| 257 | g_assert_cmpuint(counter, ==, 1); | |
| 258 | ||
| 259 | last_used = purple_command_get_last_used(command); | |
| 260 | g_assert_nonnull(last_used); | |
| 261 | ||
| 262 | use_count = purple_command_get_use_count(command); | |
| 263 | g_assert_cmpuint(use_count, ==, 1); | |
| 264 | ||
| 265 | g_assert_finalize_object(command); | |
| 266 | } | |
| 267 | ||
| 268 | static void | |
| 269 | test_purple_command_executev_empty_params(void) { | |
| 270 | PurpleCommand *command = NULL; | |
| 271 | GDateTime *last_used = NULL; | |
| 272 | guint counter = 0; | |
| 273 | guint use_count = 0; | |
| 274 | const char * const expected[] = {NULL}; | |
| 275 | ||
| 276 | command = purple_command_new("command", "test", 0); | |
| 277 | g_signal_connect(command, "executed", | |
| 278 | G_CALLBACK(test_purple_command_executed_counter_cb), | |
| 279 | &counter); | |
| 280 | g_signal_connect(command, "executed", | |
| 281 | G_CALLBACK(test_purple_command_executed_params_cb), | |
| 282 | (gpointer)expected); | |
| 283 | ||
| 284 | purple_command_executev(command, NULL, (GStrv)expected); | |
| 285 | g_assert_cmpuint(counter, ==, 1); | |
| 286 | ||
| 287 | last_used = purple_command_get_last_used(command); | |
| 288 | g_assert_nonnull(last_used); | |
| 289 | ||
| 290 | use_count = purple_command_get_use_count(command); | |
| 291 | g_assert_cmpuint(use_count, ==, 1); | |
| 292 | ||
| 293 | g_assert_finalize_object(command); | |
| 294 | } | |
| 295 | ||
| 296 | static void | |
| 297 | test_purple_command_executev_single_param(void) { | |
| 298 | PurpleCommand *command = NULL; | |
| 299 | GDateTime *last_used = NULL; | |
| 300 | guint counter = 0; | |
| 301 | guint use_count = 0; | |
| 302 | const char * const expected[] = {"param1", NULL}; | |
| 303 | ||
| 304 | command = purple_command_new("command", "test", 0); | |
| 305 | g_signal_connect(command, "executed", | |
| 306 | G_CALLBACK(test_purple_command_executed_counter_cb), | |
| 307 | &counter); | |
| 308 | g_signal_connect(command, "executed", | |
| 309 | G_CALLBACK(test_purple_command_executed_params_cb), | |
| 310 | (gpointer)expected); | |
| 311 | ||
| 312 | purple_command_executev(command, NULL, (GStrv)expected); | |
| 313 | g_assert_cmpuint(counter, ==, 1); | |
| 314 | ||
| 315 | last_used = purple_command_get_last_used(command); | |
| 316 | g_assert_nonnull(last_used); | |
| 317 | ||
| 318 | use_count = purple_command_get_use_count(command); | |
| 319 | g_assert_cmpuint(use_count, ==, 1); | |
| 320 | ||
| 321 | g_assert_finalize_object(command); | |
| 322 | } | |
| 323 | ||
| 324 | static void | |
| 325 | test_purple_command_executev_multiple_params(void) { | |
| 326 | PurpleCommand *command = NULL; | |
| 327 | GDateTime *last_used = NULL; | |
| 328 | guint counter = 0; | |
| 329 | guint use_count = 0; | |
| 330 | const char * const expected[] = {"param1", "param2", "param3", NULL}; | |
| 331 | ||
| 332 | command = purple_command_new("command", "test", 0); | |
| 333 | g_signal_connect(command, "executed", | |
| 334 | G_CALLBACK(test_purple_command_executed_counter_cb), | |
| 335 | &counter); | |
| 336 | g_signal_connect(command, "executed", | |
| 337 | G_CALLBACK(test_purple_command_executed_params_cb), | |
| 338 | (gpointer)expected); | |
| 339 | ||
| 340 | purple_command_executev(command, NULL, (GStrv)expected); | |
| 341 | g_assert_cmpuint(counter, ==, 1); | |
| 342 | ||
| 343 | last_used = purple_command_get_last_used(command); | |
| 344 | g_assert_nonnull(last_used); | |
| 345 | ||
| 346 | use_count = purple_command_get_use_count(command); | |
| 347 | g_assert_cmpuint(use_count, ==, 1); | |
| 348 | ||
| 349 | g_assert_finalize_object(command); | |
| 350 | } | |
| 351 | ||
| 352 | /****************************************************************************** | |
| 353 | * Main | |
| 354 | *****************************************************************************/ | |
| 355 | int | |
| 356 | main(int argc, char *argv[]) { | |
| 357 | g_test_init(&argc, &argv, NULL); | |
| 358 | g_test_set_nonfatal_assertions(); | |
| 359 | ||
| 360 | g_test_add_func("/command/new", test_purple_command_new); | |
| 361 | g_test_add_func("/command/properties", test_purple_command_properties); | |
| 362 | ||
| 363 | g_test_add_func("/command/execute/null-params", | |
| 364 | test_purple_command_execute_null_params); | |
| 365 | g_test_add_func("/command/execute/empty-params", | |
| 366 | test_purple_command_execute_empty_params); | |
| 367 | g_test_add_func("/command/execute/single-param", | |
| 368 | test_purple_command_execute_single_param); | |
| 369 | g_test_add_func("/command/execute/multiple-params", | |
| 370 | test_purple_command_execute_multiple_params); | |
| 371 | ||
| 372 | g_test_add_func("/command/executev/null-params", | |
| 373 | test_purple_command_executev_null_params); | |
| 374 | g_test_add_func("/command/executev/empty-params", | |
| 375 | test_purple_command_executev_empty_params); | |
| 376 | g_test_add_func("/command/executev/single-param", | |
| 377 | test_purple_command_executev_single_param); | |
| 378 | g_test_add_func("/command/executev/multiple-params", | |
| 379 | test_purple_command_executev_multiple_params); | |
| 380 | ||
| 381 | return g_test_run(); | |
| 382 | } |