Mercurial > malikania
comparison tests/libclient/js-size/main.cpp @ 44:f30c84b4b9ed
Tests: switch from GoogleTest to Boost.Unit, closes #588
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 30 Nov 2016 21:15:53 +0100 |
parents | fabbe1759cec |
children | fe7e3524e571 |
comparison
equal
deleted
inserted
replaced
43:fabbe1759cec | 44:f30c84b4b9ed |
---|---|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
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 #define BOOST_TEST_MODULE "Javascript Size" |
20 #include <boost/test/unit_test.hpp> | |
20 | 21 |
21 #include <malikania/js_size.hpp> | 22 #include <malikania/js_size.hpp> |
22 | 23 |
23 using namespace mlk; | 24 using namespace mlk; |
24 | 25 |
25 class TestSize : public testing::Test { | 26 class test_size { |
26 protected: | 27 protected: |
27 UniqueContext m_ctx; | 28 UniqueContext m_ctx; |
28 | 29 |
29 public: | 30 public: |
30 TestSize() | 31 test_size() |
31 { | 32 { |
32 duk_push_object(m_ctx); | 33 duk_push_object(m_ctx); |
33 duk_put_global_string(m_ctx, "Malikania"); | 34 duk_put_global_string(m_ctx, "Malikania"); |
34 dukx_load_size(m_ctx); | 35 dukx_load_size(m_ctx); |
35 } | 36 } |
36 }; | 37 }; |
37 | 38 |
39 BOOST_FIXTURE_TEST_SUITE(test_size_suite, test_size) | |
40 | |
38 /* | 41 /* |
39 * Valid constructors | 42 * Valid constructors |
40 * ------------------------------------------------------------------ | 43 * ------------------------------------------------------------------ |
41 */ | 44 */ |
42 | 45 |
43 TEST_F(TestSize, ConstructorNoArgs) | 46 BOOST_AUTO_TEST_CASE(ConstructorNoArgs) |
44 { | 47 { |
45 try { | 48 try { |
46 auto ret = duk_peval_string(m_ctx, | 49 auto ret = duk_peval_string(m_ctx, |
47 "s = Malikania.Size();" | 50 "s = Malikania.Size();" |
48 "w = s.width;" | 51 "w = s.width;" |
52 if (ret != 0) { | 55 if (ret != 0) { |
53 throw dukx_exception(m_ctx, -1); | 56 throw dukx_exception(m_ctx, -1); |
54 } | 57 } |
55 | 58 |
56 duk_get_global_string(m_ctx, "w"); | 59 duk_get_global_string(m_ctx, "w"); |
57 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); | 60 BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); |
58 duk_pop(m_ctx); | 61 duk_pop(m_ctx); |
59 duk_get_global_string(m_ctx, "h"); | 62 duk_get_global_string(m_ctx, "h"); |
60 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); | 63 BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); |
61 duk_pop(m_ctx); | 64 duk_pop(m_ctx); |
62 } catch (const std::exception &ex) { | 65 } catch (const std::exception &ex) { |
63 FAIL() << ex.what(); | 66 BOOST_FAIL(ex.what()); |
64 } | 67 } |
65 } | 68 } |
66 | 69 |
67 TEST_F(TestSize, Constructor2Args) | 70 BOOST_AUTO_TEST_CASE(Constructor2Args) |
68 { | 71 { |
69 try { | 72 try { |
70 auto ret = duk_peval_string(m_ctx, | 73 auto ret = duk_peval_string(m_ctx, |
71 "s = Malikania.Size(100, 200);" | 74 "s = Malikania.Size(100, 200);" |
72 "w = s.width;" | 75 "w = s.width;" |
76 if (ret != 0) { | 79 if (ret != 0) { |
77 throw dukx_exception(m_ctx, -1); | 80 throw dukx_exception(m_ctx, -1); |
78 } | 81 } |
79 | 82 |
80 duk_get_global_string(m_ctx, "w"); | 83 duk_get_global_string(m_ctx, "w"); |
81 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1)); | 84 BOOST_REQUIRE_EQUAL(100U, duk_to_uint(m_ctx, -1)); |
82 duk_pop(m_ctx); | 85 duk_pop(m_ctx); |
83 duk_get_global_string(m_ctx, "h"); | 86 duk_get_global_string(m_ctx, "h"); |
84 ASSERT_EQ(200U, duk_to_uint(m_ctx, -1)); | 87 BOOST_REQUIRE_EQUAL(200U, duk_to_uint(m_ctx, -1)); |
85 duk_pop(m_ctx); | 88 duk_pop(m_ctx); |
86 } catch (const std::exception &ex) { | 89 } catch (const std::exception &ex) { |
87 FAIL() << ex.what(); | 90 BOOST_FAIL(ex.what()); |
88 } | 91 } |
89 } | 92 } |
90 | 93 |
91 TEST_F(TestSize, ConstructorObject) | 94 BOOST_AUTO_TEST_CASE(ConstructorObject) |
92 { | 95 { |
93 try { | 96 try { |
94 auto ret = duk_peval_string(m_ctx, | 97 auto ret = duk_peval_string(m_ctx, |
95 "s = Malikania.Size({ width: 100, height: 200 });" | 98 "s = Malikania.Size({ width: 100, height: 200 });" |
96 "w = s.width;" | 99 "w = s.width;" |
100 if (ret != 0) { | 103 if (ret != 0) { |
101 throw dukx_exception(m_ctx, -1); | 104 throw dukx_exception(m_ctx, -1); |
102 } | 105 } |
103 | 106 |
104 duk_get_global_string(m_ctx, "w"); | 107 duk_get_global_string(m_ctx, "w"); |
105 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1)); | 108 BOOST_REQUIRE_EQUAL(100U, duk_to_uint(m_ctx, -1)); |
106 duk_pop(m_ctx); | 109 duk_pop(m_ctx); |
107 duk_get_global_string(m_ctx, "h"); | 110 duk_get_global_string(m_ctx, "h"); |
108 ASSERT_EQ(200U, duk_to_uint(m_ctx, -1)); | 111 BOOST_REQUIRE_EQUAL(200U, duk_to_uint(m_ctx, -1)); |
109 duk_pop(m_ctx); | 112 duk_pop(m_ctx); |
110 } catch (const std::exception &ex) { | 113 } catch (const std::exception &ex) { |
111 FAIL() << ex.what(); | 114 BOOST_FAIL(ex.what()); |
112 } | 115 } |
113 } | 116 } |
114 | 117 |
115 TEST_F(TestSize, ConstructorNew) | 118 BOOST_AUTO_TEST_CASE(ConstructorNew) |
116 { | 119 { |
117 try { | 120 try { |
118 auto ret = duk_peval_string(m_ctx, | 121 auto ret = duk_peval_string(m_ctx, |
119 "s = new Malikania.Size({ width: 100, height: 200 });" | 122 "s = new Malikania.Size({ width: 100, height: 200 });" |
120 "w = s.width;" | 123 "w = s.width;" |
124 if (ret != 0) { | 127 if (ret != 0) { |
125 throw dukx_exception(m_ctx, -1); | 128 throw dukx_exception(m_ctx, -1); |
126 } | 129 } |
127 | 130 |
128 duk_get_global_string(m_ctx, "w"); | 131 duk_get_global_string(m_ctx, "w"); |
129 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1)); | 132 BOOST_REQUIRE_EQUAL(100U, duk_to_uint(m_ctx, -1)); |
130 duk_pop(m_ctx); | 133 duk_pop(m_ctx); |
131 duk_get_global_string(m_ctx, "h"); | 134 duk_get_global_string(m_ctx, "h"); |
132 ASSERT_EQ(200U, duk_to_uint(m_ctx, -1)); | 135 BOOST_REQUIRE_EQUAL(200U, duk_to_uint(m_ctx, -1)); |
133 duk_pop(m_ctx); | 136 duk_pop(m_ctx); |
134 } catch (const std::exception &ex) { | 137 } catch (const std::exception &ex) { |
135 FAIL() << ex.what(); | 138 BOOST_FAIL(ex.what()); |
136 } | 139 } |
137 } | 140 } |
138 | 141 |
139 /* | 142 /* |
140 * Invalid constructors | 143 * Invalid constructors |
141 * ------------------------------------------------------------------ | 144 * ------------------------------------------------------------------ |
142 */ | 145 */ |
143 | 146 |
144 TEST_F(TestSize, InvalidConstructorArg1) | 147 BOOST_AUTO_TEST_CASE(InvalidConstructorArg1) |
145 { | 148 { |
146 try { | 149 try { |
147 auto ret = duk_peval_string(m_ctx, | 150 auto ret = duk_peval_string(m_ctx, |
148 "try {" | 151 "try {" |
149 " Malikania.Size(null);" | 152 " Malikania.Size(null);" |
156 if (ret != 0) { | 159 if (ret != 0) { |
157 throw dukx_exception(m_ctx, -1); | 160 throw dukx_exception(m_ctx, -1); |
158 } | 161 } |
159 | 162 |
160 duk_get_global_string(m_ctx, "name"); | 163 duk_get_global_string(m_ctx, "name"); |
161 ASSERT_STREQ("TypeError", duk_to_string(m_ctx, -1)); | 164 BOOST_REQUIRE_EQUAL("TypeError", duk_to_string(m_ctx, -1)); |
162 duk_pop(m_ctx); | 165 duk_pop(m_ctx); |
163 duk_get_global_string(m_ctx, "correct"); | 166 duk_get_global_string(m_ctx, "correct"); |
164 ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); | 167 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1)); |
165 duk_pop(m_ctx); | 168 duk_pop(m_ctx); |
166 } catch (const std::exception &ex) { | 169 } catch (const std::exception &ex) { |
167 FAIL() << ex.what(); | 170 BOOST_FAIL(ex.what()); |
168 } | 171 } |
169 } | 172 } |
170 | 173 |
171 TEST_F(TestSize, InvalidConstructorRange1) | 174 BOOST_AUTO_TEST_CASE(InvalidConstructorRange1) |
172 { | 175 { |
173 try { | 176 try { |
174 auto ret = duk_peval_string(m_ctx, | 177 auto ret = duk_peval_string(m_ctx, |
175 "try {" | 178 "try {" |
176 " Malikania.Size(-1, 200);" | 179 " Malikania.Size(-1, 200);" |
183 if (ret != 0) { | 186 if (ret != 0) { |
184 throw dukx_exception(m_ctx, -1); | 187 throw dukx_exception(m_ctx, -1); |
185 } | 188 } |
186 | 189 |
187 duk_get_global_string(m_ctx, "name"); | 190 duk_get_global_string(m_ctx, "name"); |
188 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); | 191 BOOST_REQUIRE_EQUAL("RangeError", duk_to_string(m_ctx, -1)); |
189 duk_pop(m_ctx); | 192 duk_pop(m_ctx); |
190 duk_get_global_string(m_ctx, "correct"); | 193 duk_get_global_string(m_ctx, "correct"); |
191 ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); | 194 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1)); |
192 duk_pop(m_ctx); | 195 duk_pop(m_ctx); |
193 } catch (const std::exception &ex) { | 196 } catch (const std::exception &ex) { |
194 FAIL() << ex.what(); | 197 BOOST_FAIL(ex.what()); |
195 } | 198 } |
196 } | 199 } |
197 | 200 |
198 TEST_F(TestSize, InvalidConstructorRange2) | 201 BOOST_AUTO_TEST_CASE(InvalidConstructorRange2) |
199 { | 202 { |
200 try { | 203 try { |
201 auto ret = duk_peval_string(m_ctx, | 204 auto ret = duk_peval_string(m_ctx, |
202 "try {" | 205 "try {" |
203 " Malikania.Size(100, -1);" | 206 " Malikania.Size(100, -1);" |
210 if (ret != 0) { | 213 if (ret != 0) { |
211 throw dukx_exception(m_ctx, -1); | 214 throw dukx_exception(m_ctx, -1); |
212 } | 215 } |
213 | 216 |
214 duk_get_global_string(m_ctx, "name"); | 217 duk_get_global_string(m_ctx, "name"); |
215 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); | 218 BOOST_REQUIRE_EQUAL("RangeError", duk_to_string(m_ctx, -1)); |
216 duk_pop(m_ctx); | 219 duk_pop(m_ctx); |
217 duk_get_global_string(m_ctx, "correct"); | 220 duk_get_global_string(m_ctx, "correct"); |
218 ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); | 221 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1)); |
219 duk_pop(m_ctx); | 222 duk_pop(m_ctx); |
220 } catch (const std::exception &ex) { | 223 } catch (const std::exception &ex) { |
221 FAIL() << ex.what(); | 224 BOOST_FAIL(ex.what()); |
222 } | 225 } |
223 } | 226 } |
224 | 227 |
225 TEST_F(TestSize, InvalidConstructorRange3) | 228 BOOST_AUTO_TEST_CASE(InvalidConstructorRange3) |
226 { | 229 { |
227 try { | 230 try { |
228 auto ret = duk_peval_string(m_ctx, | 231 auto ret = duk_peval_string(m_ctx, |
229 "try {" | 232 "try {" |
230 " Malikania.Size({ width: -1, height: 200 });" | 233 " Malikania.Size({ width: -1, height: 200 });" |
237 if (ret != 0) { | 240 if (ret != 0) { |
238 throw dukx_exception(m_ctx, -1); | 241 throw dukx_exception(m_ctx, -1); |
239 } | 242 } |
240 | 243 |
241 duk_get_global_string(m_ctx, "name"); | 244 duk_get_global_string(m_ctx, "name"); |
242 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); | 245 BOOST_REQUIRE_EQUAL("RangeError", duk_to_string(m_ctx, -1)); |
243 duk_pop(m_ctx); | 246 duk_pop(m_ctx); |
244 duk_get_global_string(m_ctx, "correct"); | 247 duk_get_global_string(m_ctx, "correct"); |
245 ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); | 248 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1)); |
246 duk_pop(m_ctx); | 249 duk_pop(m_ctx); |
247 } catch (const std::exception &ex) { | 250 } catch (const std::exception &ex) { |
248 FAIL() << ex.what(); | 251 BOOST_FAIL(ex.what()); |
249 } | 252 } |
250 } | 253 } |
251 | 254 |
252 TEST_F(TestSize, InvalidConstructorRange4) | 255 BOOST_AUTO_TEST_CASE(InvalidConstructorRange4) |
253 { | 256 { |
254 try { | 257 try { |
255 auto ret = duk_peval_string(m_ctx, | 258 auto ret = duk_peval_string(m_ctx, |
256 "try {" | 259 "try {" |
257 " Malikania.Size({ width: 100, height: -1 });" | 260 " Malikania.Size({ width: 100, height: -1 });" |
264 if (ret != 0) { | 267 if (ret != 0) { |
265 throw dukx_exception(m_ctx, -1); | 268 throw dukx_exception(m_ctx, -1); |
266 } | 269 } |
267 | 270 |
268 duk_get_global_string(m_ctx, "name"); | 271 duk_get_global_string(m_ctx, "name"); |
269 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); | 272 BOOST_REQUIRE_EQUAL("RangeError", duk_to_string(m_ctx, -1)); |
270 duk_pop(m_ctx); | 273 duk_pop(m_ctx); |
271 duk_get_global_string(m_ctx, "correct"); | 274 duk_get_global_string(m_ctx, "correct"); |
272 ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); | 275 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1)); |
273 duk_pop(m_ctx); | 276 duk_pop(m_ctx); |
274 } catch (const std::exception &ex) { | 277 } catch (const std::exception &ex) { |
275 FAIL() << ex.what(); | 278 BOOST_FAIL(ex.what()); |
276 } | 279 } |
277 } | 280 } |
278 | 281 |
279 /* | 282 /* |
280 * Require. | 283 * Require. |
281 * ------------------------------------------------------------------ | 284 * ------------------------------------------------------------------ |
282 */ | 285 */ |
283 | 286 |
284 TEST_F(TestSize, requireSuccess) | 287 BOOST_AUTO_TEST_CASE(requireSuccess) |
285 { | 288 { |
286 try { | 289 try { |
287 duk_push_c_function(m_ctx, [] (auto ctx) { | 290 duk_push_c_function(m_ctx, [] (auto ctx) { |
288 auto size = dukx_require_size(ctx, 0); | 291 auto size = dukx_require_size(ctx, 0); |
289 | 292 |
301 if (ret != 0) { | 304 if (ret != 0) { |
302 throw dukx_exception(m_ctx, -1); | 305 throw dukx_exception(m_ctx, -1); |
303 } | 306 } |
304 | 307 |
305 duk_get_global_string(m_ctx, "w"); | 308 duk_get_global_string(m_ctx, "w"); |
306 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1)); | 309 BOOST_REQUIRE_EQUAL(100U, duk_to_uint(m_ctx, -1)); |
307 duk_pop(m_ctx); | 310 duk_pop(m_ctx); |
308 duk_get_global_string(m_ctx, "h"); | 311 duk_get_global_string(m_ctx, "h"); |
309 ASSERT_EQ(200U, duk_to_uint(m_ctx, -1)); | 312 BOOST_REQUIRE_EQUAL(200U, duk_to_uint(m_ctx, -1)); |
310 duk_pop(m_ctx); | 313 duk_pop(m_ctx); |
311 } catch (const std::exception &ex) { | 314 } catch (const std::exception &ex) { |
312 FAIL() << ex.what(); | 315 BOOST_FAIL(ex.what()); |
313 } | 316 } |
314 } | 317 } |
315 | 318 |
316 TEST_F(TestSize, requireFail) | 319 BOOST_AUTO_TEST_CASE(requireFail) |
317 { | 320 { |
318 try { | 321 try { |
319 duk_push_c_function(m_ctx, [] (auto ctx) { | 322 duk_push_c_function(m_ctx, [] (auto ctx) { |
320 dukx_require_size(ctx, 0); | 323 dukx_require_size(ctx, 0); |
321 | 324 |
335 if (ret != 0) { | 338 if (ret != 0) { |
336 throw dukx_exception(m_ctx, -1); | 339 throw dukx_exception(m_ctx, -1); |
337 } | 340 } |
338 | 341 |
339 duk_get_global_string(m_ctx, "name"); | 342 duk_get_global_string(m_ctx, "name"); |
340 ASSERT_STREQ("Error", duk_to_string(m_ctx, -1)); | 343 BOOST_REQUIRE_EQUAL("Error", duk_to_string(m_ctx, -1)); |
341 duk_pop(m_ctx); | 344 duk_pop(m_ctx); |
342 duk_get_global_string(m_ctx, "correct"); | 345 duk_get_global_string(m_ctx, "correct"); |
343 ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); | 346 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1)); |
344 duk_pop(m_ctx); | 347 duk_pop(m_ctx); |
345 } catch (const std::exception &ex) { | 348 } catch (const std::exception &ex) { |
346 FAIL() << ex.what(); | 349 BOOST_FAIL(ex.what()); |
347 } | 350 } |
348 } | 351 } |
349 | 352 |
350 /* | 353 /* |
351 * Get. | 354 * Get. |
352 * ------------------------------------------------------------------ | 355 * ------------------------------------------------------------------ |
353 */ | 356 */ |
354 | 357 |
355 TEST_F(TestSize, getAdjustAll) | 358 BOOST_AUTO_TEST_CASE(getAdjustAll) |
356 { | 359 { |
357 try { | 360 try { |
358 duk_push_c_function(m_ctx, [] (auto ctx) { | 361 duk_push_c_function(m_ctx, [] (auto ctx) { |
359 auto size = dukx_get_size(ctx, 0); | 362 auto size = dukx_get_size(ctx, 0); |
360 | 363 |
372 if (ret != 0) { | 375 if (ret != 0) { |
373 throw dukx_exception(m_ctx, -1); | 376 throw dukx_exception(m_ctx, -1); |
374 } | 377 } |
375 | 378 |
376 duk_get_global_string(m_ctx, "w"); | 379 duk_get_global_string(m_ctx, "w"); |
377 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); | 380 BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); |
378 duk_pop(m_ctx); | 381 duk_pop(m_ctx); |
379 duk_get_global_string(m_ctx, "h"); | 382 duk_get_global_string(m_ctx, "h"); |
380 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); | 383 BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1)); |
381 duk_pop(m_ctx); | 384 duk_pop(m_ctx); |
382 } catch (const std::exception &ex) { | 385 } catch (const std::exception &ex) { |
383 FAIL() << ex.what(); | 386 BOOST_FAIL(ex.what()); |
384 } | 387 } |
385 } | 388 } |
386 | 389 |
387 int main(int argc, char **argv) | 390 BOOST_AUTO_TEST_SUITE_END() |
388 { | |
389 testing::InitGoogleTest(&argc, argv); | |
390 | |
391 return RUN_ALL_TESTS(); | |
392 } |