Mercurial > malikania
comparison tests/libcommon/js-size/main.cpp @ 182:3107ce017c3a
Misc: switch back to SDL
Qt Quick and QML was an exciting experiment but it's definitely not enough
flexible and easy to use for game development.
Using SDL2 will let us focusing on our own drawing functions without any kind of
overhead.
While here, start massive cleanup.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 19 Oct 2018 20:18:19 +0200 |
parents | |
children | eaa7f85bfc22 |
comparison
equal
deleted
inserted
replaced
181:fbfc2555bda5 | 182:3107ce017c3a |
---|---|
1 /* | |
2 * main.cpp -- test Size (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 Size" | |
20 #include <boost/test/unit_test.hpp> | |
21 | |
22 #include <malikania/js_size.hpp> | |
23 | |
24 namespace mlk { | |
25 | |
26 namespace { | |
27 | |
28 class test_size { | |
29 protected: | |
30 dukx_context m_ctx; | |
31 | |
32 public: | |
33 test_size() | |
34 { | |
35 duk_push_object(m_ctx); | |
36 duk_put_global_string(m_ctx, "Malikania"); | |
37 dukx_load_size(m_ctx); | |
38 } | |
39 }; | |
40 | |
41 BOOST_FIXTURE_TEST_SUITE(test_size_suite, test_size) | |
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 "s = Malikania.Size();" | |
55 "w = s.width;" | |
56 "h = s.height;" | |
57 ); | |
58 | |
59 if (ret != 0) | |
60 throw dukx_get_exception(m_ctx, -1); | |
61 | |
62 duk_get_global_string(m_ctx, "w"); | |
63 BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); | |
64 duk_pop(m_ctx); | |
65 duk_get_global_string(m_ctx, "h"); | |
66 BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); | |
67 duk_pop(m_ctx); | |
68 } catch (const std::exception &ex) { | |
69 BOOST_FAIL(ex.what()); | |
70 } | |
71 } | |
72 | |
73 BOOST_AUTO_TEST_CASE(constructor_2_args) | |
74 { | |
75 try { | |
76 const auto ret = duk_peval_string(m_ctx, | |
77 "s = Malikania.Size(100, 200);" | |
78 "w = s.width;" | |
79 "h = s.height;" | |
80 ); | |
81 | |
82 if (ret != 0) | |
83 throw dukx_get_exception(m_ctx, -1); | |
84 | |
85 duk_get_global_string(m_ctx, "w"); | |
86 BOOST_REQUIRE_EQUAL(100U, duk_to_uint(m_ctx, -1)); | |
87 duk_pop(m_ctx); | |
88 duk_get_global_string(m_ctx, "h"); | |
89 BOOST_REQUIRE_EQUAL(200U, duk_to_uint(m_ctx, -1)); | |
90 duk_pop(m_ctx); | |
91 } catch (const std::exception &ex) { | |
92 BOOST_FAIL(ex.what()); | |
93 } | |
94 } | |
95 | |
96 BOOST_AUTO_TEST_CASE(constructor_object) | |
97 { | |
98 try { | |
99 const auto ret = duk_peval_string(m_ctx, | |
100 "s = Malikania.Size({ width: 100, height: 200 });" | |
101 "w = s.width;" | |
102 "h = s.height;" | |
103 ); | |
104 | |
105 if (ret != 0) | |
106 throw dukx_get_exception(m_ctx, -1); | |
107 | |
108 duk_get_global_string(m_ctx, "w"); | |
109 BOOST_REQUIRE_EQUAL(100U, duk_to_uint(m_ctx, -1)); | |
110 duk_pop(m_ctx); | |
111 duk_get_global_string(m_ctx, "h"); | |
112 BOOST_REQUIRE_EQUAL(200U, duk_to_uint(m_ctx, -1)); | |
113 duk_pop(m_ctx); | |
114 } catch (const std::exception &ex) { | |
115 BOOST_FAIL(ex.what()); | |
116 } | |
117 } | |
118 | |
119 BOOST_AUTO_TEST_CASE(constructor_new) | |
120 { | |
121 try { | |
122 const auto ret = duk_peval_string(m_ctx, | |
123 "s = new Malikania.Size({ width: 100, height: 200 });" | |
124 "w = s.width;" | |
125 "h = s.height;" | |
126 ); | |
127 | |
128 if (ret != 0) | |
129 throw dukx_get_exception(m_ctx, -1); | |
130 | |
131 duk_get_global_string(m_ctx, "w"); | |
132 BOOST_REQUIRE_EQUAL(100U, duk_to_uint(m_ctx, -1)); | |
133 duk_pop(m_ctx); | |
134 duk_get_global_string(m_ctx, "h"); | |
135 BOOST_REQUIRE_EQUAL(200U, duk_to_uint(m_ctx, -1)); | |
136 duk_pop(m_ctx); | |
137 } catch (const std::exception &ex) { | |
138 BOOST_FAIL(ex.what()); | |
139 } | |
140 } | |
141 | |
142 BOOST_AUTO_TEST_SUITE_END() | |
143 | |
144 /* | |
145 * Invalid constructors. | |
146 * ------------------------------------------------------------------ | |
147 */ | |
148 | |
149 BOOST_AUTO_TEST_SUITE(invalid_constructors) | |
150 | |
151 BOOST_AUTO_TEST_CASE(constructor_arg_1) | |
152 { | |
153 try { | |
154 const auto ret = duk_peval_string(m_ctx, | |
155 "try {" | |
156 " Malikania.Size(null);" | |
157 "} catch (e) {" | |
158 " name = e.name;" | |
159 " correct = (e instanceof TypeError);" | |
160 "}" | |
161 ); | |
162 | |
163 if (ret != 0) | |
164 throw dukx_get_exception(m_ctx, -1); | |
165 | |
166 duk_get_global_string(m_ctx, "name"); | |
167 BOOST_REQUIRE_EQUAL("TypeError", duk_to_string(m_ctx, -1)); | |
168 duk_pop(m_ctx); | |
169 duk_get_global_string(m_ctx, "correct"); | |
170 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1)); | |
171 duk_pop(m_ctx); | |
172 } catch (const std::exception &ex) { | |
173 BOOST_FAIL(ex.what()); | |
174 } | |
175 } | |
176 | |
177 BOOST_AUTO_TEST_CASE(constructor_range_1) | |
178 { | |
179 try { | |
180 const auto ret = duk_peval_string(m_ctx, | |
181 "try {" | |
182 " Malikania.Size(-1, 200);" | |
183 "} catch (e) {" | |
184 " name = e.name;" | |
185 " correct = (e instanceof RangeError);" | |
186 "}" | |
187 ); | |
188 | |
189 if (ret != 0) | |
190 throw dukx_get_exception(m_ctx, -1); | |
191 | |
192 duk_get_global_string(m_ctx, "name"); | |
193 BOOST_REQUIRE_EQUAL("RangeError", duk_to_string(m_ctx, -1)); | |
194 duk_pop(m_ctx); | |
195 duk_get_global_string(m_ctx, "correct"); | |
196 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1)); | |
197 duk_pop(m_ctx); | |
198 } catch (const std::exception &ex) { | |
199 BOOST_FAIL(ex.what()); | |
200 } | |
201 } | |
202 | |
203 BOOST_AUTO_TEST_CASE(constructor_range_2) | |
204 { | |
205 try { | |
206 const auto ret = duk_peval_string(m_ctx, | |
207 "try {" | |
208 " Malikania.Size(100, -1);" | |
209 "} catch (e) {" | |
210 " name = e.name;" | |
211 " correct = (e instanceof RangeError);" | |
212 "}" | |
213 ); | |
214 | |
215 if (ret != 0) | |
216 throw dukx_get_exception(m_ctx, -1); | |
217 | |
218 duk_get_global_string(m_ctx, "name"); | |
219 BOOST_REQUIRE_EQUAL("RangeError", duk_to_string(m_ctx, -1)); | |
220 duk_pop(m_ctx); | |
221 duk_get_global_string(m_ctx, "correct"); | |
222 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1)); | |
223 duk_pop(m_ctx); | |
224 } catch (const std::exception &ex) { | |
225 BOOST_FAIL(ex.what()); | |
226 } | |
227 } | |
228 | |
229 BOOST_AUTO_TEST_CASE(constructor_range_3) | |
230 { | |
231 try { | |
232 const auto ret = duk_peval_string(m_ctx, | |
233 "try {" | |
234 " Malikania.Size({ width: -1, height: 200 });" | |
235 "} catch (e) {" | |
236 " name = e.name;" | |
237 " correct = (e instanceof RangeError);" | |
238 "}" | |
239 ); | |
240 | |
241 if (ret != 0) | |
242 throw dukx_get_exception(m_ctx, -1); | |
243 | |
244 duk_get_global_string(m_ctx, "name"); | |
245 BOOST_REQUIRE_EQUAL("RangeError", duk_to_string(m_ctx, -1)); | |
246 duk_pop(m_ctx); | |
247 duk_get_global_string(m_ctx, "correct"); | |
248 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1)); | |
249 duk_pop(m_ctx); | |
250 } catch (const std::exception &ex) { | |
251 BOOST_FAIL(ex.what()); | |
252 } | |
253 } | |
254 | |
255 BOOST_AUTO_TEST_CASE(constructor_range_4) | |
256 { | |
257 try { | |
258 const auto ret = duk_peval_string(m_ctx, | |
259 "try {" | |
260 " Malikania.Size({ width: 100, height: -1 });" | |
261 "} catch (e) {" | |
262 " name = e.name;" | |
263 " correct = (e instanceof RangeError);" | |
264 "}" | |
265 ); | |
266 | |
267 if (ret != 0) | |
268 throw dukx_get_exception(m_ctx, -1); | |
269 | |
270 duk_get_global_string(m_ctx, "name"); | |
271 BOOST_REQUIRE_EQUAL("RangeError", duk_to_string(m_ctx, -1)); | |
272 duk_pop(m_ctx); | |
273 duk_get_global_string(m_ctx, "correct"); | |
274 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1)); | |
275 duk_pop(m_ctx); | |
276 } catch (const std::exception &ex) { | |
277 BOOST_FAIL(ex.what()); | |
278 } | |
279 } | |
280 | |
281 BOOST_AUTO_TEST_SUITE_END() | |
282 | |
283 /* | |
284 * Require. | |
285 * ------------------------------------------------------------------ | |
286 */ | |
287 | |
288 BOOST_AUTO_TEST_SUITE(require) | |
289 | |
290 BOOST_AUTO_TEST_CASE(success) | |
291 { | |
292 try { | |
293 duk_push_c_function(m_ctx, [] (auto ctx) { | |
294 const auto size = dukx_require_size(ctx, 0); | |
295 | |
296 duk_push_uint(ctx, size.width); | |
297 duk_put_global_string(ctx, "w"); | |
298 duk_push_uint(ctx, size.height); | |
299 duk_put_global_string(ctx, "h"); | |
300 | |
301 return 0; | |
302 }, 1); | |
303 duk_put_global_string(m_ctx, "build"); | |
304 | |
305 const auto ret = duk_peval_string(m_ctx, "build({ width: 100, height: 200 });"); | |
306 | |
307 if (ret != 0) | |
308 throw dukx_get_exception(m_ctx, -1); | |
309 | |
310 duk_get_global_string(m_ctx, "w"); | |
311 BOOST_REQUIRE_EQUAL(100U, duk_to_uint(m_ctx, -1)); | |
312 duk_pop(m_ctx); | |
313 duk_get_global_string(m_ctx, "h"); | |
314 BOOST_REQUIRE_EQUAL(200U, duk_to_uint(m_ctx, -1)); | |
315 duk_pop(m_ctx); | |
316 } catch (const std::exception &ex) { | |
317 BOOST_FAIL(ex.what()); | |
318 } | |
319 } | |
320 | |
321 BOOST_AUTO_TEST_CASE(fail) | |
322 { | |
323 try { | |
324 duk_push_c_function(m_ctx, [] (auto ctx) { | |
325 dukx_require_size(ctx, 0); | |
326 | |
327 return 0; | |
328 }, 1); | |
329 duk_put_global_string(m_ctx, "build"); | |
330 | |
331 const auto ret = duk_peval_string(m_ctx, | |
332 "try {" | |
333 " build({});" | |
334 "} catch (e) {" | |
335 " name = e.name;" | |
336 " correct = (e instanceof Error);" | |
337 "}" | |
338 ); | |
339 | |
340 if (ret != 0) | |
341 throw dukx_get_exception(m_ctx, -1); | |
342 | |
343 duk_get_global_string(m_ctx, "name"); | |
344 BOOST_REQUIRE_EQUAL("Error", duk_to_string(m_ctx, -1)); | |
345 duk_pop(m_ctx); | |
346 duk_get_global_string(m_ctx, "correct"); | |
347 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1)); | |
348 duk_pop(m_ctx); | |
349 } catch (const std::exception &ex) { | |
350 BOOST_FAIL(ex.what()); | |
351 } | |
352 } | |
353 | |
354 BOOST_AUTO_TEST_SUITE_END() | |
355 | |
356 /* | |
357 * Get. | |
358 * ------------------------------------------------------------------ | |
359 */ | |
360 | |
361 BOOST_AUTO_TEST_SUITE(get) | |
362 | |
363 BOOST_AUTO_TEST_CASE(adjust_all) | |
364 { | |
365 try { | |
366 duk_push_c_function(m_ctx, [] (auto ctx) { | |
367 const auto size = dukx_get_size(ctx, 0); | |
368 | |
369 duk_push_uint(ctx, size.width); | |
370 duk_put_global_string(ctx, "w"); | |
371 duk_push_uint(ctx, size.height); | |
372 duk_put_global_string(ctx, "h"); | |
373 | |
374 return 0; | |
375 }, 1); | |
376 duk_put_global_string(m_ctx, "build"); | |
377 | |
378 const auto ret = duk_peval_string(m_ctx, "build({});"); | |
379 | |
380 if (ret != 0) | |
381 throw dukx_get_exception(m_ctx, -1); | |
382 | |
383 duk_get_global_string(m_ctx, "w"); | |
384 BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); | |
385 duk_pop(m_ctx); | |
386 duk_get_global_string(m_ctx, "h"); | |
387 BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); | |
388 duk_pop(m_ctx); | |
389 } catch (const std::exception &ex) { | |
390 BOOST_FAIL(ex.what()); | |
391 } | |
392 } | |
393 | |
394 BOOST_AUTO_TEST_SUITE_END() | |
395 | |
396 BOOST_AUTO_TEST_SUITE_END() | |
397 | |
398 } // !namespace | |
399 | |
400 } // !mlk |