Mercurial > irccd
comparison tests/src/libirccd-js/js-api-util/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-util/main.cpp@97b356010785 |
children | 35c1517d705d |
comparison
equal
deleted
inserted
replaced
757:97b356010785 | 758:445c071e8efb |
---|---|
1 /* | |
2 * main.cpp -- test Irccd.Util 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 "Unicode Javascript API" | |
20 #include <boost/test/unit_test.hpp> | |
21 | |
22 #include <irccd/test/js_fixture.hpp> | |
23 | |
24 namespace irccd::test { | |
25 | |
26 using namespace irccd::js; | |
27 | |
28 namespace { | |
29 | |
30 BOOST_FIXTURE_TEST_SUITE(util_js_api_suite, js_fixture) | |
31 | |
32 /* | |
33 * Irccd.Util misc. | |
34 * ------------------------------------------------------------------ | |
35 */ | |
36 | |
37 BOOST_AUTO_TEST_CASE(format_simple) | |
38 { | |
39 const auto ret = duk_peval_string(plugin_->get_context(), | |
40 "result = Irccd.Util.format(\"#{target}\", { target: \"markand\" })" | |
41 ); | |
42 | |
43 if (ret != 0) | |
44 throw duk::get_stack(plugin_->get_context(), -1); | |
45 | |
46 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "result")); | |
47 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "markand"); | |
48 } | |
49 | |
50 BOOST_AUTO_TEST_CASE(splituser) | |
51 { | |
52 if (duk_peval_string(plugin_->get_context(), "result = Irccd.Util.splituser(\"user!~user@hyper/super/host\");") != 0) | |
53 throw duk::get_stack(plugin_->get_context(), -1); | |
54 | |
55 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "result")); | |
56 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "user"); | |
57 } | |
58 | |
59 BOOST_AUTO_TEST_CASE(splithost) | |
60 { | |
61 if (duk_peval_string(plugin_->get_context(), "result = Irccd.Util.splithost(\"user!~user@hyper/super/host\");") != 0) | |
62 throw duk::get_stack(plugin_->get_context(), -1); | |
63 | |
64 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "result")); | |
65 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "~user@hyper/super/host"); | |
66 } | |
67 | |
68 /* | |
69 * Irccd.Util.cut. | |
70 * ------------------------------------------------------------------ | |
71 */ | |
72 | |
73 BOOST_AUTO_TEST_CASE(cut_string_simple) | |
74 { | |
75 const auto ret = duk_peval_string(plugin_->get_context(), | |
76 "lines = Irccd.Util.cut('hello world');\n" | |
77 "line0 = lines[0];\n" | |
78 ); | |
79 | |
80 if (ret != 0) | |
81 throw duk::get_stack(plugin_->get_context(), -1); | |
82 | |
83 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); | |
84 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello world"); | |
85 } | |
86 | |
87 BOOST_AUTO_TEST_CASE(cut_string_double) | |
88 { | |
89 const auto ret = duk_peval_string(plugin_->get_context(), | |
90 "lines = Irccd.Util.cut('hello world', 5);\n" | |
91 "line0 = lines[0];\n" | |
92 "line1 = lines[1];\n" | |
93 ); | |
94 | |
95 if (ret != 0) | |
96 throw duk::get_stack(plugin_->get_context(), -1); | |
97 | |
98 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); | |
99 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello"); | |
100 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line1")); | |
101 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world"); | |
102 } | |
103 | |
104 BOOST_AUTO_TEST_CASE(cut_string_dirty) | |
105 { | |
106 const auto ret = duk_peval_string(plugin_->get_context(), | |
107 "lines = Irccd.Util.cut(' hello world ', 5);\n" | |
108 "line0 = lines[0];\n" | |
109 "line1 = lines[1];\n" | |
110 ); | |
111 | |
112 if (ret != 0) | |
113 throw duk::get_stack(plugin_->get_context(), -1); | |
114 | |
115 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); | |
116 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello"); | |
117 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line1")); | |
118 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world"); | |
119 } | |
120 | |
121 BOOST_AUTO_TEST_CASE(cut_string_too_much_lines) | |
122 { | |
123 const auto ret = duk_peval_string(plugin_->get_context(), | |
124 "lines = Irccd.Util.cut('abc def ghi jkl', 3, 3);\n" | |
125 ); | |
126 | |
127 if (ret != 0) | |
128 throw duk::get_stack(plugin_->get_context(), -1); | |
129 | |
130 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "lines")); | |
131 BOOST_TEST(duk_is_undefined(plugin_->get_context(), -1)); | |
132 } | |
133 | |
134 BOOST_AUTO_TEST_CASE(cut_string_token_too_big) | |
135 { | |
136 const auto ret = duk_peval_string(plugin_->get_context(), | |
137 "try {\n" | |
138 " lines = Irccd.Util.cut('hello world', 3);\n" | |
139 "} catch (e) {\n" | |
140 " name = e.name;\n" | |
141 " message = e.message;\n" | |
142 "}\n" | |
143 ); | |
144 | |
145 if (ret != 0) | |
146 throw duk::get_stack(plugin_->get_context(), -1); | |
147 | |
148 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "name")); | |
149 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "RangeError"); | |
150 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "message")); | |
151 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "word 'hello' could not fit in maxc limit (3)"); | |
152 } | |
153 | |
154 BOOST_AUTO_TEST_CASE(cut_string_negative_maxc) | |
155 { | |
156 const auto ret = duk_peval_string(plugin_->get_context(), | |
157 "try {\n" | |
158 " lines = Irccd.Util.cut('hello world', -3);\n" | |
159 "} catch (e) {\n" | |
160 " name = e.name;\n" | |
161 " message = e.message;\n" | |
162 "}\n" | |
163 ); | |
164 | |
165 if (ret != 0) | |
166 throw duk::get_stack(plugin_->get_context(), -1); | |
167 | |
168 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "name")); | |
169 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "RangeError"); | |
170 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "message")); | |
171 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "argument 1 (maxc) must be positive"); | |
172 } | |
173 | |
174 BOOST_AUTO_TEST_CASE(cut_string_negative_maxl) | |
175 { | |
176 const auto ret = duk_peval_string(plugin_->get_context(), | |
177 "try {\n" | |
178 " lines = Irccd.Util.cut('hello world', undefined, -1);\n" | |
179 "} catch (e) {\n" | |
180 " name = e.name;\n" | |
181 " message = e.message;\n" | |
182 "}\n" | |
183 ); | |
184 | |
185 if (ret != 0) | |
186 throw duk::get_stack(plugin_->get_context(), -1); | |
187 | |
188 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "name")); | |
189 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "RangeError"); | |
190 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "message")); | |
191 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "argument 2 (maxl) must be positive"); | |
192 } | |
193 | |
194 BOOST_AUTO_TEST_CASE(cut_array_simple) | |
195 { | |
196 const auto ret = duk_peval_string(plugin_->get_context(), | |
197 "lines = Irccd.Util.cut([ 'hello', 'world' ]);\n" | |
198 "line0 = lines[0];\n" | |
199 ); | |
200 | |
201 if (ret != 0) | |
202 throw duk::get_stack(plugin_->get_context(), -1); | |
203 | |
204 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); | |
205 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello world"); | |
206 } | |
207 | |
208 BOOST_AUTO_TEST_CASE(cut_array_double) | |
209 { | |
210 const auto ret = duk_peval_string(plugin_->get_context(), | |
211 "lines = Irccd.Util.cut([ 'hello', 'world' ], 5);\n" | |
212 "line0 = lines[0];\n" | |
213 "line1 = lines[1];\n" | |
214 ); | |
215 | |
216 if (ret != 0) | |
217 throw duk::get_stack(plugin_->get_context(), -1); | |
218 | |
219 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); | |
220 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello"); | |
221 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line1")); | |
222 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world"); | |
223 } | |
224 | |
225 BOOST_AUTO_TEST_CASE(cut_array_dirty) | |
226 { | |
227 const auto ret = duk_peval_string(plugin_->get_context(), | |
228 "lines = Irccd.Util.cut([ ' ', ' hello ', ' world ', ' '], 5);\n" | |
229 "line0 = lines[0];\n" | |
230 "line1 = lines[1];\n" | |
231 ); | |
232 | |
233 if (ret != 0) | |
234 throw duk::get_stack(plugin_->get_context(), -1); | |
235 | |
236 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); | |
237 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello"); | |
238 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line1")); | |
239 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world"); | |
240 } | |
241 | |
242 BOOST_AUTO_TEST_CASE(cut_invalid_data) | |
243 { | |
244 const auto ret = duk_peval_string(plugin_->get_context(), | |
245 "try {\n" | |
246 " lines = Irccd.Util.cut(123);\n" | |
247 "} catch (e) {\n" | |
248 " name = e.name;\n" | |
249 " message = e.message;\n" | |
250 "}\n" | |
251 ); | |
252 | |
253 if (ret != 0) | |
254 throw duk::get_stack(plugin_->get_context(), -1); | |
255 | |
256 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "name")); | |
257 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "TypeError"); | |
258 } | |
259 | |
260 BOOST_AUTO_TEST_SUITE_END() | |
261 | |
262 } // !namespace | |
263 | |
264 } // !irccd::test |