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