comparison tests/libclient/js-rectangle/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 Rectangle"
20 #include <boost/test/unit_test.hpp>
20 21
21 #include <malikania/js_rectangle.hpp> 22 #include <malikania/js_rectangle.hpp>
22 23
23 class TestRectangle : public testing::Test { 24 class test_rectangle {
24 protected: 25 protected:
25 UniqueContext m_ctx; 26 UniqueContext m_ctx;
26 27
27 public: 28 public:
28 TestRectangle() 29 test_rectangle()
29 { 30 {
30 duk_push_object(m_ctx); 31 duk_push_object(m_ctx);
31 duk_put_global_string(m_ctx, "Malikania"); 32 duk_put_global_string(m_ctx, "Malikania");
32 mlk::dukx_load_rect(m_ctx); 33 mlk::dukx_load_rect(m_ctx);
33 } 34 }
34 }; 35 };
35 36
37 BOOST_FIXTURE_TEST_SUITE(test_rectangle_suite, test_rectangle)
38
36 /* 39 /*
37 * Valid constructors 40 * Valid constructors
38 * ------------------------------------------------------------------ 41 * ------------------------------------------------------------------
39 */ 42 */
40 43
41 TEST_F(TestRectangle, ConstructorNoArgs) 44 BOOST_AUTO_TEST_CASE(ConstructorNoArgs)
42 { 45 {
43 try { 46 try {
44 auto ret = duk_peval_string(m_ctx, 47 auto ret = duk_peval_string(m_ctx,
45 "r = Malikania.Rectangle();" 48 "r = Malikania.Rectangle();"
46 "x = r.x;" 49 "x = r.x;"
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, "x"); 59 duk_get_global_string(m_ctx, "x");
57 ASSERT_EQ(0, duk_to_int(m_ctx, -1)); 60 BOOST_REQUIRE_EQUAL(0, duk_to_int(m_ctx, -1));
58 duk_pop(m_ctx); 61 duk_pop(m_ctx);
59 duk_get_global_string(m_ctx, "y"); 62 duk_get_global_string(m_ctx, "y");
60 ASSERT_EQ(0, duk_to_int(m_ctx, -1)); 63 BOOST_REQUIRE_EQUAL(0, duk_to_int(m_ctx, -1));
61 duk_pop(m_ctx); 64 duk_pop(m_ctx);
62 duk_get_global_string(m_ctx, "w"); 65 duk_get_global_string(m_ctx, "w");
63 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); 66 BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1));
64 duk_pop(m_ctx); 67 duk_pop(m_ctx);
65 duk_get_global_string(m_ctx, "h"); 68 duk_get_global_string(m_ctx, "h");
66 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); 69 BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1));
67 duk_pop(m_ctx); 70 duk_pop(m_ctx);
68 } catch (const std::exception &ex) { 71 } catch (const std::exception &ex) {
69 FAIL() << ex.what(); 72 BOOST_FAIL(ex.what());
70 } 73 }
71 } 74 }
72 75
73 TEST_F(TestRectangle, Constructor4Args) 76 BOOST_AUTO_TEST_CASE(Constructor4Args)
74 { 77 {
75 try { 78 try {
76 auto ret = duk_peval_string(m_ctx, 79 auto ret = duk_peval_string(m_ctx,
77 "r = Malikania.Rectangle(10, 20, 30, 40);" 80 "r = Malikania.Rectangle(10, 20, 30, 40);"
78 "x = r.x;" 81 "x = r.x;"
84 if (ret != 0) { 87 if (ret != 0) {
85 throw dukx_exception(m_ctx, -1); 88 throw dukx_exception(m_ctx, -1);
86 } 89 }
87 90
88 duk_get_global_string(m_ctx, "x"); 91 duk_get_global_string(m_ctx, "x");
89 ASSERT_EQ(10, duk_to_int(m_ctx, -1)); 92 BOOST_REQUIRE_EQUAL(10, duk_to_int(m_ctx, -1));
90 duk_pop(m_ctx); 93 duk_pop(m_ctx);
91 duk_get_global_string(m_ctx, "y"); 94 duk_get_global_string(m_ctx, "y");
92 ASSERT_EQ(20, duk_to_int(m_ctx, -1)); 95 BOOST_REQUIRE_EQUAL(20, duk_to_int(m_ctx, -1));
93 duk_pop(m_ctx); 96 duk_pop(m_ctx);
94 duk_get_global_string(m_ctx, "w"); 97 duk_get_global_string(m_ctx, "w");
95 ASSERT_EQ(30U, duk_to_uint(m_ctx, -1)); 98 BOOST_REQUIRE_EQUAL(30U, duk_to_uint(m_ctx, -1));
96 duk_pop(m_ctx); 99 duk_pop(m_ctx);
97 duk_get_global_string(m_ctx, "h"); 100 duk_get_global_string(m_ctx, "h");
98 ASSERT_EQ(40U, duk_to_uint(m_ctx, -1)); 101 BOOST_REQUIRE_EQUAL(40U, duk_to_uint(m_ctx, -1));
99 duk_pop(m_ctx); 102 duk_pop(m_ctx);
100 } catch (const std::exception &ex) { 103 } catch (const std::exception &ex) {
101 FAIL() << ex.what(); 104 BOOST_FAIL(ex.what());
102 } 105 }
103 } 106 }
104 107
105 TEST_F(TestRectangle, ConstructorObject) 108 BOOST_AUTO_TEST_CASE(ConstructorObject)
106 { 109 {
107 try { 110 try {
108 auto ret = duk_peval_string(m_ctx, 111 auto ret = duk_peval_string(m_ctx,
109 "r = Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });" 112 "r = Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });"
110 "x = r.x;" 113 "x = r.x;"
116 if (ret != 0) { 119 if (ret != 0) {
117 throw dukx_exception(m_ctx, -1); 120 throw dukx_exception(m_ctx, -1);
118 } 121 }
119 122
120 duk_get_global_string(m_ctx, "x"); 123 duk_get_global_string(m_ctx, "x");
121 ASSERT_EQ(10, duk_to_int(m_ctx, -1)); 124 BOOST_REQUIRE_EQUAL(10, duk_to_int(m_ctx, -1));
122 duk_pop(m_ctx); 125 duk_pop(m_ctx);
123 duk_get_global_string(m_ctx, "y"); 126 duk_get_global_string(m_ctx, "y");
124 ASSERT_EQ(20, duk_to_int(m_ctx, -1)); 127 BOOST_REQUIRE_EQUAL(20, duk_to_int(m_ctx, -1));
125 duk_pop(m_ctx); 128 duk_pop(m_ctx);
126 duk_get_global_string(m_ctx, "w"); 129 duk_get_global_string(m_ctx, "w");
127 ASSERT_EQ(30U, duk_to_uint(m_ctx, -1)); 130 BOOST_REQUIRE_EQUAL(30U, duk_to_uint(m_ctx, -1));
128 duk_pop(m_ctx); 131 duk_pop(m_ctx);
129 duk_get_global_string(m_ctx, "h"); 132 duk_get_global_string(m_ctx, "h");
130 ASSERT_EQ(40U, duk_to_uint(m_ctx, -1)); 133 BOOST_REQUIRE_EQUAL(40U, duk_to_uint(m_ctx, -1));
131 duk_pop(m_ctx); 134 duk_pop(m_ctx);
132 } catch (const std::exception &ex) { 135 } catch (const std::exception &ex) {
133 FAIL() << ex.what(); 136 BOOST_FAIL(ex.what());
134 } 137 }
135 } 138 }
136 139
137 TEST_F(TestRectangle, ConstructorNew) 140 BOOST_AUTO_TEST_CASE(ConstructorNew)
138 { 141 {
139 try { 142 try {
140 auto ret = duk_peval_string(m_ctx, 143 auto ret = duk_peval_string(m_ctx,
141 "r = new Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });" 144 "r = new Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });"
142 "x = r.x;" 145 "x = r.x;"
148 if (ret != 0) { 151 if (ret != 0) {
149 throw dukx_exception(m_ctx, -1); 152 throw dukx_exception(m_ctx, -1);
150 } 153 }
151 154
152 duk_get_global_string(m_ctx, "x"); 155 duk_get_global_string(m_ctx, "x");
153 ASSERT_EQ(10, duk_to_int(m_ctx, -1)); 156 BOOST_REQUIRE_EQUAL(10, duk_to_int(m_ctx, -1));
154 duk_pop(m_ctx); 157 duk_pop(m_ctx);
155 duk_get_global_string(m_ctx, "y"); 158 duk_get_global_string(m_ctx, "y");
156 ASSERT_EQ(20, duk_to_int(m_ctx, -1)); 159 BOOST_REQUIRE_EQUAL(20, duk_to_int(m_ctx, -1));
157 duk_pop(m_ctx); 160 duk_pop(m_ctx);
158 duk_get_global_string(m_ctx, "w"); 161 duk_get_global_string(m_ctx, "w");
159 ASSERT_EQ(30U, duk_to_uint(m_ctx, -1)); 162 BOOST_REQUIRE_EQUAL(30U, duk_to_uint(m_ctx, -1));
160 duk_pop(m_ctx); 163 duk_pop(m_ctx);
161 duk_get_global_string(m_ctx, "h"); 164 duk_get_global_string(m_ctx, "h");
162 ASSERT_EQ(40U, duk_to_uint(m_ctx, -1)); 165 BOOST_REQUIRE_EQUAL(40U, duk_to_uint(m_ctx, -1));
163 duk_pop(m_ctx); 166 duk_pop(m_ctx);
164 } catch (const std::exception &ex) { 167 } catch (const std::exception &ex) {
165 FAIL() << ex.what(); 168 BOOST_FAIL(ex.what());
166 } 169 }
167 } 170 }
168 171
169 /* 172 /*
170 * Invalid constructors 173 * Invalid constructors
171 * ------------------------------------------------------------------ 174 * ------------------------------------------------------------------
172 */ 175 */
173 176
174 TEST_F(TestRectangle, InvalidConstructorArg1) 177 BOOST_AUTO_TEST_CASE(InvalidConstructorArg1)
175 { 178 {
176 try { 179 try {
177 auto ret = duk_peval_string(m_ctx, 180 auto ret = duk_peval_string(m_ctx,
178 "try {" 181 "try {"
179 " Malikania.Rectangle(null);" 182 " Malikania.Rectangle(null);"
186 if (ret != 0) { 189 if (ret != 0) {
187 throw dukx_exception(m_ctx, -1); 190 throw dukx_exception(m_ctx, -1);
188 } 191 }
189 192
190 duk_get_global_string(m_ctx, "name"); 193 duk_get_global_string(m_ctx, "name");
191 ASSERT_STREQ("TypeError", duk_to_string(m_ctx, -1)); 194 BOOST_REQUIRE_EQUAL("TypeError", duk_to_string(m_ctx, -1));
192 duk_pop(m_ctx); 195 duk_pop(m_ctx);
193 duk_get_global_string(m_ctx, "correct"); 196 duk_get_global_string(m_ctx, "correct");
194 ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); 197 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1));
195 duk_pop(m_ctx); 198 duk_pop(m_ctx);
196 } catch (const std::exception &ex) { 199 } catch (const std::exception &ex) {
197 FAIL() << ex.what(); 200 BOOST_FAIL(ex.what());
198 } 201 }
199 } 202 }
200 203
201 TEST_F(TestRectangle, InvalidConstructorRange1) 204 BOOST_AUTO_TEST_CASE(InvalidConstructorRange1)
202 { 205 {
203 try { 206 try {
204 auto ret = duk_peval_string(m_ctx, 207 auto ret = duk_peval_string(m_ctx,
205 "try {" 208 "try {"
206 " Malikania.Rectangle(0, 0, -10, -10);" 209 " Malikania.Rectangle(0, 0, -10, -10);"
213 if (ret != 0) { 216 if (ret != 0) {
214 throw dukx_exception(m_ctx, -1); 217 throw dukx_exception(m_ctx, -1);
215 } 218 }
216 219
217 duk_get_global_string(m_ctx, "name"); 220 duk_get_global_string(m_ctx, "name");
218 ASSERT_STREQ("RangeError", duk_to_string(m_ctx, -1)); 221 BOOST_REQUIRE_EQUAL("RangeError", duk_to_string(m_ctx, -1));
219 duk_pop(m_ctx); 222 duk_pop(m_ctx);
220 duk_get_global_string(m_ctx, "correct"); 223 duk_get_global_string(m_ctx, "correct");
221 ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); 224 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1));
222 duk_pop(m_ctx); 225 duk_pop(m_ctx);
223 } catch (const std::exception &ex) { 226 } catch (const std::exception &ex) {
224 FAIL() << ex.what(); 227 BOOST_FAIL(ex.what());
225 } 228 }
226 } 229 }
227 230
228 /* 231 /*
229 * Require. 232 * Require.
230 * ------------------------------------------------------------------ 233 * ------------------------------------------------------------------
231 */ 234 */
232 235
233 TEST_F(TestRectangle, requireSuccess) 236 BOOST_AUTO_TEST_CASE(requireSuccess)
234 { 237 {
235 try { 238 try {
236 duk_push_c_function(m_ctx, [] (auto ctx) { 239 duk_push_c_function(m_ctx, [] (auto ctx) {
237 auto rect = mlk::dukx_require_rect(ctx, 0); 240 auto rect = mlk::dukx_require_rect(ctx, 0);
238 241
254 if (ret != 0) { 257 if (ret != 0) {
255 throw dukx_exception(m_ctx, -1); 258 throw dukx_exception(m_ctx, -1);
256 } 259 }
257 260
258 duk_get_global_string(m_ctx, "x"); 261 duk_get_global_string(m_ctx, "x");
259 ASSERT_EQ(50, duk_to_int(m_ctx, -1)); 262 BOOST_REQUIRE_EQUAL(50, duk_to_int(m_ctx, -1));
260 duk_pop(m_ctx); 263 duk_pop(m_ctx);
261 duk_get_global_string(m_ctx, "y"); 264 duk_get_global_string(m_ctx, "y");
262 ASSERT_EQ(80, duk_to_int(m_ctx, -1)); 265 BOOST_REQUIRE_EQUAL(80, duk_to_int(m_ctx, -1));
263 duk_pop(m_ctx); 266 duk_pop(m_ctx);
264 duk_get_global_string(m_ctx, "w"); 267 duk_get_global_string(m_ctx, "w");
265 ASSERT_EQ(100U, duk_to_uint(m_ctx, -1)); 268 BOOST_REQUIRE_EQUAL(100U, duk_to_uint(m_ctx, -1));
266 duk_pop(m_ctx); 269 duk_pop(m_ctx);
267 duk_get_global_string(m_ctx, "h"); 270 duk_get_global_string(m_ctx, "h");
268 ASSERT_EQ(200U, duk_to_uint(m_ctx, -1)); 271 BOOST_REQUIRE_EQUAL(200U, duk_to_uint(m_ctx, -1));
269 duk_pop(m_ctx); 272 duk_pop(m_ctx);
270 } catch (const std::exception &ex) { 273 } catch (const std::exception &ex) {
271 FAIL() << ex.what(); 274 BOOST_FAIL(ex.what());
272 } 275 }
273 } 276 }
274 277
275 TEST_F(TestRectangle, requireFail) 278 BOOST_AUTO_TEST_CASE(requireFail)
276 { 279 {
277 try { 280 try {
278 duk_push_c_function(m_ctx, [] (auto ctx) { 281 duk_push_c_function(m_ctx, [] (auto ctx) {
279 mlk::dukx_require_rect(ctx, 0); 282 mlk::dukx_require_rect(ctx, 0);
280 283
294 if (ret != 0) { 297 if (ret != 0) {
295 throw dukx_exception(m_ctx, -1); 298 throw dukx_exception(m_ctx, -1);
296 } 299 }
297 300
298 duk_get_global_string(m_ctx, "name"); 301 duk_get_global_string(m_ctx, "name");
299 ASSERT_STREQ("Error", duk_to_string(m_ctx, -1)); 302 BOOST_REQUIRE_EQUAL("Error", duk_to_string(m_ctx, -1));
300 duk_pop(m_ctx); 303 duk_pop(m_ctx);
301 duk_get_global_string(m_ctx, "correct"); 304 duk_get_global_string(m_ctx, "correct");
302 ASSERT_TRUE(duk_to_boolean(m_ctx, -1)); 305 BOOST_REQUIRE(duk_to_boolean(m_ctx, -1));
303 duk_pop(m_ctx); 306 duk_pop(m_ctx);
304 } catch (const std::exception &ex) { 307 } catch (const std::exception &ex) {
305 FAIL() << ex.what(); 308 BOOST_FAIL(ex.what());
306 } 309 }
307 } 310 }
308 311
309 /* 312 /*
310 * Get. 313 * Get.
311 * ------------------------------------------------------------------ 314 * ------------------------------------------------------------------
312 */ 315 */
313 316
314 TEST_F(TestRectangle, getAdjustAll) 317 BOOST_AUTO_TEST_CASE(getAdjustAll)
315 { 318 {
316 try { 319 try {
317 duk_push_c_function(m_ctx, [] (auto ctx) { 320 duk_push_c_function(m_ctx, [] (auto ctx) {
318 auto rect = mlk::dukx_get_rect(ctx, 0); 321 auto rect = mlk::dukx_get_rect(ctx, 0);
319 322
334 if (ret != 0) { 337 if (ret != 0) {
335 throw dukx_exception(m_ctx, -1); 338 throw dukx_exception(m_ctx, -1);
336 } 339 }
337 340
338 duk_get_global_string(m_ctx, "x"); 341 duk_get_global_string(m_ctx, "x");
339 ASSERT_EQ(0, duk_to_int(m_ctx, -1)); 342 BOOST_REQUIRE_EQUAL(0, duk_to_int(m_ctx, -1));
340 duk_pop(m_ctx); 343 duk_pop(m_ctx);
341 duk_get_global_string(m_ctx, "y"); 344 duk_get_global_string(m_ctx, "y");
342 ASSERT_EQ(0, duk_to_int(m_ctx, -1)); 345 BOOST_REQUIRE_EQUAL(0, duk_to_int(m_ctx, -1));
343 duk_pop(m_ctx); 346 duk_pop(m_ctx);
344 duk_get_global_string(m_ctx, "w"); 347 duk_get_global_string(m_ctx, "w");
345 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); 348 BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1));
346 duk_pop(m_ctx); 349 duk_pop(m_ctx);
347 duk_get_global_string(m_ctx, "h"); 350 duk_get_global_string(m_ctx, "h");
348 ASSERT_EQ(0U, duk_to_uint(m_ctx, -1)); 351 BOOST_REQUIRE_EQUAL(0U, duk_to_uint(m_ctx, -1));
349 duk_pop(m_ctx); 352 duk_pop(m_ctx);
350 } catch (const std::exception &ex) { 353 } catch (const std::exception &ex) {
351 FAIL() << ex.what(); 354 BOOST_FAIL(ex.what());
352 } 355 }
353 } 356 }
354 357
355 int main(int argc, char **argv) 358 BOOST_AUTO_TEST_SUITE_END()
356 {
357 testing::InitGoogleTest(&argc, argv);
358
359 return RUN_ALL_TESTS();
360 }