Mercurial > malikania
comparison tests/libclient/js-point/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 TestPoint : public testing::Test { | 25 class TestPoint : public testing::Test { |
26 protected: | 26 protected: |
27 duk::Context m_ctx; | 27 UniqueContext m_ctx; |
28 | 28 |
29 public: | 29 public: |
30 TestPoint() | 30 TestPoint() |
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 loadMalikaniaPoint(m_ctx); | 34 dukx_load_point(m_ctx); |
35 } | 35 } |
36 }; | 36 }; |
37 | 37 |
38 /* | 38 /* |
39 * Valid constructors | 39 * Valid constructors |
41 */ | 41 */ |
42 | 42 |
43 TEST_F(TestPoint, ConstructorNoArgs) | 43 TEST_F(TestPoint, ConstructorNoArgs) |
44 { | 44 { |
45 try { | 45 try { |
46 auto ret = duk::pevalString(m_ctx, | 46 auto ret = duk_peval_string(m_ctx, |
47 "p = Malikania.Point();" | 47 "p = Malikania.Point();" |
48 "x = p.x;" | 48 "x = p.x;" |
49 "y = p.y;" | 49 "y = p.y;" |
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, "x"); |
56 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x")); | 56 ASSERT_EQ(0, duk_to_int(m_ctx, -1)); |
57 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y")); | 57 duk_pop(m_ctx); |
58 duk_get_global_string(m_ctx, "y"); | |
59 ASSERT_EQ(0, duk_to_int(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(TestPoint, Constructor2Args) | 66 TEST_F(TestPoint, Constructor2Args) |
64 { | 67 { |
65 try { | 68 try { |
66 auto ret = duk::pevalString(m_ctx, | 69 auto ret = duk_peval_string(m_ctx, |
67 "p = Malikania.Point(-10, -20);" | 70 "p = Malikania.Point(-10, -20);" |
68 "x = p.x;" | 71 "x = p.x;" |
69 "y = p.y;" | 72 "y = p.y;" |
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, "x"); |
76 ASSERT_EQ(-10, duk::getGlobal<int>(m_ctx, "x")); | 79 ASSERT_EQ(-10, duk_to_int(m_ctx, -1)); |
77 ASSERT_EQ(-20, duk::getGlobal<int>(m_ctx, "y")); | 80 duk_pop(m_ctx); |
81 duk_get_global_string(m_ctx, "y"); | |
82 ASSERT_EQ(-20, duk_to_int(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(TestPoint, ConstructorObject) | 89 TEST_F(TestPoint, ConstructorObject) |
84 { | 90 { |
85 try { | 91 try { |
86 auto ret = duk::pevalString(m_ctx, | 92 auto ret = duk_peval_string(m_ctx, |
87 "p = Malikania.Point({ x: 100, y: 200 });" | 93 "p = Malikania.Point({ x: 100, y: 200 });" |
88 "x = p.x;" | 94 "x = p.x;" |
89 "y = p.y;" | 95 "y = p.y;" |
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, "x"); |
96 ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "x")); | 102 ASSERT_EQ(100, duk_to_int(m_ctx, -1)); |
97 ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "y")); | 103 duk_pop(m_ctx); |
104 duk_get_global_string(m_ctx, "y"); | |
105 ASSERT_EQ(200, duk_to_int(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(TestPoint, ConstructorNew) | 112 TEST_F(TestPoint, ConstructorNew) |
104 { | 113 { |
105 try { | 114 try { |
106 auto ret = duk::pevalString(m_ctx, | 115 auto ret = duk_peval_string(m_ctx, |
107 "p = new Malikania.Point({ x: 100, y: 200 });" | 116 "p = new Malikania.Point({ x: 100, y: 200 });" |
108 "x = p.x;" | 117 "x = p.x;" |
109 "y = p.y;" | 118 "y = p.y;" |
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, "x"); |
116 ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "x")); | 125 ASSERT_EQ(100, duk_to_int(m_ctx, -1)); |
117 ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "y")); | 126 duk_pop(m_ctx); |
127 duk_get_global_string(m_ctx, "y"); | |
128 ASSERT_EQ(200, duk_to_int(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(TestPoint, InvalidConstructorArg1) | 140 TEST_F(TestPoint, 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.Point(null);" | 145 " Malikania.Point(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 |
154 */ | 169 */ |
155 | 170 |
156 TEST_F(TestPoint, requireSuccess) | 171 TEST_F(TestPoint, requireSuccess) |
157 { | 172 { |
158 try { | 173 try { |
159 duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { | 174 duk_push_c_function(m_ctx, [] (auto ctx) { |
160 Point point = duk::require<Point>(ctx, 0); | 175 Point point = dukx_require_point(ctx, 0); |
161 | 176 |
162 duk::putGlobal(ctx, "x", point.x()); | 177 duk_push_int(ctx, point.x()); |
163 duk::putGlobal(ctx, "y", point.y()); | 178 duk_put_global_string(ctx, "x"); |
179 duk_push_int(ctx, point.y()); | |
180 duk_put_global_string(ctx, "y"); | |
164 | 181 |
165 return 0; | 182 return 0; |
166 }, 1}); | 183 }, 1); |
167 | 184 duk_put_global_string(m_ctx, "build"); |
168 auto ret = duk::pevalString(m_ctx, "build({ x: 100, y: 200 });"); | 185 |
169 | 186 auto ret = duk_peval_string(m_ctx, "build({ x: 100, y: 200 });"); |
170 if (ret != 0) { | 187 |
171 throw duk::exception(m_ctx, -1); | 188 if (ret != 0) |
172 } | 189 throw dukx_exception(m_ctx, -1); |
173 | 190 |
174 ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "x")); | 191 duk_get_global_string(m_ctx, "x"); |
175 ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "y")); | 192 ASSERT_EQ(100, duk_to_int(m_ctx, -1)); |
193 duk_pop(m_ctx); | |
194 duk_get_global_string(m_ctx, "y"); | |
195 ASSERT_EQ(200, duk_to_int(m_ctx, -1)); | |
196 duk_pop(m_ctx); | |
176 } catch (const std::exception &ex) { | 197 } catch (const std::exception &ex) { |
177 FAIL() << ex.what(); | 198 FAIL() << ex.what(); |
178 } | 199 } |
179 } | 200 } |
180 | 201 |
181 TEST_F(TestPoint, requireFail) | 202 TEST_F(TestPoint, requireFail) |
182 { | 203 { |
183 try { | 204 try { |
184 duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { | 205 duk_push_c_function(m_ctx, [] (auto ctx) { |
185 duk::require<Point>(ctx, 0); | 206 dukx_require_point(ctx, 0); |
186 | 207 |
187 return 0; | 208 return 0; |
188 }, 1}); | 209 }, 1); |
189 | 210 duk_put_global_string(m_ctx, "build"); |
190 auto ret = duk::pevalString(m_ctx, | 211 |
212 auto ret = duk_peval_string(m_ctx, | |
191 "try {" | 213 "try {" |
192 " build({});" | 214 " build({});" |
193 "} catch (e) {" | 215 "} catch (e) {" |
194 " name = e.name;" | 216 " name = e.name;" |
195 " correct = (e instanceof Error);" | 217 " correct = (e instanceof Error);" |
196 "}" | 218 "}" |
197 ); | 219 ); |
198 | 220 |
199 if (ret != 0) { | 221 if (ret != 0) |
200 throw duk::exception(m_ctx, -1); | 222 throw dukx_exception(m_ctx, -1); |
201 } | 223 |
202 | 224 duk_get_global_string(m_ctx, "name"); |
203 ASSERT_EQ("Error", duk::getGlobal<std::string>(m_ctx, "name")); | 225 ASSERT_STREQ("Error", duk_to_string(m_ctx, -1)); |
204 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); | 226 duk_pop(m_ctx); |
227 duk_get_global_string(m_ctx, "correct"); | |
228 ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); | |
229 duk_pop(m_ctx); | |
205 } catch (const std::exception &ex) { | 230 } catch (const std::exception &ex) { |
206 FAIL() << ex.what(); | 231 FAIL() << ex.what(); |
207 } | 232 } |
208 } | 233 } |
209 | 234 |
213 */ | 238 */ |
214 | 239 |
215 TEST_F(TestPoint, getAdjustAll) | 240 TEST_F(TestPoint, getAdjustAll) |
216 { | 241 { |
217 try { | 242 try { |
218 duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { | 243 duk_push_c_function(m_ctx, [] (auto ctx) { |
219 Point point = duk::get<Point>(ctx, 0); | 244 Point point = dukx_get_point(ctx, 0); |
220 | 245 |
221 duk::putGlobal(ctx, "x", point.x()); | 246 duk_push_int(ctx, point.x()); |
222 duk::putGlobal(ctx, "y", point.y()); | 247 duk_put_global_string(ctx, "x"); |
248 duk_push_int(ctx, point.y()); | |
249 duk_put_global_string(ctx, "y"); | |
223 | 250 |
224 return 0; | 251 return 0; |
225 }, 1}); | 252 }, 1); |
226 | 253 duk_put_global_string(m_ctx, "build"); |
227 auto ret = duk::pevalString(m_ctx, "build({});"); | 254 |
228 | 255 auto ret = duk_peval_string(m_ctx, "build({});"); |
229 if (ret != 0) { | 256 |
230 throw duk::exception(m_ctx, -1); | 257 if (ret != 0) |
231 } | 258 throw dukx_exception(m_ctx, -1); |
232 | 259 |
233 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x")); | 260 duk_get_global_string(m_ctx, "x"); |
234 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y")); | 261 ASSERT_EQ(0, duk_to_int(m_ctx, -1)); |
262 duk_pop(m_ctx); | |
263 duk_get_global_string(m_ctx, "y"); | |
264 ASSERT_EQ(0, duk_to_int(m_ctx, -1)); | |
265 duk_pop(m_ctx); | |
235 } catch (const std::exception &ex) { | 266 } catch (const std::exception &ex) { |
236 FAIL() << ex.what(); | 267 FAIL() << ex.what(); |
237 } | 268 } |
238 } | 269 } |
239 | 270 |