Mercurial > irccd
comparison tests/src/libirccd-js/js-api-util/main.cpp @ 773:8c44bbcbbab9
Misc: style, cleanup and update
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 26 Oct 2018 13:01:00 +0200 |
parents | 35c1517d705d |
children | 06cc2f95f479 |
comparison
equal
deleted
inserted
replaced
772:f5ccf65ae929 | 773:8c44bbcbbab9 |
---|---|
35 * ------------------------------------------------------------------ | 35 * ------------------------------------------------------------------ |
36 */ | 36 */ |
37 | 37 |
38 BOOST_AUTO_TEST_CASE(format_simple) | 38 BOOST_AUTO_TEST_CASE(format_simple) |
39 { | 39 { |
40 const auto ret = duk_peval_string(plugin_->get_context(), | 40 const auto ret = duk_peval_string(plugin_->get_context(), |
41 "result = Irccd.Util.format(\"#{target}\", { target: \"markand\" })" | 41 "result = Irccd.Util.format(\"#{target}\", { target: \"markand\" })" |
42 ); | 42 ); |
43 | 43 |
44 if (ret != 0) | 44 if (ret != 0) |
45 throw duk::get_stack(plugin_->get_context(), -1); | 45 throw duk::get_stack(plugin_->get_context(), -1); |
46 | 46 |
47 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "result")); | 47 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "result")); |
48 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "markand"); | 48 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "markand"); |
49 } | 49 } |
50 | 50 |
51 BOOST_AUTO_TEST_CASE(splituser) | 51 BOOST_AUTO_TEST_CASE(splituser) |
52 { | 52 { |
53 if (duk_peval_string(plugin_->get_context(), "result = Irccd.Util.splituser(\"user!~user@hyper/super/host\");") != 0) | 53 if (duk_peval_string(plugin_->get_context(), "result = Irccd.Util.splituser(\"user!~user@hyper/super/host\");") != 0) |
54 throw duk::get_stack(plugin_->get_context(), -1); | 54 throw duk::get_stack(plugin_->get_context(), -1); |
55 | 55 |
56 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "result")); | 56 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "result")); |
57 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "user"); | 57 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "user"); |
58 } | 58 } |
59 | 59 |
60 BOOST_AUTO_TEST_CASE(splithost) | 60 BOOST_AUTO_TEST_CASE(splithost) |
61 { | 61 { |
62 if (duk_peval_string(plugin_->get_context(), "result = Irccd.Util.splithost(\"user!~user@hyper/super/host\");") != 0) | 62 if (duk_peval_string(plugin_->get_context(), "result = Irccd.Util.splithost(\"user!~user@hyper/super/host\");") != 0) |
63 throw duk::get_stack(plugin_->get_context(), -1); | 63 throw duk::get_stack(plugin_->get_context(), -1); |
64 | 64 |
65 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "result")); | 65 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "result")); |
66 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "~user@hyper/super/host"); | 66 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "~user@hyper/super/host"); |
67 } | 67 } |
68 | 68 |
69 /* | 69 /* |
70 * Irccd.Util.cut. | 70 * Irccd.Util.cut. |
71 * ------------------------------------------------------------------ | 71 * ------------------------------------------------------------------ |
72 */ | 72 */ |
73 | 73 |
74 BOOST_AUTO_TEST_CASE(cut_string_simple) | 74 BOOST_AUTO_TEST_CASE(cut_string_simple) |
75 { | 75 { |
76 const auto ret = duk_peval_string(plugin_->get_context(), | 76 const auto ret = duk_peval_string(plugin_->get_context(), |
77 "lines = Irccd.Util.cut('hello world');\n" | 77 "lines = Irccd.Util.cut('hello world');\n" |
78 "line0 = lines[0];\n" | 78 "line0 = lines[0];\n" |
79 ); | 79 ); |
80 | 80 |
81 if (ret != 0) | 81 if (ret != 0) |
82 throw duk::get_stack(plugin_->get_context(), -1); | 82 throw duk::get_stack(plugin_->get_context(), -1); |
83 | 83 |
84 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); | 84 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); |
85 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello world"); | 85 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello world"); |
86 } | 86 } |
87 | 87 |
88 BOOST_AUTO_TEST_CASE(cut_string_double) | 88 BOOST_AUTO_TEST_CASE(cut_string_double) |
89 { | 89 { |
90 const auto ret = duk_peval_string(plugin_->get_context(), | 90 const auto ret = duk_peval_string(plugin_->get_context(), |
91 "lines = Irccd.Util.cut('hello world', 5);\n" | 91 "lines = Irccd.Util.cut('hello world', 5);\n" |
92 "line0 = lines[0];\n" | 92 "line0 = lines[0];\n" |
93 "line1 = lines[1];\n" | 93 "line1 = lines[1];\n" |
94 ); | 94 ); |
95 | 95 |
96 if (ret != 0) | 96 if (ret != 0) |
97 throw duk::get_stack(plugin_->get_context(), -1); | 97 throw duk::get_stack(plugin_->get_context(), -1); |
98 | 98 |
99 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); | 99 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); |
100 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello"); | 100 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello"); |
101 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line1")); | 101 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line1")); |
102 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world"); | 102 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world"); |
103 } | 103 } |
104 | 104 |
105 BOOST_AUTO_TEST_CASE(cut_string_dirty) | 105 BOOST_AUTO_TEST_CASE(cut_string_dirty) |
106 { | 106 { |
107 const auto ret = duk_peval_string(plugin_->get_context(), | 107 const auto ret = duk_peval_string(plugin_->get_context(), |
108 "lines = Irccd.Util.cut(' hello world ', 5);\n" | 108 "lines = Irccd.Util.cut(' hello world ', 5);\n" |
109 "line0 = lines[0];\n" | 109 "line0 = lines[0];\n" |
110 "line1 = lines[1];\n" | 110 "line1 = lines[1];\n" |
111 ); | 111 ); |
112 | 112 |
113 if (ret != 0) | 113 if (ret != 0) |
114 throw duk::get_stack(plugin_->get_context(), -1); | 114 throw duk::get_stack(plugin_->get_context(), -1); |
115 | 115 |
116 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); | 116 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); |
117 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello"); | 117 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello"); |
118 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line1")); | 118 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line1")); |
119 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world"); | 119 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world"); |
120 } | 120 } |
121 | 121 |
122 BOOST_AUTO_TEST_CASE(cut_string_too_much_lines) | 122 BOOST_AUTO_TEST_CASE(cut_string_too_much_lines) |
123 { | 123 { |
124 const auto ret = duk_peval_string(plugin_->get_context(), | 124 const auto ret = duk_peval_string(plugin_->get_context(), |
125 "lines = Irccd.Util.cut('abc def ghi jkl', 3, 3);\n" | 125 "lines = Irccd.Util.cut('abc def ghi jkl', 3, 3);\n" |
126 ); | 126 ); |
127 | 127 |
128 if (ret != 0) | 128 if (ret != 0) |
129 throw duk::get_stack(plugin_->get_context(), -1); | 129 throw duk::get_stack(plugin_->get_context(), -1); |
130 | 130 |
131 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "lines")); | 131 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "lines")); |
132 BOOST_TEST(duk_is_undefined(plugin_->get_context(), -1)); | 132 BOOST_TEST(duk_is_undefined(plugin_->get_context(), -1)); |
133 } | 133 } |
134 | 134 |
135 BOOST_AUTO_TEST_CASE(cut_string_token_too_big) | 135 BOOST_AUTO_TEST_CASE(cut_string_token_too_big) |
136 { | 136 { |
137 const auto ret = duk_peval_string(plugin_->get_context(), | 137 const auto ret = duk_peval_string(plugin_->get_context(), |
138 "try {\n" | 138 "try {\n" |
139 " lines = Irccd.Util.cut('hello world', 3);\n" | 139 " lines = Irccd.Util.cut('hello world', 3);\n" |
140 "} catch (e) {\n" | 140 "} catch (e) {\n" |
141 " name = e.name;\n" | 141 " name = e.name;\n" |
142 " message = e.message;\n" | 142 " message = e.message;\n" |
143 "}\n" | 143 "}\n" |
144 ); | 144 ); |
145 | 145 |
146 if (ret != 0) | 146 if (ret != 0) |
147 throw duk::get_stack(plugin_->get_context(), -1); | 147 throw duk::get_stack(plugin_->get_context(), -1); |
148 | 148 |
149 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "name")); | 149 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "name")); |
150 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "RangeError"); | 150 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "RangeError"); |
151 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "message")); | 151 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "message")); |
152 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "word 'hello' could not fit in maxc limit (3)"); | 152 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "word 'hello' could not fit in maxc limit (3)"); |
153 } | 153 } |
154 | 154 |
155 BOOST_AUTO_TEST_CASE(cut_string_negative_maxc) | 155 BOOST_AUTO_TEST_CASE(cut_string_negative_maxc) |
156 { | 156 { |
157 const auto ret = duk_peval_string(plugin_->get_context(), | 157 const auto ret = duk_peval_string(plugin_->get_context(), |
158 "try {\n" | 158 "try {\n" |
159 " lines = Irccd.Util.cut('hello world', -3);\n" | 159 " lines = Irccd.Util.cut('hello world', -3);\n" |
160 "} catch (e) {\n" | 160 "} catch (e) {\n" |
161 " name = e.name;\n" | 161 " name = e.name;\n" |
162 " message = e.message;\n" | 162 " message = e.message;\n" |
163 "}\n" | 163 "}\n" |
164 ); | 164 ); |
165 | 165 |
166 if (ret != 0) | 166 if (ret != 0) |
167 throw duk::get_stack(plugin_->get_context(), -1); | 167 throw duk::get_stack(plugin_->get_context(), -1); |
168 | 168 |
169 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "name")); | 169 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "name")); |
170 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "RangeError"); | 170 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "RangeError"); |
171 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "message")); | 171 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "message")); |
172 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "argument 1 (maxc) must be positive"); | 172 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "argument 1 (maxc) must be positive"); |
173 } | 173 } |
174 | 174 |
175 BOOST_AUTO_TEST_CASE(cut_string_negative_maxl) | 175 BOOST_AUTO_TEST_CASE(cut_string_negative_maxl) |
176 { | 176 { |
177 const auto ret = duk_peval_string(plugin_->get_context(), | 177 const auto ret = duk_peval_string(plugin_->get_context(), |
178 "try {\n" | 178 "try {\n" |
179 " lines = Irccd.Util.cut('hello world', undefined, -1);\n" | 179 " lines = Irccd.Util.cut('hello world', undefined, -1);\n" |
180 "} catch (e) {\n" | 180 "} catch (e) {\n" |
181 " name = e.name;\n" | 181 " name = e.name;\n" |
182 " message = e.message;\n" | 182 " message = e.message;\n" |
183 "}\n" | 183 "}\n" |
184 ); | 184 ); |
185 | 185 |
186 if (ret != 0) | 186 if (ret != 0) |
187 throw duk::get_stack(plugin_->get_context(), -1); | 187 throw duk::get_stack(plugin_->get_context(), -1); |
188 | 188 |
189 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "name")); | 189 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "name")); |
190 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "RangeError"); | 190 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "RangeError"); |
191 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "message")); | 191 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "message")); |
192 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "argument 2 (maxl) must be positive"); | 192 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "argument 2 (maxl) must be positive"); |
193 } | 193 } |
194 | 194 |
195 BOOST_AUTO_TEST_CASE(cut_array_simple) | 195 BOOST_AUTO_TEST_CASE(cut_array_simple) |
196 { | 196 { |
197 const auto ret = duk_peval_string(plugin_->get_context(), | 197 const auto ret = duk_peval_string(plugin_->get_context(), |
198 "lines = Irccd.Util.cut([ 'hello', 'world' ]);\n" | 198 "lines = Irccd.Util.cut([ 'hello', 'world' ]);\n" |
199 "line0 = lines[0];\n" | 199 "line0 = lines[0];\n" |
200 ); | 200 ); |
201 | 201 |
202 if (ret != 0) | 202 if (ret != 0) |
203 throw duk::get_stack(plugin_->get_context(), -1); | 203 throw duk::get_stack(plugin_->get_context(), -1); |
204 | 204 |
205 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); | 205 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); |
206 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello world"); | 206 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello world"); |
207 } | 207 } |
208 | 208 |
209 BOOST_AUTO_TEST_CASE(cut_array_double) | 209 BOOST_AUTO_TEST_CASE(cut_array_double) |
210 { | 210 { |
211 const auto ret = duk_peval_string(plugin_->get_context(), | 211 const auto ret = duk_peval_string(plugin_->get_context(), |
212 "lines = Irccd.Util.cut([ 'hello', 'world' ], 5);\n" | 212 "lines = Irccd.Util.cut([ 'hello', 'world' ], 5);\n" |
213 "line0 = lines[0];\n" | 213 "line0 = lines[0];\n" |
214 "line1 = lines[1];\n" | 214 "line1 = lines[1];\n" |
215 ); | 215 ); |
216 | 216 |
217 if (ret != 0) | 217 if (ret != 0) |
218 throw duk::get_stack(plugin_->get_context(), -1); | 218 throw duk::get_stack(plugin_->get_context(), -1); |
219 | 219 |
220 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); | 220 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); |
221 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello"); | 221 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello"); |
222 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line1")); | 222 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line1")); |
223 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world"); | 223 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world"); |
224 } | 224 } |
225 | 225 |
226 BOOST_AUTO_TEST_CASE(cut_array_dirty) | 226 BOOST_AUTO_TEST_CASE(cut_array_dirty) |
227 { | 227 { |
228 const auto ret = duk_peval_string(plugin_->get_context(), | 228 const auto ret = duk_peval_string(plugin_->get_context(), |
229 "lines = Irccd.Util.cut([ ' ', ' hello ', ' world ', ' '], 5);\n" | 229 "lines = Irccd.Util.cut([ ' ', ' hello ', ' world ', ' '], 5);\n" |
230 "line0 = lines[0];\n" | 230 "line0 = lines[0];\n" |
231 "line1 = lines[1];\n" | 231 "line1 = lines[1];\n" |
232 ); | 232 ); |
233 | 233 |
234 if (ret != 0) | 234 if (ret != 0) |
235 throw duk::get_stack(plugin_->get_context(), -1); | 235 throw duk::get_stack(plugin_->get_context(), -1); |
236 | 236 |
237 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); | 237 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line0")); |
238 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello"); | 238 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "hello"); |
239 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line1")); | 239 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "line1")); |
240 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world"); | 240 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "world"); |
241 } | 241 } |
242 | 242 |
243 BOOST_AUTO_TEST_CASE(cut_invalid_data) | 243 BOOST_AUTO_TEST_CASE(cut_invalid_data) |
244 { | 244 { |
245 const auto ret = duk_peval_string(plugin_->get_context(), | 245 const auto ret = duk_peval_string(plugin_->get_context(), |
246 "try {\n" | 246 "try {\n" |
247 " lines = Irccd.Util.cut(123);\n" | 247 " lines = Irccd.Util.cut(123);\n" |
248 "} catch (e) {\n" | 248 "} catch (e) {\n" |
249 " name = e.name;\n" | 249 " name = e.name;\n" |
250 " message = e.message;\n" | 250 " message = e.message;\n" |
251 "}\n" | 251 "}\n" |
252 ); | 252 ); |
253 | 253 |
254 if (ret != 0) | 254 if (ret != 0) |
255 throw duk::get_stack(plugin_->get_context(), -1); | 255 throw duk::get_stack(plugin_->get_context(), -1); |
256 | 256 |
257 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "name")); | 257 BOOST_TEST(duk_get_global_string(plugin_->get_context(), "name")); |
258 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "TypeError"); | 258 BOOST_TEST(duk_get_string(plugin_->get_context(), -1) == "TypeError"); |
259 } | 259 } |
260 | 260 |
261 BOOST_AUTO_TEST_SUITE_END() | 261 BOOST_AUTO_TEST_SUITE_END() |
262 | 262 |
263 } // !namespace | 263 } // !namespace |