forked from tom-wa/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testmod_mini.c
137 lines (106 loc) · 4.27 KB
/
testmod_mini.c
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
/**
* @file
*
* @brief Tests for mini plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
/* -- Imports --------------------------------------------------------------------------------------------------------------------------- */
#include <stdlib.h>
#include <string.h>
#include <kdbconfig.h>
#include <tests_plugin.h>
/* -- Macros ---------------------------------------------------------------------------------------------------------------------------- */
#define MAX_LENGTH_TEXT 500
/* -- Functions ------------------------------------------------------------------------------------------------------------------------- */
static void test_basics (void)
{
printf ("• Test basic functionality of plugin\n");
Key * parentKey = keyNew ("system/elektra/modules/mini", KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("mini");
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Could not retrieve plugin contract");
keyDel (parentKey);
ksDel (ks);
PLUGIN_CLOSE ();
}
static void test_get (void)
{
char const * const fileName = "mini/read.ini";
printf ("• Parse file “%s”\n", fileName);
char const * const prefix = "user/mini/tests/read";
Key * parentKey = keyNew (prefix, KEY_VALUE, srcdir_file (fileName), KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("mini");
KeySet * keySet = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, keySet, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Unable to open or parse file");
succeed_if (output_error (parentKey), "Received unexpected error while reading the configuration");
char keyValues[][2][50] = {
{ "keyWithoutLeadingWhitespace", "valueWithLeadingWhiteSpace" },
{ "keyWithLeadingWhitespace", "valueWithoutLeadingWhiteSpace" },
{ "keyNoSpace", "valueNoSpace" },
{ "wide", "open spaces" },
{ "key containing space", "value" },
{ "empty", "" },
{ "esc\\/a\\/ped/level1/level2", "🌻" },
};
Key * key;
char text[MAX_LENGTH_TEXT];
for (size_t pair = 0; pair < sizeof (keyValues) / sizeof (keyValues[0]); pair++)
{
char * name = keyValues[pair][0];
char * value = keyValues[pair][1];
snprintf (text, MAX_LENGTH_TEXT, "%s/%s", prefix, name);
key = ksLookupByName (keySet, text, KDB_O_NONE);
snprintf (text, MAX_LENGTH_TEXT, "Key “%s” not found", name);
exit_if_fail (key, text);
succeed_if_same_string (keyString (key), value);
}
keyDel (parentKey);
ksDel (keySet);
PLUGIN_CLOSE ();
}
static void test_set (void)
{
printf ("• Write configuration data\n");
char const * const fileName = "mini/write.ini";
char const * const prefix = "user/mini/tests/write";
Key * parentKey = keyNew (prefix, KEY_VALUE, elektraFilename (), KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("mini");
char keyValues[][2][50] = {
{ "key", "value" }, { "space", "wide open spaces" }, { "empty", "" }, { "esc\\/aped/level1/", "🐌" }
};
char text[MAX_LENGTH_TEXT];
KeySet * keySet = ksNew (0, KS_END);
for (size_t pair = 0; pair < sizeof (keyValues) / sizeof (keyValues[0]); pair++)
{
char * name = keyValues[pair][0];
char * value = keyValues[pair][1];
snprintf (text, MAX_LENGTH_TEXT, "%s/%s", prefix, name);
ksAppendKey (keySet, keyNew (text, KEY_VALUE, value, KEY_END));
}
succeed_if (plugin->kdbSet (plugin, keySet, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "Unable to write to file");
succeed_if (output_error (parentKey), "Received unexpected error while writing the configuration");
succeed_if (output_warnings (parentKey), "Received unexpected warning while writing the configuration");
snprintf (text, MAX_LENGTH_TEXT, "Output of plugin stored in “%s” does not match the expected output stored in “%s”",
keyString (parentKey), srcdir_file (fileName));
succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), text);
keyDel (parentKey);
ksDel (keySet);
PLUGIN_CLOSE ();
}
/* -- Main ------------------------------------------------------------------------------------------------------------------------------ */
int main (int argc, char ** argv)
{
printf ("mINI Tests 🚙\n");
printf ("==============\n\n");
init (argc, argv);
test_basics ();
test_get ();
test_set ();
print_result ("testmod_mini");
return nbError;
}