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