LCOV - code coverage report
Current view: top level - src/plugins/yamlcpp - testmod_yamlcpp.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 65 65 100.0 %
Date: 2019-09-12 12:28:41 Functions: 14 22 63.6 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Tests for yamlcpp plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : // -- Imports ------------------------------------------------------------------------------------------------------------------------------
      11             : 
      12             : #include "yamlcpp.hpp"
      13             : 
      14             : #include <kdbmodule.h>
      15             : #include <kdbprivate.h>
      16             : 
      17             : #include <tests.h>
      18             : #include <tests.hpp>
      19             : 
      20             : using ckdb::keyNew;
      21             : 
      22             : using CppKeySet = kdb::KeySet;
      23             : using CppKey = kdb::Key;
      24             : 
      25             : // -- Macros -------------------------------------------------------------------------------------------------------------------------------
      26             : 
      27             : #define OPEN_PLUGIN(parentName, filepath)                                                                                                  \
      28             :         CppKeySet modules{ 0, KS_END };                                                                                                    \
      29             :         CppKeySet config{ 0, KS_END };                                                                                                     \
      30             :         elektraModulesInit (modules.getKeySet (), 0);                                                                                      \
      31             :         CppKey parent{ parentName, KEY_VALUE, filepath, KEY_END };                                                                         \
      32             :         Plugin * plugin = elektraPluginOpen ("yamlcpp", modules.getKeySet (), config.getKeySet (), *parent);                               \
      33             :         exit_if_fail (plugin != NULL, "Could not open yamlcpp plugin")
      34             : 
      35             : #define CLOSE_PLUGIN()                                                                                                                     \
      36             :         elektraPluginClose (plugin, 0);                                                                                                    \
      37             :         elektraModulesClose (modules.getKeySet (), 0);                                                                                     \
      38             :         ksDel (modules.release ());                                                                                                        \
      39             :         config.release ()
      40             : 
      41             : /* We use this prefix in all header files that contain test data. */
      42             : #define PREFIX "user/examples/yamlcpp/"
      43             : 
      44             : // -- Functions ----------------------------------------------------------------------------------------------------------------------------
      45             : 
      46          20 : TEST (yamlcpp, contract)
      47             : {
      48          20 :         OPEN_PLUGIN ("system/elektra/modules/yamlcpp", "file/path");
      49             : 
      50           4 :         CppKeySet keys;
      51           8 :         succeed_if_same (plugin->kdbGet (plugin, keys.getKeySet (), *parent), ELEKTRA_PLUGIN_STATUS_SUCCESS, "Call of `kdbGet` failed");
      52             : 
      53           8 :         CLOSE_PLUGIN ();
      54             : }
      55             : 
      56          28 : void update_parent (CppKeySet expected, string const filepath)
      57             : {
      58             :         // We replace the value of the parent key of expected keyset, if the header file specifies the value @CONFIG_FILEPATH@.
      59             :         // We could also do that via CMake, but the current solution should be easier for now.
      60         168 :         CppKey root = expected.lookup (PREFIX, KDB_O_POP);
      61          28 :         if (root)
      62             :         {
      63          24 :                 if (root.getString () == "@CONFIG_FILEPATH@") root.setString (filepath);
      64             :                 expected.append (root);
      65             :         }
      66          28 : }
      67             : 
      68          12 : static void test_read (string const & filename, CppKeySet expected, int const status = ELEKTRA_PLUGIN_STATUS_SUCCESS)
      69             : #ifdef __llvm__
      70             :         __attribute__ ((annotate ("oclint:suppress")))
      71             : #endif
      72             : {
      73          60 :         string filepath = srcdir_file (filename.c_str ());
      74          48 :         update_parent (expected, filepath);
      75             : 
      76         132 :         OPEN_PLUGIN (PREFIX, filepath.c_str ());
      77             : 
      78          24 :         CppKeySet keys;
      79          48 :         succeed_if_same (plugin->kdbGet (plugin, keys.getKeySet (), *parent), status, "Call of `kdbGet` failed");
      80          24 :         compare_keyset (keys, expected);
      81             : 
      82          48 :         CLOSE_PLUGIN ();
      83             : }
      84             : 
      85          16 : static void test_write_read (CppKeySet expected)
      86             : #ifdef __llvm__
      87             :         __attribute__ ((annotate ("oclint:suppress")))
      88             : #endif
      89             : {
      90          64 :         string filepath = elektraFilename ();
      91          64 :         update_parent (expected, filepath);
      92             : 
      93         176 :         OPEN_PLUGIN (PREFIX, filepath.c_str ());
      94             : 
      95             :         // Write key set to file
      96          80 :         succeed_if_same (plugin->kdbSet (plugin, expected.getKeySet (), *parent), ELEKTRA_PLUGIN_STATUS_SUCCESS, "Unable to write to file");
      97             : 
      98             :         // Read written data
      99          32 :         CppKeySet keySetRead;
     100          64 :         succeed_if_same (plugin->kdbGet (plugin, keySetRead.getKeySet (), *parent), ELEKTRA_PLUGIN_STATUS_SUCCESS,
     101             :                          "Unable to open or parse file");
     102             : 
     103             :         // Compare data
     104          32 :         compare_keyset (keySetRead, expected);
     105             : 
     106             :         // Clean up
     107          64 :         CLOSE_PLUGIN ();
     108             : }
     109             : 
     110          20 : TEST (yamlcpp, flat)
     111             : {
     112          12 :         test_read ("yamlcpp/flat_block_mapping.yaml",
     113             : #include "yamlcpp/flat_block_mapping.h"
     114           2 :         );
     115           6 :         test_write_read (
     116             : #include "yamlcpp/flat_block_mapping.h"
     117           2 :         );
     118             : 
     119          12 :         test_read ("yamlcpp/flat_flow_mapping.yaml",
     120             : #include "yamlcpp/flat_flow_mapping.h"
     121           2 :         );
     122           6 :         test_write_read (
     123             : #include "yamlcpp/flat_flow_mapping.h"
     124           2 :         );
     125           2 : }
     126             : 
     127          20 : TEST (yamlcpp, nested)
     128             : {
     129          12 :         test_read ("yamlcpp/nested_block_mapping.yaml",
     130             : #include "yamlcpp/nested_block_mapping.h"
     131           2 :         );
     132           6 :         test_write_read (
     133             : #include "yamlcpp/nested_block_mapping.h"
     134           2 :         );
     135          12 :         test_read ("yamlcpp/nested_mixed_mapping.yaml",
     136             : #include "yamlcpp/nested_mixed_mapping.h"
     137           2 :         );
     138           6 :         test_write_read (
     139             : #include "yamlcpp/nested_mixed_mapping.h"
     140           2 :         );
     141           2 : }
     142             : 
     143          20 : TEST (yamlcpp, array)
     144             : {
     145          12 :         test_read ("yamlcpp/simple_sequence.yaml",
     146             : #include "yamlcpp/simple_sequence.h"
     147           2 :         );
     148           6 :         test_write_read (
     149             : #include "yamlcpp/simple_sequence.h"
     150           2 :         );
     151             : 
     152          12 :         test_read ("yamlcpp/nested_sequences.yaml",
     153             : #include "yamlcpp/nested_sequences.h"
     154           2 :         );
     155           6 :         test_write_read (
     156             : #include "yamlcpp/nested_sequences.h"
     157           2 :         );
     158           6 :         test_write_read (
     159             : #include "yamlcpp/mapping_with_array_key.h"
     160           2 :         );
     161           6 :         test_write_read (
     162             : #include "yamlcpp/mixed.h"
     163           2 :         );
     164           2 : }
     165             : 
     166             : // -- Main ---------------------------------------------------------------------------------------------------------------------------------
     167             : 
     168           2 : int main (int argc, char * argv[])
     169             : {
     170           2 :         init (argc, argv); // Required for `srcdir_file` to work properly
     171           2 :         ::testing::InitGoogleTest (&argc, argv);
     172             :         return RUN_ALL_TESTS ();
     173           6 : }

Generated by: LCOV version 1.13