Mercurial > malikania
comparison tests/libcommon/js-rectangle/main.cpp @ 186:16ff680a8a94
Common: move point, line and rectangle, closes #913 @30m
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 20 Oct 2018 21:12:20 +0200 |
parents | |
children | eaa7f85bfc22 |
comparison
equal
deleted
inserted
replaced
185:975dffc6567a | 186:16ff680a8a94 |
---|---|
1 /* | |
2 * main.cpp -- test Rectangle (JavaScript binding) | |
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 "Javascript Rectangle" | |
20 #include <boost/test/unit_test.hpp> | |
21 | |
22 #include <malikania/js_rectangle.hpp> | |
23 | |
24 namespace mlk { | |
25 | |
26 namespace { | |
27 | |
28 class test_rectangle { | |
29 protected: | |
30 dukx_context m_ctx; | |
31 | |
32 public: | |
33 test_rectangle() | |
34 { | |
35 duk_push_object(m_ctx); | |
36 duk_put_global_string(m_ctx, "Malikania"); | |
37 dukx_load_rect(m_ctx); | |
38 } | |
39 }; | |
40 | |
41 BOOST_FIXTURE_TEST_SUITE(test_rectangle_suite, test_rectangle) | |
42 | |
43 /* | |
44 * Valid constructors. | |
45 * ------------------------------------------------------------------ | |
46 */ | |
47 | |
48 BOOST_AUTO_TEST_SUITE(constructors) | |
49 | |
50 BOOST_AUTO_TEST_CASE(constructor_default) | |
51 { | |
52 try { | |
53 const auto ret = duk_peval_string(m_ctx, | |
54 "r = Malikania.Rectangle();" | |
55 "x = r.x;" | |
56 "y = r.y;" | |
57 "w = r.width;" | |
58 "h = r.height;" | |
59 ); | |
60 | |
61 if (ret != 0) | |
62 throw dukx_get_exception(m_ctx, -1); | |
63 | |
64 duk_get_global_string(m_ctx, "x"); | |
65 BOOST_REQUIRE_EQUAL(0, duk_to_int(m_ctx, -1)); | |
66 duk_pop(m_ctx); | |
67 duk_get_global_string(m_ctx, "y"); | |
68 BOOST_REQUIRE_EQUAL(0, duk_to_int(m_ctx, -1)); | |
69 duk_pop(m_ctx); | |
70 duk_get_global_string(m_ctx, "w"); | |
71 BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); | |
72 duk_pop(m_ctx); | |
73 duk_get_global_string(m_ctx, "h"); | |
74 BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); | |
75 duk_pop(m_ctx); | |
76 } catch (const std::exception &ex) { | |
77 BOOST_FAIL(ex.what()); | |
78 } | |
79 } | |
80 | |
81 BOOST_AUTO_TEST_CASE(constructor_4_args) | |
82 { | |
83 try { | |
84 const auto ret = duk_peval_string(m_ctx, | |
85 "r = Malikania.Rectangle(10, 20, 30, 40);" | |
86 "x = r.x;" | |
87 "y = r.y;" | |
88 "w = r.width;" | |
89 "h = r.height;" | |
90 ); | |
91 | |
92 if (ret != 0) | |
93 throw dukx_get_exception(m_ctx, -1); | |
94 | |
95 duk_get_global_string(m_ctx, "x"); | |
96 BOOST_REQUIRE_EQUAL(10, duk_to_int(m_ctx, -1)); | |
97 duk_pop(m_ctx); | |
98 duk_get_global_string(m_ctx, "y"); | |
99 BOOST_REQUIRE_EQUAL(20, duk_to_int(m_ctx, -1)); | |
100 duk_pop(m_ctx); | |
101 duk_get_global_string(m_ctx, "w"); | |
102 BOOST_REQUIRE_EQUAL(30U, duk_to_uint(m_ctx, -1)); | |
103 duk_pop(m_ctx); | |
104 duk_get_global_string(m_ctx, "h"); | |
105 BOOST_REQUIRE_EQUAL(40U, duk_to_uint(m_ctx, -1)); | |
106 duk_pop(m_ctx); | |
107 } catch (const std::exception &ex) { | |
108 BOOST_FAIL(ex.what()); | |
109 } | |
110 } | |
111 | |
112 BOOST_AUTO_TEST_CASE(constructor_object) | |
113 { | |
114 try { | |
115 const auto ret = duk_peval_string(m_ctx, | |
116 "r = Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });" | |
117 "x = r.x;" | |
118 "y = r.y;" | |
119 "w = r.width;" | |
120 "h = r.height;" | |
121 ); | |
122 | |
123 if (ret != 0) | |
124 throw dukx_get_exception(m_ctx, -1); | |
125 | |
126 duk_get_global_string(m_ctx, "x"); | |
127 BOOST_REQUIRE_EQUAL(10, duk_to_int(m_ctx, -1)); | |
128 duk_pop(m_ctx); | |
129 duk_get_global_string(m_ctx, "y"); | |
130 BOOST_REQUIRE_EQUAL(20, duk_to_int(m_ctx, -1)); | |
131 duk_pop(m_ctx); | |
132 duk_get_global_string(m_ctx, "w"); | |
133 BOOST_REQUIRE_EQUAL(30U, duk_to_uint(m_ctx, -1)); | |
134 duk_pop(m_ctx); | |
135 duk_get_global_string(m_ctx, "h"); | |
136 BOOST_REQUIRE_EQUAL(40U, duk_to_uint(m_ctx, -1)); | |
137 duk_pop(m_ctx); | |
138 } catch (const std::exception &ex) { | |
139 BOOST_FAIL(ex.what()); | |
140 } | |
141 } | |
142 | |
143 BOOST_AUTO_TEST_CASE(constructor_new) | |
144 { | |
145 try { | |
146 const auto ret = duk_peval_string(m_ctx, | |
147 "r = new Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });" | |
148 "x = r.x;" | |
149 "y = r.y;" | |
150 "w = r.width;" | |
151 "h = r.height;" | |
152 ); | |
153 | |
154 if (ret != 0) | |
155 throw dukx_get_exception(m_ctx, -1); | |
156 | |
157 duk_get_global_string(m_ctx, "x"); | |
158 BOOST_REQUIRE_EQUAL(10, duk_to_int(m_ctx, -1)); | |
159 duk_pop(m_ctx); | |
160 duk_get_global_string(m_ctx, "y"); | |
161 BOOST_REQUIRE_EQUAL(20, duk_to_int(m_ctx, -1)); | |
162 duk_pop(m_ctx); | |
163 duk_get_global_string(m_ctx, "w"); | |
164 BOOST_REQUIRE_EQUAL(30U, duk_to_uint(m_ctx, -1)); | |
165 duk_pop(m_ctx); | |
166 duk_get_global_string(m_ctx, "h"); | |
167 BOOST_REQUIRE_EQUAL(40U, duk_to_uint(m_ctx, -1)); | |
168 duk_pop(m_ctx); | |
169 } catch (const std::exception &ex) { | |
170 BOOST_FAIL(ex.what()); | |
171 } | |
172 } | |
173 | |
174 BOOST_AUTO_TEST_SUITE_END() | |
175 | |
176 /* | |
177 * Invalid constructors. | |
178 * ------------------------------------------------------------------ | |
179 */ | |
180 | |
181 BOOST_AUTO_TEST_SUITE(invalid_constructors) | |
182 | |
183 BOOST_AUTO_TEST_CASE(constructor_arg_1) | |
184 { | |
185 try { | |
186 const auto ret = duk_peval_string(m_ctx, | |
187 "try {" | |
188 " Malikania.Rectangle(null);" | |
189 "} catch (e) {" | |
190 " name = e.name;" | |
191 " correct = (e instanceof TypeError);" | |
192 "}" | |
193 ); | |
194 | |
195 if (ret != 0) | |
196 throw dukx_get_exception(m_ctx, -1); | |
197 | |
198 duk_get_global_string(m_ctx, "name"); | |
199 BOOST_REQUIRE_EQUAL("TypeError", duk_to_string(m_ctx, -1)); | |
200 duk_pop(m_ctx); | |
201 duk_get_global_string(m_ctx, "correct"); | |
202 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1)); | |
203 duk_pop(m_ctx); | |
204 } catch (const std::exception &ex) { | |
205 BOOST_FAIL(ex.what()); | |
206 } | |
207 } | |
208 | |
209 BOOST_AUTO_TEST_CASE(constructor_range_1) | |
210 { | |
211 try { | |
212 const auto ret = duk_peval_string(m_ctx, | |
213 "try {" | |
214 " Malikania.Rectangle(0, 0, -10, -10);" | |
215 "} catch (e) {" | |
216 " name = e.name;" | |
217 " correct = (e instanceof RangeError);" | |
218 "}" | |
219 ); | |
220 | |
221 if (ret != 0) | |
222 throw dukx_get_exception(m_ctx, -1); | |
223 | |
224 duk_get_global_string(m_ctx, "name"); | |
225 BOOST_REQUIRE_EQUAL("RangeError", duk_to_string(m_ctx, -1)); | |
226 duk_pop(m_ctx); | |
227 duk_get_global_string(m_ctx, "correct"); | |
228 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1)); | |
229 duk_pop(m_ctx); | |
230 } catch (const std::exception &ex) { | |
231 BOOST_FAIL(ex.what()); | |
232 } | |
233 } | |
234 | |
235 BOOST_AUTO_TEST_SUITE_END() | |
236 | |
237 /* | |
238 * Require. | |
239 * ------------------------------------------------------------------ | |
240 */ | |
241 | |
242 BOOST_AUTO_TEST_SUITE(require) | |
243 | |
244 BOOST_AUTO_TEST_CASE(success) | |
245 { | |
246 try { | |
247 duk_push_c_function(m_ctx, [] (auto ctx) { | |
248 const auto rect = dukx_require_rect(ctx, 0); | |
249 | |
250 duk_push_int(ctx, rect.x); | |
251 duk_put_global_string(ctx, "x"); | |
252 duk_push_int(ctx, rect.y); | |
253 duk_put_global_string(ctx, "y"); | |
254 duk_push_uint(ctx, rect.width); | |
255 duk_put_global_string(ctx, "w"); | |
256 duk_push_uint(ctx, rect.height); | |
257 duk_put_global_string(ctx, "h"); | |
258 | |
259 return 0; | |
260 }, 1); | |
261 duk_put_global_string(m_ctx, "build"); | |
262 | |
263 auto ret = duk_peval_string(m_ctx, "build({ x: 50, y: 80, width: 100, height: 200 });"); | |
264 | |
265 if (ret != 0) | |
266 throw dukx_get_exception(m_ctx, -1); | |
267 | |
268 duk_get_global_string(m_ctx, "x"); | |
269 BOOST_REQUIRE_EQUAL(50, duk_to_int(m_ctx, -1)); | |
270 duk_pop(m_ctx); | |
271 duk_get_global_string(m_ctx, "y"); | |
272 BOOST_REQUIRE_EQUAL(80, duk_to_int(m_ctx, -1)); | |
273 duk_pop(m_ctx); | |
274 duk_get_global_string(m_ctx, "w"); | |
275 BOOST_REQUIRE_EQUAL(100U, duk_to_uint(m_ctx, -1)); | |
276 duk_pop(m_ctx); | |
277 duk_get_global_string(m_ctx, "h"); | |
278 BOOST_REQUIRE_EQUAL(200U, duk_to_uint(m_ctx, -1)); | |
279 duk_pop(m_ctx); | |
280 } catch (const std::exception &ex) { | |
281 BOOST_FAIL(ex.what()); | |
282 } | |
283 } | |
284 | |
285 BOOST_AUTO_TEST_CASE(fail) | |
286 { | |
287 try { | |
288 duk_push_c_function(m_ctx, [] (auto ctx) { | |
289 dukx_require_rect(ctx, 0); | |
290 | |
291 return 0; | |
292 }, 1); | |
293 duk_put_global_string(m_ctx, "build"); | |
294 | |
295 const auto ret = duk_peval_string(m_ctx, | |
296 "try {" | |
297 " build({});" | |
298 "} catch (e) {" | |
299 " name = e.name;" | |
300 " correct = (e instanceof Error);" | |
301 "}" | |
302 ); | |
303 | |
304 if (ret != 0) | |
305 throw dukx_get_exception(m_ctx, -1); | |
306 | |
307 duk_get_global_string(m_ctx, "name"); | |
308 BOOST_REQUIRE_EQUAL("Error", duk_to_string(m_ctx, -1)); | |
309 duk_pop(m_ctx); | |
310 duk_get_global_string(m_ctx, "correct"); | |
311 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1)); | |
312 duk_pop(m_ctx); | |
313 } catch (const std::exception &ex) { | |
314 BOOST_FAIL(ex.what()); | |
315 } | |
316 } | |
317 | |
318 BOOST_AUTO_TEST_SUITE_END() | |
319 | |
320 /* | |
321 * Get. | |
322 * ------------------------------------------------------------------ | |
323 */ | |
324 | |
325 BOOST_AUTO_TEST_SUITE(get) | |
326 | |
327 BOOST_AUTO_TEST_CASE(adjust_all) | |
328 { | |
329 try { | |
330 duk_push_c_function(m_ctx, [] (auto ctx) { | |
331 const auto rect = dukx_get_rect(ctx, 0); | |
332 | |
333 duk_push_int(ctx, rect.x); | |
334 duk_put_global_string(ctx, "x"); | |
335 duk_push_int(ctx, rect.y); | |
336 duk_put_global_string(ctx, "y"); | |
337 duk_push_uint(ctx, rect.width); | |
338 duk_put_global_string(ctx, "w"); | |
339 duk_push_uint(ctx, rect.height); | |
340 | |
341 return 0; | |
342 }, 1); | |
343 duk_put_global_string(m_ctx, "build"); | |
344 | |
345 const auto ret = duk_peval_string(m_ctx, "build({});"); | |
346 | |
347 if (ret != 0) | |
348 throw dukx_get_exception(m_ctx, -1); | |
349 | |
350 duk_get_global_string(m_ctx, "x"); | |
351 BOOST_REQUIRE_EQUAL(0, duk_to_int(m_ctx, -1)); | |
352 duk_pop(m_ctx); | |
353 duk_get_global_string(m_ctx, "y"); | |
354 BOOST_REQUIRE_EQUAL(0, duk_to_int(m_ctx, -1)); | |
355 duk_pop(m_ctx); | |
356 duk_get_global_string(m_ctx, "w"); | |
357 BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); | |
358 duk_pop(m_ctx); | |
359 duk_get_global_string(m_ctx, "h"); | |
360 BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); | |
361 duk_pop(m_ctx); | |
362 } catch (const std::exception &ex) { | |
363 BOOST_FAIL(ex.what()); | |
364 } | |
365 } | |
366 | |
367 BOOST_AUTO_TEST_SUITE_END() | |
368 | |
369 BOOST_AUTO_TEST_SUITE_END() | |
370 | |
371 } // !namespace | |
372 | |
373 } // !mlk |