comparison tests/libclient/js-line/main.cpp @ 17:63ba461b7f84

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