forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestmod_yamlcpp.cpp
More file actions
175 lines (143 loc) · 5.55 KB
/
Copy pathtestmod_yamlcpp.cpp
File metadata and controls
175 lines (143 loc) · 5.55 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* @file
*
* @brief Tests for yamlcpp plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
// -- Imports ------------------------------------------------------------------------------------------------------------------------------
#include "yamlcpp.hpp"
#include <kdbmodule.h>
#include <kdbprivate.h>
#include <tests.h>
#include <tests.hpp>
using ckdb::keyNew;
using CppKeySet = kdb::KeySet;
using CppKey = kdb::Key;
// -- Macros -------------------------------------------------------------------------------------------------------------------------------
#define OPEN_PLUGIN(parentName, filepath) \
CppKeySet modules{ 0, KS_END }; \
CppKeySet config{ 0, KS_END }; \
elektraModulesInit (modules.getKeySet (), 0); \
CppKey parent{ parentName, KEY_VALUE, filepath, KEY_END }; \
Plugin * plugin = elektraPluginOpen ("yamlcpp", modules.getKeySet (), config.getKeySet (), *parent); \
exit_if_fail (plugin != NULL, "Could not open yamlcpp plugin")
#define CLOSE_PLUGIN() \
elektraPluginClose (plugin, 0); \
elektraModulesClose (modules.getKeySet (), 0); \
ksDel (modules.release ()); \
config.release ()
/* We use this prefix in all header files that contain test data. */
#define PREFIX "user/examples/yamlcpp/"
// -- Functions ----------------------------------------------------------------------------------------------------------------------------
TEST (yamlcpp, contract)
{
OPEN_PLUGIN ("system/elektra/modules/yamlcpp", "file/path");
CppKeySet keys;
succeed_if_same (plugin->kdbGet (plugin, keys.getKeySet (), *parent), ELEKTRA_PLUGIN_STATUS_SUCCESS,
"Unable to retrieve plugin contract");
CLOSE_PLUGIN ();
}
void update_parent (CppKeySet expected, string const filepath)
{
// We replace the value of the parent key of expected keyset, if the header file specifies the value @CONFIG_FILEPATH@.
// We could also do that via CMake, but the current solution should be easier for now.
CppKey root = expected.lookup (PREFIX, KDB_O_POP);
if (root)
{
if (root.getString () == "@CONFIG_FILEPATH@") root.setString (filepath);
expected.append (root);
}
}
static void test_read (string const & filename, CppKeySet expected, int const status = ELEKTRA_PLUGIN_STATUS_SUCCESS)
#ifdef __llvm__
__attribute__ ((annotate ("oclint:suppress")))
#endif
{
string filepath = srcdir_file (filename.c_str ());
update_parent (expected, filepath);
OPEN_PLUGIN (PREFIX, filepath.c_str ());
CppKeySet keys;
succeed_if_same (plugin->kdbGet (plugin, keys.getKeySet (), *parent), status, parent.getMeta<string> ("error/reason"));
compare_keyset (keys, expected);
CLOSE_PLUGIN ();
}
static void test_write_read (CppKeySet expected)
#ifdef __llvm__
__attribute__ ((annotate ("oclint:suppress")))
#endif
{
string filepath = elektraFilename ();
update_parent (expected, filepath);
OPEN_PLUGIN (PREFIX, filepath.c_str ());
// Write key set to file
succeed_if_same (plugin->kdbSet (plugin, expected.getKeySet (), *parent), ELEKTRA_PLUGIN_STATUS_SUCCESS,
parent.getMeta<string> ("error/reason"));
// Read written data
CppKeySet keySetRead;
succeed_if_same (plugin->kdbGet (plugin, keySetRead.getKeySet (), *parent), ELEKTRA_PLUGIN_STATUS_SUCCESS,
parent.getMeta<string> ("error/reason"));
// Compare data
compare_keyset (keySetRead, expected);
// Clean up
CLOSE_PLUGIN ();
}
TEST (yamlcpp, flat)
{
test_read ("yamlcpp/flat_block_mapping.yaml",
#include "yamlcpp/flat_block_mapping.h"
);
test_write_read (
#include "yamlcpp/flat_block_mapping.h"
);
test_read ("yamlcpp/flat_flow_mapping.yaml",
#include "yamlcpp/flat_flow_mapping.h"
);
test_write_read (
#include "yamlcpp/flat_flow_mapping.h"
);
}
TEST (yamlcpp, nested)
{
test_read ("yamlcpp/nested_block_mapping.yaml",
#include "yamlcpp/nested_block_mapping.h"
);
test_write_read (
#include "yamlcpp/nested_block_mapping.h"
);
test_read ("yamlcpp/nested_mixed_mapping.yaml",
#include "yamlcpp/nested_mixed_mapping.h"
);
test_write_read (
#include "yamlcpp/nested_mixed_mapping.h"
);
}
TEST (yamlcpp, array)
{
test_read ("yamlcpp/simple_sequence.yaml",
#include "yamlcpp/simple_sequence.h"
);
test_write_read (
#include "yamlcpp/simple_sequence.h"
);
test_read ("yamlcpp/nested_sequences.yaml",
#include "yamlcpp/nested_sequences.h"
);
test_write_read (
#include "yamlcpp/nested_sequences.h"
);
test_write_read (
#include "yamlcpp/mapping_with_array_key.h"
);
test_write_read (
#include "yamlcpp/mixed.h"
);
}
// -- Main ---------------------------------------------------------------------------------------------------------------------------------
int main (int argc, char * argv[])
{
init (argc, argv); // Required for `srcdir_file` to work properly
::testing::InitGoogleTest (&argc, argv);
return RUN_ALL_TESTS ();
}