comparison tests/src/libirccd-js/js-api-irccd/main.cpp @ 758:445c071e8efb

Irccd: Javascript API cleanup - Place all related code to `js` namespace, - Import duk.hpp and duk.cpp from libduk.
author David Demelier <markand@malikania.fr>
date Thu, 09 Aug 2018 13:07:19 +0200
parents tests/src/libirccd-js/jsapi-irccd/main.cpp@97b356010785
children 35c1517d705d
comparison
equal deleted inserted replaced
757:97b356010785 758:445c071e8efb
1 /*
2 * main.cpp -- test Irccd API
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 "Irccd Javascript API"
20 #include <boost/test/unit_test.hpp>
21
22 #include <irccd/test/js_fixture.hpp>
23
24 using namespace irccd::js;
25
26 namespace irccd::test {
27
28 namespace {
29
30 BOOST_FIXTURE_TEST_SUITE(irccd_js_api_suite, js_fixture)
31
32 BOOST_AUTO_TEST_CASE(version)
33 {
34 const auto ret = duk_peval_string(plugin_->get_context(),
35 "major = Irccd.version.major;"
36 "minor = Irccd.version.minor;"
37 "patch = Irccd.version.patch;"
38 );
39
40 if (ret != 0)
41 throw duk::get_stack(plugin_->get_context(), -1);
42
43 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "major"));
44 BOOST_TEST(IRCCD_VERSION_MAJOR == duk_get_int(plugin_->get_context(), -1));
45 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "minor"));
46 BOOST_TEST(IRCCD_VERSION_MINOR == duk_get_int(plugin_->get_context(), -1));
47 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "patch"));
48 BOOST_TEST(IRCCD_VERSION_PATCH == duk_get_int(plugin_->get_context(), -1));
49 }
50
51 BOOST_AUTO_TEST_CASE(from_javascript)
52 {
53 const auto ret = duk_peval_string(plugin_->get_context(),
54 "try {"
55 " throw new Irccd.SystemError(1, 'test');"
56 "} catch (e) {"
57 " errno = e.errno;"
58 " name = e.name;"
59 " message = e.message;"
60 " v1 = (e instanceof Error);"
61 " v2 = (e instanceof Irccd.SystemError);"
62 "}"
63 );
64
65 if (ret != 0)
66 throw duk::get_stack(plugin_->get_context(), -1);
67
68 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "errno"));
69 BOOST_TEST(1 == duk_get_int(plugin_->get_context(), -1));
70 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "name"));
71 BOOST_TEST("SystemError" == duk_get_string(plugin_->get_context(), -1));
72 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "message"));
73 BOOST_TEST("test" == duk_get_string(plugin_->get_context(), -1));
74 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "v1"));
75 BOOST_TEST(duk_get_boolean(plugin_->get_context(), -1));
76 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "v2"));
77 BOOST_TEST(duk_get_boolean(plugin_->get_context(), -1));
78 }
79
80 BOOST_AUTO_TEST_CASE(from_native)
81 {
82 duk_push_c_function(plugin_->get_context(), [] (duk_context *ctx) -> duk_ret_t {
83 duk::raise(ctx, std::system_error(make_error_code(std::errc::invalid_argument)));
84
85 return 0;
86 }, 0);
87
88 duk_put_global_string(plugin_->get_context(), "f");
89
90 const auto ret = duk_peval_string(plugin_->get_context(),
91 "try {"
92 " f();"
93 "} catch (e) {"
94 " errno = e.errno;"
95 " name = e.name;"
96 " v1 = (e instanceof Error);"
97 " v2 = (e instanceof Irccd.SystemError);"
98 "}"
99 );
100
101 if (ret != 0)
102 throw duk::get_stack(plugin_->get_context(), -1);
103
104 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "errno"));
105 BOOST_TEST(EINVAL == duk_get_int(plugin_->get_context(), -1));
106 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "name"));
107 BOOST_TEST("SystemError" == duk_get_string(plugin_->get_context(), -1));
108 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "v1"));
109 BOOST_TEST(duk_get_boolean(plugin_->get_context(), -1));
110 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "v2"));
111 BOOST_TEST(duk_get_boolean(plugin_->get_context(), -1));
112 }
113
114 BOOST_AUTO_TEST_SUITE_END()
115
116 } // !namespace
117
118 } // !irccd::test