comparison tests/src/libirccd-js/jsapi-file/main.cpp @ 611:9fbd1700435b

Tests: reoganize hierarchy
author David Demelier <markand@malikania.fr>
date Fri, 15 Dec 2017 15:37:58 +0100
parents tests/src/file-jsapi/main.cpp@9d4da384f5d6
children 27587ff92a64
comparison
equal deleted inserted replaced
610:22b3cd6f991f 611:9fbd1700435b
1 /*
2 * main.cpp -- test Irccd.File API
3 *
4 * Copyright (c) 2013-2017 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 #define BOOST_TEST_MODULE "File Javascript API"
20 #include <boost/test/unit_test.hpp>
21
22 #include <fstream>
23
24 #include <irccd/js/duktape_vector.hpp>
25 #include <irccd/js/file_jsapi.hpp>
26
27 #include <irccd/test/js_test.hpp>
28
29 namespace irccd {
30
31 BOOST_FIXTURE_TEST_SUITE(file_jsapi_suite, js_test<file_jsapi>)
32
33 BOOST_AUTO_TEST_CASE(function_basename)
34 {
35 if (duk_peval_string(plugin_->context(), "result = Irccd.File.basename('/usr/local/etc/irccd.conf');"))
36 throw dukx_stack(plugin_->context(), -1);
37
38 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
39 BOOST_TEST("irccd.conf" == duk_get_string(plugin_->context(), -1));
40 }
41
42 BOOST_AUTO_TEST_CASE(function_dirname)
43 {
44 if (duk_peval_string(plugin_->context(), "result = Irccd.File.dirname('/usr/local/etc/irccd.conf');"))
45 throw dukx_stack(plugin_->context(), -1);
46
47 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
48 BOOST_TEST("/usr/local/etc" == duk_get_string(plugin_->context(), -1));
49 }
50
51 BOOST_AUTO_TEST_CASE(function_exists)
52 {
53 if (duk_peval_string(plugin_->context(), "result = Irccd.File.exists(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt')"))
54 throw dukx_stack(plugin_->context(), -1);
55
56 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
57 BOOST_TEST(duk_get_boolean(plugin_->context(), -1));
58 }
59
60 BOOST_AUTO_TEST_CASE(function_exists2)
61 {
62 if (duk_peval_string(plugin_->context(), "result = Irccd.File.exists('file_which_does_not_exist.txt')"))
63 throw dukx_stack(plugin_->context(), -1);
64
65 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
66 BOOST_TEST(!duk_get_boolean(plugin_->context(), -1));
67 }
68
69 BOOST_AUTO_TEST_CASE(function_remove)
70 {
71 // First create a dummy file
72 std::ofstream("test-js-fs.remove");
73
74 if (duk_peval_string(plugin_->context(), "Irccd.File.remove('test-js-fs.remove');") != 0)
75 throw dukx_stack(plugin_->context(), -1);
76
77 std::ifstream in("test-js-fs.remove");
78
79 BOOST_TEST(!in.is_open());
80 }
81
82 BOOST_AUTO_TEST_CASE(method_basename)
83 {
84 auto ret = duk_peval_string(plugin_->context(),
85 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');"
86 "result = f.basename();"
87 );
88
89 if (ret != 0)
90 throw dukx_stack(plugin_->context(), -1);
91
92 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
93 BOOST_TEST("file-1.txt" == duk_get_string(plugin_->context(), -1));
94 }
95
96 BOOST_AUTO_TEST_CASE(method_basename_closed)
97 {
98 auto ret = duk_peval_string(plugin_->context(),
99 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');"
100 "f.close();"
101 "result = f.basename();"
102 );
103
104 if (ret != 0)
105 throw dukx_stack(plugin_->context(), -1);
106
107 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
108 BOOST_TEST("file-1.txt" == duk_get_string(plugin_->context(), -1));
109 }
110
111 BOOST_AUTO_TEST_CASE(method_dirname)
112 {
113 auto ret = duk_peval_string(plugin_->context(),
114 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');"
115 "result = f.dirname();"
116 );
117
118 if (ret != 0)
119 throw dukx_stack(plugin_->context(), -1);
120
121 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
122 BOOST_TEST(CMAKE_SOURCE_DIR "/tests/root" == duk_get_string(plugin_->context(), -1));
123 }
124
125 BOOST_AUTO_TEST_CASE(method_dirname_closed)
126 {
127 auto ret = duk_peval_string(plugin_->context(),
128 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');"
129 "f.close();"
130 "result = f.dirname();"
131 );
132
133 if (ret != 0)
134 throw dukx_stack(plugin_->context(), -1);
135
136 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
137 BOOST_TEST(CMAKE_SOURCE_DIR "/tests/root" == duk_get_string(plugin_->context(), -1));
138 }
139
140 BOOST_AUTO_TEST_CASE(method_lines)
141 {
142 auto ret = duk_peval_string(plugin_->context(),
143 "result = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/lines.txt', 'r').lines();"
144 );
145
146 if (ret != 0)
147 throw dukx_stack(plugin_->context(), -1);
148
149 std::vector<std::string> expected{"a", "b", "c"};
150
151 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
152 BOOST_TEST(expected == dukx_get<std::vector<std::string>>(plugin_->context(), -1));
153 }
154
155 BOOST_AUTO_TEST_CASE(method_seek1)
156 {
157 auto ret = duk_peval_string(plugin_->context(),
158 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');"
159 "f.seek(Irccd.File.SeekSet, 6);"
160 "result = f.read(1);"
161 );
162
163 if (ret != 0)
164 throw dukx_stack(plugin_->context(), -1);
165
166 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
167 BOOST_TEST(".", dukx_get<std::string>(plugin_->context(), -1));
168 }
169
170 BOOST_AUTO_TEST_CASE(method_seek1_closed)
171 {
172 auto ret = duk_peval_string(plugin_->context(),
173 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');"
174 "f.close();"
175 "f.seek(Irccd.File.SeekSet, 4);"
176 "result = f.read(1);"
177 "result = typeof (result) === \"undefined\";"
178 );
179
180 if (ret != 0)
181 throw dukx_stack(plugin_->context(), -1);
182
183 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
184 BOOST_TEST(duk_get_boolean(plugin_->context(), -1));
185 }
186
187 BOOST_AUTO_TEST_CASE(method_seek2)
188 {
189 auto ret = duk_peval_string(plugin_->context(),
190 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');"
191 "f.seek(Irccd.File.SeekSet, 2);"
192 "f.seek(Irccd.File.SeekCur, 4);"
193 "result = f.read(1);"
194 );
195
196 if (ret != 0)
197 throw dukx_stack(plugin_->context(), -1);
198
199 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
200 BOOST_TEST("." == dukx_get<std::string>(plugin_->context(), -1));
201 }
202
203 BOOST_AUTO_TEST_CASE(method_seek2c_losed)
204 {
205 auto ret = duk_peval_string(plugin_->context(),
206 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');"
207 "f.close();"
208 "f.seek(Irccd.File.SeekSet, 2);"
209 "f.seek(Irccd.File.SeekCur, 2);"
210 "result = f.read(1);"
211 "result = typeof (result) === \"undefined\";"
212 );
213
214 if (ret != 0)
215 throw dukx_stack(plugin_->context(), -1);
216
217 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
218 BOOST_TEST(duk_get_boolean(plugin_->context(), -1));
219 }
220
221 BOOST_AUTO_TEST_CASE(method_seek3)
222 {
223 auto ret = duk_peval_string(plugin_->context(),
224 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');"
225 "f.seek(Irccd.File.SeekEnd, -2);"
226 "result = f.read(1);"
227 );
228
229 if (ret != 0)
230 throw dukx_stack(plugin_->context(), -1);
231
232 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
233 BOOST_TEST("t" == duk_get_string(plugin_->context(), -1));
234 }
235
236 BOOST_AUTO_TEST_CASE(method_seek3_closed)
237 {
238 auto ret = duk_peval_string(plugin_->context(),
239 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');"
240 "f.close();"
241 "f.seek(Irccd.File.SeekEnd, -2);"
242 "result = f.read(1);"
243 "result = typeof (result) === \"undefined\";"
244 );
245
246 if (ret != 0)
247 throw dukx_stack(plugin_->context(), -1);
248
249 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
250 BOOST_TEST(duk_get_boolean(plugin_->context(), -1));
251 }
252
253 BOOST_AUTO_TEST_CASE(method_read1)
254 {
255 auto ret = duk_peval_string(plugin_->context(),
256 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');"
257 "result = f.read();"
258 );
259
260 if (ret != 0)
261 throw dukx_stack(plugin_->context(), -1);
262
263 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
264 BOOST_TEST("file-1.txt\n" == duk_get_string(plugin_->context(), -1));
265 }
266
267 BOOST_AUTO_TEST_CASE(method_readline)
268 {
269 auto ret = duk_peval_string(plugin_->context(),
270 "result = [];"
271 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/lines.txt', 'r');"
272 "for (var s; s = f.readline(); ) {"
273 " result.push(s);"
274 "}"
275 );
276
277 if (ret != 0)
278 throw dukx_stack(plugin_->context(), -1);
279
280 std::vector<std::string> expected{"a", "b", "c"};
281
282 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
283 BOOST_TEST(expected == dukx_get<std::vector<std::string>>(plugin_->context(), -1));
284 }
285
286 BOOST_AUTO_TEST_CASE(method_readline_closed)
287 {
288 auto ret = duk_peval_string(plugin_->context(),
289 "result = [];"
290 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/lines.txt', 'r');"
291 "f.close();"
292 "for (var s; s = f.readline(); ) {"
293 " result.push(s);"
294 "}"
295 );
296
297 if (ret != 0)
298 throw dukx_stack(plugin_->context(), -1);
299
300 std::vector<std::string> expected;
301
302 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
303 BOOST_TEST(expected == dukx_get<std::vector<std::string>>(plugin_->context(), -1));
304 }
305
306 BOOST_AUTO_TEST_SUITE_END()
307
308 } // !irccd