blob: 4935fead017e9a4fb2f97edaf866e47cffa935c8 [file] [log] [blame]
henrique2a7dccc2014-03-07 22:16:51 +01001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 /* jshint -W100 */
20
21/*
22 * Fully Async JavaScript test suite for ThriftTest.thrift.
23 * These tests are designed to exercise the WebSocket transport
24 * (which is exclusively async).
25 *
26 * To compile client code for this test use:
27 * $ thrift -gen js ThriftTest.thrift
28 */
29
30
31
32// all Languages in UTF-8
33var stringTest = "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, Bahasa Melayu, مازِرونی, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, ‪Norsk (nynorsk)‬, ‪Norsk (bokmål)‬, Nouormand, Diné bizaad, Occitan, Иронау, Papiamentu, Deitsch, Norfuk / Pitkern, Polski, پنجابی, پښتو, Português, Runa Simi, Rumantsch, Romani, Română, Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, Bân-lâm-gú, 粵語";
34
35function checkRecursively(map1, map2) {
36 if (typeof map1 !== 'function' && typeof map2 !== 'function') {
37 if (!map1 || typeof map1 !== 'object') {
38 equal(map1, map2);
39 } else {
40 for (var key in map1) {
41 checkRecursively(map1[key], map2[key]);
42 }
43 }
44 }
45}
46
47module("Base Types");
48
49 asyncTest("Void", function() {
50 expect( 1 );
51 client.testVoid(function(result) {
52 equal(result, undefined);
53 QUnit.start();
54 });
55 });
56
57
58 asyncTest("String", function() {
59 expect( 3 );
60 QUnit.stop(2);
61 client.testString('', function(result){
62 equal(result, '');
63 QUnit.start();
64 });
65 client.testString(stringTest, function(result){
66 equal(result, stringTest);
67 QUnit.start();
68 });
69
70 var specialCharacters = 'quote: \" backslash:' +
71 ' forwardslash-escaped: \/ ' +
72 ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
73 ' now-all-of-them-together: "\\\/\b\n\r\t' +
74 ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><';
75 client.testString(specialCharacters, function(result){
76 equal(result, specialCharacters);
77 QUnit.start();
78 });
79 });
80 asyncTest("Double", function() {
81 expect( 4 );
82 QUnit.stop(3);
83 client.testDouble(0, function(result){
84 equal(result, 0);
85 QUnit.start();
86 });
87 client.testDouble(-1, function(result){
88 equal(result, -1);
89 QUnit.start();
90 });
91 client.testDouble(3.14, function(result){
92 equal(result, 3.14);
93 QUnit.start();
94 });
95 client.testDouble(Math.pow(2,60), function(result){
96 equal(result, Math.pow(2,60));
97 QUnit.start();
98 });
99 });
100 asyncTest("Byte", function() {
101 expect( 2 );
102 QUnit.stop();
103 client.testByte(0, function(result) {
104 equal(result, 0);
105 QUnit.start();
106 });
107 client.testByte(0x01, function(result) {
108 equal(result, 0x01);
109 QUnit.start();
110 });
111 });
112 asyncTest("I32", function() {
113 expect( 3 );
114 QUnit.stop(2);
115 client.testI32(0, function(result){
116 equal(result, 0);
117 QUnit.start();
118 });
119 client.testI32(Math.pow(2,30), function(result){
120 equal(result, Math.pow(2,30));
121 QUnit.start();
122 });
123 client.testI32(-Math.pow(2,30), function(result){
124 equal(result, -Math.pow(2,30));
125 QUnit.start();
126 });
127 });
128 asyncTest("I64", function() {
129 expect( 3 );
130 QUnit.stop(2);
131 client.testI64(0, function(result){
132 equal(result, 0);
133 QUnit.start();
134 });
135 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
136 client.testI64(Math.pow(2,52), function(result){
137 equal(result, Math.pow(2,52));
138 QUnit.start();
139 });
140 client.testI64(-Math.pow(2,52), function(result){
141 equal(result, -Math.pow(2,52));
142 QUnit.start();
143 });
144 });
145
146
147
148
149module("Structured Types");
150
151 asyncTest("Struct", function() {
152 expect( 5 );
153 var structTestInput = new ThriftTest.Xtruct();
154 structTestInput.string_thing = 'worked';
155 structTestInput.byte_thing = 0x01;
156 structTestInput.i32_thing = Math.pow(2,30);
157 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
158 structTestInput.i64_thing = Math.pow(2,52);
159
160 client.testStruct(structTestInput, function(result){
161 equal(result.string_thing, structTestInput.string_thing);
162 equal(result.byte_thing, structTestInput.byte_thing);
163 equal(result.i32_thing, structTestInput.i32_thing);
164 equal(result.i64_thing, structTestInput.i64_thing);
165 equal(JSON.stringify(result), JSON.stringify(structTestInput));
166 QUnit.start();
167 });
168 });
169
170 asyncTest("Nest", function() {
171 expect( 7 );
172 var xtrTestInput = new ThriftTest.Xtruct();
173 xtrTestInput.string_thing = 'worked';
174 xtrTestInput.byte_thing = 0x01;
175 xtrTestInput.i32_thing = Math.pow(2,30);
176 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
177 xtrTestInput.i64_thing = Math.pow(2,52);
178
179 var nestTestInput = new ThriftTest.Xtruct2();
180 nestTestInput.byte_thing = 0x02;
181 nestTestInput.struct_thing = xtrTestInput;
182 nestTestInput.i32_thing = Math.pow(2,15);
183
184 client.testNest(nestTestInput, function(result){
185 equal(result.byte_thing, nestTestInput.byte_thing);
186 equal(result.struct_thing.string_thing, nestTestInput.struct_thing.string_thing);
187 equal(result.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing);
188 equal(result.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing);
189 equal(result.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing);
190 equal(result.i32_thing, nestTestInput.i32_thing);
191 equal(JSON.stringify(result), JSON.stringify(nestTestInput));
192 QUnit.start();
193 });
194 });
195
196 asyncTest("Map", function() {
197 expect( 3 );
198 var mapTestInput = {7:77, 8:88, 9:99};
199
200 client.testMap(mapTestInput, function(result){
201 for (var key in result) {
202 equal(result[key], mapTestInput[key]);
203 }
204 QUnit.start();
205 });
206 });
207
208 asyncTest("StringMap", function() {
209 expect( 6 );
210 var mapTestInput = {
211 "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
212 "longValue":stringTest, stringTest:"long key"
213 };
214
215 client.testStringMap(mapTestInput, function(result){
216 for (var key in result) {
217 equal(result[key], mapTestInput[key]);
218 }
219 QUnit.start();
220 });
221 });
222
223 asyncTest("Set", function() {
224 expect( 1 );
225 var setTestInput = [1,2,3];
226 client.testSet(setTestInput, function(result){
227 ok(result, setTestInput);
228 QUnit.start();
229 });
230 });
231
232 asyncTest("List", function() {
233 expect( 1 );
234 var listTestInput = [1,2,3];
235 client.testList(listTestInput, function(result){
236 ok(result, listTestInput);
237 QUnit.start();
238 });
239 });
240
241 asyncTest("Enum", function() {
242 expect( 1 );
243 client.testEnum(ThriftTest.Numberz.ONE, function(result){
244 equal(result, ThriftTest.Numberz.ONE);
245 QUnit.start();
246 });
247 });
248
249 asyncTest("TypeDef", function() {
250 expect( 1 );
251 client.testTypedef(69, function(result){
252 equal(result, 69);
253 QUnit.start();
254 });
255 });
256
257
258module("deeper!");
259
260 asyncTest("MapMap", function() {
261 expect( 16 );
262 var mapMapTestExpectedResult = {
263 "4":{"1":1,"2":2,"3":3,"4":4},
264 "-4":{"-4":-4, "-3":-3, "-2":-2, "-1":-1}
265 };
266
267 client.testMapMap(1, function(result){
268 for (var key in result) {
269 for (var key2 in result[key]) {
270 equal(result[key][key2], mapMapTestExpectedResult[key][key2]);
271 }
272 }
273 checkRecursively(result, mapMapTestExpectedResult);
274 QUnit.start();
275 });
276 });
277
278
279module("Exception");
280
281 asyncTest("Xception", function() {
282 expect(2);
283 client.testException("Xception", function(e){
284 equal(e.errorCode, 1001);
285 equal(e.message, "Xception");
286 QUnit.start();
287 });
288 });
289
290 asyncTest("no Exception", 0, function() {
291 expect( 1 );
292 client.testException("no Exception", function(e){
293 ok(!e);
294 QUnit.start();
295 });
296 });
297
298module("Insanity");
299
300 asyncTest("testInsanity", function() {
301 expect( 24 );
302 var insanity = {
303 "1":{
304 "2":{
305 "userMap":{ "5":5, "8":8 },
306 "xtructs":[{
307 "string_thing":"Goodbye4",
308 "byte_thing":4,
309 "i32_thing":4,
310 "i64_thing":4
311 },
312 {
313 "string_thing":"Hello2",
314 "byte_thing":2,
315 "i32_thing":2,
316 "i64_thing":2
317 }
318 ]
319 },
320 "3":{
321 "userMap":{ "5":5, "8":8 },
322 "xtructs":[{
323 "string_thing":"Goodbye4",
324 "byte_thing":4,
325 "i32_thing":4,
326 "i64_thing":4
327 },
328 {
329 "string_thing":"Hello2",
330 "byte_thing":2,
331 "i32_thing":2,
332 "i64_thing":2
333 }
334 ]
335 }
336 },
337 "2":{ "6":{ "userMap":null, "xtructs":null } }
338 };
339 client.testInsanity(new ThriftTest.Insanity(), function(res){
340 ok(res, JSON.stringify(res));
341 ok(insanity, JSON.stringify(insanity));
342 checkRecursively(res, insanity);
343 QUnit.start();
344 });
345 });
346
347