forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestmod_simpleini.c
More file actions
140 lines (109 loc) · 3.29 KB
/
Copy pathtestmod_simpleini.c
File metadata and controls
140 lines (109 loc) · 3.29 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
/**
* @file
*
* @brief Tests for simpleini plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include <stdlib.h>
#include <string.h>
#include <kdbconfig.h>
#include <tests_plugin.h>
static void test_readFormat (const char * format, const char * fileContent, int numKeys, const char ** keys, const char ** values)
{
const char * tmpFile = elektraFilename ();
FILE * fh = fopen (tmpFile, "w");
if (fh)
{
fputs (fileContent, fh);
fclose (fh);
}
Key * parentKey = keyNew ("user:/tests/simpleini", KEY_VALUE, tmpFile, KEY_END);
KeySet * conf = 0;
if (format)
{
conf = ksNew (1, keyNew ("system:/format", KEY_VALUE, format, KEY_END), KS_END);
}
else
{
conf = ksNew (0, KS_END);
}
PLUGIN_OPEN ("simpleini");
KeySet * ks = ksNew (numKeys, KS_END);
Key * key = 0;
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");
Key * lookup = 0;
for (int i = 0; i < numKeys; i++)
{
lookup = keyNew ("user:/tests/simpleini", KEY_END);
keyAddBaseName (lookup, keys[i]);
printf ("testing key '%s'\n", keyBaseName (lookup));
succeed_if ((key = ksLookup (ks, lookup, 0)) != NULL, "key not found");
succeed_if (strcmp (values[i], keyString (key)) == 0, "value of key did not match");
keyDel (lookup);
}
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
static void test_formatNotAccepted (const char * format)
{
Key * parentKey = keyNew ("user:/tests/simpleini", KEY_VALUE, elektraFilename (), KEY_END);
KeySet * conf = ksNew (1, keyNew ("system:/format", KEY_VALUE, format, KEY_END), KS_END);
PLUGIN_OPEN ("simpleini");
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) != 1, "kdbGet was successful for an invalid format");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
static void test_readDefaultFormat (void)
{
const char * expected_keys[] = { "key1", "key2", "key3", "key4" };
const char * expected_values[] = { "value1", "value2", "value3", "value4 " };
test_readFormat (NULL, // format
"key1 = value1\n"
" key2 = value2\n"
"key3 = value3\n"
"key4 = value4 \n",
4, expected_keys, expected_values);
}
static void test_readFormat_wo_space (void)
{
const char * expected_keys[] = { "key1", "key2", "key3", "key4" };
const char * expected_values[] = { "value1", "value2", "value3", " value4 " };
test_readFormat ("%=%", // format
"key1=value1\n"
" key2=value2\n"
"key3 =value3\n"
"key4= value4 \n",
4, expected_keys, expected_values);
}
static void test_readFormat_special1 (void)
{
const char * expected_keys[] = { "key1", "key2", "key3", "key4" };
const char * expected_values[] = { "value1", "value2", "value3", " value4 " };
test_readFormat ("% :=_%", // format
"key1 :=_value1\n"
" key2 :=_value2\n"
"key3 :=_value3\n"
"key4 :=_ value4 \n",
4, expected_keys, expected_values);
}
int main (int argc, char ** argv)
{
printf ("SIMPLEINI TESTS\n");
printf ("==================\n\n");
init (argc, argv);
test_readDefaultFormat ();
test_readFormat_wo_space ();
test_readFormat_special1 ();
test_formatNotAccepted ("%");
test_formatNotAccepted ("");
test_formatNotAccepted ("%%%");
test_formatNotAccepted ("%% %");
test_formatNotAccepted ("%% %%");
print_result ("testmod_simpleini");
return nbError;
}