LCOV - code coverage report
Current view: top level - tests/gtest-framework/gtest - gtest-elektra.h (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 62 78 79.5 %
Date: 2019-09-12 12:28:41 Functions: 7 9 77.8 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Common Elektra extensions for GTest
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #include "gtest/gtest.h"
      11             : 
      12             : #include <backend.hpp>
      13             : #include <backends.hpp>
      14             : 
      15             : #include <kdbconfig.h>
      16             : 
      17             : namespace testing
      18             : {
      19             : 
      20         116 : class Namespaces
      21             : {
      22             : public:
      23        2900 :         struct Namespace
      24             :         {
      25             :                 std::string name;
      26             :         };
      27             : 
      28         116 :         Namespaces ()
      29         232 :         {
      30         232 :                 Namespace n;
      31         116 :                 n.name = "system";
      32         116 :                 namespaces.push_back (n);
      33             : 
      34         116 :                 n.name = "spec";
      35         116 :                 namespaces.push_back (n);
      36             : 
      37         116 :                 n.name = "user";
      38         116 :                 namespaces.push_back (n);
      39         116 :         }
      40             : 
      41             :         Namespace & operator[] (size_t i)
      42             :         {
      43          24 :                 return namespaces[i];
      44             :         }
      45             : 
      46             :         size_t size ()
      47             :         {
      48           8 :                 return namespaces.size ();
      49             :         }
      50             : 
      51             :         std::vector<Namespace> namespaces;
      52             : };
      53             : 
      54             : /**
      55             :  * @brief Cascading + Spec mountpoint
      56             :  *
      57             :  * Hardcoded with resolver+dump
      58             :  *
      59             :  * useful to quickly mount something in tests
      60             :  * and determine the config file paths (+unlink provided)
      61             :  */
      62             : class Mountpoint
      63             : {
      64             : public:
      65             :         std::string mountpoint;
      66             :         std::string userConfigFile;
      67             :         std::string specConfigFile;
      68             :         std::string systemConfigFile;
      69             :         std::string dirConfigFile; // currently unused, but may disturb tests if present
      70             : 
      71         768 :         Mountpoint (std::string mountpoint_, std::string configFile_) : mountpoint (mountpoint_)
      72             :         {
      73         128 :                 unlink ();
      74         512 :                 mount (mountpoint, configFile_);
      75         384 :                 mount ("spec/" + mountpoint, configFile_);
      76             : 
      77         896 :                 userConfigFile = getConfigFileName ("user", mountpoint);
      78         896 :                 specConfigFile = getConfigFileName ("spec", mountpoint);
      79         896 :                 systemConfigFile = getConfigFileName ("system", mountpoint);
      80         896 :                 dirConfigFile = getConfigFileName ("dir", mountpoint);
      81             :                 // std::cout << "config files are: " << dirConfigFile << " "
      82             :                 //      << userConfigFile << " " << specConfigFile << " "
      83             :                 //      << systemConfigFile << std::endl;
      84         128 :         }
      85             : 
      86         128 :         ~Mountpoint ()
      87         768 :         {
      88         256 :                 umount ("spec/" + mountpoint);
      89         384 :                 umount (mountpoint);
      90         128 :                 unlink ();
      91         128 :         }
      92             : 
      93         256 :         void unlink ()
      94             :         {
      95         512 :                 ::unlink (userConfigFile.c_str ());
      96         512 :                 ::unlink (systemConfigFile.c_str ());
      97         512 :                 ::unlink (specConfigFile.c_str ());
      98         256 :         }
      99             : 
     100         512 :         static std::string getConfigFileName (std::string ns, std::string mp)
     101             :         {
     102             :                 using namespace kdb;
     103             :                 using namespace kdb;
     104             :                 using namespace kdb::tools;
     105             : 
     106             :                 using namespace kdb::tools;
     107             : 
     108        1024 :                 KDB kdb;
     109        2048 :                 Key parent (ns + "/" + mp, KEY_END);
     110        1024 :                 KeySet ks;
     111         512 :                 kdb.get (ks, parent);
     112        1024 :                 return parent.getString ();
     113             :         }
     114             : 
     115         256 :         static void mount (std::string mountpoint_, std::string configFile)
     116             :         {
     117             :                 using namespace kdb;
     118             :                 using namespace kdb::tools;
     119             : 
     120         512 :                 Backend b;
     121        1280 :                 b.setMountpoint (Key (mountpoint_, KEY_END), KeySet (0, KS_END));
     122        1280 :                 b.addPlugin (PluginSpec (KDB_RESOLVER));
     123         512 :                 b.useConfigFile (configFile);
     124        1280 :                 b.addPlugin (PluginSpec ("dump"));
     125        1280 :                 b.addPlugin (PluginSpec ("error"));
     126         512 :                 KeySet ks;
     127         512 :                 KDB kdb;
     128         512 :                 Key parentKey ("system/elektra/mountpoints", KEY_END);
     129         256 :                 kdb.get (ks, parentKey);
     130         256 :                 b.serialize (ks);
     131         256 :                 kdb.set (ks, parentKey);
     132         256 :         }
     133             : 
     134         256 :         static void umount (std::string mountpoint_)
     135             :         {
     136             :                 using namespace kdb;
     137             :                 using namespace kdb::tools;
     138         512 :                 KeySet ks;
     139         512 :                 KDB kdb;
     140         512 :                 Key parentKey ("system/elektra/mountpoints", KEY_END);
     141         256 :                 kdb.get (ks, parentKey);
     142         256 :                 Backends::umount (mountpoint_, ks);
     143         256 :                 kdb.set (ks, parentKey);
     144         256 :         }
     145             : };
     146             : 
     147           0 : std::string makeLiteralString (std::string str)
     148             : {
     149           0 :         std::string ret;
     150           0 :         for (size_t i = 0; i < str.length (); ++i)
     151             :         {
     152           0 :                 if (str[i] == '\\')
     153             :                 {
     154             :                         ret += "\\\\";
     155             :                 }
     156             :                 else
     157             :                 {
     158           0 :                         ret += str[i];
     159             :                 }
     160             :         }
     161           0 :         return ret;
     162             : }
     163             : 
     164             : typedef std::unique_ptr<testing::Mountpoint> MountpointPtr;
     165             : 
     166             : 
     167           0 : void outputGTest (kdb::KeySet tocheck, std::string name)
     168             : {
     169           0 :         std::cout << "ASSERT_EQ(" << name << ".size(), " << tocheck.size () << ") << \"wrong size\" << ks;" << std::endl;
     170           0 :         std::cout << name << ".rewind();" << std::endl;
     171             :         tocheck.rewind ();
     172           0 :         while (tocheck.next ())
     173             :         {
     174           0 :                 std::cout << name << ".next();" << std::endl;
     175           0 :                 std::cout << "EXPECT_EQ(" << name << ".current().getName(), \"" << makeLiteralString (tocheck.current ().getName ())
     176           0 :                           << "\") << \"name of element in keyset wrong\";" << std::endl;
     177           0 :                 std::cout << "EXPECT_EQ(" << name << ".current().getString(), \"" << makeLiteralString (tocheck.current ().getString ())
     178           0 :                           << "\") << \"string of element in keyset wrong\";" << std::endl;
     179             :         }
     180           0 : }
     181             : } // namespace testing

Generated by: LCOV version 1.13