comparison tests/libclient/js-size/main.cpp @ 30:a1e80d991968

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