Mercurial > malikania
comparison tests/libclient/js-size/main.cpp @ 42:a47a4477f347
Misc: new style, closes #578
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 29 Nov 2016 21:21:36 +0100 |
parents | 9af360f34c7d |
children | fabbe1759cec |
comparison
equal
deleted
inserted
replaced
41:3645200f46bf | 42:a47a4477f347 |
---|---|
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 #include <gtest/gtest.h> |
20 | 20 |
21 #include <malikania/js-size.hpp> | 21 #include <malikania/js_size.hpp> |
22 | 22 |
23 using namespace malikania; | 23 using namespace malikania; |
24 | 24 |
25 class TestSize : public testing::Test { | 25 class TestSize : public testing::Test { |
26 protected: | 26 protected: |
47 "s = Malikania.Size();" | 47 "s = Malikania.Size();" |
48 "w = s.width;" | 48 "w = s.width;" |
49 "h = s.height;" | 49 "h = s.height;" |
50 ); | 50 ); |
51 | 51 |
52 if (ret != 0) | 52 if (ret != 0) { |
53 throw dukx_exception(m_ctx, -1); | 53 throw dukx_exception(m_ctx, -1); |
54 } | |
54 | 55 |
55 duk_get_global_string(m_ctx, "w"); | 56 duk_get_global_string(m_ctx, "w"); |
56 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); | 57 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); |
57 duk_pop(m_ctx); | 58 duk_pop(m_ctx); |
58 duk_get_global_string(m_ctx, "h"); | 59 duk_get_global_string(m_ctx, "h"); |
70 "s = Malikania.Size(100, 200);" | 71 "s = Malikania.Size(100, 200);" |
71 "w = s.width;" | 72 "w = s.width;" |
72 "h = s.height;" | 73 "h = s.height;" |
73 ); | 74 ); |
74 | 75 |
75 if (ret != 0) | 76 if (ret != 0) { |
76 throw dukx_exception(m_ctx, -1); | 77 throw dukx_exception(m_ctx, -1); |
78 } | |
77 | 79 |
78 duk_get_global_string(m_ctx, "w"); | 80 duk_get_global_string(m_ctx, "w"); |
79 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1)); | 81 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1)); |
80 duk_pop(m_ctx); | 82 duk_pop(m_ctx); |
81 duk_get_global_string(m_ctx, "h"); | 83 duk_get_global_string(m_ctx, "h"); |
93 "s = Malikania.Size({ width: 100, height: 200 });" | 95 "s = Malikania.Size({ width: 100, height: 200 });" |
94 "w = s.width;" | 96 "w = s.width;" |
95 "h = s.height;" | 97 "h = s.height;" |
96 ); | 98 ); |
97 | 99 |
98 if (ret != 0) | 100 if (ret != 0) { |
99 throw dukx_exception(m_ctx, -1); | 101 throw dukx_exception(m_ctx, -1); |
102 } | |
100 | 103 |
101 duk_get_global_string(m_ctx, "w"); | 104 duk_get_global_string(m_ctx, "w"); |
102 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1)); | 105 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1)); |
103 duk_pop(m_ctx); | 106 duk_pop(m_ctx); |
104 duk_get_global_string(m_ctx, "h"); | 107 duk_get_global_string(m_ctx, "h"); |
116 "s = new Malikania.Size({ width: 100, height: 200 });" | 119 "s = new Malikania.Size({ width: 100, height: 200 });" |
117 "w = s.width;" | 120 "w = s.width;" |
118 "h = s.height;" | 121 "h = s.height;" |
119 ); | 122 ); |
120 | 123 |
121 if (ret != 0) | 124 if (ret != 0) { |
122 throw dukx_exception(m_ctx, -1); | 125 throw dukx_exception(m_ctx, -1); |
126 } | |
123 | 127 |
124 duk_get_global_string(m_ctx, "w"); | 128 duk_get_global_string(m_ctx, "w"); |
125 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1)); | 129 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1)); |
126 duk_pop(m_ctx); | 130 duk_pop(m_ctx); |
127 duk_get_global_string(m_ctx, "h"); | 131 duk_get_global_string(m_ctx, "h"); |
147 " name = e.name;" | 151 " name = e.name;" |
148 " correct = (e instanceof TypeError);" | 152 " correct = (e instanceof TypeError);" |
149 "}" | 153 "}" |
150 ); | 154 ); |
151 | 155 |
152 if (ret != 0) | 156 if (ret != 0) { |
153 throw dukx_exception(m_ctx, -1); | 157 throw dukx_exception(m_ctx, -1); |
158 } | |
154 | 159 |
155 duk_get_global_string(m_ctx, "name"); | 160 duk_get_global_string(m_ctx, "name"); |
156 ASSERT_STREQ("TypeError", duk_to_string(m_ctx, -1)); | 161 ASSERT_STREQ("TypeError", duk_to_string(m_ctx, -1)); |
157 duk_pop(m_ctx); | 162 duk_pop(m_ctx); |
158 duk_get_global_string(m_ctx, "correct"); | 163 duk_get_global_string(m_ctx, "correct"); |
173 " name = e.name;" | 178 " name = e.name;" |
174 " correct = (e instanceof RangeError);" | 179 " correct = (e instanceof RangeError);" |
175 "}" | 180 "}" |
176 ); | 181 ); |
177 | 182 |
178 if (ret != 0) | 183 if (ret != 0) { |
179 throw dukx_exception(m_ctx, -1); | 184 throw dukx_exception(m_ctx, -1); |
185 } | |
180 | 186 |
181 duk_get_global_string(m_ctx, "name"); | 187 duk_get_global_string(m_ctx, "name"); |
182 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); | 188 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); |
183 duk_pop(m_ctx); | 189 duk_pop(m_ctx); |
184 duk_get_global_string(m_ctx, "correct"); | 190 duk_get_global_string(m_ctx, "correct"); |
199 " name = e.name;" | 205 " name = e.name;" |
200 " correct = (e instanceof RangeError);" | 206 " correct = (e instanceof RangeError);" |
201 "}" | 207 "}" |
202 ); | 208 ); |
203 | 209 |
204 if (ret != 0) | 210 if (ret != 0) { |
205 throw dukx_exception(m_ctx, -1); | 211 throw dukx_exception(m_ctx, -1); |
212 } | |
206 | 213 |
207 duk_get_global_string(m_ctx, "name"); | 214 duk_get_global_string(m_ctx, "name"); |
208 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); | 215 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); |
209 duk_pop(m_ctx); | 216 duk_pop(m_ctx); |
210 duk_get_global_string(m_ctx, "correct"); | 217 duk_get_global_string(m_ctx, "correct"); |
225 " name = e.name;" | 232 " name = e.name;" |
226 " correct = (e instanceof RangeError);" | 233 " correct = (e instanceof RangeError);" |
227 "}" | 234 "}" |
228 ); | 235 ); |
229 | 236 |
230 if (ret != 0) | 237 if (ret != 0) { |
231 throw dukx_exception(m_ctx, -1); | 238 throw dukx_exception(m_ctx, -1); |
239 } | |
232 | 240 |
233 duk_get_global_string(m_ctx, "name"); | 241 duk_get_global_string(m_ctx, "name"); |
234 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); | 242 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); |
235 duk_pop(m_ctx); | 243 duk_pop(m_ctx); |
236 duk_get_global_string(m_ctx, "correct"); | 244 duk_get_global_string(m_ctx, "correct"); |
251 " name = e.name;" | 259 " name = e.name;" |
252 " correct = (e instanceof RangeError);" | 260 " correct = (e instanceof RangeError);" |
253 "}" | 261 "}" |
254 ); | 262 ); |
255 | 263 |
256 if (ret != 0) | 264 if (ret != 0) { |
257 throw dukx_exception(m_ctx, -1); | 265 throw dukx_exception(m_ctx, -1); |
266 } | |
258 | 267 |
259 duk_get_global_string(m_ctx, "name"); | 268 duk_get_global_string(m_ctx, "name"); |
260 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); | 269 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); |
261 duk_pop(m_ctx); | 270 duk_pop(m_ctx); |
262 duk_get_global_string(m_ctx, "correct"); | 271 duk_get_global_string(m_ctx, "correct"); |
274 | 283 |
275 TEST_F(TestSize, requireSuccess) | 284 TEST_F(TestSize, requireSuccess) |
276 { | 285 { |
277 try { | 286 try { |
278 duk_push_c_function(m_ctx, [] (auto ctx) { | 287 duk_push_c_function(m_ctx, [] (auto ctx) { |
279 Size size = dukx_require_size(ctx, 0); | 288 auto size = dukx_require_size(ctx, 0); |
280 | 289 |
281 duk_push_uint(ctx, size.width()); | 290 duk_push_uint(ctx, size.width()); |
282 duk_put_global_string(ctx, "w"); | 291 duk_put_global_string(ctx, "w"); |
283 duk_push_uint(ctx, size.height()); | 292 duk_push_uint(ctx, size.height()); |
284 duk_put_global_string(ctx, "h"); | 293 duk_put_global_string(ctx, "h"); |
287 }, 1); | 296 }, 1); |
288 duk_put_global_string(m_ctx, "build"); | 297 duk_put_global_string(m_ctx, "build"); |
289 | 298 |
290 auto ret = duk_peval_string(m_ctx, "build({ width: 100, height: 200 });"); | 299 auto ret = duk_peval_string(m_ctx, "build({ width: 100, height: 200 });"); |
291 | 300 |
292 if (ret != 0) | 301 if (ret != 0) { |
293 throw dukx_exception(m_ctx, -1); | 302 throw dukx_exception(m_ctx, -1); |
303 } | |
294 | 304 |
295 duk_get_global_string(m_ctx, "w"); | 305 duk_get_global_string(m_ctx, "w"); |
296 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1)); | 306 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1)); |
297 duk_pop(m_ctx); | 307 duk_pop(m_ctx); |
298 duk_get_global_string(m_ctx, "h"); | 308 duk_get_global_string(m_ctx, "h"); |
320 " name = e.name;" | 330 " name = e.name;" |
321 " correct = (e instanceof Error);" | 331 " correct = (e instanceof Error);" |
322 "}" | 332 "}" |
323 ); | 333 ); |
324 | 334 |
325 if (ret != 0) | 335 if (ret != 0) { |
326 throw dukx_exception(m_ctx, -1); | 336 throw dukx_exception(m_ctx, -1); |
337 } | |
327 | 338 |
328 duk_get_global_string(m_ctx, "name"); | 339 duk_get_global_string(m_ctx, "name"); |
329 ASSERT_STREQ("Error", duk_to_string(m_ctx, -1)); | 340 ASSERT_STREQ("Error", duk_to_string(m_ctx, -1)); |
330 duk_pop(m_ctx); | 341 duk_pop(m_ctx); |
331 duk_get_global_string(m_ctx, "correct"); | 342 duk_get_global_string(m_ctx, "correct"); |
343 | 354 |
344 TEST_F(TestSize, getAdjustAll) | 355 TEST_F(TestSize, getAdjustAll) |
345 { | 356 { |
346 try { | 357 try { |
347 duk_push_c_function(m_ctx, [] (auto ctx) { | 358 duk_push_c_function(m_ctx, [] (auto ctx) { |
348 Size size = dukx_get_size(ctx, 0); | 359 auto size = dukx_get_size(ctx, 0); |
349 | 360 |
350 duk_push_uint(ctx, size.width()); | 361 duk_push_uint(ctx, size.width()); |
351 duk_put_global_string(ctx, "w"); | 362 duk_put_global_string(ctx, "w"); |
352 duk_push_uint(ctx, size.height()); | 363 duk_push_uint(ctx, size.height()); |
353 duk_put_global_string(ctx, "h"); | 364 duk_put_global_string(ctx, "h"); |
356 }, 1); | 367 }, 1); |
357 duk_put_global_string(m_ctx, "build"); | 368 duk_put_global_string(m_ctx, "build"); |
358 | 369 |
359 auto ret = duk_peval_string(m_ctx, "build({});"); | 370 auto ret = duk_peval_string(m_ctx, "build({});"); |
360 | 371 |
361 if (ret != 0) | 372 if (ret != 0) { |
362 throw dukx_exception(m_ctx, -1); | 373 throw dukx_exception(m_ctx, -1); |
374 } | |
363 | 375 |
364 duk_get_global_string(m_ctx, "w"); | 376 duk_get_global_string(m_ctx, "w"); |
365 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); | 377 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); |
366 duk_pop(m_ctx); | 378 duk_pop(m_ctx); |
367 duk_get_global_string(m_ctx, "h"); | 379 duk_get_global_string(m_ctx, "h"); |