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 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 Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) 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 static int
  16 null_message_fn(pcmk__output_t *out, va_list args) {
     /* [previous][next][first][last][top][bottom][index][help] */
  17     return pcmk_rc_ok;
  18 }
  19 
  20 static int
  21 null_message_fn_2(pcmk__output_t *out, va_list args) {
     /* [previous][next][first][last][top][bottom][index][help] */
  22     return pcmk_rc_ok;
  23 }
  24 
  25 static bool
  26 fake_text_init(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  27     return true;
  28 }
  29 
  30 static void
  31 fake_text_free_priv(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  32     /* This function intentionally left blank */
  33 }
  34 
  35 static pcmk__output_t *
  36 mk_fake_text_output(char **argv) {
     /* [previous][next][first][last][top][bottom][index][help] */
  37     pcmk__output_t *retval = calloc(1, sizeof(pcmk__output_t));
  38 
  39     if (retval == NULL) {
  40         return NULL;
  41     }
  42 
  43     retval->fmt_name = "text";
  44     retval->init = fake_text_init;
  45     retval->free_priv = fake_text_free_priv;
  46 
  47     retval->register_message = pcmk__register_message;
  48     retval->message = pcmk__call_message;
  49 
  50     return retval;
  51 }
  52 
  53 static int
  54 setup(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  55     pcmk__register_format(NULL, "text", mk_fake_text_output, NULL);
  56     return 0;
  57 }
  58 
  59 static int
  60 teardown(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  61     pcmk__unregister_formats();
  62     return 0;
  63 }
  64 
  65 static void
  66 null_params(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  67     pcmk__output_t *out = NULL;
  68 
  69     pcmk__output_new(&out, "text", NULL, NULL);
  70 
  71     pcmk__assert_asserts(pcmk__register_message(NULL, "fake", null_message_fn));
  72     pcmk__assert_asserts(pcmk__register_message(out, NULL, null_message_fn));
  73     pcmk__assert_asserts(pcmk__register_message(out, "", null_message_fn));
  74     pcmk__assert_asserts(pcmk__register_message(out, "fake", NULL));
  75 
  76     pcmk__output_free(out);
  77 }
  78 
  79 static void
  80 add_message(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  81     pcmk__output_t *out = NULL;
  82 
  83     pcmk__output_new(&out, "text", NULL, NULL);
  84 
  85     /* For starters, there should be no messages defined. */
  86     assert_int_equal(g_hash_table_size(out->messages), 0);
  87 
  88     /* Add a fake function and check that it's the only item in the hash table. */
  89     pcmk__register_message(out, "fake", null_message_fn);
  90     assert_int_equal(g_hash_table_size(out->messages), 1);
  91     assert_ptr_equal(g_hash_table_lookup(out->messages, "fake"), null_message_fn);
  92 
  93     /* Add a second fake function which should overwrite the first one, leaving
  94      * only one item in the hash table but pointing at the new function.
  95      */
  96     pcmk__register_message(out, "fake", null_message_fn_2);
  97     assert_int_equal(g_hash_table_size(out->messages), 1);
  98     assert_ptr_equal(g_hash_table_lookup(out->messages, "fake"), null_message_fn_2);
  99 
 100     pcmk__output_free(out);
 101 }
 102 
 103 PCMK__UNIT_TEST(NULL, NULL,
 104                 cmocka_unit_test_setup_teardown(null_params, setup, teardown),
 105                 cmocka_unit_test_setup_teardown(add_message, setup, teardown))

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