Mercurial > irccd
comparison tests/test-jsapi-directory.c @ 963:371e1cc2c697
tests: add 80% of the Javascript API
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 28 Jan 2021 14:20:58 +0100 |
parents | |
children | a518664b20a0 |
comparison
equal
deleted
inserted
replaced
962:63208f5bb0f6 | 963:371e1cc2c697 |
---|---|
1 /* | |
2 * test-jsapi-directory.c -- test Irccd.Directory API | |
3 * | |
4 * Copyright (c) 2013-2021 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #include <sys/stat.h> | |
20 | |
21 #define GREATEST_USE_ABBREVS 0 | |
22 #include <greatest.h> | |
23 | |
24 #include <irccd/js-plugin.h> | |
25 #include <irccd/plugin.h> | |
26 | |
27 static struct irc_plugin *plugin; | |
28 static struct irc_js_plugin_data *data; | |
29 | |
30 static void | |
31 setup(void *udata) | |
32 { | |
33 (void)udata; | |
34 | |
35 plugin = irc_js_plugin_open(SOURCE "/data/example-plugin.js"); | |
36 data = plugin->data; | |
37 | |
38 duk_push_string(data->ctx, SOURCE); | |
39 duk_put_global_string(data->ctx, "SOURCE"); | |
40 | |
41 duk_push_string(data->ctx, BINARY); | |
42 duk_put_global_string(data->ctx, "BINARY"); | |
43 } | |
44 | |
45 static void | |
46 teardown(void *udata) | |
47 { | |
48 (void)udata; | |
49 | |
50 irc_plugin_finish(plugin); | |
51 | |
52 plugin = NULL; | |
53 data = NULL; | |
54 } | |
55 | |
56 GREATEST_TEST | |
57 object_constructor(void) | |
58 { | |
59 const char *script = | |
60 "d = new Irccd.Directory(SOURCE + '/data/root');" | |
61 "p = d.path;" | |
62 "l = d.entries.length;"; | |
63 | |
64 if (duk_peval_string(data->ctx, script) != 0) | |
65 GREATEST_FAIL(); | |
66 | |
67 duk_get_global_string(data->ctx, "l"); | |
68 GREATEST_ASSERT_EQ(3U, duk_get_uint(data->ctx, -1)); | |
69 duk_get_global_string(data->ctx, "p"); | |
70 GREATEST_ASSERT(duk_is_string(data->ctx, -1)); | |
71 | |
72 GREATEST_PASS(); | |
73 } | |
74 | |
75 GREATEST_TEST | |
76 object_find(void) | |
77 { | |
78 const char *script = "d = new Irccd.Directory(SOURCE + '/data/root');"; | |
79 | |
80 if (duk_peval_string(data->ctx, script) != 0) | |
81 GREATEST_FAIL(); | |
82 | |
83 /* Find "lines.txt" not recursively. */ | |
84 if (duk_peval_string(data->ctx, "p = d.find('lines.txt');") != 0) | |
85 GREATEST_FAIL(); | |
86 | |
87 duk_get_global_string(data->ctx, "p"); | |
88 GREATEST_ASSERT_STR_EQ(SOURCE "/data/root/lines.txt", duk_get_string(data->ctx, -1)); | |
89 | |
90 /* Find "unknown.txt" not recursively (not found). */ | |
91 if (duk_peval_string(data->ctx, "p = d.find('unknown.txt');") != 0) | |
92 GREATEST_FAIL(); | |
93 | |
94 duk_get_global_string(data->ctx, "p"); | |
95 GREATEST_ASSERT(duk_is_null(data->ctx, -1)); | |
96 | |
97 /* Find "file-2.txt" not recursively (exists but in sub directory). */ | |
98 if (duk_peval_string(data->ctx, "p = d.find('file-2.txt');") != 0) | |
99 GREATEST_FAIL(); | |
100 | |
101 duk_get_global_string(data->ctx, "p"); | |
102 GREATEST_ASSERT(duk_is_null(data->ctx, -1)); | |
103 | |
104 /* Find "file-2.txt" recursively. */ | |
105 if (duk_peval_string(data->ctx, "p = d.find('file-2.txt', true);") != 0) | |
106 GREATEST_FAIL(); | |
107 | |
108 duk_get_global_string(data->ctx, "p"); | |
109 GREATEST_ASSERT_STR_EQ(SOURCE "/data/root/level-1/level-2/file-2.txt", duk_get_string(data->ctx, -1)); | |
110 | |
111 GREATEST_PASS(); | |
112 } | |
113 | |
114 GREATEST_TEST | |
115 object_remove(void) | |
116 { | |
117 struct stat st; | |
118 | |
119 /* First create an empty directory. */ | |
120 mkdir(BINARY "/empty", 0700); | |
121 | |
122 if (duk_peval_string(data->ctx, "d = new Irccd.Directory(BINARY + '/empty')") != 0) | |
123 GREATEST_FAIL(); | |
124 | |
125 /* Not recursive. */ | |
126 if (duk_peval_string(data->ctx, "d.remove()") != 0) | |
127 GREATEST_FAIL(); | |
128 | |
129 GREATEST_ASSERT_EQ(-1, stat(BINARY "/empty", &st)); | |
130 | |
131 mkdir(BINARY "/notempty", 0700); | |
132 mkdir(BINARY "/notempty/empty", 0700); | |
133 | |
134 if (duk_peval_string(data->ctx, "d = new Irccd.Directory(BINARY + '/notempty')") != 0) | |
135 GREATEST_FAIL(); | |
136 | |
137 /* Not recursive. */ | |
138 if (duk_peval_string(data->ctx, "d.remove(true)") != 0) | |
139 GREATEST_FAIL(); | |
140 | |
141 GREATEST_ASSERT_EQ(-1, stat(BINARY "/notempty", &st)); | |
142 | |
143 GREATEST_PASS(); | |
144 } | |
145 | |
146 GREATEST_SUITE(suite_object) | |
147 { | |
148 GREATEST_SET_SETUP_CB(setup, NULL); | |
149 GREATEST_SET_TEARDOWN_CB(teardown, NULL); | |
150 GREATEST_RUN_TEST(object_constructor); | |
151 GREATEST_RUN_TEST(object_find); | |
152 GREATEST_RUN_TEST(object_remove); | |
153 } | |
154 | |
155 GREATEST_TEST | |
156 free_find(void) | |
157 { | |
158 /* Find "lines.txt" not recursively. */ | |
159 if (duk_peval_string(data->ctx, "p = Irccd.Directory.find(SOURCE + '/data/root', 'lines.txt');") != 0) { | |
160 puts(duk_to_string(data->ctx, -1)); | |
161 GREATEST_FAIL(); | |
162 } | |
163 | |
164 duk_get_global_string(data->ctx, "p"); | |
165 GREATEST_ASSERT_STR_EQ(SOURCE "/data/root/lines.txt", duk_get_string(data->ctx, -1)); | |
166 | |
167 /* Find "unknown.txt" not recursively (not found). */ | |
168 if (duk_peval_string(data->ctx, "p = Irccd.Directory.find(SOURCE + '/data/root', 'unknown.txt');") != 0) | |
169 GREATEST_FAIL(); | |
170 | |
171 duk_get_global_string(data->ctx, "p"); | |
172 GREATEST_ASSERT(duk_is_null(data->ctx, -1)); | |
173 | |
174 /* Find "file-2.txt" not recursively (exists but in sub directory). */ | |
175 if (duk_peval_string(data->ctx, "p = Irccd.Directory.find(SOURCE + '/data/root', 'file-2.txt');") != 0) | |
176 GREATEST_FAIL(); | |
177 | |
178 duk_get_global_string(data->ctx, "p"); | |
179 GREATEST_ASSERT(duk_is_null(data->ctx, -1)); | |
180 | |
181 /* Find "file-2.txt" recursively. */ | |
182 if (duk_peval_string(data->ctx, "p = Irccd.Directory.find(SOURCE + '/data/root', 'file-2.txt', true);") != 0) | |
183 GREATEST_FAIL(); | |
184 | |
185 duk_get_global_string(data->ctx, "p"); | |
186 GREATEST_ASSERT_STR_EQ(SOURCE "/data/root/level-1/level-2/file-2.txt", duk_get_string(data->ctx, -1)); | |
187 | |
188 GREATEST_PASS(); | |
189 } | |
190 | |
191 GREATEST_TEST | |
192 free_remove(void) | |
193 { | |
194 struct stat st; | |
195 | |
196 /* First create an empty directory. */ | |
197 mkdir(BINARY "/empty", 0700); | |
198 | |
199 /* Not recursive. */ | |
200 if (duk_peval_string(data->ctx, "Irccd.Directory.remove(BINARY + '/empty')") != 0) { | |
201 puts(duk_to_string(data->ctx, -1)); | |
202 GREATEST_FAIL(); | |
203 } | |
204 | |
205 GREATEST_ASSERT_EQ(-1, stat(BINARY "/empty", &st)); | |
206 | |
207 mkdir(BINARY "/notempty", 0700); | |
208 mkdir(BINARY "/notempty/empty", 0700); | |
209 | |
210 /* Not recursive. */ | |
211 if (duk_peval_string(data->ctx, "Irccd.Directory.remove(BINARY + '/notempty', true)") != 0) | |
212 GREATEST_FAIL(); | |
213 | |
214 GREATEST_ASSERT_EQ(-1, stat(BINARY "/notempty", &st)); | |
215 | |
216 GREATEST_PASS(); | |
217 } | |
218 | |
219 GREATEST_TEST | |
220 free_mkdir(void) | |
221 { | |
222 struct stat st; | |
223 | |
224 remove(BINARY "/1/2"); | |
225 remove(BINARY "/1"); | |
226 | |
227 if (duk_peval_string(data->ctx, "Irccd.Directory.mkdir(BINARY + '/1/2')") != 0) { | |
228 puts(duk_to_string(data->ctx, -1)); | |
229 GREATEST_FAIL(); | |
230 } | |
231 | |
232 GREATEST_ASSERT_EQ(0, stat(BINARY "/1/2", &st)); | |
233 | |
234 GREATEST_PASS(); | |
235 } | |
236 | |
237 GREATEST_SUITE(suite_free) | |
238 { | |
239 GREATEST_SET_SETUP_CB(setup, NULL); | |
240 GREATEST_SET_TEARDOWN_CB(teardown, NULL); | |
241 GREATEST_RUN_TEST(free_find); | |
242 GREATEST_RUN_TEST(free_remove); | |
243 GREATEST_RUN_TEST(free_mkdir); | |
244 } | |
245 | |
246 GREATEST_MAIN_DEFS(); | |
247 | |
248 int | |
249 main(int argc, char **argv) | |
250 { | |
251 GREATEST_MAIN_BEGIN(); | |
252 GREATEST_RUN_SUITE(suite_object); | |
253 GREATEST_RUN_SUITE(suite_free); | |
254 GREATEST_MAIN_END(); | |
255 | |
256 return 0; | |
257 } |