comparison tests/libclient/js-size/main.cpp @ 36:9af360f34c7d

Misc: use raw duktape API
author David Demelier <markand@malikania.fr>
date Wed, 10 Aug 2016 14:30:51 +0200
parents d4f5f7231b84
children a47a4477f347
comparison
equal deleted inserted replaced
35:8e1241156034 36:9af360f34c7d
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:
27 duk::Context m_ctx; 27 UniqueContext m_ctx;
28 28
29 public: 29 public:
30 TestSize() 30 TestSize()
31 { 31 {
32 duk::putGlobal(m_ctx, "Malikania", duk::Object()); 32 duk_push_object(m_ctx);
33 33 duk_put_global_string(m_ctx, "Malikania");
34 loadMalikaniaSize(m_ctx); 34 dukx_load_size(m_ctx);
35 } 35 }
36 }; 36 };
37 37
38 /* 38 /*
39 * Valid constructors 39 * Valid constructors
41 */ 41 */
42 42
43 TEST_F(TestSize, ConstructorNoArgs) 43 TEST_F(TestSize, ConstructorNoArgs)
44 { 44 {
45 try { 45 try {
46 auto ret = duk::pevalString(m_ctx, 46 auto ret = duk_peval_string(m_ctx,
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 duk::exception(m_ctx, -1); 53 throw dukx_exception(m_ctx, -1);
54 } 54
55 55 duk_get_global_string(m_ctx, "w");
56 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "w")); 56 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1));
57 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "h")); 57 duk_pop(m_ctx);
58 duk_get_global_string(m_ctx, "h");
59 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1));
60 duk_pop(m_ctx);
58 } catch (const std::exception &ex) { 61 } catch (const std::exception &ex) {
59 FAIL() << ex.what(); 62 FAIL() << ex.what();
60 } 63 }
61 } 64 }
62 65
63 TEST_F(TestSize, Constructor2Args) 66 TEST_F(TestSize, Constructor2Args)
64 { 67 {
65 try { 68 try {
66 auto ret = duk::pevalString(m_ctx, 69 auto ret = duk_peval_string(m_ctx,
67 "s = Malikania.Size(100, 200);" 70 "s = Malikania.Size(100, 200);"
68 "w = s.width;" 71 "w = s.width;"
69 "h = s.height;" 72 "h = s.height;"
70 ); 73 );
71 74
72 if (ret != 0) { 75 if (ret != 0)
73 throw duk::exception(m_ctx, -1); 76 throw dukx_exception(m_ctx, -1);
74 } 77
75 78 duk_get_global_string(m_ctx, "w");
76 ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w")); 79 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1));
77 ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h")); 80 duk_pop(m_ctx);
81 duk_get_global_string(m_ctx, "h");
82 ASSERT_EQ(200U, duk_to_uint(m_ctx, -1));
83 duk_pop(m_ctx);
78 } catch (const std::exception &ex) { 84 } catch (const std::exception &ex) {
79 FAIL() << ex.what(); 85 FAIL() << ex.what();
80 } 86 }
81 } 87 }
82 88
83 TEST_F(TestSize, ConstructorObject) 89 TEST_F(TestSize, ConstructorObject)
84 { 90 {
85 try { 91 try {
86 auto ret = duk::pevalString(m_ctx, 92 auto ret = duk_peval_string(m_ctx,
87 "s = Malikania.Size({ width: 100, height: 200 });" 93 "s = Malikania.Size({ width: 100, height: 200 });"
88 "w = s.width;" 94 "w = s.width;"
89 "h = s.height;" 95 "h = s.height;"
90 ); 96 );
91 97
92 if (ret != 0) { 98 if (ret != 0)
93 throw duk::exception(m_ctx, -1); 99 throw dukx_exception(m_ctx, -1);
94 } 100
95 101 duk_get_global_string(m_ctx, "w");
96 ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w")); 102 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1));
97 ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h")); 103 duk_pop(m_ctx);
104 duk_get_global_string(m_ctx, "h");
105 ASSERT_EQ(200U, duk_to_uint(m_ctx, -1));
106 duk_pop(m_ctx);
98 } catch (const std::exception &ex) { 107 } catch (const std::exception &ex) {
99 FAIL() << ex.what(); 108 FAIL() << ex.what();
100 } 109 }
101 } 110 }
102 111
103 TEST_F(TestSize, ConstructorNew) 112 TEST_F(TestSize, ConstructorNew)
104 { 113 {
105 try { 114 try {
106 auto ret = duk::pevalString(m_ctx, 115 auto ret = duk_peval_string(m_ctx,
107 "s = new Malikania.Size({ width: 100, height: 200 });" 116 "s = new Malikania.Size({ width: 100, height: 200 });"
108 "w = s.width;" 117 "w = s.width;"
109 "h = s.height;" 118 "h = s.height;"
110 ); 119 );
111 120
112 if (ret != 0) { 121 if (ret != 0)
113 throw duk::exception(m_ctx, -1); 122 throw dukx_exception(m_ctx, -1);
114 } 123
115 124 duk_get_global_string(m_ctx, "w");
116 ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w")); 125 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1));
117 ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h")); 126 duk_pop(m_ctx);
127 duk_get_global_string(m_ctx, "h");
128 ASSERT_EQ(200U, duk_to_uint(m_ctx, -1));
129 duk_pop(m_ctx);
118 } catch (const std::exception &ex) { 130 } catch (const std::exception &ex) {
119 FAIL() << ex.what(); 131 FAIL() << ex.what();
120 } 132 }
121 } 133 }
122 134
126 */ 138 */
127 139
128 TEST_F(TestSize, InvalidConstructorArg1) 140 TEST_F(TestSize, InvalidConstructorArg1)
129 { 141 {
130 try { 142 try {
131 auto ret = duk::pevalString(m_ctx, 143 auto ret = duk_peval_string(m_ctx,
132 "try {" 144 "try {"
133 " Malikania.Size(null);" 145 " Malikania.Size(null);"
134 "} catch (e) {" 146 "} catch (e) {"
135 " name = e.name;" 147 " name = e.name;"
136 " correct = (e instanceof TypeError);" 148 " correct = (e instanceof TypeError);"
137 "}" 149 "}"
138 ); 150 );
139 151
140 if (ret != 0) { 152 if (ret != 0)
141 throw duk::exception(m_ctx, -1); 153 throw dukx_exception(m_ctx, -1);
142 } 154
143 155 duk_get_global_string(m_ctx, "name");
144 ASSERT_EQ("TypeError", duk::getGlobal<std::string>(m_ctx, "name")); 156 ASSERT_STREQ("TypeError", duk_to_string(m_ctx, -1));
145 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); 157 duk_pop(m_ctx);
158 duk_get_global_string(m_ctx, "correct");
159 ASSERT_TRUE(duk_to_boolean(m_ctx, -1));
160 duk_pop(m_ctx);
146 } catch (const std::exception &ex) { 161 } catch (const std::exception &ex) {
147 FAIL() << ex.what(); 162 FAIL() << ex.what();
148 } 163 }
149 } 164 }
150 165
151 TEST_F(TestSize, InvalidConstructorRange1) 166 TEST_F(TestSize, InvalidConstructorRange1)
152 { 167 {
153 try { 168 try {
154 auto ret = duk::pevalString(m_ctx, 169 auto ret = duk_peval_string(m_ctx,
155 "try {" 170 "try {"
156 " Malikania.Size(-1, 200);" 171 " Malikania.Size(-1, 200);"
157 "} catch (e) {" 172 "} catch (e) {"
158 " name = e.name;" 173 " name = e.name;"
159 " correct = (e instanceof RangeError);" 174 " correct = (e instanceof RangeError);"
160 "}" 175 "}"
161 ); 176 );
162 177
163 if (ret != 0) { 178 if (ret != 0)
164 throw duk::exception(m_ctx, -1); 179 throw dukx_exception(m_ctx, -1);
165 } 180
166 181 duk_get_global_string(m_ctx, "name");
167 ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); 182 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1));
168 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); 183 duk_pop(m_ctx);
184 duk_get_global_string(m_ctx, "correct");
185 ASSERT_TRUE(duk_to_boolean(m_ctx, -1));
186 duk_pop(m_ctx);
169 } catch (const std::exception &ex) { 187 } catch (const std::exception &ex) {
170 FAIL() << ex.what(); 188 FAIL() << ex.what();
171 } 189 }
172 } 190 }
173 191
174 TEST_F(TestSize, InvalidConstructorRange2) 192 TEST_F(TestSize, InvalidConstructorRange2)
175 { 193 {
176 try { 194 try {
177 auto ret = duk::pevalString(m_ctx, 195 auto ret = duk_peval_string(m_ctx,
178 "try {" 196 "try {"
179 " Malikania.Size(100, -1);" 197 " Malikania.Size(100, -1);"
180 "} catch (e) {" 198 "} catch (e) {"
181 " name = e.name;" 199 " name = e.name;"
182 " correct = (e instanceof RangeError);" 200 " correct = (e instanceof RangeError);"
183 "}" 201 "}"
184 ); 202 );
185 203
186 if (ret != 0) { 204 if (ret != 0)
187 throw duk::exception(m_ctx, -1); 205 throw dukx_exception(m_ctx, -1);
188 } 206
189 207 duk_get_global_string(m_ctx, "name");
190 ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); 208 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1));
191 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); 209 duk_pop(m_ctx);
210 duk_get_global_string(m_ctx, "correct");
211 ASSERT_TRUE(duk_to_boolean(m_ctx, -1));
212 duk_pop(m_ctx);
192 } catch (const std::exception &ex) { 213 } catch (const std::exception &ex) {
193 FAIL() << ex.what(); 214 FAIL() << ex.what();
194 } 215 }
195 } 216 }
196 217
197 TEST_F(TestSize, InvalidConstructorRange3) 218 TEST_F(TestSize, InvalidConstructorRange3)
198 { 219 {
199 try { 220 try {
200 auto ret = duk::pevalString(m_ctx, 221 auto ret = duk_peval_string(m_ctx,
201 "try {" 222 "try {"
202 " Malikania.Size({ width: -1, height: 200 });" 223 " Malikania.Size({ width: -1, height: 200 });"
203 "} catch (e) {" 224 "} catch (e) {"
204 " name = e.name;" 225 " name = e.name;"
205 " correct = (e instanceof RangeError);" 226 " correct = (e instanceof RangeError);"
206 "}" 227 "}"
207 ); 228 );
208 229
209 if (ret != 0) { 230 if (ret != 0)
210 throw duk::exception(m_ctx, -1); 231 throw dukx_exception(m_ctx, -1);
211 } 232
212 233 duk_get_global_string(m_ctx, "name");
213 ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); 234 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1));
214 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); 235 duk_pop(m_ctx);
236 duk_get_global_string(m_ctx, "correct");
237 ASSERT_TRUE(duk_to_boolean(m_ctx, -1));
238 duk_pop(m_ctx);
215 } catch (const std::exception &ex) { 239 } catch (const std::exception &ex) {
216 FAIL() << ex.what(); 240 FAIL() << ex.what();
217 } 241 }
218 } 242 }
219 243
220 TEST_F(TestSize, InvalidConstructorRange4) 244 TEST_F(TestSize, InvalidConstructorRange4)
221 { 245 {
222 try { 246 try {
223 auto ret = duk::pevalString(m_ctx, 247 auto ret = duk_peval_string(m_ctx,
224 "try {" 248 "try {"
225 " Malikania.Size({ width: 100, height: -1 });" 249 " Malikania.Size({ width: 100, height: -1 });"
226 "} catch (e) {" 250 "} catch (e) {"
227 " name = e.name;" 251 " name = e.name;"
228 " correct = (e instanceof RangeError);" 252 " correct = (e instanceof RangeError);"
229 "}" 253 "}"
230 ); 254 );
231 255
232 if (ret != 0) { 256 if (ret != 0)
233 throw duk::exception(m_ctx, -1); 257 throw dukx_exception(m_ctx, -1);
234 } 258
235 259 duk_get_global_string(m_ctx, "name");
236 ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); 260 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1));
237 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); 261 duk_pop(m_ctx);
262 duk_get_global_string(m_ctx, "correct");
263 ASSERT_TRUE(duk_to_boolean(m_ctx, -1));
264 duk_pop(m_ctx);
238 } catch (const std::exception &ex) { 265 } catch (const std::exception &ex) {
239 FAIL() << ex.what(); 266 FAIL() << ex.what();
240 } 267 }
241 } 268 }
242 269
246 */ 273 */
247 274
248 TEST_F(TestSize, requireSuccess) 275 TEST_F(TestSize, requireSuccess)
249 { 276 {
250 try { 277 try {
251 duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { 278 duk_push_c_function(m_ctx, [] (auto ctx) {
252 Size size = duk::require<Size>(ctx, 0); 279 Size size = dukx_require_size(ctx, 0);
253 280
254 duk::putGlobal(ctx, "w", static_cast<int>(size.width())); 281 duk_push_uint(ctx, size.width());
255 duk::putGlobal(ctx, "h", static_cast<int>(size.height())); 282 duk_put_global_string(ctx, "w");
283 duk_push_uint(ctx, size.height());
284 duk_put_global_string(ctx, "h");
256 285
257 return 0; 286 return 0;
258 }, 1}); 287 }, 1);
259 288 duk_put_global_string(m_ctx, "build");
260 auto ret = duk::pevalString(m_ctx, "build({ width: 100, height: 200 });"); 289
261 290 auto ret = duk_peval_string(m_ctx, "build({ width: 100, height: 200 });");
262 if (ret != 0) { 291
263 throw duk::exception(m_ctx, -1); 292 if (ret != 0)
264 } 293 throw dukx_exception(m_ctx, -1);
265 294
266 ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w")); 295 duk_get_global_string(m_ctx, "w");
267 ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h")); 296 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1));
297 duk_pop(m_ctx);
298 duk_get_global_string(m_ctx, "h");
299 ASSERT_EQ(200U, duk_to_uint(m_ctx, -1));
300 duk_pop(m_ctx);
268 } catch (const std::exception &ex) { 301 } catch (const std::exception &ex) {
269 FAIL() << ex.what(); 302 FAIL() << ex.what();
270 } 303 }
271 } 304 }
272 305
273 TEST_F(TestSize, requireFail) 306 TEST_F(TestSize, requireFail)
274 { 307 {
275 try { 308 try {
276 duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { 309 duk_push_c_function(m_ctx, [] (auto ctx) {
277 duk::require<Size>(ctx, 0); 310 dukx_require_size(ctx, 0);
278 311
279 return 0; 312 return 0;
280 }, 1}); 313 }, 1);
281 314 duk_put_global_string(m_ctx, "build");
282 auto ret = duk::pevalString(m_ctx, 315
316 auto ret = duk_peval_string(m_ctx,
283 "try {" 317 "try {"
284 " build({});" 318 " build({});"
285 "} catch (e) {" 319 "} catch (e) {"
286 " name = e.name;" 320 " name = e.name;"
287 " correct = (e instanceof Error);" 321 " correct = (e instanceof Error);"
288 "}" 322 "}"
289 ); 323 );
290 324
291 if (ret != 0) { 325 if (ret != 0)
292 throw duk::exception(m_ctx, -1); 326 throw dukx_exception(m_ctx, -1);
293 } 327
294 328 duk_get_global_string(m_ctx, "name");
295 ASSERT_EQ("Error", duk::getGlobal<std::string>(m_ctx, "name")); 329 ASSERT_STREQ("Error", duk_to_string(m_ctx, -1));
296 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); 330 duk_pop(m_ctx);
331 duk_get_global_string(m_ctx, "correct");
332 ASSERT_TRUE(duk_to_boolean(m_ctx, -1));
333 duk_pop(m_ctx);
297 } catch (const std::exception &ex) { 334 } catch (const std::exception &ex) {
298 FAIL() << ex.what(); 335 FAIL() << ex.what();
299 } 336 }
300 } 337 }
301 338
305 */ 342 */
306 343
307 TEST_F(TestSize, getAdjustAll) 344 TEST_F(TestSize, getAdjustAll)
308 { 345 {
309 try { 346 try {
310 duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { 347 duk_push_c_function(m_ctx, [] (auto ctx) {
311 Size size = duk::get<Size>(ctx, 0); 348 Size size = dukx_get_size(ctx, 0);
312 349
313 duk::putGlobal(ctx, "w", static_cast<int>(size.width())); 350 duk_push_uint(ctx, size.width());
314 duk::putGlobal(ctx, "h", static_cast<int>(size.height())); 351 duk_put_global_string(ctx, "w");
352 duk_push_uint(ctx, size.height());
353 duk_put_global_string(ctx, "h");
315 354
316 return 0; 355 return 0;
317 }, 1}); 356 }, 1);
318 357 duk_put_global_string(m_ctx, "build");
319 auto ret = duk::pevalString(m_ctx, "build({});"); 358
320 359 auto ret = duk_peval_string(m_ctx, "build({});");
321 if (ret != 0) { 360
322 throw duk::exception(m_ctx, -1); 361 if (ret != 0)
323 } 362 throw dukx_exception(m_ctx, -1);
324 363
325 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r")); 364 duk_get_global_string(m_ctx, "w");
326 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g")); 365 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1));
366 duk_pop(m_ctx);
367 duk_get_global_string(m_ctx, "h");
368 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1));
369 duk_pop(m_ctx);
327 } catch (const std::exception &ex) { 370 } catch (const std::exception &ex) {
328 FAIL() << ex.what(); 371 FAIL() << ex.what();
329 } 372 }
330 } 373 }
331 374