Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Tests for yambi plugin
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : *
8 : */
9 :
10 : #include "yambi.hpp"
11 :
12 : #include <kdbmodule.h>
13 : #include <kdbprivate.h>
14 :
15 : #include <tests.h>
16 : #include <tests.hpp>
17 :
18 : using ckdb::keyNew;
19 :
20 : using CppKeySet = kdb::KeySet;
21 : using CppKey = kdb::Key;
22 :
23 :
24 : // -- Macros -------------------------------------------------------------------------------------------------------------------------------
25 :
26 : #define OPEN_PLUGIN(parentName, filepath) \
27 : CppKeySet modules{ 0, KS_END }; \
28 : elektraModulesInit (modules.getKeySet (), 0); \
29 : CppKeySet config{ 0, KS_END }; \
30 : CppKey parent{ parentName, KEY_VALUE, filepath, KEY_END }; \
31 : Plugin * plugin = elektraPluginOpen ("yambi", modules.getKeySet (), config.getKeySet (), *parent);
32 :
33 : #define CLOSE_PLUGIN() \
34 : config.release (); \
35 : elektraPluginClose (plugin, 0); \
36 : elektraModulesClose (modules.getKeySet (), 0)
37 :
38 : #define PREFIX "user/tests/yambi/"
39 :
40 : // -- Functions ----------------------------------------------------------------------------------------------------------------------------
41 :
42 26 : void test_read (string const & filepath, CppKeySet expected)
43 : #ifdef __llvm__
44 : __attribute__ ((annotate ("oclint:suppress[high ncss method]"), annotate ("oclint:suppress[empty if statement]"),
45 : annotate ("oclint:suppress[too few branches in switch statement]")))
46 : #endif
47 : {
48 234 : OPEN_PLUGIN (PREFIX, srcdir_file (filepath.c_str ()));
49 :
50 : // We replace the value of the parent key of expected keyset, if the header file specifies the value @CONFIG_FILEPATH@.
51 : // We could also do that via CMake, but the current solution should be easier for now.
52 156 : CppKey root = expected.lookup (PREFIX, KDB_O_POP);
53 26 : if (root)
54 : {
55 68 : if (root.getString () == "@CONFIG_FILEPATH@") root.setString (srcdir_file (filepath.c_str ()));
56 : expected.append (root);
57 : }
58 :
59 52 : CppKeySet keys;
60 104 : succeed_if_same (plugin->kdbGet (plugin, keys.getKeySet (), *parent), ELEKTRA_PLUGIN_STATUS_SUCCESS, "Call of `kdbGet` failed");
61 52 : compare_keyset (expected, keys);
62 :
63 52 : CLOSE_PLUGIN ();
64 : }
65 :
66 : // -- Tests --------------------------------------------------------------------------------------------------------------------------------
67 :
68 20 : TEST (yambi, basics)
69 : {
70 16 : OPEN_PLUGIN ("system/elektra/modules/yambi", "file/path");
71 :
72 4 : CppKeySet keys;
73 8 : succeed_if_same (plugin->kdbGet (plugin, keys.getKeySet (), *parent), ELEKTRA_PLUGIN_STATUS_SUCCESS, "Call of `kdbGet` failed");
74 :
75 8 : succeed_if_same (plugin->kdbSet (plugin, keys.getKeySet (), *parent), ELEKTRA_PLUGIN_STATUS_NO_UPDATE, "Call of `kdbSet` failed");
76 :
77 4 : CLOSE_PLUGIN ();
78 : }
79 :
80 :
81 20 : TEST (yambi, empty)
82 : {
83 8 : test_read ("yambi/null.yaml",
84 : #include "yambi/null.hpp"
85 2 : );
86 8 : test_read ("yambi/comment.yaml",
87 : #include "yambi/null.hpp"
88 2 : );
89 2 : }
90 :
91 20 : TEST (yambi, scalar)
92 : {
93 8 : test_read ("yambi/plain_scalar-word_chars.yaml",
94 : #include "yambi/plain_scalar-word_chars.hpp"
95 2 : );
96 8 : test_read ("yambi/plain_scalar-word_chars_space.yaml",
97 : #include "yambi/plain_scalar-word_chars_space.hpp"
98 2 : );
99 8 : test_read ("yambi/single_quoted_scalar.yaml",
100 : #include "yambi/single_quoted_scalar.hpp"
101 2 : );
102 8 : test_read ("yambi/double_quoted_scalar.yaml",
103 : #include "yambi/double_quoted_scalar.hpp"
104 2 : );
105 2 : }
106 :
107 20 : TEST (yambi, list)
108 : {
109 8 : test_read ("yambi/list-plain_scalars.yaml",
110 : #include "yambi/list-plain_scalars.hpp"
111 2 : );
112 8 : test_read ("yambi/list-list_map-mixed_scalars.yaml",
113 : #include "yambi/list-list_map-mixed_scalars.hpp"
114 2 : );
115 2 : }
116 :
117 20 : TEST (yambi, map)
118 : {
119 8 : test_read ("yambi/map-null.yaml",
120 : #include "yambi/map-null.hpp"
121 2 : );
122 8 : test_read ("yambi/map-plain_scalar.yaml",
123 : #include "yambi/map-plain_scalar.hpp"
124 2 : );
125 8 : test_read ("yambi/map-plain_scalars.yaml",
126 : #include "yambi/map-plain_scalars.hpp"
127 2 : );
128 8 : test_read ("yambi/map-list-plain_scalars.yaml",
129 : #include "yambi/map-list-plain_scalars.hpp"
130 2 : );
131 6 : test_read ("yambi/map-map-plain_scalars.yaml",
132 : #include "yambi/map-map-plain_scalars.hpp"
133 2 : );
134 2 : }
135 :
136 : // -- Main ---------------------------------------------------------------------------------------------------------------------------------
137 :
138 2 : int main (int argc, char * argv[])
139 : {
140 2 : init (argc, argv); // Required for `srcdir_file` to work properly
141 2 : ::testing::InitGoogleTest (&argc, argv);
142 : return RUN_ALL_TESTS ();
143 6 : }
|