forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testmod_ccode.cpp
112 lines (95 loc) · 3.84 KB
/
testmod_ccode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "ccode.hpp"
#include <kdbmodule.h>
#include <kdbprivate.h>
#include <tests.hpp>
using std::string;
using CppKeySet = kdb::KeySet;
using CppKey = kdb::Key;
using ckdb::elektraModulesClose;
using ckdb::elektraModulesInit;
using ckdb::elektraPluginClose;
using ckdb::elektraPluginOpen;
using ckdb::keyNew;
using ckdb::ksDel;
using ckdb::Plugin;
CppKeySet defaultConfig ()
{
CppKeySet config{ 20,
keyNew ("user/chars", KEY_END),
keyNew ("user/chars/0A", KEY_VALUE, "6E", KEY_END), // new line -> n
keyNew ("user/chars/20", KEY_VALUE, "77", KEY_END), // space -> w
keyNew ("user/chars/23", KEY_VALUE, "72", KEY_END), // # -> r
keyNew ("user/chars/5C", KEY_VALUE, "62", KEY_END), // \\ (backslash) -> b
keyNew ("user/chars/3D", KEY_VALUE, "65", KEY_END), // = -> e
keyNew ("user/chars/3B", KEY_VALUE, "73", KEY_END), // ; -> s
KS_END };
return config;
}
CppKeySet percentConfig ()
{
CppKeySet config{ 20,
keyNew ("user/chars", KEY_END),
keyNew ("user/chars/0A", KEY_VALUE, "6E", KEY_END), // new line -> n
keyNew ("user/chars/20", KEY_VALUE, "77", KEY_END), // space -> w
keyNew ("user/chars/23", KEY_VALUE, "72", KEY_END), // # -> r
keyNew ("user/chars/5C", KEY_VALUE, "62", KEY_END), // \\ (backslash) -> b
keyNew ("user/chars/3D", KEY_VALUE, "65", KEY_END), // = -> e
keyNew ("user/chars/3B", KEY_VALUE, "73", KEY_END), // ; -> s
keyNew ("user/escape", KEY_VALUE, "25", KEY_END), // use % as escape character
KS_END };
return config;
}
void testEnocdingDecoding (Plugin * const plugin, CppKey const & parent, string const decodedString, string const encodedString = "")
{
CppKeySet keys{ 20, keyNew ("user/tests/ccode/key", KEY_VALUE, decodedString.c_str (), KEY_END), KS_END };
succeed_if_same (plugin->kdbSet (plugin, keys.getKeySet (), *parent), //! OCLint (empty if, too few branches switch)
ELEKTRA_PLUGIN_STATUS_SUCCESS, "Call of `kdbset` was not successful");
if (!encodedString.empty ())
{
CppKey encoded = keys.lookup ("user/tests/ccode/key");
succeed_if_same (encoded.getString (), encodedString, //! OCLint (empty if, too few branches switch)
"String not correctly encoded");
}
succeed_if_same (plugin->kdbGet (plugin, keys.getKeySet (), *parent), //! OCLint (empty if, too few branches switch)
ELEKTRA_PLUGIN_STATUS_SUCCESS, "Call of `kdbGet` was not successful");
CppKey decoded = keys.lookup ("user/tests/ccode/key");
succeed_if_same (decoded.getString (), decodedString, //! OCLint (empty if, too few branches switch)
"String not correctly decoded");
}
void testRoundTrip (string const decodedString, string const encodedString = "", CppKeySet config = defaultConfig ())
{
CppKeySet modules{ 0, KS_END };
elektraModulesInit (modules.getKeySet (), NULL);
CppKey parent{ "system/elektra/modules/type", KEY_END };
Plugin * plugin = elektraPluginOpen ("ccode", modules.getKeySet (), config.getKeySet (), *parent);
exit_if_fail (plugin != NULL, "Could not open ccode plugin"); //! OCLint (empty if, too few branches switch)
testEnocdingDecoding (plugin, parent, decodedString, encodedString);
elektraPluginClose (plugin, 0);
ksDel (modules.release ());
config.release ();
elektraModulesClose (modules.getKeySet (), 0);
}
TEST (type, roundtrip)
{
testRoundTrip ("a value\nwith=;# and \\ itself", "a\\wvalue\\nwith\\e\\s\\r\\wand\\w\\b\\witself");
testRoundTrip ("hello world");
testRoundTrip ("hello world!\nnew line");
testRoundTrip ("\0");
testRoundTrip ("\n");
testRoundTrip ("\\");
testRoundTrip (" ");
testRoundTrip ("=");
testRoundTrip (";");
testRoundTrip ("#");
testRoundTrip (" =;#");
testRoundTrip ("\n\\");
testRoundTrip ("");
testRoundTrip ("a value\nwith=;# and \\ itself", "a%wvalue%nwith%e%s%r%wand%w%b%witself", percentConfig ());
}