LCOV - code coverage report
Current view: top level - src/plugins/directoryvalue - testmod_directoryvalue.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 58 58 100.0 %
Date: 2019-09-12 12:28:41 Functions: 17 29 58.6 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Tests for directoryvalue plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #include "directoryvalue.hpp"
      11             : 
      12             : #include <tuple>
      13             : 
      14             : #include <kdbmodule.h>
      15             : #include <kdbprivate.h>
      16             : 
      17             : #include <tests.hpp>
      18             : 
      19             : using std::ignore;
      20             : using std::tie;
      21             : 
      22             : using ckdb::keyNew;
      23             : 
      24             : using CppKeySet = kdb::KeySet;
      25             : using CppKey = kdb::Key;
      26             : 
      27             : using elektra::increaseArrayIndices;
      28             : using elektra::splitArrayParentsOther;
      29             : 
      30             : // -- Macros -------------------------------------------------------------------------------------------------------------------------------
      31             : 
      32             : #define OPEN_PLUGIN(parentName, filepath)                                                                                                  \
      33             :         CppKeySet modules{ 0, KS_END };                                                                                                    \
      34             :         CppKeySet config{ 0, KS_END };                                                                                                     \
      35             :         elektraModulesInit (modules.getKeySet (), 0);                                                                                      \
      36             :         CppKey parent{ parentName, KEY_VALUE, filepath, KEY_END };                                                                         \
      37             :         Plugin * plugin = elektraPluginOpen ("directoryvalue", modules.getKeySet (), config.getKeySet (), *parent);                        \
      38             :         exit_if_fail (plugin != NULL, "Could not open directoryvalue plugin");
      39             : 
      40             : #define CLOSE_PLUGIN()                                                                                                                     \
      41             :         ksDel (modules.release ());                                                                                                        \
      42             :         config.release ();                                                                                                                 \
      43             :         elektraPluginClose (plugin, 0);                                                                                                    \
      44             :         elektraModulesClose (modules.getKeySet (), 0)
      45             : 
      46             : #define PREFIX "user/tests/directoryvalue/"
      47             : 
      48             : // -- Functions ----------------------------------------------------------------------------------------------------------------------------
      49             : 
      50          10 : void test_set (CppKeySet keys, CppKeySet expected, int const status = ELEKTRA_PLUGIN_STATUS_SUCCESS)
      51             : #ifdef __llvm__
      52             :         __attribute__ ((annotate ("oclint:suppress[high ncss method]"), annotate ("oclint:suppress[empty if statement]"),
      53             :                         annotate ("oclint:suppress[too few branches in switch statement]")))
      54             : #endif
      55             : {
      56         100 :         OPEN_PLUGIN (PREFIX, "file/path");
      57             : 
      58          50 :         succeed_if_same (plugin->kdbSet (plugin, keys.getKeySet (), *parent), status, "Call of `kdbSet` failed");
      59          20 :         compare_keyset (expected, keys);
      60             : 
      61          30 :         CLOSE_PLUGIN ();
      62             : }
      63             : 
      64           6 : void test_get (CppKeySet keys, CppKeySet expected, int const status = ELEKTRA_PLUGIN_STATUS_SUCCESS)
      65             : #ifdef __llvm__
      66             :         __attribute__ ((annotate ("oclint:suppress[high ncss method]"), annotate ("oclint:suppress[empty if statement]"),
      67             :                         annotate ("oclint:suppress[too few branches in switch statement]")))
      68             : #endif
      69             : {
      70          60 :         OPEN_PLUGIN (PREFIX, "file/path");
      71             : 
      72          30 :         succeed_if_same (plugin->kdbGet (plugin, keys.getKeySet (), *parent), status, "Call of `kdbGet` failed");
      73          12 :         compare_keyset (expected, keys);
      74             : 
      75          18 :         CLOSE_PLUGIN ();
      76             : }
      77             : 
      78           8 : void test_roundtrip (CppKeySet keys, int const status = ELEKTRA_PLUGIN_STATUS_SUCCESS)
      79             : #ifdef __llvm__
      80             :         __attribute__ ((annotate ("oclint:suppress[high ncss method]"), annotate ("oclint:suppress[empty if statement]"),
      81             :                         annotate ("oclint:suppress[too few branches in switch statement]")))
      82             : #endif
      83             : {
      84          24 :         CppKeySet input = keys.dup ();
      85             : 
      86          80 :         OPEN_PLUGIN (PREFIX, "file/path");
      87             : 
      88          40 :         succeed_if_same (plugin->kdbSet (plugin, keys.getKeySet (), *parent), status, "Call of `kdbSet` failed");
      89          40 :         succeed_if_same (plugin->kdbGet (plugin, keys.getKeySet (), *parent), status, "Call of `kdbGet` failed");
      90          16 :         compare_keyset (input, keys);
      91             : 
      92          24 :         CLOSE_PLUGIN ();
      93             : }
      94             : 
      95             : // -- Tests --------------------------------------------------------------------------------------------------------------------------------
      96             : 
      97          20 : TEST (directoryvalue, splitArrayParentsOther)
      98             : {
      99             :         CppKeySet input{ 10,
     100             :                          keyNew (PREFIX "key", KEY_END),
     101             :                          keyNew (PREFIX "key/map", KEY_END),
     102             :                          keyNew (PREFIX "key/array", KEY_END),
     103             :                          keyNew (PREFIX "key/array/#0", KEY_END),
     104             :                          keyNew (PREFIX "key/array/#1", KEY_END),
     105             :                          keyNew (PREFIX "key/array/#2/nested", KEY_END),
     106             :                          keyNew (PREFIX "key/array/#2/nested/#0", KEY_END),
     107             :                          keyNew (PREFIX "key/array/#2/nested/#1", KEY_END),
     108             :                          keyNew (PREFIX "key/empty/array", KEY_META, "array", "", KEY_END),
     109           4 :                          KS_END };
     110             : 
     111             :         CppKeySet expected{ 10, keyNew (PREFIX "key/array", KEY_END), keyNew (PREFIX "key/array/#2/nested", KEY_END),
     112           4 :                             keyNew (PREFIX "key/empty/array", KEY_META, "array", "", KEY_END), KS_END };
     113             : 
     114           4 :         CppKeySet arrays;
     115           8 :         tie (arrays, ignore) = splitArrayParentsOther (input);
     116           4 :         compare_keyset (expected, arrays);
     117             : 
     118           6 :         input = CppKeySet{ 10, keyNew (PREFIX "key", KEY_END), KS_END };
     119           6 :         expected = input.dup ();
     120           8 :         tie (ignore, input) = splitArrayParentsOther (input);
     121           4 :         compare_keyset (expected, input);
     122             : }
     123             : 
     124          20 : TEST (directoryvalue, increaseArrayIndices)
     125             : {
     126           4 :         CppKeySet arrayParents{ 10, keyNew (PREFIX "key/array", KEY_END), keyNew (PREFIX "key/array/#2/nested", KEY_END), KS_END };
     127             : 
     128           4 :         CppKeySet expectedArrayParents{ 10, keyNew (PREFIX "key/array", KEY_END), keyNew (PREFIX "key/array/#3/nested", KEY_END), KS_END };
     129             : 
     130             :         CppKeySet arrays{ 10,
     131             :                           keyNew (PREFIX "key/array", KEY_END),
     132             :                           keyNew (PREFIX "key/array/#0", KEY_END),
     133             :                           keyNew (PREFIX "key/array/#1", KEY_END),
     134             :                           keyNew (PREFIX "key/array/#2/nested", KEY_END),
     135             :                           keyNew (PREFIX "key/array/#2/nested/#0", KEY_END),
     136             :                           keyNew (PREFIX "key/array/#2/nested/#1", KEY_END),
     137             :                           keyNew (PREFIX "key/array", KEY_END),
     138             :                           keyNew (PREFIX "key/array/#0", KEY_END),
     139             :                           keyNew (PREFIX "key/array/#1", KEY_END),
     140           4 :                           KS_END };
     141             : 
     142             :         CppKeySet expectedArrays{ 10,
     143             :                                   keyNew (PREFIX "key/array", KEY_END),
     144             :                                   keyNew (PREFIX "key/array/#1", KEY_END),
     145             :                                   keyNew (PREFIX "key/array/#2", KEY_END),
     146             :                                   keyNew (PREFIX "key/array/#3/nested", KEY_END),
     147             :                                   keyNew (PREFIX "key/array/#3/nested/#1", KEY_END),
     148             :                                   keyNew (PREFIX "key/array/#3/nested/#2", KEY_END),
     149             :                                   keyNew (PREFIX "key/array", KEY_END),
     150             :                                   keyNew (PREFIX "key/array/#1", KEY_END),
     151             :                                   keyNew (PREFIX "key/array/#2", KEY_END),
     152           4 :                                   KS_END };
     153             : 
     154           8 :         tie (arrayParents, arrays) = increaseArrayIndices (arrayParents, arrays);
     155           4 :         compare_keyset (expectedArrays, arrays);
     156           4 :         compare_keyset (expectedArrayParents, arrayParents);
     157             : }
     158             : 
     159          20 : TEST (directoryvalue, basics)
     160             : {
     161          20 :         OPEN_PLUGIN ("system/elektra/modules/directoryvalue", "")
     162             : 
     163           4 :         CppKeySet keys{ 0, KS_END };
     164          10 :         succeed_if_same (plugin->kdbGet (plugin, keys.getKeySet (), *parent), ELEKTRA_PLUGIN_STATUS_SUCCESS,
     165             :                          "Unable to retrieve plugin contract");
     166             : 
     167           6 :         CLOSE_PLUGIN ();
     168             : }
     169             : 
     170          20 : TEST (directoryvalue, get)
     171             : {
     172             :         test_get (
     173             : #include "directoryvalue/simple_set.hpp"
     174             :                 ,
     175             : #include "directoryvalue/simple_get.hpp"
     176           2 :         );
     177             :         test_get (
     178             : #include "directoryvalue/arrays_set.hpp"
     179             :                 ,
     180             : #include "directoryvalue/arrays_get.hpp"
     181           2 :         );
     182             :         test_get (
     183             : #include "directoryvalue/mixed_set.hpp"
     184             :                 ,
     185             : #include "directoryvalue/mixed_get.hpp"
     186           2 :         );
     187           2 : }
     188             : 
     189          20 : TEST (directoryvalue, set)
     190             : {
     191             :         test_set (
     192             : #include "directoryvalue/empty.hpp"
     193             :                 ,
     194             : #include "directoryvalue/empty.hpp"
     195           2 :                 , ELEKTRA_PLUGIN_STATUS_NO_UPDATE);
     196             : 
     197             :         test_set (
     198             : #include "directoryvalue/simple_get.hpp"
     199             :                 ,
     200             : #include "directoryvalue/simple_set.hpp"
     201           2 :         );
     202             : 
     203             :         test_set (
     204             : #include "directoryvalue/extended_get.hpp"
     205             :                 ,
     206             : #include "directoryvalue/extended_set.hpp"
     207           2 :         );
     208             : 
     209             :         test_set (
     210             : #include "directoryvalue/arrays.hpp"
     211             :                 ,
     212             : #include "directoryvalue/arrays_set.hpp"
     213           2 :         );
     214             : 
     215             :         test_set (
     216             : #include "directoryvalue/mixed_get.hpp"
     217             :                 ,
     218             : #include "directoryvalue/mixed_set.hpp"
     219           2 :         );
     220           2 : }
     221             : 
     222          20 : TEST (directoryvalue, roundtrip)
     223             : {
     224             :         test_roundtrip (
     225             : #include "directoryvalue/simple_get.hpp"
     226           2 :         );
     227             :         test_roundtrip (
     228             : #include "directoryvalue/extended_get.hpp"
     229           2 :         );
     230             :         test_roundtrip (
     231             : #include "directoryvalue/arrays_get.hpp"
     232           2 :         );
     233             :         test_roundtrip (
     234             : #include "directoryvalue/mixed_get.hpp"
     235           2 :         );
     236           8 : }

Generated by: LCOV version 1.13