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

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

DEFINITIONS

This source file includes following definitions.
  1. null_create_fn
  2. null_create_fn_2
  3. invalid_params
  4. add_format

   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 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 static pcmk__output_t *
  16 null_create_fn(char **argv) {
     /* [previous][next][first][last][top][bottom][index][help] */
  17     return NULL;
  18 }
  19 
  20 static pcmk__output_t *
  21 null_create_fn_2(char **argv) {
     /* [previous][next][first][last][top][bottom][index][help] */
  22     return NULL;
  23 }
  24 
  25 static void
  26 invalid_params(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  27     pcmk__assert_asserts(pcmk__register_format(NULL, "fake", NULL, NULL));
  28     pcmk__assert_asserts(pcmk__register_format(NULL, "", null_create_fn, NULL));
  29     pcmk__assert_asserts(pcmk__register_format(NULL, NULL, null_create_fn, NULL));
  30 }
  31 
  32 static void
  33 add_format(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  34     GHashTable *formatters = NULL;
  35     gpointer value;
  36 
  37     /* For starters, there should be no formatters defined. */
  38     assert_null(pcmk__output_formatters());
  39 
  40     /* Add a fake formatter and check that it's the only item in the hash table. */
  41     assert_int_equal(pcmk__register_format(NULL, "fake", null_create_fn, NULL), pcmk_rc_ok);
  42     formatters = pcmk__output_formatters();
  43     assert_int_equal(g_hash_table_size(formatters), 1);
  44 
  45     value = g_hash_table_lookup(formatters, "fake");
  46     assert_ptr_equal(value, null_create_fn);
  47 
  48     /* Add a second fake formatter which should overwrite the first one, leaving
  49      * only one item in the hash table but pointing at the new function.
  50      */
  51     assert_int_equal(pcmk__register_format(NULL, "fake", null_create_fn_2, NULL), pcmk_rc_ok);
  52     formatters = pcmk__output_formatters();
  53     assert_int_equal(g_hash_table_size(formatters), 1);
  54 
  55     value = g_hash_table_lookup(formatters, "fake");
  56     assert_ptr_equal(value, null_create_fn_2);
  57 
  58     pcmk__unregister_formats();
  59 }
  60 
  61 PCMK__UNIT_TEST(NULL, NULL,
  62                 cmocka_unit_test(invalid_params),
  63                 cmocka_unit_test(add_format))

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