comparison tests/src/util-jsapi/main.cpp @ 581:a51b5dd5b761

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