comparison tests/src/libirccd-js/jsapi-util/main.cpp @ 757:97b356010785

Irccd: create (command|jsapi)::registry Make two list of constructor functions to initialize all command and all Javascript modules to avoid duplicating efforts in case of change. While here, update test fixtures to load all of them for simplicity.
author David Demelier <markand@malikania.fr>
date Mon, 06 Aug 2018 21:27:00 +0200
parents e8c4ba5ed1c6
children
comparison
equal deleted inserted replaced
756:1b4f82c952d6 757:97b356010785
17 */ 17 */
18 18
19 #define BOOST_TEST_MODULE "Unicode Javascript API" 19 #define BOOST_TEST_MODULE "Unicode Javascript API"
20 #include <boost/test/unit_test.hpp> 20 #include <boost/test/unit_test.hpp>
21 21
22 #include <irccd/js/util_jsapi.hpp> 22 #include <irccd/test/javascript_fixture.hpp>
23 23
24 #include <irccd/test/js_test.hpp> 24 namespace irccd::test {
25
26 namespace irccd {
27 25
28 namespace { 26 namespace {
29 27
30 BOOST_FIXTURE_TEST_SUITE(util_jsapi_suite, js_test<util_jsapi>) 28 BOOST_FIXTURE_TEST_SUITE(util_jsapi_suite, javascript_fixture)
31 29
32 /* 30 /*
33 * Irccd.Util misc. 31 * Irccd.Util misc.
34 * ------------------------------------------------------------------ 32 * ------------------------------------------------------------------
35 */ 33 */
36 34
37 BOOST_AUTO_TEST_CASE(format_simple) 35 BOOST_AUTO_TEST_CASE(format_simple)
38 { 36 {
39 auto ret = duk_peval_string(plugin_->get_context(), 37 const auto ret = duk_peval_string(plugin_->get_context(),
40 "result = Irccd.Util.format(\"#{target}\", { target: \"markand\" })" 38 "result = Irccd.Util.format(\"#{target}\", { target: \"markand\" })"
41 ); 39 );
42 40
43 if (ret != 0) 41 if (ret != 0)
44 throw dukx_stack(plugin_->get_context(), -1); 42 throw dukx_stack(plugin_->get_context(), -1);
70 * ------------------------------------------------------------------ 68 * ------------------------------------------------------------------
71 */ 69 */
72 70
73 BOOST_AUTO_TEST_CASE(cut_string_simple) 71 BOOST_AUTO_TEST_CASE(cut_string_simple)
74 { 72 {
75 auto ret = duk_peval_string(plugin_->get_context(), 73 const auto ret = duk_peval_string(plugin_->get_context(),
76 "lines = Irccd.Util.cut('hello world');\n" 74 "lines = Irccd.Util.cut('hello world');\n"
77 "line0 = lines[0];\n" 75 "line0 = lines[0];\n"
78 ); 76 );
79 77
80 if (ret != 0) 78 if (ret != 0)
84 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello world"); 82 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello world");
85 } 83 }
86 84
87 BOOST_AUTO_TEST_CASE(cut_string_double) 85 BOOST_AUTO_TEST_CASE(cut_string_double)
88 { 86 {
89 auto ret = duk_peval_string(plugin_->get_context(), 87 const auto ret = duk_peval_string(plugin_->get_context(),
90 "lines = Irccd.Util.cut('hello world', 5);\n" 88 "lines = Irccd.Util.cut('hello world', 5);\n"
91 "line0 = lines[0];\n" 89 "line0 = lines[0];\n"
92 "line1 = lines[1];\n" 90 "line1 = lines[1];\n"
93 ); 91 );
94 92
101 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world"); 99 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world");
102 } 100 }
103 101
104 BOOST_AUTO_TEST_CASE(cut_string_dirty) 102 BOOST_AUTO_TEST_CASE(cut_string_dirty)
105 { 103 {
106 auto ret = duk_peval_string(plugin_->get_context(), 104 const auto ret = duk_peval_string(plugin_->get_context(),
107 "lines = Irccd.Util.cut(' hello world ', 5);\n" 105 "lines = Irccd.Util.cut(' hello world ', 5);\n"
108 "line0 = lines[0];\n" 106 "line0 = lines[0];\n"
109 "line1 = lines[1];\n" 107 "line1 = lines[1];\n"
110 ); 108 );
111 109
118 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world"); 116 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world");
119 } 117 }
120 118
121 BOOST_AUTO_TEST_CASE(cut_string_too_much_lines) 119 BOOST_AUTO_TEST_CASE(cut_string_too_much_lines)
122 { 120 {
123 auto ret = duk_peval_string(plugin_->get_context(), 121 const auto ret = duk_peval_string(plugin_->get_context(),
124 "lines = Irccd.Util.cut('abc def ghi jkl', 3, 3);\n" 122 "lines = Irccd.Util.cut('abc def ghi jkl', 3, 3);\n"
125 ); 123 );
126 124
127 if (ret != 0) 125 if (ret != 0)
128 throw dukx_stack(plugin_->get_context(), -1); 126 throw dukx_stack(plugin_->get_context(), -1);
131 BOOST_TEST(duk_is_undefined(plugin_->get_context(), -1)); 129 BOOST_TEST(duk_is_undefined(plugin_->get_context(), -1));
132 } 130 }
133 131
134 BOOST_AUTO_TEST_CASE(cut_string_token_too_big) 132 BOOST_AUTO_TEST_CASE(cut_string_token_too_big)
135 { 133 {
136 auto ret = duk_peval_string(plugin_->get_context(), 134 const auto ret = duk_peval_string(plugin_->get_context(),
137 "try {\n" 135 "try {\n"
138 " lines = Irccd.Util.cut('hello world', 3);\n" 136 " lines = Irccd.Util.cut('hello world', 3);\n"
139 "} catch (e) {\n" 137 "} catch (e) {\n"
140 " name = e.name;\n" 138 " name = e.name;\n"
141 " message = e.message;\n" 139 " message = e.message;\n"
151 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "word 'hello' could not fit in maxc limit (3)"); 149 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "word 'hello' could not fit in maxc limit (3)");
152 } 150 }
153 151
154 BOOST_AUTO_TEST_CASE(cut_string_negative_maxc) 152 BOOST_AUTO_TEST_CASE(cut_string_negative_maxc)
155 { 153 {
156 auto ret = duk_peval_string(plugin_->get_context(), 154 const auto ret = duk_peval_string(plugin_->get_context(),
157 "try {\n" 155 "try {\n"
158 " lines = Irccd.Util.cut('hello world', -3);\n" 156 " lines = Irccd.Util.cut('hello world', -3);\n"
159 "} catch (e) {\n" 157 "} catch (e) {\n"
160 " name = e.name;\n" 158 " name = e.name;\n"
161 " message = e.message;\n" 159 " message = e.message;\n"
171 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "argument 1 (maxc) must be positive"); 169 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "argument 1 (maxc) must be positive");
172 } 170 }
173 171
174 BOOST_AUTO_TEST_CASE(cut_string_negative_maxl) 172 BOOST_AUTO_TEST_CASE(cut_string_negative_maxl)
175 { 173 {
176 auto ret = duk_peval_string(plugin_->get_context(), 174 const auto ret = duk_peval_string(plugin_->get_context(),
177 "try {\n" 175 "try {\n"
178 " lines = Irccd.Util.cut('hello world', undefined, -1);\n" 176 " lines = Irccd.Util.cut('hello world', undefined, -1);\n"
179 "} catch (e) {\n" 177 "} catch (e) {\n"
180 " name = e.name;\n" 178 " name = e.name;\n"
181 " message = e.message;\n" 179 " message = e.message;\n"
191 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "argument 2 (maxl) must be positive"); 189 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "argument 2 (maxl) must be positive");
192 } 190 }
193 191
194 BOOST_AUTO_TEST_CASE(cut_array_simple) 192 BOOST_AUTO_TEST_CASE(cut_array_simple)
195 { 193 {
196 auto ret = duk_peval_string(plugin_->get_context(), 194 const auto ret = duk_peval_string(plugin_->get_context(),
197 "lines = Irccd.Util.cut([ 'hello', 'world' ]);\n" 195 "lines = Irccd.Util.cut([ 'hello', 'world' ]);\n"
198 "line0 = lines[0];\n" 196 "line0 = lines[0];\n"
199 ); 197 );
200 198
201 if (ret != 0) 199 if (ret != 0)
205 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello world"); 203 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello world");
206 } 204 }
207 205
208 BOOST_AUTO_TEST_CASE(cut_array_double) 206 BOOST_AUTO_TEST_CASE(cut_array_double)
209 { 207 {
210 auto ret = duk_peval_string(plugin_->get_context(), 208 const auto ret = duk_peval_string(plugin_->get_context(),
211 "lines = Irccd.Util.cut([ 'hello', 'world' ], 5);\n" 209 "lines = Irccd.Util.cut([ 'hello', 'world' ], 5);\n"
212 "line0 = lines[0];\n" 210 "line0 = lines[0];\n"
213 "line1 = lines[1];\n" 211 "line1 = lines[1];\n"
214 ); 212 );
215 213
222 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world"); 220 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world");
223 } 221 }
224 222
225 BOOST_AUTO_TEST_CASE(cut_array_dirty) 223 BOOST_AUTO_TEST_CASE(cut_array_dirty)
226 { 224 {
227 auto ret = duk_peval_string(plugin_->get_context(), 225 const auto ret = duk_peval_string(plugin_->get_context(),
228 "lines = Irccd.Util.cut([ ' ', ' hello ', ' world ', ' '], 5);\n" 226 "lines = Irccd.Util.cut([ ' ', ' hello ', ' world ', ' '], 5);\n"
229 "line0 = lines[0];\n" 227 "line0 = lines[0];\n"
230 "line1 = lines[1];\n" 228 "line1 = lines[1];\n"
231 ); 229 );
232 230
239 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world"); 237 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world");
240 } 238 }
241 239
242 BOOST_AUTO_TEST_CASE(cut_invalid_data) 240 BOOST_AUTO_TEST_CASE(cut_invalid_data)
243 { 241 {
244 auto ret = duk_peval_string(plugin_->get_context(), 242 const auto ret = duk_peval_string(plugin_->get_context(),
245 "try {\n" 243 "try {\n"
246 " lines = Irccd.Util.cut(123);\n" 244 " lines = Irccd.Util.cut(123);\n"
247 "} catch (e) {\n" 245 "} catch (e) {\n"
248 " name = e.name;\n" 246 " name = e.name;\n"
249 " message = e.message;\n" 247 " message = e.message;\n"
259 257
260 BOOST_AUTO_TEST_SUITE_END() 258 BOOST_AUTO_TEST_SUITE_END()
261 259
262 } // !namespace 260 } // !namespace
263 261
264 } // !irccd 262 } // !irccd::test