This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
testmod_file.c
141 lines (92 loc) · 3.65 KB
/
testmod_file.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
138
139
140
141
/**
* @file
*
* @brief Tests for file 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>
void testReadSingleLine (const char * fileName)
{
Key * parentKey = keyNew ("user:/tests/file", KEY_VALUE, srcdir_file (fileName), KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("file");
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
const Key * key = ksLookupByName (ks, "user:/tests/file", KDB_O_NONE);
exit_if_fail (key, "key not found");
succeed_if (!strcmp ("this is a single line testfile\n", keyString (key)), "read single line data doesn't match expected string");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void testReadMultiLine (const char * fileName)
{
Key * parentKey = keyNew ("user:/tests/file", KEY_VALUE, srcdir_file (fileName), KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("file");
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
const Key * key = ksLookupByName (ks, "user:/tests/file", KDB_O_NONE);
exit_if_fail (key, "key not found");
succeed_if (!strcmp ("\nthis\n\n\tis a\n multi line test-\nfile\n\n", keyString (key)),
"read multiline data doesn't match expected string");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void testWriteSingleLine (const char * compareTo)
{
Key * parentKey = keyNew ("user:/tests/file", KEY_VALUE, elektraFilename (), KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("file");
KeySet * ks = ksNew (3, keyNew ("user:/tests/file", KEY_VALUE, "this is a single line testfile\n", KEY_END), KS_END);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "call to kdbSet was not successful");
succeed_if (compare_line_files (srcdir_file (compareTo), keyString (parentKey)), "files do not match as expected");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void testWriteMultiLine (const char * compareTo)
{
Key * parentKey = keyNew ("user:/tests/file", KEY_VALUE, elektraFilename (), KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("file");
KeySet * ks = ksNew (3, keyNew ("user:/tests/file", KEY_VALUE, "\nthis\n\n\tis a\n multi line test-\nfile\n\n", KEY_END), KS_END);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "call to kdbSet was not successful");
succeed_if (compare_line_files (srcdir_file (compareTo), keyString (parentKey)), "files do not match as expected");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void testRoundTrip (const char * fileName)
{
Key * parentKey = keyNew ("user:/tests/file", KEY_VALUE, srcdir_file (fileName), KEY_END);
KeySet * conf = ksNew (0, KS_END);
PLUGIN_OPEN ("file");
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
keySetString (parentKey, elektraFilename ());
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "call to kdbSet was not successful");
succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
int main (int argc, char ** argv)
{
printf ("FILE TESTS\n");
printf ("==================\n\n");
init (argc, argv);
testReadSingleLine ("file/singleline");
testReadMultiLine ("file/multiline");
testWriteSingleLine ("file/singleline");
testWriteMultiLine ("file/multiline");
testRoundTrip ("file/multiline");
print_result ("testmod_file");
return nbError;
}