Mercurial > malikania
comparison examples/js-window/main.cpp @ 80:a162f380f02e
CMake: create examples, closes #615
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 22 Jan 2017 09:59:14 +0100 |
parents | 78de82cc6bde |
children | 119bcc5a727e |
comparison
equal
deleted
inserted
replaced
79:8b41e9a2e095 | 80:a162f380f02e |
---|---|
1 /* | 1 /* |
2 * main.cpp -- test Window (JavaScript binding) | 2 * main.cpp -- test Window (JavaScript binding) |
3 * | 3 * |
4 * Copyright (c) 2013-2016 Malikania Authors | 4 * Copyright (c) 2013-2017 Malikania Authors |
5 * | 5 * |
6 * Permission to use, copy, modify, and/or distribute this software for any | 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 | 7 * purpose with or without fee is hereby granted, provided that the above |
8 * copyright notice and this permission notice appear in all copies. | 8 * copyright notice and this permission notice appear in all copies. |
9 * | 9 * |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 */ | 17 */ |
18 | 18 |
19 #include <chrono> | 19 #include <chrono> |
20 #include <iostream> | |
20 #include <thread> | 21 #include <thread> |
21 | |
22 #define BOOST_TEST_MODULE "Javascript Window" | |
23 #include <boost/test/unit_test.hpp> | |
24 | 22 |
25 #include <malikania/js_window.hpp> | 23 #include <malikania/js_window.hpp> |
26 | 24 |
27 using namespace mlk; | |
28 using namespace std::chrono_literals; | 25 using namespace std::chrono_literals; |
29 | 26 |
30 class test_window { | 27 void basic(dukx_context& ctx) |
31 protected: | 28 { |
32 dukx_context m_ctx; | 29 auto ret = duk_peval_string(ctx, |
30 "w = new Malikania.Window();" | |
31 "w.setDrawingColor('lightskyblue');" | |
32 "w.clear();" | |
33 "w.present();" | |
34 ); | |
33 | 35 |
34 public: | 36 if (ret != 0) { |
35 test_window() | 37 throw dukx_get_exception(ctx, -1); |
36 { | |
37 duk_push_object(m_ctx); | |
38 duk_put_global_string(m_ctx, "Malikania"); | |
39 mlk::dukx_load_window(m_ctx); | |
40 } | 38 } |
41 }; | |
42 | 39 |
43 BOOST_FIXTURE_TEST_SUITE(test_window_suite, test_window) | 40 std::this_thread::sleep_for(3s); |
41 } | |
44 | 42 |
45 BOOST_AUTO_TEST_CASE(basic) | 43 void rect(dukx_context& ctx) |
44 { | |
45 auto ret = duk_peval_string(ctx, | |
46 "w = new Malikania.Window();" | |
47 "w.setDrawingColor('lightskyblue');" | |
48 "w.clear();" | |
49 "w.setDrawingColor('white');" | |
50 "w.drawRectangle({ x: 10, y: 10, width: 10, height: 10 });" | |
51 "w.present();" | |
52 ); | |
53 | |
54 if (ret != 0) { | |
55 throw dukx_get_exception(ctx, -1); | |
56 } | |
57 | |
58 std::this_thread::sleep_for(3s); | |
59 } | |
60 | |
61 int main() | |
46 { | 62 { |
47 try { | 63 try { |
48 auto ret = duk_peval_string(m_ctx, | 64 dukx_context ctx; |
49 "w = new Malikania.Window();" | |
50 "w.setDrawingColor('lightskyblue');" | |
51 "w.clear();" | |
52 "w.present();" | |
53 ); | |
54 | 65 |
55 if (ret != 0) { | 66 duk_push_object(ctx); |
56 throw dukx_get_exception(m_ctx, -1); | 67 duk_put_global_string(ctx, "Malikania"); |
57 } | 68 mlk::dukx_load_window(ctx); |
58 | 69 |
59 std::this_thread::sleep_for(3s); | 70 basic(ctx); |
60 } catch (const std::exception &ex) { | 71 rect(ctx); |
61 BOOST_FAIL(ex.what()); | 72 } catch (const std::exception& ex) { |
73 std::cerr << ex.what() << std::endl; | |
74 return 1; | |
62 } | 75 } |
63 } | 76 } |
64 | |
65 BOOST_AUTO_TEST_CASE(rect) | |
66 { | |
67 try { | |
68 auto ret = duk_peval_string(m_ctx, | |
69 "w = new Malikania.Window();" | |
70 "w.setDrawingColor('lightskyblue');" | |
71 "w.clear();" | |
72 "w.setDrawingColor('white');" | |
73 "w.drawRectangle({ x: 10, y: 10, width: 10, height: 10 });" | |
74 "w.present();" | |
75 ); | |
76 | |
77 if (ret != 0) { | |
78 throw dukx_get_exception(m_ctx, -1); | |
79 } | |
80 | |
81 std::this_thread::sleep_for(3s); | |
82 } catch (const std::exception &ex) { | |
83 BOOST_FAIL(ex.what()); | |
84 } | |
85 } | |
86 | |
87 BOOST_AUTO_TEST_SUITE_END() |