Mercurial > malikania
comparison tests/libclient/js-point/main.cpp @ 15:3b9ea4072263
Client: add JavaScript bindings for Point, #458
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 03 Apr 2016 11:42:03 +0200 |
parents | |
children | 0a1adf7dcca0 |
comparison
equal
deleted
inserted
replaced
14:26efd2928f01 | 15:3b9ea4072263 |
---|---|
1 /* | |
2 * main.cpp -- test Point (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-point.h> | |
22 | |
23 using namespace malikania; | |
24 | |
25 class TestPoint : public testing::Test { | |
26 protected: | |
27 duk::Context m_ctx; | |
28 | |
29 public: | |
30 TestPoint() | |
31 { | |
32 duk::putGlobal(m_ctx, "Malikania", duk::Object()); | |
33 | |
34 loadMalikaniaPoint(m_ctx); | |
35 } | |
36 }; | |
37 | |
38 /* | |
39 * Valid constructors | |
40 * ------------------------------------------------------------------ | |
41 */ | |
42 | |
43 TEST_F(TestPoint, ConstructorNoArgs) | |
44 { | |
45 try { | |
46 auto ret = duk::pevalString(m_ctx, | |
47 "p = Malikania.Point();" | |
48 "x = p.x;" | |
49 "y = p.y;" | |
50 ); | |
51 | |
52 if (ret != 0) { | |
53 throw duk::error(m_ctx, -1); | |
54 } | |
55 | |
56 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x")); | |
57 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y")); | |
58 } catch (const std::exception &ex) { | |
59 FAIL() << ex.what(); | |
60 } | |
61 } | |
62 | |
63 TEST_F(TestPoint, Constructor2Args) | |
64 { | |
65 try { | |
66 auto ret = duk::pevalString(m_ctx, | |
67 "p = Malikania.Point(-10, -20);" | |
68 "x = p.x;" | |
69 "y = p.y;" | |
70 ); | |
71 | |
72 if (ret != 0) { | |
73 throw duk::error(m_ctx, -1); | |
74 } | |
75 | |
76 ASSERT_EQ(-10, duk::getGlobal<int>(m_ctx, "x")); | |
77 ASSERT_EQ(-20, duk::getGlobal<int>(m_ctx, "y")); | |
78 } catch (const std::exception &ex) { | |
79 FAIL() << ex.what(); | |
80 } | |
81 } | |
82 | |
83 TEST_F(TestPoint, ConstructorObject) | |
84 { | |
85 try { | |
86 auto ret = duk::pevalString(m_ctx, | |
87 "p = Malikania.Point({ x: 100, y: 200 });" | |
88 "x = p.x;" | |
89 "y = p.y;" | |
90 ); | |
91 | |
92 if (ret != 0) { | |
93 throw duk::error(m_ctx, -1); | |
94 } | |
95 | |
96 ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "x")); | |
97 ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "y")); | |
98 } catch (const std::exception &ex) { | |
99 FAIL() << ex.what(); | |
100 } | |
101 } | |
102 | |
103 TEST_F(TestPoint, ConstructorNew) | |
104 { | |
105 try { | |
106 auto ret = duk::pevalString(m_ctx, | |
107 "p = new Malikania.Point({ x: 100, y: 200 });" | |
108 "x = p.x;" | |
109 "y = p.y;" | |
110 ); | |
111 | |
112 if (ret != 0) { | |
113 throw duk::error(m_ctx, -1); | |
114 } | |
115 | |
116 ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "x")); | |
117 ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "y")); | |
118 } catch (const std::exception &ex) { | |
119 FAIL() << ex.what(); | |
120 } | |
121 } | |
122 | |
123 /* | |
124 * Invalid constructors | |
125 * ------------------------------------------------------------------ | |
126 */ | |
127 | |
128 TEST_F(TestPoint, InvalidConstructorArg1) | |
129 { | |
130 try { | |
131 auto ret = duk::pevalString(m_ctx, | |
132 "try {" | |
133 " Malikania.Point(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 /* | |
152 * Require. | |
153 * ------------------------------------------------------------------ | |
154 */ | |
155 | |
156 TEST_F(TestPoint, requireSuccess) | |
157 { | |
158 try { | |
159 duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { | |
160 Point point = duk::require<Point>(ctx, 0); | |
161 | |
162 duk::putGlobal(ctx, "x", point.x()); | |
163 duk::putGlobal(ctx, "y", point.y()); | |
164 | |
165 return 0; | |
166 }, 1}); | |
167 | |
168 auto ret = duk::pevalString(m_ctx, "build({ x: 100, y: 200 });"); | |
169 | |
170 if (ret != 0) { | |
171 throw duk::error(m_ctx, -1); | |
172 } | |
173 | |
174 ASSERT_EQ(100, duk::getGlobal<int>(m_ctx, "x")); | |
175 ASSERT_EQ(200, duk::getGlobal<int>(m_ctx, "y")); | |
176 } catch (const std::exception &ex) { | |
177 FAIL() << ex.what(); | |
178 } | |
179 } | |
180 | |
181 TEST_F(TestPoint, requireFail) | |
182 { | |
183 try { | |
184 duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { | |
185 duk::require<Point>(ctx, 0); | |
186 | |
187 return 0; | |
188 }, 1}); | |
189 | |
190 auto ret = duk::pevalString(m_ctx, | |
191 "try {" | |
192 " build({});" | |
193 "} catch (e) {" | |
194 " name = e.name;" | |
195 " correct = (e instanceof Error);" | |
196 "}" | |
197 ); | |
198 | |
199 if (ret != 0) { | |
200 throw duk::error(m_ctx, -1); | |
201 } | |
202 | |
203 ASSERT_EQ("Error", duk::getGlobal<std::string>(m_ctx, "name")); | |
204 ASSERT_TRUE(duk::getGlobal<bool>(m_ctx, "correct")); | |
205 } catch (const std::exception &ex) { | |
206 FAIL() << ex.what(); | |
207 } | |
208 } | |
209 | |
210 /* | |
211 * Get. | |
212 * ------------------------------------------------------------------ | |
213 */ | |
214 | |
215 TEST_F(TestPoint, getAdjustAll) | |
216 { | |
217 try { | |
218 duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { | |
219 Point point = duk::get<Point>(ctx, 0); | |
220 | |
221 duk::putGlobal(ctx, "x", point.x()); | |
222 duk::putGlobal(ctx, "y", point.y()); | |
223 | |
224 return 0; | |
225 }, 1}); | |
226 | |
227 auto ret = duk::pevalString(m_ctx, "build({});"); | |
228 | |
229 if (ret != 0) { | |
230 throw duk::error(m_ctx, -1); | |
231 } | |
232 | |
233 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "x")); | |
234 ASSERT_EQ(0, duk::getGlobal<int>(m_ctx, "y")); | |
235 } catch (const std::exception &ex) { | |
236 FAIL() << ex.what(); | |
237 } | |
238 } | |
239 | |
240 int main(int argc, char **argv) | |
241 { | |
242 testing::InitGoogleTest(&argc, argv); | |
243 | |
244 return RUN_ALL_TESTS(); | |
245 } |