Mercurial > irccd
comparison tests/js-irccd/main.cpp @ 492:173c52d3120b
Tests: create js_test fixture, closes #688
Create a js_test fixture class which generates a fake javascript plugin and
loads appropriate javascript modules to test.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 27 Sep 2017 09:42:57 +0200 |
parents | 7e273b7f4f92 |
children | b3a0f61a35fe |
comparison
equal
deleted
inserted
replaced
491:7f2ebbb7a45d | 492:173c52d3120b |
---|---|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 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 | 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 <gtest/gtest.h> | 19 #define BOOST_TEST_MODULE "Irccd Javascript API" |
20 #include <boost/test/unit_test.hpp> | |
20 | 21 |
21 #include <irccd/irccd.hpp> | 22 #include <js_test.hpp> |
22 #include <irccd/js_irccd_module.hpp> | |
23 #include <irccd/js_plugin.hpp> | |
24 #include <irccd/service.hpp> | |
25 #include <irccd/sysconfig.hpp> | |
26 | 23 |
27 using namespace irccd; | 24 namespace irccd { |
28 | 25 |
29 class TestJsIrccd : public testing::Test { | 26 BOOST_FIXTURE_TEST_SUITE(js_irccd_suite, js_test<js_irccd_module>) |
30 protected: | |
31 irccd::irccd m_irccd; | |
32 std::shared_ptr<js_plugin> m_plugin; | |
33 | 27 |
34 TestJsIrccd() | 28 BOOST_AUTO_TEST_CASE(version) |
35 : m_plugin(std::make_shared<js_plugin>("empty", SOURCEDIR "/empty.js")) | 29 { |
36 { | 30 auto ret = duk_peval_string(plugin_->context(), |
37 js_irccd_module().load(m_irccd, m_plugin); | 31 "major = Irccd.version.major;" |
38 } | 32 "minor = Irccd.version.minor;" |
39 }; | 33 "patch = Irccd.version.patch;" |
34 ); | |
40 | 35 |
41 TEST_F(TestJsIrccd, version) | 36 if (ret != 0) |
42 { | 37 throw dukx_exception(plugin_->context(), -1); |
43 try { | |
44 auto ret = duk_peval_string(m_plugin->context(), | |
45 "major = Irccd.version.major;" | |
46 "minor = Irccd.version.minor;" | |
47 "patch = Irccd.version.patch;" | |
48 ); | |
49 | 38 |
50 if (ret != 0) | 39 BOOST_TEST(duk_get_global_string(plugin_->context(), "major")); |
51 throw dukx_exception(m_plugin->context(), -1); | 40 BOOST_TEST(IRCCD_VERSION_MAJOR == duk_get_int(plugin_->context(), -1)); |
52 | 41 BOOST_TEST(duk_get_global_string(plugin_->context(), "minor")); |
53 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "major")); | 42 BOOST_TEST(IRCCD_VERSION_MINOR == duk_get_int(plugin_->context(), -1)); |
54 ASSERT_EQ(IRCCD_VERSION_MAJOR, duk_get_int(m_plugin->context(), -1)); | 43 BOOST_TEST(duk_get_global_string(plugin_->context(), "patch")); |
55 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "minor")); | 44 BOOST_TEST(IRCCD_VERSION_PATCH == duk_get_int(plugin_->context(), -1)); |
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)); | |
59 } catch (const std::exception &ex) { | |
60 FAIL() << ex.what(); | |
61 } | |
62 } | 45 } |
63 | 46 |
64 TEST_F(TestJsIrccd, fromJavascript) | 47 BOOST_AUTO_TEST_CASE(from_javascript) |
65 { | 48 { |
66 try { | 49 auto ret = duk_peval_string(plugin_->context(), |
67 auto ret = duk_peval_string(m_plugin->context(), | 50 "try {" |
68 "try {" | 51 " throw new Irccd.SystemError(1, 'test');" |
69 " throw new Irccd.SystemError(1, 'test');" | 52 "} catch (e) {" |
70 "} catch (e) {" | 53 " errno = e.errno;" |
71 " errno = e.errno;" | 54 " name = e.name;" |
72 " name = e.name;" | 55 " message = e.message;" |
73 " message = e.message;" | 56 " v1 = (e instanceof Error);" |
74 " v1 = (e instanceof Error);" | 57 " v2 = (e instanceof Irccd.SystemError);" |
75 " v2 = (e instanceof Irccd.SystemError);" | 58 "}" |
76 "}" | 59 ); |
77 ); | |
78 | 60 |
79 if (ret != 0) | 61 if (ret != 0) |
80 throw dukx_exception(m_plugin->context(), -1); | 62 throw dukx_exception(plugin_->context(), -1); |
81 | 63 |
82 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "errno")); | 64 BOOST_TEST(duk_get_global_string(plugin_->context(), "errno")); |
83 ASSERT_EQ(1, duk_get_int(m_plugin->context(), -1)); | 65 BOOST_TEST(1 == duk_get_int(plugin_->context(), -1)); |
84 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "name")); | 66 BOOST_TEST(duk_get_global_string(plugin_->context(), "name")); |
85 ASSERT_STREQ("SystemError", duk_get_string(m_plugin->context(), -1)); | 67 BOOST_TEST("SystemError" == duk_get_string(plugin_->context(), -1)); |
86 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "message")); | 68 BOOST_TEST(duk_get_global_string(plugin_->context(), "message")); |
87 ASSERT_STREQ("test", duk_get_string(m_plugin->context(), -1)); | 69 BOOST_TEST("test" == duk_get_string(plugin_->context(), -1)); |
88 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "v1")); | 70 BOOST_TEST(duk_get_global_string(plugin_->context(), "v1")); |
89 ASSERT_TRUE(duk_get_boolean(m_plugin->context(), -1)); | 71 BOOST_TEST(duk_get_boolean(plugin_->context(), -1)); |
90 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "v2")); | 72 BOOST_TEST(duk_get_global_string(plugin_->context(), "v2")); |
91 ASSERT_TRUE(duk_get_boolean(m_plugin->context(), -1)); | 73 BOOST_TEST(duk_get_boolean(plugin_->context(), -1)); |
92 } catch (const std::exception &ex) { | |
93 FAIL() << ex.what(); | |
94 } | |
95 } | 74 } |
96 | 75 |
97 TEST_F(TestJsIrccd, fromNative) | 76 BOOST_AUTO_TEST_CASE(fromNative) |
98 { | 77 { |
99 try { | 78 duk_push_c_function(plugin_->context(), [] (duk_context *ctx) -> duk_ret_t { |
100 duk_push_c_function(m_plugin->context(), [] (duk_context *ctx) -> duk_ret_t { | 79 dukx_throw(ctx, system_error(EINVAL, "hey")); |
101 dukx_throw(ctx, system_error(EINVAL, "hey")); | |
102 | 80 |
103 return 0; | 81 return 0; |
104 }, 0); | 82 }, 0); |
105 | 83 |
106 duk_put_global_string(m_plugin->context(), "f"); | 84 duk_put_global_string(plugin_->context(), "f"); |
107 | 85 |
108 auto ret = duk_peval_string(m_plugin->context(), | 86 auto ret = duk_peval_string(plugin_->context(), |
109 "try {" | 87 "try {" |
110 " f();" | 88 " f();" |
111 "} catch (e) {" | 89 "} catch (e) {" |
112 " errno = e.errno;" | 90 " errno = e.errno;" |
113 " name = e.name;" | 91 " name = e.name;" |
114 " message = e.message;" | 92 " message = e.message;" |
115 " v1 = (e instanceof Error);" | 93 " v1 = (e instanceof Error);" |
116 " v2 = (e instanceof Irccd.SystemError);" | 94 " v2 = (e instanceof Irccd.SystemError);" |
117 "}" | 95 "}" |
118 ); | 96 ); |
119 | 97 |
120 if (ret != 0) | 98 if (ret != 0) |
121 throw dukx_exception(m_plugin->context(), -1); | 99 throw dukx_exception(plugin_->context(), -1); |
122 | 100 |
123 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "errno")); | 101 BOOST_TEST(duk_get_global_string(plugin_->context(), "errno")); |
124 ASSERT_EQ(EINVAL, duk_get_int(m_plugin->context(), -1)); | 102 BOOST_TEST(EINVAL == duk_get_int(plugin_->context(), -1)); |
125 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "name")); | 103 BOOST_TEST(duk_get_global_string(plugin_->context(), "name")); |
126 ASSERT_STREQ("SystemError", duk_get_string(m_plugin->context(), -1)); | 104 BOOST_TEST("SystemError" == duk_get_string(plugin_->context(), -1)); |
127 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "message")); | 105 BOOST_TEST(duk_get_global_string(plugin_->context(), "message")); |
128 ASSERT_STREQ("hey", duk_get_string(m_plugin->context(), -1)); | 106 BOOST_TEST("hey" == duk_get_string(plugin_->context(), -1)); |
129 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "v1")); | 107 BOOST_TEST(duk_get_global_string(plugin_->context(), "v1")); |
130 ASSERT_TRUE(duk_get_boolean(m_plugin->context(), -1)); | 108 BOOST_TEST(duk_get_boolean(plugin_->context(), -1)); |
131 ASSERT_TRUE(duk_get_global_string(m_plugin->context(), "v2")); | 109 BOOST_TEST(duk_get_global_string(plugin_->context(), "v2")); |
132 ASSERT_TRUE(duk_get_boolean(m_plugin->context(), -1)); | 110 BOOST_TEST(duk_get_boolean(plugin_->context(), -1)); |
133 } catch (const std::exception &ex) { | |
134 FAIL() << ex.what(); | |
135 } | |
136 } | 111 } |
137 | 112 |
138 int main(int argc, char **argv) | 113 BOOST_AUTO_TEST_SUITE_END() |
139 { | |
140 testing::InitGoogleTest(&argc, argv); | |
141 | 114 |
142 return RUN_ALL_TESTS(); | 115 } // !irccd |
143 } |