Mercurial > malikania
comparison tests/libclient/js-color/main.cpp @ 12:0c62e0af6af7
Client: add JavaScript bindings for Color, #445
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 01 Apr 2016 13:45:45 +0200 |
parents | |
children | 0a1adf7dcca0 |
comparison
equal
deleted
inserted
replaced
11:fe95a8db2970 | 12:0c62e0af6af7 |
---|---|
1 /* | |
2 * main.cpp -- test Color (JavaScript binding) | |
3 * | |
4 * Copyright (c) 2013-2016 Malikania Authors | |
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 #include <gtest/gtest.h> | |
20 | |
21 #include <malikania/js-color.h> | |
22 | |
23 using namespace malikania; | |
24 | |
25 class TestColor : public testing::Test { | |
26 protected: | |
27 duk::Context m_ctx; | |
28 | |
29 public: | |
30 TestColor() | |
31 { | |
32 duk::putGlobal(m_ctx, "Malikania", duk::Object()); | |
33 | |
34 loadMalikaniaColor(m_ctx); | |
35 } | |
36 }; | |
37 | |
38 /* | |
39 * Valid constructors | |
40 * ------------------------------------------------------------------ | |
41 */ | |
42 | |
43 TEST_F(TestColor, ConstructorNoArgs) | |
44 { | |
45 try { | |
46 auto ret = duk::pevalString(m_ctx, | |
47 "c = Malikania.Color();" | |
48 "r = c.red;" | |
49 "g = c.green;" | |
50 "b = c.blue;" | |
51 "a = c.alpha;" | |
52 ); | |
53 | |
54 if (ret != 0) { | |
55 throw duk::error(m_ctx, -1); | |
56 } | |
57 | |
58 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r")); | |
59 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g")); | |
60 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "b")); | |
61 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); | |
62 } catch (const std::exception &ex) { | |
63 FAIL() << ex.what(); | |
64 } | |
65 } | |
66 | |
67 TEST_F(TestColor, ConstructorString) | |
68 { | |
69 try { | |
70 auto ret = duk::pevalString(m_ctx, | |
71 "c = Malikania.Color('white');" | |
72 "r = c.red;" | |
73 "g = c.green;" | |
74 "b = c.blue;" | |
75 "a = c.alpha;" | |
76 ); | |
77 | |
78 if (ret != 0) { | |
79 throw duk::error(m_ctx, -1); | |
80 } | |
81 | |
82 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "r")); | |
83 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "g")); | |
84 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "b")); | |
85 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); | |
86 } catch (const std::exception &ex) { | |
87 FAIL() << ex.what(); | |
88 } | |
89 } | |
90 | |
91 TEST_F(TestColor, ConstructorStringRgb) | |
92 { | |
93 try { | |
94 auto ret = duk::pevalString(m_ctx, | |
95 "c = Malikania.Color('#0000ff');" | |
96 "r = c.red;" | |
97 "g = c.green;" | |
98 "b = c.blue;" | |
99 "a = c.alpha;" | |
100 ); | |
101 | |
102 if (ret != 0) { | |
103 throw duk::error(m_ctx, -1); | |
104 } | |
105 | |
106 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r")); | |
107 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g")); | |
108 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "b")); | |
109 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); | |
110 } catch (const std::exception &ex) { | |
111 FAIL() << ex.what(); | |
112 } | |
113 } | |
114 | |
115 TEST_F(TestColor, Constructor3Args) | |
116 { | |
117 try { | |
118 auto ret = duk::pevalString(m_ctx, | |
119 "c = Malikania.Color(10, 20, 30);" | |
120 "r = c.red;" | |
121 "g = c.green;" | |
122 "b = c.blue;" | |
123 "a = c.alpha;" | |
124 ); | |
125 | |
126 if (ret != 0) { | |
127 throw duk::error(m_ctx, -1); | |
128 } | |
129 | |
130 ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "r")); | |
131 ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "g")); | |
132 ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "b")); | |
133 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); | |
134 } catch (const std::exception &ex) { | |
135 FAIL() << ex.what(); | |
136 } | |
137 } | |
138 | |
139 TEST_F(TestColor, Constructor4Args) | |
140 { | |
141 try { | |
142 auto ret = duk::pevalString(m_ctx, | |
143 "c = Malikania.Color(10, 20, 30, 40);" | |
144 "r = c.red;" | |
145 "g = c.green;" | |
146 "b = c.blue;" | |
147 "a = c.alpha;" | |
148 ); | |
149 | |
150 if (ret != 0) { | |
151 throw duk::error(m_ctx, -1); | |
152 } | |
153 | |
154 ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "r")); | |
155 ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "g")); | |
156 ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "b")); | |
157 ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "a")); | |
158 } catch (const std::exception &ex) { | |
159 FAIL() << ex.what(); | |
160 } | |
161 } | |
162 | |
163 TEST_F(TestColor, ConstructorObjectNoAlpha) | |
164 { | |
165 try { | |
166 auto ret = duk::pevalString(m_ctx, | |
167 "c = Malikania.Color({ red: 10, green: 20, blue: 30 });" | |
168 "r = c.red;" | |
169 "g = c.green;" | |
170 "b = c.blue;" | |
171 "a = c.alpha;" | |
172 ); | |
173 | |
174 if (ret != 0) { | |
175 throw duk::error(m_ctx, -1); | |
176 } | |
177 | |
178 ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "r")); | |
179 ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "g")); | |
180 ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "b")); | |
181 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); | |
182 } catch (const std::exception &ex) { | |
183 FAIL() << ex.what(); | |
184 } | |
185 } | |
186 | |
187 TEST_F(TestColor, ConstructorObjectAlpha) | |
188 { | |
189 try { | |
190 auto ret = duk::pevalString(m_ctx, | |
191 "c = Malikania.Color({ red: 10, green: 20, blue: 30, alpha: 40 });" | |
192 "r = c.red;" | |
193 "g = c.green;" | |
194 "b = c.blue;" | |
195 "a = c.alpha;" | |
196 ); | |
197 | |
198 if (ret != 0) { | |
199 throw duk::error(m_ctx, -1); | |
200 } | |
201 | |
202 ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "r")); | |
203 ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "g")); | |
204 ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "b")); | |
205 ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "a")); | |
206 } catch (const std::exception &ex) { | |
207 FAIL() << ex.what(); | |
208 } | |
209 } | |
210 | |
211 TEST_F(TestColor, ConstructorNew) | |
212 { | |
213 /* | |
214 * Just one test, function parse the same way, only 'this' or a new object is returned. | |
215 */ | |
216 try { | |
217 auto ret = duk::pevalString(m_ctx, | |
218 "c = new Malikania.Color({ red: 10, green: 20, blue: 30, alpha: 40 });" | |
219 "r = c.red;" | |
220 "g = c.green;" | |
221 "b = c.blue;" | |
222 "a = c.alpha;" | |
223 ); | |
224 | |
225 if (ret != 0) { | |
226 throw duk::error(m_ctx, -1); | |
227 } | |
228 | |
229 ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "r")); | |
230 ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "g")); | |
231 ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "b")); | |
232 ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "a")); | |
233 } catch (const std::exception &ex) { | |
234 FAIL() << ex.what(); | |
235 } | |
236 } | |
237 | |
238 /* | |
239 * Valid constructors | |
240 * ------------------------------------------------------------------ | |
241 */ | |
242 | |
243 TEST_F(TestColor, InvalidConstructorArg1) | |
244 { | |
245 try { | |
246 auto ret = duk::pevalString(m_ctx, | |
247 "try {" | |
248 " Malikania.Color(null);" | |
249 "} catch (e) {" | |
250 " name = e.name;" | |
251 " correct = (e instanceof TypeError);" | |
252 "}" | |
253 ); | |
254 | |
255 if (ret != 0) { | |
256 throw duk::error(m_ctx, -1); | |
257 } | |
258 | |
259 ASSERT_EQ("TypeError", duk::getGlobal<std::string>(m_ctx, "name")); | |
260 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); | |
261 } catch (const std::exception &ex) { | |
262 FAIL() << ex.what(); | |
263 } | |
264 } | |
265 | |
266 TEST_F(TestColor, InvalidConstructorArg2) | |
267 { | |
268 try { | |
269 auto ret = duk::pevalString(m_ctx, | |
270 "try {" | |
271 " Malikania.Color(10, null, 30);" | |
272 "} catch (e) {" | |
273 " name = e.name;" | |
274 " correct = (e instanceof TypeError);" | |
275 "}" | |
276 ); | |
277 | |
278 if (ret != 0) { | |
279 throw duk::error(m_ctx, -1); | |
280 } | |
281 | |
282 ASSERT_EQ("TypeError", duk::getGlobal<std::string>(m_ctx, "name")); | |
283 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); | |
284 } catch (const std::exception &ex) { | |
285 FAIL() << ex.what(); | |
286 } | |
287 } | |
288 | |
289 TEST_F(TestColor, InvalidConstructorRange1) | |
290 { | |
291 try { | |
292 auto ret = duk::pevalString(m_ctx, | |
293 "try {" | |
294 " Malikania.Color(-1, 20, 30);" | |
295 "} catch (e) {" | |
296 " name = e.name;" | |
297 " correct = (e instanceof RangeError);" | |
298 "}" | |
299 ); | |
300 | |
301 if (ret != 0) { | |
302 throw duk::error(m_ctx, -1); | |
303 } | |
304 | |
305 ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); | |
306 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); | |
307 } catch (const std::exception &ex) { | |
308 FAIL() << ex.what(); | |
309 } | |
310 } | |
311 | |
312 TEST_F(TestColor, InvalidConstructorRange2) | |
313 { | |
314 try { | |
315 auto ret = duk::pevalString(m_ctx, | |
316 "try {" | |
317 " Malikania.Color(10, 20, 256);" | |
318 "} catch (e) {" | |
319 " name = e.name;" | |
320 " correct = (e instanceof RangeError);" | |
321 "}" | |
322 ); | |
323 | |
324 if (ret != 0) { | |
325 throw duk::error(m_ctx, -1); | |
326 } | |
327 | |
328 ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); | |
329 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); | |
330 } catch (const std::exception &ex) { | |
331 FAIL() << ex.what(); | |
332 } | |
333 } | |
334 | |
335 TEST_F(TestColor, InvalidConstructorRange3) | |
336 { | |
337 try { | |
338 auto ret = duk::pevalString(m_ctx, | |
339 "try {" | |
340 " Malikania.Color(10, 20, 30, 800);" | |
341 "} catch (e) {" | |
342 " name = e.name;" | |
343 " correct = (e instanceof RangeError);" | |
344 "}" | |
345 ); | |
346 | |
347 if (ret != 0) { | |
348 throw duk::error(m_ctx, -1); | |
349 } | |
350 | |
351 ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); | |
352 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); | |
353 } catch (const std::exception &ex) { | |
354 FAIL() << ex.what(); | |
355 } | |
356 } | |
357 | |
358 TEST_F(TestColor, InvalidConstructorRange4) | |
359 { | |
360 try { | |
361 auto ret = duk::pevalString(m_ctx, | |
362 "try {" | |
363 " Malikania.Color({ red: -1, green: 20, blue: 30 });" | |
364 "} catch (e) {" | |
365 " name = e.name;" | |
366 " correct = (e instanceof RangeError);" | |
367 "}" | |
368 ); | |
369 | |
370 if (ret != 0) { | |
371 throw duk::error(m_ctx, -1); | |
372 } | |
373 | |
374 ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); | |
375 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); | |
376 } catch (const std::exception &ex) { | |
377 FAIL() << ex.what(); | |
378 } | |
379 } | |
380 | |
381 TEST_F(TestColor, InvalidConstructorRange5) | |
382 { | |
383 try { | |
384 auto ret = duk::pevalString(m_ctx, | |
385 "try {" | |
386 " Malikania.Color({ red: 10, green: 20, blue: 30, alpha: 800 });" | |
387 "} catch (e) {" | |
388 " name = e.name;" | |
389 " correct = (e instanceof RangeError);" | |
390 "}" | |
391 ); | |
392 | |
393 if (ret != 0) { | |
394 throw duk::error(m_ctx, -1); | |
395 } | |
396 | |
397 ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); | |
398 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); | |
399 } catch (const std::exception &ex) { | |
400 FAIL() << ex.what(); | |
401 } | |
402 } | |
403 | |
404 TEST_F(TestColor, InvalidConstructorStringUnknown) | |
405 { | |
406 try { | |
407 auto ret = duk::pevalString(m_ctx, | |
408 "try {" | |
409 " Malikania.Color('does not exist');" | |
410 "} catch (e) {" | |
411 " name = e.name;" | |
412 " correct = (e instanceof Error);" | |
413 "}" | |
414 ); | |
415 | |
416 if (ret != 0) { | |
417 throw duk::error(m_ctx, -1); | |
418 } | |
419 | |
420 ASSERT_EQ("Error", duk::getGlobal<std::string>(m_ctx, "name")); | |
421 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); | |
422 } catch (const std::exception &ex) { | |
423 FAIL() << ex.what(); | |
424 } | |
425 } | |
426 | |
427 TEST_F(TestColor, InvalidConstructorStringRgb) | |
428 { | |
429 try { | |
430 auto ret = duk::pevalString(m_ctx, | |
431 "try {" | |
432 " Malikania.Color('#ghijkl');" | |
433 "} catch (e) {" | |
434 " name = e.name;" | |
435 " correct = (e instanceof Error);" | |
436 "}" | |
437 ); | |
438 | |
439 if (ret != 0) { | |
440 throw duk::error(m_ctx, -1); | |
441 } | |
442 | |
443 ASSERT_EQ("Error", duk::getGlobal<std::string>(m_ctx, "name")); | |
444 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); | |
445 } catch (const std::exception &ex) { | |
446 FAIL() << ex.what(); | |
447 } | |
448 } | |
449 | |
450 /* | |
451 * Require. | |
452 * ------------------------------------------------------------------ | |
453 * | |
454 * TypeTraits<Color>::require expect to have a valid color, it raises an error for any invalid component. Only alpha | |
455 * can be ommitted but if it is present and incorrect, an exception is also raised. | |
456 */ | |
457 | |
458 TEST_F(TestColor, requireSuccess) | |
459 { | |
460 try { | |
461 duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { | |
462 Color color = duk::require<Color>(ctx, 0); | |
463 | |
464 duk::putGlobal(ctx, "r", static_cast<int>(color.red())); | |
465 duk::putGlobal(ctx, "g", static_cast<int>(color.green())); | |
466 duk::putGlobal(ctx, "b", static_cast<int>(color.blue())); | |
467 duk::putGlobal(ctx, "a", static_cast<int>(color.alpha())); | |
468 | |
469 return 0; | |
470 }, 1}); | |
471 | |
472 auto ret = duk::pevalString(m_ctx, "draw('#ff0000');"); | |
473 | |
474 if (ret != 0) { | |
475 throw duk::error(m_ctx, -1); | |
476 } | |
477 | |
478 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "r")); | |
479 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g")); | |
480 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "b")); | |
481 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); | |
482 } catch (const std::exception &ex) { | |
483 FAIL() << ex.what(); | |
484 } | |
485 } | |
486 | |
487 TEST_F(TestColor, requireFail) | |
488 { | |
489 try { | |
490 duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { | |
491 duk::require<Color>(ctx, 0); | |
492 | |
493 return 0; | |
494 }, 1}); | |
495 | |
496 auto ret = duk::pevalString(m_ctx, | |
497 "try {" | |
498 " draw('#ghijkl');" | |
499 "} catch (e) {" | |
500 " name = e.name;" | |
501 " correct = (e instanceof Error);" | |
502 "}" | |
503 ); | |
504 | |
505 if (ret != 0) { | |
506 throw duk::error(m_ctx, -1); | |
507 } | |
508 | |
509 ASSERT_EQ("Error", duk::getGlobal<std::string>(m_ctx, "name")); | |
510 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); | |
511 } catch (const std::exception &ex) { | |
512 FAIL() << ex.what(); | |
513 } | |
514 } | |
515 | |
516 TEST_F(TestColor, requireFailAlpha) | |
517 { | |
518 try { | |
519 duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { | |
520 duk::require<Color>(ctx, 0); | |
521 | |
522 return 0; | |
523 }, 1}); | |
524 | |
525 auto ret = duk::pevalString(m_ctx, | |
526 "try {" | |
527 " draw({ red: 10, green: 20, blue: 30, alpha: 800 });" | |
528 "} catch (e) {" | |
529 " name = e.name;" | |
530 " correct = (e instanceof RangeError);" | |
531 "}" | |
532 ); | |
533 | |
534 if (ret != 0) { | |
535 throw duk::error(m_ctx, -1); | |
536 } | |
537 | |
538 ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); | |
539 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); | |
540 } catch (const std::exception &ex) { | |
541 FAIL() << ex.what(); | |
542 } | |
543 } | |
544 | |
545 /* | |
546 * Get. | |
547 * ------------------------------------------------------------------ | |
548 * | |
549 * TypeTraits<Color>::get readjust any invalid values. | |
550 */ | |
551 | |
552 TEST_F(TestColor, getNormal) | |
553 { | |
554 try { | |
555 duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { | |
556 Color color = duk::get<Color>(ctx, 0); | |
557 | |
558 duk::putGlobal(ctx, "r", static_cast<int>(color.red())); | |
559 duk::putGlobal(ctx, "g", static_cast<int>(color.green())); | |
560 duk::putGlobal(ctx, "b", static_cast<int>(color.blue())); | |
561 duk::putGlobal(ctx, "a", static_cast<int>(color.alpha())); | |
562 | |
563 return 0; | |
564 }, 1}); | |
565 | |
566 auto ret = duk::pevalString(m_ctx, "draw('#ff0000');"); | |
567 | |
568 if (ret != 0) { | |
569 throw duk::error(m_ctx, -1); | |
570 } | |
571 | |
572 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "r")); | |
573 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g")); | |
574 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "b")); | |
575 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); | |
576 } catch (const std::exception &ex) { | |
577 FAIL() << ex.what(); | |
578 } | |
579 } | |
580 | |
581 TEST_F(TestColor, getAdjustRgb) | |
582 { | |
583 try { | |
584 duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { | |
585 Color color = duk::get<Color>(ctx, 0); | |
586 | |
587 duk::putGlobal(ctx, "r", static_cast<int>(color.red())); | |
588 duk::putGlobal(ctx, "g", static_cast<int>(color.green())); | |
589 duk::putGlobal(ctx, "b", static_cast<int>(color.blue())); | |
590 duk::putGlobal(ctx, "a", static_cast<int>(color.alpha())); | |
591 | |
592 return 0; | |
593 }, 1}); | |
594 | |
595 auto ret = duk::pevalString(m_ctx, "draw('#ghijkl');"); | |
596 | |
597 if (ret != 0) { | |
598 throw duk::error(m_ctx, -1); | |
599 } | |
600 | |
601 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r")); | |
602 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g")); | |
603 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "b")); | |
604 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); | |
605 } catch (const std::exception &ex) { | |
606 FAIL() << ex.what(); | |
607 } | |
608 } | |
609 | |
610 TEST_F(TestColor, getAdjustAll) | |
611 { | |
612 try { | |
613 duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { | |
614 Color color = duk::get<Color>(ctx, 0); | |
615 | |
616 duk::putGlobal(ctx, "r", static_cast<int>(color.red())); | |
617 duk::putGlobal(ctx, "g", static_cast<int>(color.green())); | |
618 duk::putGlobal(ctx, "b", static_cast<int>(color.blue())); | |
619 duk::putGlobal(ctx, "a", static_cast<int>(color.alpha())); | |
620 | |
621 return 0; | |
622 }, 1}); | |
623 | |
624 auto ret = duk::pevalString(m_ctx, "draw({ red: -1, green: 256, blue: 100, alpha: 800 });"); | |
625 | |
626 if (ret != 0) { | |
627 throw duk::error(m_ctx, -1); | |
628 } | |
629 | |
630 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r")); | |
631 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "g")); | |
632 ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "b")); | |
633 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); | |
634 } catch (const std::exception &ex) { | |
635 FAIL() << ex.what(); | |
636 } | |
637 } | |
638 | |
639 TEST_F(TestColor, getSomethingElse) | |
640 { | |
641 try { | |
642 duk::putGlobal(m_ctx, "draw", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { | |
643 Color color = duk::get<Color>(ctx, 0); | |
644 | |
645 duk::putGlobal(ctx, "r", static_cast<int>(color.red())); | |
646 duk::putGlobal(ctx, "g", static_cast<int>(color.green())); | |
647 duk::putGlobal(ctx, "b", static_cast<int>(color.blue())); | |
648 duk::putGlobal(ctx, "a", static_cast<int>(color.alpha())); | |
649 | |
650 return 0; | |
651 }, 1}); | |
652 | |
653 auto ret = duk::pevalString(m_ctx, "draw(null);"); | |
654 | |
655 if (ret != 0) { | |
656 throw duk::error(m_ctx, -1); | |
657 } | |
658 | |
659 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r")); | |
660 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g")); | |
661 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "b")); | |
662 ASSERT_EQ(255, duk::getGlobal<int>(m_ctx, "a")); | |
663 } catch (const std::exception &ex) { | |
664 FAIL() << ex.what(); | |
665 } | |
666 } | |
667 | |
668 int main(int argc, char **argv) | |
669 { | |
670 testing::InitGoogleTest(&argc, argv); | |
671 | |
672 return RUN_ALL_TESTS(); | |
673 } |