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

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Tests for simpleini 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             : 
      18           6 : static void test_readFormat (const char * format, const char * fileContent, int numKeys, const char ** keys, const char ** values)
      19             : {
      20           6 :         const char * tmpFile = elektraFilename ();
      21           6 :         FILE * fh = fopen (tmpFile, "w");
      22           6 :         if (fh)
      23             :         {
      24           6 :                 fputs (fileContent, fh);
      25           6 :                 fclose (fh);
      26             :         }
      27             : 
      28           6 :         Key * parentKey = keyNew ("user/tests/simpleini", KEY_VALUE, tmpFile, KEY_END);
      29           6 :         KeySet * conf = 0;
      30           6 :         if (format)
      31             :         {
      32           4 :                 conf = ksNew (1, keyNew ("system/format", KEY_VALUE, format, KEY_END), KS_END);
      33             :         }
      34             :         else
      35             :         {
      36           2 :                 conf = ksNew (0, KS_END);
      37             :         }
      38             : 
      39           6 :         PLUGIN_OPEN ("simpleini");
      40             : 
      41           6 :         KeySet * ks = ksNew (numKeys, KS_END);
      42           6 :         Key * key = 0;
      43             : 
      44           6 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
      45             : 
      46             :         Key * lookup = 0;
      47          24 :         for (int i = 0; i < numKeys; i++)
      48             :         {
      49          24 :                 lookup = keyNew ("user/tests/simpleini", KEY_END);
      50          24 :                 keyAddBaseName (lookup, keys[i]);
      51          24 :                 printf ("testing key '%s'\n", keyBaseName (lookup));
      52          24 :                 succeed_if ((key = ksLookup (ks, lookup, 0)) != NULL, "key not found");
      53          24 :                 succeed_if (strcmp (values[i], keyString (key)) == 0, "value of key did not match");
      54          24 :                 keyDel (lookup);
      55             :         }
      56             : 
      57           6 :         keyDel (key);
      58           6 :         ksDel (ks);
      59           6 :         keyDel (parentKey);
      60           6 :         PLUGIN_CLOSE ();
      61           6 : }
      62             : 
      63          10 : static void test_formatNotAccepted (const char * format)
      64             : {
      65          10 :         Key * parentKey = keyNew ("user/tests/simpleini", KEY_VALUE, elektraFilename (), KEY_END);
      66          10 :         KeySet * conf = ksNew (1, keyNew ("system/format", KEY_VALUE, format, KEY_END), KS_END);
      67          10 :         PLUGIN_OPEN ("simpleini");
      68             : 
      69          10 :         KeySet * ks = ksNew (0, KS_END);
      70             : 
      71          10 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) != 1, "kdbGet was successful for an invalid format");
      72             : 
      73          10 :         ksDel (ks);
      74          10 :         keyDel (parentKey);
      75          10 :         PLUGIN_CLOSE ();
      76          10 : }
      77             : 
      78             : 
      79           2 : static void test_readDefaultFormat (void)
      80             : {
      81           2 :         const char * expected_keys[] = { "key1", "key2", "key3", "key4" };
      82           2 :         const char * expected_values[] = { "value1", "value2", "value3", "value4 " };
      83             : 
      84           2 :         test_readFormat (NULL, // format
      85             :                          "key1 = value1\n"
      86             :                          " key2 = value2\n"
      87             :                          "key3  = value3\n"
      88             :                          "key4 = value4 \n",
      89             :                          4, expected_keys, expected_values);
      90           2 : }
      91             : 
      92           2 : static void test_readFormat_wo_space (void)
      93             : {
      94             : 
      95           2 :         const char * expected_keys[] = { "key1", "key2", "key3", "key4" };
      96           2 :         const char * expected_values[] = { "value1", "value2", "value3", " value4 " };
      97             : 
      98           2 :         test_readFormat ("%=%", // format
      99             :                          "key1=value1\n"
     100             :                          " key2=value2\n"
     101             :                          "key3 =value3\n"
     102             :                          "key4= value4 \n",
     103             :                          4, expected_keys, expected_values);
     104           2 : }
     105             : 
     106           2 : static void test_readFormat_special1 (void)
     107             : {
     108             : 
     109           2 :         const char * expected_keys[] = { "key1", "key2", "key3", "key4" };
     110           2 :         const char * expected_values[] = { "value1", "value2", "value3", " value4 " };
     111             : 
     112           2 :         test_readFormat ("% :=_%", // format
     113             :                          "key1 :=_value1\n"
     114             :                          " key2 :=_value2\n"
     115             :                          "key3  :=_value3\n"
     116             :                          "key4 :=_ value4 \n",
     117             :                          4, expected_keys, expected_values);
     118           2 : }
     119             : 
     120             : 
     121           2 : int main (int argc, char ** argv)
     122             : {
     123           2 :         printf ("SIMPLEINI    TESTS\n");
     124           2 :         printf ("==================\n\n");
     125             : 
     126           2 :         init (argc, argv);
     127             : 
     128           2 :         test_readDefaultFormat ();
     129           2 :         test_readFormat_wo_space ();
     130           2 :         test_readFormat_special1 ();
     131           2 :         test_formatNotAccepted ("%");
     132           2 :         test_formatNotAccepted ("");
     133           2 :         test_formatNotAccepted ("%%%");
     134           2 :         test_formatNotAccepted ("%% %");
     135           2 :         test_formatNotAccepted ("%% %%");
     136             : 
     137             : 
     138           2 :         print_result ("testmod_simpleini");
     139             : 
     140           2 :         return nbError;
     141             : }

Generated by: LCOV version 1.13