comparison tests/libclient/js-rectangle/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 TestRectangle : public testing::Test { 25 class TestRectangle : public testing::Test {
26 protected: 26 protected:
27 duk::Context m_ctx; 27 UniqueContext m_ctx;
28 28
29 public: 29 public:
30 TestRectangle() 30 TestRectangle()
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 loadMalikaniaRectangle(m_ctx); 34 dukx_load_rect(m_ctx);
35 } 35 }
36 }; 36 };
37 37
38 /* 38 /*
39 * Valid constructors 39 * Valid constructors
41 */ 41 */
42 42
43 TEST_F(TestRectangle, ConstructorNoArgs) 43 TEST_F(TestRectangle, ConstructorNoArgs)
44 { 44 {
45 try { 45 try {
46 auto ret = duk::pevalString(m_ctx, 46 auto ret = duk_peval_string(m_ctx,
47 "r = Malikania.Rectangle();" 47 "r = Malikania.Rectangle();"
48 "x = r.x;" 48 "x = r.x;"
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 duk::exception(m_ctx, -1); 55 throw dukx_exception(m_ctx, -1);
56 } 56
57 57 duk_get_global_string(m_ctx, "x");
58 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x")); 58 ASSERT_EQ(0, duk_to_int(m_ctx, -1));
59 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y")); 59 duk_pop(m_ctx);
60 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "w")); 60 duk_get_global_string(m_ctx, "y");
61 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "h")); 61 ASSERT_EQ(0, duk_to_int(m_ctx, -1));
62 duk_pop(m_ctx);
63 duk_get_global_string(m_ctx, "w");
64 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1));
65 duk_pop(m_ctx);
66 duk_get_global_string(m_ctx, "h");
67 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1));
68 duk_pop(m_ctx);
62 } catch (const std::exception &ex) { 69 } catch (const std::exception &ex) {
63 FAIL() << ex.what(); 70 FAIL() << ex.what();
64 } 71 }
65 } 72 }
66 73
67 TEST_F(TestRectangle, Constructor4Args) 74 TEST_F(TestRectangle, Constructor4Args)
68 { 75 {
69 try { 76 try {
70 auto ret = duk::pevalString(m_ctx, 77 auto ret = duk_peval_string(m_ctx,
71 "r = Malikania.Rectangle(10, 20, 30, 40);" 78 "r = Malikania.Rectangle(10, 20, 30, 40);"
72 "x = r.x;" 79 "x = r.x;"
73 "y = r.y;" 80 "y = r.y;"
74 "w = r.width;" 81 "w = r.width;"
75 "h = r.height;" 82 "h = r.height;"
76 ); 83 );
77 84
78 if (ret != 0) { 85 if (ret != 0)
79 throw duk::exception(m_ctx, -1); 86 throw dukx_exception(m_ctx, -1);
80 } 87
81 88 duk_get_global_string(m_ctx, "x");
82 ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "x")); 89 ASSERT_EQ(10, duk_to_int(m_ctx, -1));
83 ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "y")); 90 duk_pop(m_ctx);
84 ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "w")); 91 duk_get_global_string(m_ctx, "y");
85 ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "h")); 92 ASSERT_EQ(20, duk_to_int(m_ctx, -1));
93 duk_pop(m_ctx);
94 duk_get_global_string(m_ctx, "w");
95 ASSERT_EQ(30U, duk_to_uint(m_ctx, -1));
96 duk_pop(m_ctx);
97 duk_get_global_string(m_ctx, "h");
98 ASSERT_EQ(40U, duk_to_uint(m_ctx, -1));
99 duk_pop(m_ctx);
86 } catch (const std::exception &ex) { 100 } catch (const std::exception &ex) {
87 FAIL() << ex.what(); 101 FAIL() << ex.what();
88 } 102 }
89 } 103 }
90 104
91 TEST_F(TestRectangle, ConstructorObject) 105 TEST_F(TestRectangle, ConstructorObject)
92 { 106 {
93 try { 107 try {
94 auto ret = duk::pevalString(m_ctx, 108 auto ret = duk_peval_string(m_ctx,
95 "r = Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });" 109 "r = Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });"
96 "x = r.x;" 110 "x = r.x;"
97 "y = r.y;" 111 "y = r.y;"
98 "w = r.width;" 112 "w = r.width;"
99 "h = r.height;" 113 "h = r.height;"
100 ); 114 );
101 115
102 if (ret != 0) { 116 if (ret != 0)
103 throw duk::exception(m_ctx, -1); 117 throw dukx_exception(m_ctx, -1);
104 } 118
105 119 duk_get_global_string(m_ctx, "x");
106 ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "x")); 120 ASSERT_EQ(10, duk_to_int(m_ctx, -1));
107 ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "y")); 121 duk_pop(m_ctx);
108 ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "w")); 122 duk_get_global_string(m_ctx, "y");
109 ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "h")); 123 ASSERT_EQ(20, duk_to_int(m_ctx, -1));
124 duk_pop(m_ctx);
125 duk_get_global_string(m_ctx, "w");
126 ASSERT_EQ(30U, duk_to_uint(m_ctx, -1));
127 duk_pop(m_ctx);
128 duk_get_global_string(m_ctx, "h");
129 ASSERT_EQ(40U, duk_to_uint(m_ctx, -1));
130 duk_pop(m_ctx);
110 } catch (const std::exception &ex) { 131 } catch (const std::exception &ex) {
111 FAIL() << ex.what(); 132 FAIL() << ex.what();
112 } 133 }
113 } 134 }
114 135
115 TEST_F(TestRectangle, ConstructorNew) 136 TEST_F(TestRectangle, ConstructorNew)
116 { 137 {
117 try { 138 try {
118 auto ret = duk::pevalString(m_ctx, 139 auto ret = duk_peval_string(m_ctx,
119 "r = new Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });" 140 "r = new Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });"
120 "x = r.x;" 141 "x = r.x;"
121 "y = r.y;" 142 "y = r.y;"
122 "w = r.width;" 143 "w = r.width;"
123 "h = r.height;" 144 "h = r.height;"
124 ); 145 );
125 146
126 if (ret != 0) { 147 if (ret != 0)
127 throw duk::exception(m_ctx, -1); 148 throw dukx_exception(m_ctx, -1);
128 } 149
129 150 duk_get_global_string(m_ctx, "x");
130 ASSERT_EQ(10, duk::getGlobal<int>(m_ctx, "x")); 151 ASSERT_EQ(10, duk_to_int(m_ctx, -1));
131 ASSERT_EQ(20, duk::getGlobal<int>(m_ctx, "y")); 152 duk_pop(m_ctx);
132 ASSERT_EQ(30, duk::getGlobal<int>(m_ctx, "w")); 153 duk_get_global_string(m_ctx, "y");
133 ASSERT_EQ(40, duk::getGlobal<int>(m_ctx, "h")); 154 ASSERT_EQ(20, duk_to_int(m_ctx, -1));
155 duk_pop(m_ctx);
156 duk_get_global_string(m_ctx, "w");
157 ASSERT_EQ(30U, duk_to_uint(m_ctx, -1));
158 duk_pop(m_ctx);
159 duk_get_global_string(m_ctx, "h");
160 ASSERT_EQ(40U, duk_to_uint(m_ctx, -1));
161 duk_pop(m_ctx);
134 } catch (const std::exception &ex) { 162 } catch (const std::exception &ex) {
135 FAIL() << ex.what(); 163 FAIL() << ex.what();
136 } 164 }
137 } 165 }
138 166
142 */ 170 */
143 171
144 TEST_F(TestRectangle, InvalidConstructorArg1) 172 TEST_F(TestRectangle, InvalidConstructorArg1)
145 { 173 {
146 try { 174 try {
147 auto ret = duk::pevalString(m_ctx, 175 auto ret = duk_peval_string(m_ctx,
148 "try {" 176 "try {"
149 " Malikania.Rectangle(null);" 177 " Malikania.Rectangle(null);"
150 "} catch (e) {" 178 "} catch (e) {"
151 " name = e.name;" 179 " name = e.name;"
152 " correct = (e instanceof TypeError);" 180 " correct = (e instanceof TypeError);"
153 "}" 181 "}"
154 ); 182 );
155 183
156 if (ret != 0) { 184 if (ret != 0)
157 throw duk::exception(m_ctx, -1); 185 throw dukx_exception(m_ctx, -1);
158 } 186
159 187 duk_get_global_string(m_ctx, "name");
160 ASSERT_EQ("TypeError", duk::getGlobal<std::string>(m_ctx, "name")); 188 ASSERT_STREQ("TypeError", duk_to_string(m_ctx, -1));
161 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); 189 duk_pop(m_ctx);
190 duk_get_global_string(m_ctx, "correct");
191 ASSERT_TRUE(duk_to_boolean(m_ctx, -1));
192 duk_pop(m_ctx);
162 } catch (const std::exception &ex) { 193 } catch (const std::exception &ex) {
163 FAIL() << ex.what(); 194 FAIL() << ex.what();
164 } 195 }
165 } 196 }
166 197
167 TEST_F(TestRectangle, InvalidConstructorRange1) 198 TEST_F(TestRectangle, InvalidConstructorRange1)
168 { 199 {
169 try { 200 try {
170 auto ret = duk::pevalString(m_ctx, 201 auto ret = duk_peval_string(m_ctx,
171 "try {" 202 "try {"
172 " Malikania.Rectangle(0, 0, -10, -10);" 203 " Malikania.Rectangle(0, 0, -10, -10);"
173 "} catch (e) {" 204 "} catch (e) {"
174 " name = e.name;" 205 " name = e.name;"
175 " correct = (e instanceof RangeError);" 206 " correct = (e instanceof RangeError);"
176 "}" 207 "}"
177 ); 208 );
178 209
179 if (ret != 0) { 210 if (ret != 0)
180 throw duk::exception(m_ctx, -1); 211 throw dukx_exception(m_ctx, -1);
181 } 212
182 213 duk_get_global_string(m_ctx, "name");
183 ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name")); 214 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1));
184 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); 215 duk_pop(m_ctx);
216 duk_get_global_string(m_ctx, "correct");
217 ASSERT_TRUE(duk_to_boolean(m_ctx, -1));
218 duk_pop(m_ctx);
185 } catch (const std::exception &ex) { 219 } catch (const std::exception &ex) {
186 FAIL() << ex.what(); 220 FAIL() << ex.what();
187 } 221 }
188 } 222 }
189 223
193 */ 227 */
194 228
195 TEST_F(TestRectangle, requireSuccess) 229 TEST_F(TestRectangle, requireSuccess)
196 { 230 {
197 try { 231 try {
198 duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { 232 duk_push_c_function(m_ctx, [] (auto ctx) {
199 Rectangle rect = duk::require<Rectangle>(ctx, 0); 233 Rectangle rect = dukx_require_rect(ctx, 0);
200 234
201 duk::putGlobal(ctx, "x", rect.x()); 235 duk_push_int(ctx, rect.x());
202 duk::putGlobal(ctx, "y", rect.y()); 236 duk_put_global_string(ctx, "x");
203 duk::putGlobal(ctx, "w", static_cast<int>(rect.width())); 237 duk_push_int(ctx, rect.y());
204 duk::putGlobal(ctx, "h", static_cast<int>(rect.height())); 238 duk_put_global_string(ctx, "y");
239 duk_push_uint(ctx, rect.width());
240 duk_put_global_string(ctx, "w");
241 duk_push_uint(ctx, rect.height());
242 duk_put_global_string(ctx, "h");
205 243
206 return 0; 244 return 0;
207 }, 1}); 245 }, 1);
208 246 duk_put_global_string(m_ctx, "build");
209 auto ret = duk::pevalString(m_ctx, "build({ x: 50, y: 80, width: 100, height: 200 });"); 247
210 248 auto ret = duk_peval_string(m_ctx, "build({ x: 50, y: 80, width: 100, height: 200 });");
211 if (ret != 0) { 249
212 throw duk::exception(m_ctx, -1); 250 if (ret != 0)
213 } 251 throw dukx_exception(m_ctx, -1);
214 252
215 ASSERT_EQ(50, duk::getGlobal<int>(m_ctx, "x")); 253 duk_get_global_string(m_ctx, "x");
216 ASSERT_EQ(80, duk::getGlobal<int>(m_ctx, "y")); 254 ASSERT_EQ(50, duk_to_int(m_ctx, -1));
217 ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w")); 255 duk_pop(m_ctx);
218 ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h")); 256 duk_get_global_string(m_ctx, "y");
257 ASSERT_EQ(80, duk_to_int(m_ctx, -1));
258 duk_pop(m_ctx);
259 duk_get_global_string(m_ctx, "w");
260 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1));
261 duk_pop(m_ctx);
262 duk_get_global_string(m_ctx, "h");
263 ASSERT_EQ(200U, duk_to_uint(m_ctx, -1));
264 duk_pop(m_ctx);
219 } catch (const std::exception &ex) { 265 } catch (const std::exception &ex) {
220 FAIL() << ex.what(); 266 FAIL() << ex.what();
221 } 267 }
222 } 268 }
223 269
224 TEST_F(TestRectangle, requireFail) 270 TEST_F(TestRectangle, requireFail)
225 { 271 {
226 try { 272 try {
227 duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { 273 duk_push_c_function(m_ctx, [] (auto ctx) {
228 duk::require<Rectangle>(ctx, 0); 274 dukx_require_rect(ctx, 0);
229 275
230 return 0; 276 return 0;
231 }, 1}); 277 }, 1);
232 278 duk_put_global_string(m_ctx, "build");
233 auto ret = duk::pevalString(m_ctx, 279
280 auto ret = duk_peval_string(m_ctx,
234 "try {" 281 "try {"
235 " build({});" 282 " build({});"
236 "} catch (e) {" 283 "} catch (e) {"
237 " name = e.name;" 284 " name = e.name;"
238 " correct = (e instanceof Error);" 285 " correct = (e instanceof Error);"
239 "}" 286 "}"
240 ); 287 );
241 288
242 if (ret != 0) { 289 if (ret != 0)
243 throw duk::exception(m_ctx, -1); 290 throw dukx_exception(m_ctx, -1);
244 } 291
245 292 duk_get_global_string(m_ctx, "name");
246 ASSERT_EQ("Error", duk::getGlobal<std::string>(m_ctx, "name")); 293 ASSERT_STREQ("Error", duk_to_string(m_ctx, -1));
247 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); 294 duk_pop(m_ctx);
295 duk_get_global_string(m_ctx, "correct");
296 ASSERT_TRUE(duk_to_boolean(m_ctx, -1));
297 duk_pop(m_ctx);
248 } catch (const std::exception &ex) { 298 } catch (const std::exception &ex) {
249 FAIL() << ex.what(); 299 FAIL() << ex.what();
250 } 300 }
251 } 301 }
252 302
256 */ 306 */
257 307
258 TEST_F(TestRectangle, getAdjustAll) 308 TEST_F(TestRectangle, getAdjustAll)
259 { 309 {
260 try { 310 try {
261 duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { 311 duk_push_c_function(m_ctx, [] (auto ctx) {
262 Rectangle rect = duk::get<Rectangle>(ctx, 0); 312 Rectangle rect = dukx_get_rect(ctx, 0);
263 313
264 duk::putGlobal(ctx, "x", rect.x()); 314 duk_push_int(ctx, rect.x());
265 duk::putGlobal(ctx, "y", rect.y()); 315 duk_put_global_string(ctx, "x");
266 duk::putGlobal(ctx, "w", static_cast<int>(rect.width())); 316 duk_push_int(ctx, rect.y());
267 duk::putGlobal(ctx, "h", static_cast<int>(rect.height())); 317 duk_put_global_string(ctx, "y");
318 duk_push_uint(ctx, rect.width());
319 duk_put_global_string(ctx, "w");
320 duk_push_uint(ctx, rect.height());
268 321
269 return 0; 322 return 0;
270 }, 1}); 323 }, 1);
271 324 duk_put_global_string(m_ctx, "build");
272 auto ret = duk::pevalString(m_ctx, "build({});"); 325
273 326 auto ret = duk_peval_string(m_ctx, "build({});");
274 if (ret != 0) { 327
275 throw duk::exception(m_ctx, -1); 328 if (ret != 0)
276 } 329 throw dukx_exception(m_ctx, -1);
277 330
278 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x")); 331 duk_get_global_string(m_ctx, "x");
279 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y")); 332 ASSERT_EQ(0, duk_to_int(m_ctx, -1));
280 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "w")); 333 duk_pop(m_ctx);
281 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "h")); 334 duk_get_global_string(m_ctx, "y");
335 ASSERT_EQ(0, duk_to_int(m_ctx, -1));
336 duk_pop(m_ctx);
337 duk_get_global_string(m_ctx, "w");
338 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1));
339 duk_pop(m_ctx);
340 duk_get_global_string(m_ctx, "h");
341 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1));
342 duk_pop(m_ctx);
282 } catch (const std::exception &ex) { 343 } catch (const std::exception &ex) {
283 FAIL() << ex.what(); 344 FAIL() << ex.what();
284 } 345 }
285 } 346 }
286 347