comparison tests/js-file/main.cpp @ 566:bf56628e070b

Irccd: import new Duktape helpers
author David Demelier <markand@malikania.fr>
date Mon, 27 Nov 2017 15:03:38 +0100
parents 194162717ec9
children
comparison
equal deleted inserted replaced
565:194162717ec9 566:bf56628e070b
25 25
26 #include <js_test.hpp> 26 #include <js_test.hpp>
27 27
28 namespace irccd { 28 namespace irccd {
29 29
30 namespace {
31
32 std::vector<std::string> array(duk_context* ctx, int index)
33 {
34 std::vector<std::string> result;
35 std::size_t length = duk_get_length(ctx, index);
36
37 for (std::size_t i = 0U; i < length; ++i) {
38 duk_get_prop_index(ctx, -1, i);
39 result.push_back(dukx_get_string(ctx, -1));
40 duk_pop(ctx);
41 }
42
43 return result;
44 }
45
46 } // !namespace
47
48 BOOST_FIXTURE_TEST_SUITE(file_jsapi_suite, js_test<file_jsapi>) 30 BOOST_FIXTURE_TEST_SUITE(file_jsapi_suite, js_test<file_jsapi>)
49 31
50 BOOST_AUTO_TEST_CASE(function_basename) 32 BOOST_AUTO_TEST_CASE(function_basename)
51 { 33 {
52 if (duk_peval_string(plugin_->context(), "result = Irccd.File.basename('/usr/local/etc/irccd.conf');")) 34 if (duk_peval_string(plugin_->context(), "result = Irccd.File.basename('/usr/local/etc/irccd.conf');"))
53 throw dukx_get_exception(plugin_->context(), -1); 35 throw dukx_stack(plugin_->context(), -1);
54 36
55 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 37 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
56 BOOST_TEST("irccd.conf" == duk_get_string(plugin_->context(), -1)); 38 BOOST_TEST("irccd.conf" == duk_get_string(plugin_->context(), -1));
57 } 39 }
58 40
59 BOOST_AUTO_TEST_CASE(function_dirname) 41 BOOST_AUTO_TEST_CASE(function_dirname)
60 { 42 {
61 if (duk_peval_string(plugin_->context(), "result = Irccd.File.dirname('/usr/local/etc/irccd.conf');")) 43 if (duk_peval_string(plugin_->context(), "result = Irccd.File.dirname('/usr/local/etc/irccd.conf');"))
62 throw dukx_get_exception(plugin_->context(), -1); 44 throw dukx_stack(plugin_->context(), -1);
63 45
64 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 46 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
65 BOOST_TEST("/usr/local/etc" == duk_get_string(plugin_->context(), -1)); 47 BOOST_TEST("/usr/local/etc" == duk_get_string(plugin_->context(), -1));
66 } 48 }
67 49
68 50
69 BOOST_AUTO_TEST_CASE(function_exists) 51 BOOST_AUTO_TEST_CASE(function_exists)
70 { 52 {
71 if (duk_peval_string(plugin_->context(), "result = Irccd.File.exists(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt')")) 53 if (duk_peval_string(plugin_->context(), "result = Irccd.File.exists(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt')"))
72 throw dukx_get_exception(plugin_->context(), -1); 54 throw dukx_stack(plugin_->context(), -1);
73 55
74 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 56 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
75 BOOST_TEST(duk_get_boolean(plugin_->context(), -1)); 57 BOOST_TEST(duk_get_boolean(plugin_->context(), -1));
76 } 58 }
77 59
78 BOOST_AUTO_TEST_CASE(function_exists2) 60 BOOST_AUTO_TEST_CASE(function_exists2)
79 { 61 {
80 if (duk_peval_string(plugin_->context(), "result = Irccd.File.exists('file_which_does_not_exist.txt')")) 62 if (duk_peval_string(plugin_->context(), "result = Irccd.File.exists('file_which_does_not_exist.txt')"))
81 throw dukx_get_exception(plugin_->context(), -1); 63 throw dukx_stack(plugin_->context(), -1);
82 64
83 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 65 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
84 BOOST_TEST(!duk_get_boolean(plugin_->context(), -1)); 66 BOOST_TEST(!duk_get_boolean(plugin_->context(), -1));
85 } 67 }
86 68
88 { 70 {
89 // First create a dummy file 71 // First create a dummy file
90 std::ofstream("test-js-fs.remove"); 72 std::ofstream("test-js-fs.remove");
91 73
92 if (duk_peval_string(plugin_->context(), "Irccd.File.remove('test-js-fs.remove');") != 0) 74 if (duk_peval_string(plugin_->context(), "Irccd.File.remove('test-js-fs.remove');") != 0)
93 throw dukx_get_exception(plugin_->context(), -1); 75 throw dukx_stack(plugin_->context(), -1);
94 76
95 std::ifstream in("test-js-fs.remove"); 77 std::ifstream in("test-js-fs.remove");
96 78
97 BOOST_TEST(!in.is_open()); 79 BOOST_TEST(!in.is_open());
98 } 80 }
103 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');" 85 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');"
104 "result = f.basename();" 86 "result = f.basename();"
105 ); 87 );
106 88
107 if (ret != 0) 89 if (ret != 0)
108 throw dukx_get_exception(plugin_->context(), -1); 90 throw dukx_stack(plugin_->context(), -1);
109 91
110 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 92 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
111 BOOST_TEST("file-1.txt" == duk_get_string(plugin_->context(), -1)); 93 BOOST_TEST("file-1.txt" == duk_get_string(plugin_->context(), -1));
112 } 94 }
113 95
118 "f.close();" 100 "f.close();"
119 "result = f.basename();" 101 "result = f.basename();"
120 ); 102 );
121 103
122 if (ret != 0) 104 if (ret != 0)
123 throw dukx_get_exception(plugin_->context(), -1); 105 throw dukx_stack(plugin_->context(), -1);
124 106
125 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 107 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
126 BOOST_TEST("file-1.txt" == duk_get_string(plugin_->context(), -1)); 108 BOOST_TEST("file-1.txt" == duk_get_string(plugin_->context(), -1));
127 } 109 }
128 110
132 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');" 114 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');"
133 "result = f.dirname();" 115 "result = f.dirname();"
134 ); 116 );
135 117
136 if (ret != 0) 118 if (ret != 0)
137 throw dukx_get_exception(plugin_->context(), -1); 119 throw dukx_stack(plugin_->context(), -1);
138 120
139 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 121 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
140 BOOST_TEST(CMAKE_SOURCE_DIR "/tests/root" == duk_get_string(plugin_->context(), -1)); 122 BOOST_TEST(CMAKE_SOURCE_DIR "/tests/root" == duk_get_string(plugin_->context(), -1));
141 } 123 }
142 124
147 "f.close();" 129 "f.close();"
148 "result = f.dirname();" 130 "result = f.dirname();"
149 ); 131 );
150 132
151 if (ret != 0) 133 if (ret != 0)
152 throw dukx_get_exception(plugin_->context(), -1); 134 throw dukx_stack(plugin_->context(), -1);
153 135
154 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 136 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
155 BOOST_TEST(CMAKE_SOURCE_DIR "/tests/root" == duk_get_string(plugin_->context(), -1)); 137 BOOST_TEST(CMAKE_SOURCE_DIR "/tests/root" == duk_get_string(plugin_->context(), -1));
156 } 138 }
157 139
160 auto ret = duk_peval_string(plugin_->context(), 142 auto ret = duk_peval_string(plugin_->context(),
161 "result = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/lines.txt', 'r').lines();" 143 "result = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/lines.txt', 'r').lines();"
162 ); 144 );
163 145
164 if (ret != 0) 146 if (ret != 0)
165 throw dukx_get_exception(plugin_->context(), -1); 147 throw dukx_stack(plugin_->context(), -1);
166 148
167 std::vector<std::string> expected{"a", "b", "c"}; 149 std::vector<std::string> expected{"a", "b", "c"};
168 150
169 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 151 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
170 BOOST_TEST(expected == array(plugin_->context(), -1)); 152 BOOST_TEST(expected == dukx_get_array<std::vector<std::string>>(plugin_->context(), -1));
171 } 153 }
172 154
173 BOOST_AUTO_TEST_CASE(method_seek1) 155 BOOST_AUTO_TEST_CASE(method_seek1)
174 { 156 {
175 auto ret = duk_peval_string(plugin_->context(), 157 auto ret = duk_peval_string(plugin_->context(),
177 "f.seek(Irccd.File.SeekSet, 6);" 159 "f.seek(Irccd.File.SeekSet, 6);"
178 "result = f.read(1);" 160 "result = f.read(1);"
179 ); 161 );
180 162
181 if (ret != 0) 163 if (ret != 0)
182 throw dukx_get_exception(plugin_->context(), -1); 164 throw dukx_stack(plugin_->context(), -1);
183 165
184 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 166 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
185 BOOST_TEST(".", dukx_get_string(plugin_->context(), -1)); 167 BOOST_TEST(".", dukx_get<std::string>(plugin_->context(), -1));
186 } 168 }
187 169
188 BOOST_AUTO_TEST_CASE(method_seek1_closed) 170 BOOST_AUTO_TEST_CASE(method_seek1_closed)
189 { 171 {
190 auto ret = duk_peval_string(plugin_->context(), 172 auto ret = duk_peval_string(plugin_->context(),
194 "result = f.read(1);" 176 "result = f.read(1);"
195 "result = typeof (result) === \"undefined\";" 177 "result = typeof (result) === \"undefined\";"
196 ); 178 );
197 179
198 if (ret != 0) 180 if (ret != 0)
199 throw dukx_get_exception(plugin_->context(), -1); 181 throw dukx_stack(plugin_->context(), -1);
200 182
201 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 183 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
202 BOOST_TEST(duk_get_boolean(plugin_->context(), -1)); 184 BOOST_TEST(duk_get_boolean(plugin_->context(), -1));
203 } 185 }
204 186
210 "f.seek(Irccd.File.SeekCur, 4);" 192 "f.seek(Irccd.File.SeekCur, 4);"
211 "result = f.read(1);" 193 "result = f.read(1);"
212 ); 194 );
213 195
214 if (ret != 0) 196 if (ret != 0)
215 throw dukx_get_exception(plugin_->context(), -1); 197 throw dukx_stack(plugin_->context(), -1);
216 198
217 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 199 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
218 BOOST_TEST("." == dukx_get_string(plugin_->context(), -1)); 200 BOOST_TEST("." == dukx_get<std::string>(plugin_->context(), -1));
219 } 201 }
220 202
221 BOOST_AUTO_TEST_CASE(method_seek2c_losed) 203 BOOST_AUTO_TEST_CASE(method_seek2c_losed)
222 { 204 {
223 auto ret = duk_peval_string(plugin_->context(), 205 auto ret = duk_peval_string(plugin_->context(),
228 "result = f.read(1);" 210 "result = f.read(1);"
229 "result = typeof (result) === \"undefined\";" 211 "result = typeof (result) === \"undefined\";"
230 ); 212 );
231 213
232 if (ret != 0) 214 if (ret != 0)
233 throw dukx_get_exception(plugin_->context(), -1); 215 throw dukx_stack(plugin_->context(), -1);
234 216
235 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 217 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
236 BOOST_TEST(duk_get_boolean(plugin_->context(), -1)); 218 BOOST_TEST(duk_get_boolean(plugin_->context(), -1));
237 } 219 }
238 220
243 "f.seek(Irccd.File.SeekEnd, -2);" 225 "f.seek(Irccd.File.SeekEnd, -2);"
244 "result = f.read(1);" 226 "result = f.read(1);"
245 ); 227 );
246 228
247 if (ret != 0) 229 if (ret != 0)
248 throw dukx_get_exception(plugin_->context(), -1); 230 throw dukx_stack(plugin_->context(), -1);
249 231
250 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 232 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
251 BOOST_TEST("t" == duk_get_string(plugin_->context(), -1)); 233 BOOST_TEST("t" == duk_get_string(plugin_->context(), -1));
252 } 234 }
253 235
260 "result = f.read(1);" 242 "result = f.read(1);"
261 "result = typeof (result) === \"undefined\";" 243 "result = typeof (result) === \"undefined\";"
262 ); 244 );
263 245
264 if (ret != 0) 246 if (ret != 0)
265 throw dukx_get_exception(plugin_->context(), -1); 247 throw dukx_stack(plugin_->context(), -1);
266 248
267 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 249 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
268 BOOST_TEST(duk_get_boolean(plugin_->context(), -1)); 250 BOOST_TEST(duk_get_boolean(plugin_->context(), -1));
269 } 251 }
270 252
274 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');" 256 "f = new Irccd.File(CMAKE_SOURCE_DIR + '/tests/root/file-1.txt', 'r');"
275 "result = f.read();" 257 "result = f.read();"
276 ); 258 );
277 259
278 if (ret != 0) 260 if (ret != 0)
279 throw dukx_get_exception(plugin_->context(), -1); 261 throw dukx_stack(plugin_->context(), -1);
280 262
281 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 263 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
282 BOOST_TEST("file-1.txt\n" == duk_get_string(plugin_->context(), -1)); 264 BOOST_TEST("file-1.txt\n" == duk_get_string(plugin_->context(), -1));
283 } 265 }
284 266
291 " result.push(s);" 273 " result.push(s);"
292 "}" 274 "}"
293 ); 275 );
294 276
295 if (ret != 0) 277 if (ret != 0)
296 throw dukx_get_exception(plugin_->context(), -1); 278 throw dukx_stack(plugin_->context(), -1);
297 279
298 std::vector<std::string> expected{"a", "b", "c"}; 280 std::vector<std::string> expected{"a", "b", "c"};
299 281
300 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 282 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
301 BOOST_TEST(expected == array(plugin_->context(), -1)); 283 BOOST_TEST(expected == dukx_get_array<std::vector<std::string>>(plugin_->context(), -1));
302 } 284 }
303 285
304 BOOST_AUTO_TEST_CASE(method_readline_closed) 286 BOOST_AUTO_TEST_CASE(method_readline_closed)
305 { 287 {
306 auto ret = duk_peval_string(plugin_->context(), 288 auto ret = duk_peval_string(plugin_->context(),
311 " result.push(s);" 293 " result.push(s);"
312 "}" 294 "}"
313 ); 295 );
314 296
315 if (ret != 0) 297 if (ret != 0)
316 throw dukx_get_exception(plugin_->context(), -1); 298 throw dukx_stack(plugin_->context(), -1);
317 299
318 std::vector<std::string> expected; 300 std::vector<std::string> expected;
319 301
320 BOOST_TEST(duk_get_global_string(plugin_->context(), "result")); 302 BOOST_TEST(duk_get_global_string(plugin_->context(), "result"));
321 BOOST_TEST(expected == array(plugin_->context(), -1)); 303 BOOST_TEST(expected == dukx_get_array<std::vector<std::string>>(plugin_->context(), -1));
322 } 304 }
323 305
324 BOOST_AUTO_TEST_SUITE_END() 306 BOOST_AUTO_TEST_SUITE_END()
325 307
326 } // !irccd 308 } // !irccd