comparison tests/libclient/js-size/main.cpp @ 13:fb7c2c096101

Client: add JavaScript bindings for Size, #460
author David Demelier <markand@malikania.fr>
date Sat, 02 Apr 2016 16:05:44 +0200
parents
children 0a1adf7dcca0
comparison
equal deleted inserted replaced
12:0c62e0af6af7 13:fb7c2c096101
1 /*
2 * main.cpp -- test Size (JavaScript binding)
3 *
4 * Copyright (c) 2013-2016 Malikania Authors
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 #include <gtest/gtest.h>
20
21 #include <malikania/js-size.h>
22
23 using namespace malikania;
24
25 class TestSize : public testing::Test {
26 protected:
27 duk::Context m_ctx;
28
29 public:
30 TestSize()
31 {
32 duk::putGlobal(m_ctx, "Malikania", duk::Object());
33
34 loadMalikaniaSize(m_ctx);
35 }
36 };
37
38 /*
39 * Valid constructors
40 * ------------------------------------------------------------------
41 */
42
43 TEST_F(TestSize, ConstructorNoArgs)
44 {
45 try {
46 auto ret = duk::pevalString(m_ctx,
47 "s = Malikania.Size();"
48 "w = s.width;"
49 "h = s.height;"
50 );
51
52 if (ret != 0) {
53 throw duk::error(m_ctx, -1);
54 }
55
56 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "w"));
57 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "h"));
58 } catch (const std::exception &ex) {
59 FAIL() << ex.what();
60 }
61 }
62
63 TEST_F(TestSize, Constructor2Args)
64 {
65 try {
66 auto ret = duk::pevalString(m_ctx,
67 "s = Malikania.Size(100, 200);"
68 "w = s.width;"
69 "h = s.height;"
70 );
71
72 if (ret != 0) {
73 throw duk::error(m_ctx, -1);
74 }
75
76 ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w"));
77 ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h"));
78 } catch (const std::exception &ex) {
79 FAIL() << ex.what();
80 }
81 }
82
83 TEST_F(TestSize, ConstructorObject)
84 {
85 try {
86 auto ret = duk::pevalString(m_ctx,
87 "s = Malikania.Size({ width: 100, height: 200 });"
88 "w = s.width;"
89 "h = s.height;"
90 );
91
92 if (ret != 0) {
93 throw duk::error(m_ctx, -1);
94 }
95
96 ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w"));
97 ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h"));
98 } catch (const std::exception &ex) {
99 FAIL() << ex.what();
100 }
101 }
102
103 TEST_F(TestSize, ConstructorNew)
104 {
105 try {
106 auto ret = duk::pevalString(m_ctx,
107 "s = new Malikania.Size({ width: 100, height: 200 });"
108 "w = s.width;"
109 "h = s.height;"
110 );
111
112 if (ret != 0) {
113 throw duk::error(m_ctx, -1);
114 }
115
116 ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w"));
117 ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h"));
118 } catch (const std::exception &ex) {
119 FAIL() << ex.what();
120 }
121 }
122
123 /*
124 * Invalid constructors
125 * ------------------------------------------------------------------
126 */
127
128 TEST_F(TestSize, InvalidConstructorArg1)
129 {
130 try {
131 auto ret = duk::pevalString(m_ctx,
132 "try {"
133 " Malikania.Size(null);"
134 "} catch (e) {"
135 " name = e.name;"
136 " correct = (e instanceof TypeError);"
137 "}"
138 );
139
140 if (ret != 0) {
141 throw duk::error(m_ctx, -1);
142 }
143
144 ASSERT_EQ("TypeError", duk::getGlobal<std::string>(m_ctx, "name"));
145 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
146 } catch (const std::exception &ex) {
147 FAIL() << ex.what();
148 }
149 }
150
151 TEST_F(TestSize, InvalidConstructorRange1)
152 {
153 try {
154 auto ret = duk::pevalString(m_ctx,
155 "try {"
156 " Malikania.Size(-1, 200);"
157 "} catch (e) {"
158 " name = e.name;"
159 " correct = (e instanceof RangeError);"
160 "}"
161 );
162
163 if (ret != 0) {
164 throw duk::error(m_ctx, -1);
165 }
166
167 ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name"));
168 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
169 } catch (const std::exception &ex) {
170 FAIL() << ex.what();
171 }
172 }
173
174 TEST_F(TestSize, InvalidConstructorRange2)
175 {
176 try {
177 auto ret = duk::pevalString(m_ctx,
178 "try {"
179 " Malikania.Size(100, -1);"
180 "} catch (e) {"
181 " name = e.name;"
182 " correct = (e instanceof RangeError);"
183 "}"
184 );
185
186 if (ret != 0) {
187 throw duk::error(m_ctx, -1);
188 }
189
190 ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name"));
191 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
192 } catch (const std::exception &ex) {
193 FAIL() << ex.what();
194 }
195 }
196
197 TEST_F(TestSize, InvalidConstructorRange3)
198 {
199 try {
200 auto ret = duk::pevalString(m_ctx,
201 "try {"
202 " Malikania.Size({ width: -1, height: 200 });"
203 "} catch (e) {"
204 " name = e.name;"
205 " correct = (e instanceof RangeError);"
206 "}"
207 );
208
209 if (ret != 0) {
210 throw duk::error(m_ctx, -1);
211 }
212
213 ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name"));
214 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
215 } catch (const std::exception &ex) {
216 FAIL() << ex.what();
217 }
218 }
219
220 TEST_F(TestSize, InvalidConstructorRange4)
221 {
222 try {
223 auto ret = duk::pevalString(m_ctx,
224 "try {"
225 " Malikania.Size({ width: 100, height: -1 });"
226 "} catch (e) {"
227 " name = e.name;"
228 " correct = (e instanceof RangeError);"
229 "}"
230 );
231
232 if (ret != 0) {
233 throw duk::error(m_ctx, -1);
234 }
235
236 ASSERT_EQ("RangeError", duk::getGlobal<std::string>(m_ctx, "name"));
237 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
238 } catch (const std::exception &ex) {
239 FAIL() << ex.what();
240 }
241 }
242
243 /*
244 * Require.
245 * ------------------------------------------------------------------
246 */
247
248 TEST_F(TestSize, requireSuccess)
249 {
250 try {
251 duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
252 Size size = duk::require<Size>(ctx, 0);
253
254 duk::putGlobal(ctx, "w", static_cast<int>(size.width()));
255 duk::putGlobal(ctx, "h", static_cast<int>(size.height()));
256
257 return 0;
258 }, 1});
259
260 auto ret = duk::pevalString(m_ctx, "build({ width: 100, height: 200 });");
261
262 if (ret != 0) {
263 throw duk::error(m_ctx, -1);
264 }
265
266 ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "w"));
267 ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "h"));
268 } catch (const std::exception &ex) {
269 FAIL() << ex.what();
270 }
271 }
272
273 TEST_F(TestSize, requireFail)
274 {
275 try {
276 duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
277 duk::require<Size>(ctx, 0);
278
279 return 0;
280 }, 1});
281
282 auto ret = duk::pevalString(m_ctx,
283 "try {"
284 " build({});"
285 "} catch (e) {"
286 " name = e.name;"
287 " correct = (e instanceof Error);"
288 "}"
289 );
290
291 if (ret != 0) {
292 throw duk::error(m_ctx, -1);
293 }
294
295 ASSERT_EQ("Error", duk::getGlobal<std::string>(m_ctx, "name"));
296 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct"));
297 } catch (const std::exception &ex) {
298 FAIL() << ex.what();
299 }
300 }
301
302 /*
303 * Get.
304 * ------------------------------------------------------------------
305 */
306
307 TEST_F(TestSize, getAdjustAll)
308 {
309 try {
310 duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
311 Size size = duk::get<Size>(ctx, 0);
312
313 duk::putGlobal(ctx, "w", static_cast<int>(size.width()));
314 duk::putGlobal(ctx, "h", static_cast<int>(size.height()));
315
316 return 0;
317 }, 1});
318
319 auto ret = duk::pevalString(m_ctx, "build({});");
320
321 if (ret != 0) {
322 throw duk::error(m_ctx, -1);
323 }
324
325 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "r"));
326 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "g"));
327 } catch (const std::exception &ex) {
328 FAIL() << ex.what();
329 }
330 }
331
332 int main(int argc, char **argv)
333 {
334 testing::InitGoogleTest(&argc, argv);
335
336 return RUN_ALL_TESTS();
337 }