comparison tests/libclient/js-rectangle/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-rectangle.hpp> 21 #include <malikania/js_rectangle.hpp>
22 22
23 using namespace malikania; 23 using namespace malikania;
24 24
25 class TestRectangle : public testing::Test { 25 class TestRectangle : public testing::Test {
26 protected: 26 protected:
49 "y = r.y;" 49 "y = r.y;"
50 "w = r.width;" 50 "w = r.width;"
51 "h = r.height;" 51 "h = r.height;"
52 ); 52 );
53 53
54 if (ret != 0) 54 if (ret != 0) {
55 throw dukx_exception(m_ctx, -1); 55 throw dukx_exception(m_ctx, -1);
56 }
56 57
57 duk_get_global_string(m_ctx, "x"); 58 duk_get_global_string(m_ctx, "x");
58 ASSERT_EQ(0, duk_to_int(m_ctx, -1)); 59 ASSERT_EQ(0, duk_to_int(m_ctx, -1));
59 duk_pop(m_ctx); 60 duk_pop(m_ctx);
60 duk_get_global_string(m_ctx, "y"); 61 duk_get_global_string(m_ctx, "y");
80 "y = r.y;" 81 "y = r.y;"
81 "w = r.width;" 82 "w = r.width;"
82 "h = r.height;" 83 "h = r.height;"
83 ); 84 );
84 85
85 if (ret != 0) 86 if (ret != 0) {
86 throw dukx_exception(m_ctx, -1); 87 throw dukx_exception(m_ctx, -1);
88 }
87 89
88 duk_get_global_string(m_ctx, "x"); 90 duk_get_global_string(m_ctx, "x");
89 ASSERT_EQ(10, duk_to_int(m_ctx, -1)); 91 ASSERT_EQ(10, duk_to_int(m_ctx, -1));
90 duk_pop(m_ctx); 92 duk_pop(m_ctx);
91 duk_get_global_string(m_ctx, "y"); 93 duk_get_global_string(m_ctx, "y");
111 "y = r.y;" 113 "y = r.y;"
112 "w = r.width;" 114 "w = r.width;"
113 "h = r.height;" 115 "h = r.height;"
114 ); 116 );
115 117
116 if (ret != 0) 118 if (ret != 0) {
117 throw dukx_exception(m_ctx, -1); 119 throw dukx_exception(m_ctx, -1);
120 }
118 121
119 duk_get_global_string(m_ctx, "x"); 122 duk_get_global_string(m_ctx, "x");
120 ASSERT_EQ(10, duk_to_int(m_ctx, -1)); 123 ASSERT_EQ(10, duk_to_int(m_ctx, -1));
121 duk_pop(m_ctx); 124 duk_pop(m_ctx);
122 duk_get_global_string(m_ctx, "y"); 125 duk_get_global_string(m_ctx, "y");
142 "y = r.y;" 145 "y = r.y;"
143 "w = r.width;" 146 "w = r.width;"
144 "h = r.height;" 147 "h = r.height;"
145 ); 148 );
146 149
147 if (ret != 0) 150 if (ret != 0) {
148 throw dukx_exception(m_ctx, -1); 151 throw dukx_exception(m_ctx, -1);
152 }
149 153
150 duk_get_global_string(m_ctx, "x"); 154 duk_get_global_string(m_ctx, "x");
151 ASSERT_EQ(10, duk_to_int(m_ctx, -1)); 155 ASSERT_EQ(10, duk_to_int(m_ctx, -1));
152 duk_pop(m_ctx); 156 duk_pop(m_ctx);
153 duk_get_global_string(m_ctx, "y"); 157 duk_get_global_string(m_ctx, "y");
179 " name = e.name;" 183 " name = e.name;"
180 " correct = (e instanceof TypeError);" 184 " correct = (e instanceof TypeError);"
181 "}" 185 "}"
182 ); 186 );
183 187
184 if (ret != 0) 188 if (ret != 0) {
185 throw dukx_exception(m_ctx, -1); 189 throw dukx_exception(m_ctx, -1);
190 }
186 191
187 duk_get_global_string(m_ctx, "name"); 192 duk_get_global_string(m_ctx, "name");
188 ASSERT_STREQ("TypeError", duk_to_string(m_ctx, -1)); 193 ASSERT_STREQ("TypeError", duk_to_string(m_ctx, -1));
189 duk_pop(m_ctx); 194 duk_pop(m_ctx);
190 duk_get_global_string(m_ctx, "correct"); 195 duk_get_global_string(m_ctx, "correct");
205 " name = e.name;" 210 " name = e.name;"
206 " correct = (e instanceof RangeError);" 211 " correct = (e instanceof RangeError);"
207 "}" 212 "}"
208 ); 213 );
209 214
210 if (ret != 0) 215 if (ret != 0) {
211 throw dukx_exception(m_ctx, -1); 216 throw dukx_exception(m_ctx, -1);
217 }
212 218
213 duk_get_global_string(m_ctx, "name"); 219 duk_get_global_string(m_ctx, "name");
214 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); 220 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1));
215 duk_pop(m_ctx); 221 duk_pop(m_ctx);
216 duk_get_global_string(m_ctx, "correct"); 222 duk_get_global_string(m_ctx, "correct");
228 234
229 TEST_F(TestRectangle, requireSuccess) 235 TEST_F(TestRectangle, requireSuccess)
230 { 236 {
231 try { 237 try {
232 duk_push_c_function(m_ctx, [] (auto ctx) { 238 duk_push_c_function(m_ctx, [] (auto ctx) {
233 Rectangle rect = dukx_require_rect(ctx, 0); 239 auto rect = dukx_require_rect(ctx, 0);
234 240
235 duk_push_int(ctx, rect.x()); 241 duk_push_int(ctx, rect.x());
236 duk_put_global_string(ctx, "x"); 242 duk_put_global_string(ctx, "x");
237 duk_push_int(ctx, rect.y()); 243 duk_push_int(ctx, rect.y());
238 duk_put_global_string(ctx, "y"); 244 duk_put_global_string(ctx, "y");
245 }, 1); 251 }, 1);
246 duk_put_global_string(m_ctx, "build"); 252 duk_put_global_string(m_ctx, "build");
247 253
248 auto ret = duk_peval_string(m_ctx, "build({ x: 50, y: 80, width: 100, height: 200 });"); 254 auto ret = duk_peval_string(m_ctx, "build({ x: 50, y: 80, width: 100, height: 200 });");
249 255
250 if (ret != 0) 256 if (ret != 0) {
251 throw dukx_exception(m_ctx, -1); 257 throw dukx_exception(m_ctx, -1);
258 }
252 259
253 duk_get_global_string(m_ctx, "x"); 260 duk_get_global_string(m_ctx, "x");
254 ASSERT_EQ(50, duk_to_int(m_ctx, -1)); 261 ASSERT_EQ(50, duk_to_int(m_ctx, -1));
255 duk_pop(m_ctx); 262 duk_pop(m_ctx);
256 duk_get_global_string(m_ctx, "y"); 263 duk_get_global_string(m_ctx, "y");
284 " name = e.name;" 291 " name = e.name;"
285 " correct = (e instanceof Error);" 292 " correct = (e instanceof Error);"
286 "}" 293 "}"
287 ); 294 );
288 295
289 if (ret != 0) 296 if (ret != 0) {
290 throw dukx_exception(m_ctx, -1); 297 throw dukx_exception(m_ctx, -1);
298 }
291 299
292 duk_get_global_string(m_ctx, "name"); 300 duk_get_global_string(m_ctx, "name");
293 ASSERT_STREQ("Error", duk_to_string(m_ctx, -1)); 301 ASSERT_STREQ("Error", duk_to_string(m_ctx, -1));
294 duk_pop(m_ctx); 302 duk_pop(m_ctx);
295 duk_get_global_string(m_ctx, "correct"); 303 duk_get_global_string(m_ctx, "correct");
307 315
308 TEST_F(TestRectangle, getAdjustAll) 316 TEST_F(TestRectangle, getAdjustAll)
309 { 317 {
310 try { 318 try {
311 duk_push_c_function(m_ctx, [] (auto ctx) { 319 duk_push_c_function(m_ctx, [] (auto ctx) {
312 Rectangle rect = dukx_get_rect(ctx, 0); 320 auto rect = dukx_get_rect(ctx, 0);
313 321
314 duk_push_int(ctx, rect.x()); 322 duk_push_int(ctx, rect.x());
315 duk_put_global_string(ctx, "x"); 323 duk_put_global_string(ctx, "x");
316 duk_push_int(ctx, rect.y()); 324 duk_push_int(ctx, rect.y());
317 duk_put_global_string(ctx, "y"); 325 duk_put_global_string(ctx, "y");
323 }, 1); 331 }, 1);
324 duk_put_global_string(m_ctx, "build"); 332 duk_put_global_string(m_ctx, "build");
325 333
326 auto ret = duk_peval_string(m_ctx, "build({});"); 334 auto ret = duk_peval_string(m_ctx, "build({});");
327 335
328 if (ret != 0) 336 if (ret != 0) {
329 throw dukx_exception(m_ctx, -1); 337 throw dukx_exception(m_ctx, -1);
338 }
330 339
331 duk_get_global_string(m_ctx, "x"); 340 duk_get_global_string(m_ctx, "x");
332 ASSERT_EQ(0, duk_to_int(m_ctx, -1)); 341 ASSERT_EQ(0, duk_to_int(m_ctx, -1));
333 duk_pop(m_ctx); 342 duk_pop(m_ctx);
334 duk_get_global_string(m_ctx, "y"); 343 duk_get_global_string(m_ctx, "y");