LCOV - code coverage report
Current view: top level - src/plugins/regexdispatcher - testmod_regexdispatcher.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 66 66 100.0 %
Date: 2019-09-12 12:28:41 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Tests for regexdispatcher plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #include <stdlib.h>
      11             : #include <string.h>
      12             : 
      13             : #include <kdbconfig.h>
      14             : 
      15             : #include <tests_plugin.h>
      16             : 
      17           1 : static void test_rangedispatcher (void)
      18             : {
      19           1 :         printf ("test_rangedispatcher\n");
      20             : 
      21           1 :         Key * parentKey = keyNew ("user/tests/regexdispatcher", KEY_END);
      22           1 :         KeySet * conf = ksNew (0, KS_END);
      23             : 
      24           1 :         PLUGIN_OPEN ("regexdispatcher");
      25             : 
      26           1 :         KeySet * ks = ksNew (1, KS_END);
      27           1 :         Key * key1 = keyNew ("/key1", KEY_META, "check/range", "0-5000", KEY_END);
      28           1 :         ksAppendKey (ks, key1);
      29             : 
      30           1 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbSet was not successful");
      31             : 
      32           1 :         const Key * pKey1 = ksLookupByName (ks, "/key1", KDB_O_NONE);
      33           1 :         const Key * checkRange1 = keyGetMeta (pKey1, "check/validation");
      34             : 
      35           1 :         succeed_if (checkRange1, "the range regex hasn't been generated");
      36           1 :         succeed_if (0 == strcmp (keyString (checkRange1), "[0-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-4][0-9][0-9][0-9]|5000"),
      37             :                     "the range regex is invalid");
      38             : 
      39           1 :         keyDel (parentKey);
      40           1 :         ksDel (ks);
      41           1 :         PLUGIN_CLOSE ();
      42           1 : }
      43             : 
      44           1 : static void test_enumdispatcher (void)
      45             : {
      46           1 :         printf ("test_enumdispatcher\n");
      47             : 
      48           1 :         Key * parentKey = keyNew ("user/tests/regexdispatcher", KEY_END);
      49           1 :         KeySet * conf = ksNew (0, KS_END);
      50             : 
      51           1 :         PLUGIN_OPEN ("regexdispatcher");
      52             : 
      53           1 :         KeySet * ks = ksNew (1, KS_END);
      54           1 :         Key * key1 = keyNew ("/key1", KEY_META, "check/enum/#1", "a", KEY_META, "check/enum/#2", "b", KEY_END);
      55           1 :         Key * key2 = keyNew ("/key2", KEY_META, "check/enum/#1", "a", KEY_META, "check/enum/#2", "b", KEY_META, "check/enum/multi", ",",
      56             :                              KEY_END);
      57           1 :         ksAppendKey (ks, key1);
      58           1 :         ksAppendKey (ks, key2);
      59             : 
      60           1 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbSet was not successful");
      61             : 
      62           1 :         const Key * pKey1 = ksLookupByName (ks, "/key1", KDB_O_NONE);
      63           1 :         const Key * checkEnum1 = keyGetMeta (pKey1, "check/validation");
      64             : 
      65           1 :         succeed_if (checkEnum1, "the single enum regex hasn't been generated");
      66           1 :         succeed_if (0 == strcmp (keyString (checkEnum1), "a|b"), "the single enum regex is invalid");
      67             : 
      68           1 :         const Key * pKey2 = ksLookupByName (ks, "/key2", KDB_O_NONE);
      69           1 :         const Key * checkEnum2 = keyGetMeta (pKey2, "check/validation");
      70             : 
      71           1 :         succeed_if (checkEnum2, "the multi enum regex hasn't been generated");
      72           1 :         succeed_if (0 == strcmp (keyString (checkEnum2), "(a,b)|(b,a)"), "the mutli enum regex is invalid");
      73             : 
      74           1 :         keyDel (parentKey);
      75           1 :         ksDel (ks);
      76           1 :         PLUGIN_CLOSE ();
      77           1 : }
      78             : 
      79           1 : static void test_defaultdispatcher (void)
      80             : {
      81           1 :         printf ("test_defaultdispatcher\n");
      82             : 
      83           1 :         Key * parentKey = keyNew ("user/tests/regexdispatcher", KEY_END);
      84           1 :         KeySet * conf = ksNew (0, KS_END);
      85             : 
      86           1 :         PLUGIN_OPEN ("regexdispatcher");
      87             : 
      88           1 :         KeySet * ks = ksNew (1, KS_END);
      89           1 :         Key * key = keyNew ("/key", KEY_META, "default", ".\\+*?[^]$(){}=!<>|:-asfdjklö123", KEY_END);
      90           1 :         ksAppendKey (ks, key);
      91             : 
      92           1 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "call to kdbSet was not successful");
      93             : 
      94           1 :         const Key * pKey = ksLookupByName (ks, "/key", KDB_O_NONE);
      95           1 :         const Key * defaultValue = keyGetMeta (pKey, "defaultValue");
      96             : 
      97           1 :         succeed_if (defaultValue, "the default value regex hasn't been generated");
      98           1 :         succeed_if (0 == strcmp (keyString (defaultValue), "\\.\\\\\\+\\*\\?\\[\\^\\]\\$\\(\\)\\{\\}\\=\\!\\<\\>\\|\\:\\-asfdjklö123"),
      99             :                     "the default value regex is invalid");
     100             : 
     101           1 :         keyDel (parentKey);
     102           1 :         ksDel (ks);
     103             : 
     104           1 :         PLUGIN_CLOSE ();
     105           1 : }
     106             : 
     107           1 : int main (int argc, char ** argv)
     108             : {
     109           1 :         printf ("REGEXDISPATCHER     TESTS\n");
     110           1 :         printf ("==================\n\n");
     111             : 
     112           1 :         init (argc, argv);
     113             : 
     114           1 :         test_rangedispatcher ();
     115           1 :         test_enumdispatcher ();
     116           1 :         test_defaultdispatcher ();
     117             : 
     118           1 :         print_result ("testmod_regexdispatcher");
     119             : 
     120           1 :         return nbError;
     121             : }

Generated by: LCOV version 1.13