root/lib/common/tests/output/pcmk__register_message_test.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. null_message_fn
  2. null_message_fn_2
  3. fake_text_init
  4. fake_text_free_priv
  5. mk_fake_text_output
  6. setup
  7. teardown
  8. null_params
  9. add_message

   1 /*
   2  * Copyright 2022-2023 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU General Public License version 2
   7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <crm/common/unittest_internal.h>
  13 #include <crm/common/output_internal.h>
  14 
  15 #include "../../crmcommon_private.h"
  16 
  17 static int
  18 null_message_fn(pcmk__output_t *out, va_list args) {
     /* [previous][next][first][last][top][bottom][index][help] */
  19     return pcmk_rc_ok;
  20 }
  21 
  22 static int
  23 null_message_fn_2(pcmk__output_t *out, va_list args) {
     /* [previous][next][first][last][top][bottom][index][help] */
  24     return pcmk_rc_ok;
  25 }
  26 
  27 static bool
  28 fake_text_init(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  29     return true;
  30 }
  31 
  32 static void
  33 fake_text_free_priv(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  34     /* This function intentionally left blank */
  35 }
  36 
  37 static pcmk__output_t *
  38 mk_fake_text_output(char **argv) {
     /* [previous][next][first][last][top][bottom][index][help] */
  39     pcmk__output_t *retval = calloc(1, sizeof(pcmk__output_t));
  40 
  41     if (retval == NULL) {
  42         return NULL;
  43     }
  44 
  45     retval->fmt_name = "text";
  46     retval->init = fake_text_init;
  47     retval->free_priv = fake_text_free_priv;
  48 
  49     retval->register_message = pcmk__register_message;
  50     retval->message = pcmk__call_message;
  51 
  52     return retval;
  53 }
  54 
  55 static int
  56 setup(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  57     pcmk__register_format(NULL, "text", mk_fake_text_output, NULL);
  58     return 0;
  59 }
  60 
  61 static int
  62 teardown(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  63     pcmk__unregister_formats();
  64     return 0;
  65 }
  66 
  67 static void
  68 null_params(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  69     pcmk__output_t *out = NULL;
  70 
  71     pcmk__output_new(&out, "text", NULL, NULL);
  72 
  73     pcmk__assert_asserts(pcmk__register_message(NULL, "fake", null_message_fn));
  74     pcmk__assert_asserts(pcmk__register_message(out, NULL, null_message_fn));
  75     pcmk__assert_asserts(pcmk__register_message(out, "", null_message_fn));
  76     pcmk__assert_asserts(pcmk__register_message(out, "fake", NULL));
  77 
  78     pcmk__output_free(out);
  79 }
  80 
  81 static void
  82 add_message(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  83     pcmk__output_t *out = NULL;
  84 
  85     pcmk__bare_output_new(&out, "text", NULL, NULL);
  86 
  87     /* For starters, there should be no messages defined. */
  88     assert_int_equal(g_hash_table_size(out->messages), 0);
  89 
  90     /* Add a fake function and check that it's the only item in the hash table. */
  91     pcmk__register_message(out, "fake", null_message_fn);
  92     assert_int_equal(g_hash_table_size(out->messages), 1);
  93     assert_ptr_equal(g_hash_table_lookup(out->messages, "fake"), null_message_fn);
  94 
  95     /* Add a second fake function which should overwrite the first one, leaving
  96      * only one item in the hash table but pointing at the new function.
  97      */
  98     pcmk__register_message(out, "fake", null_message_fn_2);
  99     assert_int_equal(g_hash_table_size(out->messages), 1);
 100     assert_ptr_equal(g_hash_table_lookup(out->messages, "fake"), null_message_fn_2);
 101 
 102     pcmk__output_free(out);
 103 }
 104 
 105 PCMK__UNIT_TEST(NULL, NULL,
 106                 cmocka_unit_test_setup_teardown(null_params, setup, teardown),
 107                 cmocka_unit_test_setup_teardown(add_message, setup, teardown))

/* [previous][next][first][last][top][bottom][index][help] */