comparison tests/libmlk/js-point/main.cpp @ 193:209bdaa13a49

Tests: rename directories to match targets
author David Demelier <markand@malikania.fr>
date Sat, 27 Oct 2018 07:16:28 +0200
parents tests/libcommon/js-point/main.cpp@f28cb6d04731
children
comparison
equal deleted inserted replaced
192:74afc5a41c83 193:209bdaa13a49
1 /*
2 * main.cpp -- test Point (JavaScript binding)
3 *
4 * Copyright (c) 2013-2018 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
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
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #define BOOST_TEST_MODULE "Javascript Point"
20 #include <boost/test/unit_test.hpp>
21
22 #include <malikania/point.hpp>
23
24 #include <malikania/js/test/js_api_fixture.hpp>
25
26 namespace mlk {
27
28 namespace {
29
30 BOOST_FIXTURE_TEST_SUITE(test_point_suite, js::test::js_api_fixture)
31
32 /*
33 * Valid constructors.
34 * ------------------------------------------------------------------
35 */
36
37 BOOST_AUTO_TEST_SUITE(constructors)
38
39 BOOST_AUTO_TEST_CASE(constructor_default)
40 {
41 const auto ret = duk_peval_string(ctx_,
42 "p = Malikania.Point();"
43 "x = p.x;"
44 "y = p.y;"
45 );
46
47 if (ret != 0)
48 throw mlk::js::duk::get_stack(ctx_, -1);
49
50 duk_get_global_string(ctx_, "x");
51 BOOST_TEST(duk_to_int(ctx_, -1) == 0);
52 duk_pop(ctx_);
53 duk_get_global_string(ctx_, "y");
54 BOOST_TEST(duk_to_int(ctx_, -1) == 0);
55 duk_pop(ctx_);
56 }
57
58 BOOST_AUTO_TEST_CASE(constructor_2_args)
59 {
60 const auto ret = duk_peval_string(ctx_,
61 "p = Malikania.Point(-10, -20);"
62 "x = p.x;"
63 "y = p.y;"
64 );
65
66 if (ret != 0)
67 throw mlk::js::duk::get_stack(ctx_, -1);
68
69 duk_get_global_string(ctx_, "x");
70 BOOST_TEST(duk_to_int(ctx_, -1) == -10);
71 duk_pop(ctx_);
72 duk_get_global_string(ctx_, "y");
73 BOOST_TEST(duk_to_int(ctx_, -1) == -20);
74 duk_pop(ctx_);
75 }
76
77 BOOST_AUTO_TEST_CASE(constructor_object)
78 {
79 const auto ret = duk_peval_string(ctx_,
80 "p = Malikania.Point({ x: 100, y: 200 });"
81 "x = p.x;"
82 "y = p.y;"
83 );
84
85 if (ret != 0)
86 throw mlk::js::duk::get_stack(ctx_, -1);
87
88 duk_get_global_string(ctx_, "x");
89 BOOST_TEST(duk_to_int(ctx_, -1) == 100);
90 duk_pop(ctx_);
91 duk_get_global_string(ctx_, "y");
92 BOOST_TEST(duk_to_int(ctx_, -1) == 200);
93 duk_pop(ctx_);
94 }
95
96 BOOST_AUTO_TEST_CASE(constructor_new)
97 {
98 const auto ret = duk_peval_string(ctx_,
99 "p = new Malikania.Point({ x: 100, y: 200 });"
100 "x = p.x;"
101 "y = p.y;"
102 );
103
104 if (ret != 0)
105 throw mlk::js::duk::get_stack(ctx_, -1);
106
107 duk_get_global_string(ctx_, "x");
108 BOOST_TEST(duk_to_int(ctx_, -1) == 100);
109 duk_pop(ctx_);
110 duk_get_global_string(ctx_, "y");
111 BOOST_TEST(duk_to_int(ctx_, -1) == 200);
112 duk_pop(ctx_);
113 }
114
115 BOOST_AUTO_TEST_SUITE_END()
116
117 /*
118 * Invalid constructors.
119 * ------------------------------------------------------------------
120 */
121
122 BOOST_AUTO_TEST_SUITE(invalid_constructors)
123
124 BOOST_AUTO_TEST_CASE(constructor_arg_1)
125 {
126 const auto ret = duk_peval_string(ctx_,
127 "try {"
128 " Malikania.Point(null);"
129 "} catch (e) {"
130 " name = e.name;"
131 " correct = (e instanceof TypeError);"
132 "}"
133 );
134
135 if (ret != 0)
136 throw mlk::js::duk::get_stack(ctx_, -1);
137
138 duk_get_global_string(ctx_, "name");
139 BOOST_TEST(duk_to_string(ctx_, -1) == "TypeError");
140 duk_pop(ctx_);
141 duk_get_global_string(ctx_, "correct");
142 BOOST_TEST(duk_to_boolean(ctx_, -1));
143 duk_pop(ctx_);
144 }
145
146 BOOST_AUTO_TEST_SUITE_END()
147
148 /*
149 * Require.
150 * ------------------------------------------------------------------
151 */
152
153 BOOST_AUTO_TEST_SUITE(require)
154
155 BOOST_AUTO_TEST_CASE(success)
156 {
157 duk_push_c_function(ctx_, [] (auto ctx) {
158 const auto point = mlk::js::duk::require<mlk::point>(ctx, 0);
159
160 duk_push_int(ctx, point.x);
161 duk_put_global_string(ctx, "x");
162 duk_push_int(ctx, point.y);
163 duk_put_global_string(ctx, "y");
164
165 return 0;
166 }, 1);
167 duk_put_global_string(ctx_, "build");
168
169 const auto ret = duk_peval_string(ctx_, "build({ x: 100, y: 200 });");
170
171 if (ret != 0)
172 throw mlk::js::duk::get_stack(ctx_, -1);
173
174 duk_get_global_string(ctx_, "x");
175 BOOST_TEST(duk_to_int(ctx_, -1) == 100);
176 duk_pop(ctx_);
177 duk_get_global_string(ctx_, "y");
178 BOOST_TEST(duk_to_int(ctx_, -1) == 200);
179 duk_pop(ctx_);
180 }
181
182 BOOST_AUTO_TEST_CASE(fail)
183 {
184 duk_push_c_function(ctx_, [] (auto ctx) {
185 mlk::js::duk::require<mlk::point>(ctx, 0);
186
187 return 0;
188 }, 1);
189 duk_put_global_string(ctx_, "build");
190
191 const auto ret = duk_peval_string(ctx_,
192 "try {"
193 " build({});"
194 "} catch (e) {"
195 " name = e.name;"
196 " correct = (e instanceof TypeError);"
197 "}"
198 );
199
200 if (ret != 0)
201 throw mlk::js::duk::get_stack(ctx_, -1);
202
203 duk_get_global_string(ctx_, "name");
204 BOOST_TEST(duk_to_string(ctx_, -1) == "TypeError");
205 duk_pop(ctx_);
206 duk_get_global_string(ctx_, "correct");
207 BOOST_TEST(duk_to_boolean(ctx_, -1));
208 duk_pop(ctx_);
209 }
210
211 BOOST_AUTO_TEST_SUITE_END()
212
213 /*
214 * Get.
215 * ------------------------------------------------------------------
216 */
217
218 BOOST_AUTO_TEST_SUITE(get)
219
220 BOOST_AUTO_TEST_CASE(adjust_all)
221 {
222 duk_push_c_function(ctx_, [] (auto ctx) {
223 const auto point = mlk::js::duk::get<mlk::point>(ctx, 0);
224
225 duk_push_int(ctx, point.x);
226 duk_put_global_string(ctx, "x");
227 duk_push_int(ctx, point.y);
228 duk_put_global_string(ctx, "y");
229
230 return 0;
231 }, 1);
232 duk_put_global_string(ctx_, "build");
233
234 const auto ret = duk_peval_string(ctx_, "build({});");
235
236 if (ret != 0)
237 throw mlk::js::duk::get_stack(ctx_, -1);
238
239 duk_get_global_string(ctx_, "x");
240 BOOST_TEST(duk_to_int(ctx_, -1) == 0);
241 duk_pop(ctx_);
242 duk_get_global_string(ctx_, "y");
243 BOOST_TEST(duk_to_int(ctx_, -1) == 0);
244 duk_pop(ctx_);
245 }
246
247 BOOST_AUTO_TEST_SUITE_END()
248
249 BOOST_AUTO_TEST_SUITE_END()
250
251 } // !namespace
252
253 } // !mlk