forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testmod_yamlcpp.cpp
164 lines (134 loc) · 5.4 KB
/
testmod_yamlcpp.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
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
/**
* @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>
using ckdb::Key;
using ckdb::KeySet;
using ckdb::Plugin;
#include <tests.h>
#include <tests.hpp>
using ckdb::keyNew;
// -- Macros -------------------------------------------------------------------------------------------------------------------------------
#define OPEN_PLUGIN(parentName, filepath) \
kdb::KeySet modules{ 0, KS_END }; \
kdb::KeySet config{ 0, KS_END }; \
elektraModulesInit (modules.getKeySet (), 0); \
kdb::Key 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) //! OCLint (avoid private static members)
#ifdef __llvm__
__attribute__ ((annotate ("oclint:suppress[too few branches in switch statement]"), annotate ("oclint:suppress[empty if statement]")))
#endif
{
OPEN_PLUGIN ("system:/elektra/modules/yamlcpp", "file/path");
kdb::KeySet keys;
succeed_if_same (plugin->kdbGet (plugin, keys.getKeySet (), *parent), ELEKTRA_PLUGIN_STATUS_SUCCESS,
"Unable to retrieve plugin contract");
CLOSE_PLUGIN ();
}
static void test_read (string const & filename, kdb::KeySet expected, int const status = ELEKTRA_PLUGIN_STATUS_SUCCESS)
#ifdef __llvm__
__attribute__ ((annotate ("oclint:suppress")))
#endif
{
string filepath = srcdir_file (filename.c_str ());
OPEN_PLUGIN (PREFIX, filepath.c_str ());
kdb::KeySet 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 (kdb::KeySet expected)
#ifdef __llvm__
__attribute__ ((annotate ("oclint:suppress")))
#endif
{
string filepath = elektraFilename ();
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
kdb::KeySet 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) //! OCLint (avoid private static members)
{
test_read ("yamlcpp/flat_block_mapping.yaml",
#include "yamlcpp/flat_block_mapping.hpp"
);
test_write_read (
#include "yamlcpp/flat_block_mapping.hpp"
);
test_read ("yamlcpp/flat_flow_mapping.yaml",
#include "yamlcpp/flat_flow_mapping.hpp"
);
test_write_read (
#include "yamlcpp/flat_flow_mapping.hpp"
);
}
TEST (yamlcpp, nested) //! OCLint (avoid private static members)
{
test_read ("yamlcpp/nested_block_mapping.yaml",
#include "yamlcpp/nested_block_mapping.hpp"
);
test_write_read (
#include "yamlcpp/nested_block_mapping.hpp"
);
test_read ("yamlcpp/nested_mixed_mapping.yaml",
#include "yamlcpp/nested_mixed_mapping.hpp"
);
test_write_read (
#include "yamlcpp/nested_mixed_mapping.hpp"
);
}
TEST (yamlcpp, array) //! OCLint (avoid private static members)
{
test_read ("yamlcpp/simple_sequence.yaml",
#include "yamlcpp/simple_sequence.hpp"
);
test_write_read (
#include "yamlcpp/simple_sequence.hpp"
);
test_read ("yamlcpp/nested_sequences.yaml",
#include "yamlcpp/nested_sequences.hpp"
);
test_write_read (
#include "yamlcpp/nested_sequences.hpp"
);
test_write_read (
#include "yamlcpp/mapping_with_array_key.hpp"
);
test_write_read (
#include "yamlcpp/mixed.hpp"
);
}
// -- 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 ();
}