Mercurial > irccd
comparison tests/js-irccd/main.cpp @ 190:cb61cc16e2b6
Irccd: update all JavaScript to native code
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 05 Jun 2016 10:50:55 +0200 |
parents | ef527409e638 |
children | bdeda4baf684 |
comparison
equal
deleted
inserted
replaced
189:bb70bb9e41eb | 190:cb61cc16e2b6 |
---|---|
39 }; | 39 }; |
40 | 40 |
41 TEST_F(TestJsIrccd, version) | 41 TEST_F(TestJsIrccd, version) |
42 { | 42 { |
43 try { | 43 try { |
44 auto ret = duk::pevalString(m_plugin->context(), | 44 auto ret = duk_peval_string(m_plugin->context(), |
45 "major = Irccd.version.major;" | 45 "major = Irccd.version.major;" |
46 "minor = Irccd.version.minor;" | 46 "minor = Irccd.version.minor;" |
47 "patch = Irccd.version.patch;" | 47 "patch = Irccd.version.patch;" |
48 ); | 48 ); |
49 | 49 |
50 if (ret != 0) | 50 if (ret != 0) |
51 throw duk::exception(m_plugin->context(), -1); | 51 throw dukx_exception(m_plugin->context(), -1); |
52 | 52 |
53 ASSERT_EQ(IRCCD_VERSION_MAJOR, duk::getGlobal<int>(m_plugin->context(), "major")); | 53 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "major")); |
54 ASSERT_EQ(IRCCD_VERSION_MINOR, duk::getGlobal<int>(m_plugin->context(), "minor")); | 54 ASSERT_EQ(IRCCD_VERSION_MAJOR, duk_get_int(m_plugin->context(), -1)); |
55 ASSERT_EQ(IRCCD_VERSION_PATCH, duk::getGlobal<int>(m_plugin->context(), "patch")); | 55 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "minor")); |
56 ASSERT_EQ(IRCCD_VERSION_MINOR, duk_get_int(m_plugin->context(), -1)); | |
57 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "patch")); | |
58 ASSERT_EQ(IRCCD_VERSION_PATCH, duk_get_int(m_plugin->context(), -1)); | |
56 } catch (const std::exception &ex) { | 59 } catch (const std::exception &ex) { |
57 FAIL() << ex.what(); | 60 FAIL() << ex.what(); |
58 } | 61 } |
59 } | 62 } |
60 | 63 |
61 TEST_F(TestJsIrccd, fromJavascript) | 64 TEST_F(TestJsIrccd, fromJavascript) |
62 { | 65 { |
63 try { | 66 try { |
64 auto ret = duk::pevalString(m_plugin->context(), | 67 auto ret = duk_peval_string(m_plugin->context(), |
65 "try {" | 68 "try {" |
66 " throw new Irccd.SystemError(1, 'test');" | 69 " throw new Irccd.SystemError(1, 'test');" |
67 "} catch (e) {" | 70 "} catch (e) {" |
68 " errno = e.errno;" | 71 " errno = e.errno;" |
69 " name = e.name;" | 72 " name = e.name;" |
72 " v2 = (e instanceof Irccd.SystemError);" | 75 " v2 = (e instanceof Irccd.SystemError);" |
73 "}" | 76 "}" |
74 ); | 77 ); |
75 | 78 |
76 if (ret != 0) | 79 if (ret != 0) |
77 throw duk::exception(m_plugin->context(), -1); | 80 throw dukx_exception(m_plugin->context(), -1); |
78 | 81 |
79 ASSERT_EQ(1, duk::getGlobal<int>(m_plugin->context(), "errno")); | 82 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "errno")); |
80 ASSERT_EQ("SystemError", duk::getGlobal<std::string>(m_plugin->context(), "name")); | 83 ASSERT_EQ(1, duk_get_int(m_plugin->context(), -1)); |
81 ASSERT_EQ("test", duk::getGlobal<std::string>(m_plugin->context(), "message")); | 84 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "name")); |
82 ASSERT_TRUE(duk::getGlobal<bool>(m_plugin->context(), "v1")); | 85 ASSERT_STREQ("SystemError", duk_get_string(m_plugin->context(), -1)); |
83 ASSERT_TRUE(duk::getGlobal<bool>(m_plugin->context(), "v2")); | 86 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "message")); |
87 ASSERT_STREQ("test", duk_get_string(m_plugin->context(), -1)); | |
88 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "v1")); | |
89 ASSERT_TRUE(duk_get_boolean(m_plugin->context(), -1)); | |
90 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "v2")); | |
91 ASSERT_TRUE(duk_get_boolean(m_plugin->context(), -1)); | |
84 } catch (const std::exception &ex) { | 92 } catch (const std::exception &ex) { |
85 FAIL() << ex.what(); | 93 FAIL() << ex.what(); |
86 } | 94 } |
87 } | 95 } |
88 | 96 |
89 TEST_F(TestJsIrccd, fromNative) | 97 TEST_F(TestJsIrccd, fromNative) |
90 { | 98 { |
91 try { | 99 try { |
92 duk::push(m_plugin->context(), duk::Function{[] (duk::Context *ctx) -> duk::Ret { | 100 duk_push_c_function(m_plugin->context(), [] (duk_context *ctx) -> duk_ret_t { |
93 duk::raise(ctx, SystemError{EINVAL, "hey"}); | 101 dukx_throw(ctx, SystemError(EINVAL, "hey")); |
94 | 102 |
95 return 0; | 103 return 0; |
96 }}); | 104 }, 0); |
97 | 105 |
98 duk::putGlobal(m_plugin->context(), "f"); | 106 duk_put_global_string(m_plugin->context(), "f"); |
99 | 107 |
100 auto ret = duk::pevalString(m_plugin->context(), | 108 auto ret = duk_peval_string(m_plugin->context(), |
101 "try {" | 109 "try {" |
102 " f();" | 110 " f();" |
103 "} catch (e) {" | 111 "} catch (e) {" |
104 " errno = e.errno;" | 112 " errno = e.errno;" |
105 " name = e.name;" | 113 " name = e.name;" |
108 " v2 = (e instanceof Irccd.SystemError);" | 116 " v2 = (e instanceof Irccd.SystemError);" |
109 "}" | 117 "}" |
110 ); | 118 ); |
111 | 119 |
112 if (ret != 0) | 120 if (ret != 0) |
113 throw duk::exception(m_plugin->context(), -1); | 121 throw dukx_exception(m_plugin->context(), -1); |
114 | 122 |
115 ASSERT_EQ(EINVAL, duk::getGlobal<int>(m_plugin->context(), "errno")); | 123 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "errno")); |
116 ASSERT_EQ("SystemError", duk::getGlobal<std::string>(m_plugin->context(), "name")); | 124 ASSERT_EQ(EINVAL, duk_get_int(m_plugin->context(), -1)); |
117 ASSERT_EQ("hey", duk::getGlobal<std::string>(m_plugin->context(), "message")); | 125 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "name")); |
118 ASSERT_TRUE(duk::getGlobal<bool>(m_plugin->context(), "v1")); | 126 ASSERT_STREQ("SystemError", duk_get_string(m_plugin->context(), -1)); |
119 ASSERT_TRUE(duk::getGlobal<bool>(m_plugin->context(), "v2")); | 127 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "message")); |
128 ASSERT_STREQ("hey", duk_get_string(m_plugin->context(), -1)); | |
129 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "v1")); | |
130 ASSERT_TRUE(duk_get_boolean(m_plugin->context(), -1)); | |
131 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "v2")); | |
132 ASSERT_TRUE(duk_get_boolean(m_plugin->context(), -1)); | |
120 } catch (const std::exception &ex) { | 133 } catch (const std::exception &ex) { |
121 FAIL() << ex.what(); | 134 FAIL() << ex.what(); |
122 } | 135 } |
123 } | 136 } |
124 | 137 |