LCOV - code coverage report
Current view: top level - src/plugins/internalnotification/macros - create_type_tests.h (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 35 35 100.0 %
Date: 2019-09-12 12:28:41 Functions: 55 55 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @copyright BSD License (see doc/COPYING or http://www.libelektra.org)
       3             :  *
       4             :  * @brief Create test cases for internalnotification type.
       5             :  *
       6             :  * This supermacro creates the following functions:
       7             :  * - int internalnotificationRegisterTYPE_NAME (Plugin * plugin, Key * key, TYPE * variable)
       8             :  * - static void test_updateTYPE_NAME (void)
       9             :  * - static void test_noUpdateTYPE_NAME (void) (only if INVALID_VALUE is defined)
      10             :  *
      11             :  * @param  TYPE           valid C type (e.g. int or kdb_short_t)
      12             :  * @param  TYPE_NAME      name suffix for the functions (e.g. Int or UnsignedLong)
      13             :  * @param  TEST_VALUE     value of type TYPE. Used for the "update" test case
      14             :  * @param  FORMAT_STRING  format to convert TEST_VALUE to string (passed to elektraFormat())
      15             :  * @param  CHECK_VALUE    optional, default is (value == TEST_VALUE). Boolean expression to check if `value` equals the test value
      16             :  * @param  INVALID_VALUE  optional. Value of type string. Used for the no update test case. If not defined, "no update" test case is
      17             :  * omitted
      18             :  * @param  CHECK_INVALID  optioal, defaults to (value == 0). Check if the variable `value` has not been updated. Value should be 0.
      19             :  */
      20             : #ifndef TYPE
      21             : #error "You have to #define TYPE, TYPE_NAME, FORMAT_STRING, TEST_VALUE and CHECK_VALUE before including the create_type_tests supermacro"
      22             : #endif
      23             : #ifndef TYPE_NAME
      24             : #error "You have to #define TYPE, TYPE_NAME, FORMAT_STRING, TEST_VALUE and CHECK_VALUE before including the create_type_tests supermacro"
      25             : #endif
      26             : #ifndef FORMAT_STRING
      27             : #error "You have to #define TYPE, TYPE_NAME, FORMAT_STRING, TEST_VALUE and CHECK_VALUE before including the create_type_tests supermacro"
      28             : #endif
      29             : #ifndef TEST_VALUE
      30             : #error "You have to #define TYPE, TYPE_NAME, FORMAT_STRING, TEST_VALUE and CHECK_VALUE before including the create_type_tests supermacro"
      31             : #endif
      32             : #ifndef CHECK_VALUE
      33             : #define CHECK_VALUE (value == TEST_VALUE)
      34             : #endif
      35             : #ifndef CHECK_INVALID
      36             : #define CHECK_INVALID (value == 0)
      37             : #endif
      38             : 
      39             : #define REGISTER_FUNC_NAME(TYPE_NAME) ELEKTRA_CONCAT (internalnotificationRegister, TYPE_NAME)
      40             : 
      41             : #define TEST_CASE_UPDATE_SIGNATURE(TYPE_NAME) static void TEST_CASE_UPDATE_NAME (TYPE_NAME) (void)
      42             : #define TEST_CASE_NO_UPDATE_SIGNATURE(TYPE_NAME) static void TEST_CASE_NO_UPDATE_NAME (TYPE_NAME) (void)
      43             : 
      44          72 : static int REGISTER_FUNC_NAME (TYPE_NAME) (Plugin * plugin, Key * key, TYPE * variable)
      45             : {
      46          72 :         size_t address = elektraPluginGetFunction (plugin, ELEKTRA_STRINGIFY (ELEKTRA_CONCAT (register, TYPE_NAME)));
      47          72 :         if (!address) yield_error ("function not exported");
      48             : 
      49             :         /* register key with plugin */
      50             :         ELEKTRA_NOTIFICATION_REGISTERFUNC_TYPEDEF (RegisterFuncType, TYPE)
      51          72 :         return ((RegisterFuncType) address) (plugin, key, variable);
      52             : }
      53             : 
      54          38 : TEST_CASE_UPDATE_SIGNATURE (TYPE_NAME)
      55             : {
      56          38 :         printf ("test update\n");
      57          38 :         KeySet * conf = ksNew (0, KS_END);
      58          38 :         PLUGIN_OPEN ("internalnotification");
      59          38 :         Key * registeredKey = keyNew ("/test/internalnotification/value", KEY_END);
      60          38 :         TYPE value = 0;
      61          38 :         succeed_if (REGISTER_FUNC_NAME (TYPE_NAME) (plugin, registeredKey, &value) == 1, "registration was not successful");
      62          38 :         char * valueStr = elektraFormat (FORMAT_STRING, TEST_VALUE);
      63          38 :         Key * valueKey = keyNew ("user/test/internalnotification/value", KEY_VALUE, valueStr, KEY_END);
      64          38 :         KeySet * ks = ksNew (1, valueKey, KS_END);
      65          38 :         elektraInternalnotificationUpdateRegisteredKeys (plugin, ks);
      66          38 :         succeed_if (CHECK_VALUE, "registered value was not updated");
      67          38 :         free (valueStr);
      68          38 :         keyDel (registeredKey);
      69          38 :         ksDel (ks);
      70          38 :         PLUGIN_CLOSE ();
      71          38 : }
      72             : 
      73             : #ifdef INVALID_VALUE
      74          34 : TEST_CASE_NO_UPDATE_SIGNATURE (TYPE_NAME)
      75             : {
      76          34 :         printf ("test no update with invalid value\n");
      77          34 :         KeySet * conf = ksNew (0, KS_END);
      78          34 :         PLUGIN_OPEN ("internalnotification");
      79          34 :         Key * valueKey = keyNew ("user/test/internalnotification/value", KEY_END);
      80          34 :         KeySet * ks = ksNew (1, valueKey, KS_END);
      81          34 :         TYPE value = 0;
      82          34 :         succeed_if (REGISTER_FUNC_NAME (TYPE_NAME) (plugin, valueKey, &value) == 1, "registration was not successful");
      83          34 :         keySetString (valueKey, INVALID_VALUE);
      84          34 :         elektraInternalnotificationUpdateRegisteredKeys (plugin, ks);
      85          34 :         succeed_if (CHECK_INVALID, "registered value was updated");
      86          34 :         ksDel (ks);
      87          34 :         PLUGIN_CLOSE ();
      88          34 : }
      89             : #endif
      90             : 
      91             : #undef TYPE
      92             : #undef TYPE_NAME
      93             : #undef FORMAT_STRING
      94             : #undef TEST_VALUE
      95             : #undef CHECK_VALUE
      96             : #undef CHECK_INVALID
      97             : #undef INVALID_VALUE

Generated by: LCOV version 1.13