blob: a86a50935c9fb7c0fbb81a3d394e57883a67f1ff [file] [log] [blame]
henriquea2de4102014-02-07 14:12:56 +01001/*
Roger Meierbb267d42011-07-30 15:10:42 +00002 * 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 */
Henrique Mendonça095ddb72013-09-20 19:38:03 +020019 /* jshint -W100 */
Roger Meier08562732015-06-14 22:30:22 +020020
Roger Meierbb267d42011-07-30 15:10:42 +000021/*
henriquea2de4102014-02-07 14:12:56 +010022 * JavaScript test suite for ThriftTest.thrift. These tests
23 * will run against Normal (-gen js) and jQuery (-gen js:jquery)
24 * Apache Thrift interfaces.
25 *
Kazuki Matsudab909a382016-02-13 19:36:09 +090026 * Synchronous blocking calls should be identical in both
henriquea2de4102014-02-07 14:12:56 +010027 * Normal and jQuery interfaces. All synchronous tests belong
28 * here.
Roger Meier08562732015-06-14 22:30:22 +020029 *
30 * Asynchronous success callbacks passed as the last parameter
henriquea2de4102014-02-07 14:12:56 +010031 * of an RPC call should be identical in both Normal and jQuery
32 * interfaces. Async success tests belong here.
Roger Meier08562732015-06-14 22:30:22 +020033 *
henriquea2de4102014-02-07 14:12:56 +010034 * Asynchronous exception processing is different in Normal
35 * and jQuery interfaces. Such tests belong in the test-nojq.js
36 * or test-jq.js files respectively. jQuery specific XHR object
Roger Meier08562732015-06-14 22:30:22 +020037 * tests also belong in test-jq.js. Do not create any jQuery
henriquea2de4102014-02-07 14:12:56 +010038 * dependencies in this file or in test-nojq.js
39 *
40 * To compile client code for this test use:
41 * $ thrift -gen js ThriftTest.thrift
42 * -- or --
43 * $ thrift -gen js:jquery ThriftTest.thrift
44 *
45 * See also:
Roger Meier08562732015-06-14 22:30:22 +020046 * ++ test-nojq.js for "-gen js" only tests
henriquea2de4102014-02-07 14:12:56 +010047 * ++ test-jq.js for "-gen js:jquery" only tests
Roger Meierbb267d42011-07-30 15:10:42 +000048 */
49
Brian Forbisb5d6ea32018-08-25 23:39:29 -040050const transport = new Thrift.Transport('/service');
51const protocol = new Thrift.Protocol(transport);
52const client = new ThriftTest.ThriftTestClient(protocol);
Roger Meierbb267d42011-07-30 15:10:42 +000053
Nobuaki Sukegawa72f88092015-05-10 23:35:29 +090054// Work around for old API used by QUnitAdapter of jsTestDriver
55if (typeof QUnit.log == 'function') {
56 // When using real QUnit (fron PhantomJS) log failures to console
57 QUnit.log(function(details) {
58 if (!details.result) {
59 console.log('======== FAIL ========');
60 console.log('TestName: ' + details.name);
Nobuaki Sukegawa6defea52015-11-14 17:36:29 +090061 if (details.message) console.log(details.message);
Nobuaki Sukegawa72f88092015-05-10 23:35:29 +090062 console.log('Expected: ' + details.expected);
63 console.log('Actual : ' + details.actual);
64 console.log('======================');
65 }
66 });
67}
68
Roger Meierbb267d42011-07-30 15:10:42 +000069// all Languages in UTF-8
Brian Forbisb5d6ea32018-08-25 23:39:29 -040070const 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ú, 粵語";
Roger Meier08562732015-06-14 22:30:22 +020071
Brian Forbisb5d6ea32018-08-25 23:39:29 -040072function checkRecursively(assert, map1, map2) {
Roger Meier4f8a5232011-09-09 08:17:02 +000073 if (typeof map1 !== 'function' && typeof map2 !== 'function') {
74 if (!map1 || typeof map1 !== 'object') {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040075 assert.equal(map1, map2);
Roger Meier4f8a5232011-09-09 08:17:02 +000076 } else {
Brian Forbisb5d6ea32018-08-25 23:39:29 -040077 for (let key in map1) {
78 checkRecursively(assert, map1[key], map2[key]);
Roger Meier4f8a5232011-09-09 08:17:02 +000079 }
80 }
81 }
82}
Roger Meierbb267d42011-07-30 15:10:42 +000083
Brian Forbisb5d6ea32018-08-25 23:39:29 -040084QUnit.module('Base Types');
Roger Meierbb267d42011-07-30 15:10:42 +000085
Brian Forbisb5d6ea32018-08-25 23:39:29 -040086 QUnit.test('Void', function(assert) {
87 assert.equal(client.testVoid(), undefined);
Roger Meierbb267d42011-07-30 15:10:42 +000088 });
Brian Forbisb5d6ea32018-08-25 23:39:29 -040089 QUnit.test('Binary (String)', function(assert) {
90 let binary = '';
91 for (let v = 255; v >= 0; --v) {
Nobuaki Sukegawa6defea52015-11-14 17:36:29 +090092 binary += String.fromCharCode(v);
93 }
Brian Forbisb5d6ea32018-08-25 23:39:29 -040094 assert.equal(client.testBinary(binary), binary);
Nobuaki Sukegawa6defea52015-11-14 17:36:29 +090095 });
Brian Forbisb5d6ea32018-08-25 23:39:29 -040096 QUnit.test('Binary (Uint8Array)', function(assert) {
97 let binary = '';
98 for (let v = 255; v >= 0; --v) {
Nobuaki Sukegawa6defea52015-11-14 17:36:29 +090099 binary += String.fromCharCode(v);
100 }
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400101 const arr = new Uint8Array(binary.length);
102 for (let i = 0; i < binary.length; ++i) {
Nobuaki Sukegawa6defea52015-11-14 17:36:29 +0900103 arr[i] = binary[i].charCodeAt();
104 }
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400105 assert.equal(client.testBinary(arr), binary);
Nobuaki Sukegawa6defea52015-11-14 17:36:29 +0900106 });
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400107 QUnit.test('String', function(assert) {
108 assert.equal(client.testString(''), '');
109 assert.equal(client.testString(stringTest), stringTest);
Roger Meierbb267d42011-07-30 15:10:42 +0000110
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400111 const specialCharacters = 'quote: \" backslash:' +
Roger Meierbb267d42011-07-30 15:10:42 +0000112 ' forwardslash-escaped: \/ ' +
113 ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
114 ' now-all-of-them-together: "\\\/\b\n\r\t' +
115 ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><';
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400116 assert.equal(client.testString(specialCharacters), specialCharacters);
Roger Meierbb267d42011-07-30 15:10:42 +0000117 });
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400118 QUnit.test('Double', function(assert) {
119 assert.equal(client.testDouble(0), 0);
120 assert.equal(client.testDouble(-1), -1);
121 assert.equal(client.testDouble(3.14), 3.14);
122 assert.equal(client.testDouble(Math.pow(2, 60)), Math.pow(2, 60));
Roger Meierbb267d42011-07-30 15:10:42 +0000123 });
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400124 QUnit.test('Byte', function(assert) {
125 assert.equal(client.testByte(0), 0);
126 assert.equal(client.testByte(0x01), 0x01);
Roger Meierbb267d42011-07-30 15:10:42 +0000127 });
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400128 QUnit.test('I32', function(assert) {
129 assert.equal(client.testI32(0), 0);
130 assert.equal(client.testI32(Math.pow(2, 30)), Math.pow(2, 30));
131 assert.equal(client.testI32(-Math.pow(2, 30)), -Math.pow(2, 30));
Roger Meierbb267d42011-07-30 15:10:42 +0000132 });
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400133 QUnit.test('I64', function(assert) {
134 assert.equal(client.testI64(0), 0);
henriquea2de4102014-02-07 14:12:56 +0100135 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400136 assert.equal(client.testI64(Math.pow(2, 52)), Math.pow(2, 52));
137 assert.equal(client.testI64(-Math.pow(2, 52)), -Math.pow(2, 52));
Roger Meierbb267d42011-07-30 15:10:42 +0000138 });
139
140
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400141QUnit.module('Structured Types');
Roger Meierbb267d42011-07-30 15:10:42 +0000142
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400143 QUnit.test('Struct', function(assert) {
144 const structTestInput = new ThriftTest.Xtruct();
Roger Meieredf0d1d2011-08-03 21:21:43 +0000145 structTestInput.string_thing = 'worked';
146 structTestInput.byte_thing = 0x01;
Kazuki Matsudab909a382016-02-13 19:36:09 +0900147 structTestInput.i32_thing = Math.pow(2, 30);
henriquea2de4102014-02-07 14:12:56 +0100148 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
Kazuki Matsudab909a382016-02-13 19:36:09 +0900149 structTestInput.i64_thing = Math.pow(2, 52);
Roger Meierbb267d42011-07-30 15:10:42 +0000150
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400151 const structTestOutput = client.testStruct(structTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000152
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400153 assert.equal(structTestOutput.string_thing, structTestInput.string_thing);
154 assert.equal(structTestOutput.byte_thing, structTestInput.byte_thing);
155 assert.equal(structTestOutput.i32_thing, structTestInput.i32_thing);
156 assert.equal(structTestOutput.i64_thing, structTestInput.i64_thing);
Roger Meier08562732015-06-14 22:30:22 +0200157
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400158 assert.equal(JSON.stringify(structTestOutput), JSON.stringify(structTestInput));
Roger Meierbb267d42011-07-30 15:10:42 +0000159 });
160
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400161 QUnit.test('Nest', function(assert) {
162 const xtrTestInput = new ThriftTest.Xtruct();
Roger Meieredf0d1d2011-08-03 21:21:43 +0000163 xtrTestInput.string_thing = 'worked';
164 xtrTestInput.byte_thing = 0x01;
Kazuki Matsudab909a382016-02-13 19:36:09 +0900165 xtrTestInput.i32_thing = Math.pow(2, 30);
henriquea2de4102014-02-07 14:12:56 +0100166 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
Kazuki Matsudab909a382016-02-13 19:36:09 +0900167 xtrTestInput.i64_thing = Math.pow(2, 52);
Roger Meier08562732015-06-14 22:30:22 +0200168
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400169 const nestTestInput = new ThriftTest.Xtruct2();
Roger Meieredf0d1d2011-08-03 21:21:43 +0000170 nestTestInput.byte_thing = 0x02;
171 nestTestInput.struct_thing = xtrTestInput;
Kazuki Matsudab909a382016-02-13 19:36:09 +0900172 nestTestInput.i32_thing = Math.pow(2, 15);
Roger Meier08562732015-06-14 22:30:22 +0200173
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400174 const nestTestOutput = client.testNest(nestTestInput);
Roger Meier08562732015-06-14 22:30:22 +0200175
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400176 assert.equal(nestTestOutput.byte_thing, nestTestInput.byte_thing);
177 assert.equal(nestTestOutput.struct_thing.string_thing, nestTestInput.struct_thing.string_thing);
178 assert.equal(nestTestOutput.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing);
179 assert.equal(nestTestOutput.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing);
180 assert.equal(nestTestOutput.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing);
181 assert.equal(nestTestOutput.i32_thing, nestTestInput.i32_thing);
Roger Meier08562732015-06-14 22:30:22 +0200182
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400183 assert.equal(JSON.stringify(nestTestOutput), JSON.stringify(nestTestInput));
Roger Meierbb267d42011-07-30 15:10:42 +0000184 });
185
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400186 QUnit.test('Map', function(assert) {
187 const mapTestInput = {7: 77, 8: 88, 9: 99};
Roger Meierbb267d42011-07-30 15:10:42 +0000188
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400189 const mapTestOutput = client.testMap(mapTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000190
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400191 for (let key in mapTestOutput) {
192 assert.equal(mapTestOutput[key], mapTestInput[key]);
Roger Meierbb267d42011-07-30 15:10:42 +0000193 }
194 });
195
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400196 QUnit.test('StringMap', function(assert) {
197 const mapTestInput = {
Kazuki Matsudab909a382016-02-13 19:36:09 +0900198 'a': '123', 'a b': 'with spaces ', 'same': 'same', '0': 'numeric key',
199 'longValue': stringTest, stringTest: 'long key'
Roger Meierbb267d42011-07-30 15:10:42 +0000200 };
201
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400202 const mapTestOutput = client.testStringMap(mapTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000203
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400204 for (let key in mapTestOutput) {
205 assert.equal(mapTestOutput[key], mapTestInput[key]);
Roger Meierbb267d42011-07-30 15:10:42 +0000206 }
207 });
208
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400209 QUnit.test('Set', function(assert) {
210 const setTestInput = [1, 2, 3];
211 assert.ok(client.testSet(setTestInput), setTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000212 });
213
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400214 QUnit.test('List', function(assert) {
215 const listTestInput = [1, 2, 3];
216 assert.ok(client.testList(listTestInput), listTestInput);
Roger Meierbb267d42011-07-30 15:10:42 +0000217 });
218
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400219 QUnit.test('Enum', function(assert) {
220 assert.equal(client.testEnum(ThriftTest.Numberz.ONE), ThriftTest.Numberz.ONE);
Roger Meierbb267d42011-07-30 15:10:42 +0000221 });
222
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400223 QUnit.test('TypeDef', function(assert) {
224 assert.equal(client.testTypedef(69), 69);
Roger Meierbb267d42011-07-30 15:10:42 +0000225 });
226
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400227 QUnit.test('Skip', function(assert) {
228 const structTestInput = new ThriftTest.Xtruct();
229 const modifiedClient = new ThriftTest.ThriftTestClient(protocol);
Jens Geyer329d59a2014-06-19 22:11:53 +0200230
231 modifiedClient.recv_testStruct = function() {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400232 const input = modifiedClient.input;
233 const xtruct3 = new ThriftTest.Xtruct3();
Jens Geyer329d59a2014-06-19 22:11:53 +0200234
235 input.readMessageBegin();
236 input.readStructBegin();
237
Roger Meier08562732015-06-14 22:30:22 +0200238 // read Xtruct data with Xtruct3
Jens Geyer329d59a2014-06-19 22:11:53 +0200239 input.readFieldBegin();
240 xtruct3.read(input);
241 input.readFieldEnd();
242 // read Thrift.Type.STOP message
243 input.readFieldBegin();
244 input.readFieldEnd();
245
246 input.readStructEnd();
247 input.readMessageEnd();
248
249 return xtruct3;
250 };
251
252 structTestInput.string_thing = 'worked';
Kazuki Matsudab909a382016-02-13 19:36:09 +0900253 structTestInput.byte_thing = 0x01;
254 structTestInput.i32_thing = Math.pow(2, 30);
255 structTestInput.i64_thing = Math.pow(2, 52);
Jens Geyer329d59a2014-06-19 22:11:53 +0200256
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400257 const structTestOutput = modifiedClient.testStruct(structTestInput);
Jens Geyer329d59a2014-06-19 22:11:53 +0200258
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400259 assert.equal(structTestOutput instanceof ThriftTest.Xtruct3, true);
260 assert.equal(structTestOutput.string_thing, structTestInput.string_thing);
261 assert.equal(structTestOutput.changed, null);
262 assert.equal(structTestOutput.i32_thing, structTestInput.i32_thing);
263 assert.equal(structTestOutput.i64_thing, structTestInput.i64_thing);
Jens Geyer329d59a2014-06-19 22:11:53 +0200264 });
265
Roger Meierbb267d42011-07-30 15:10:42 +0000266
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400267QUnit.module('deeper!');
Roger Meierbb267d42011-07-30 15:10:42 +0000268
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400269 QUnit.test('MapMap', function(assert) {
270 const mapMapTestExpectedResult = {
Kazuki Matsudab909a382016-02-13 19:36:09 +0900271 '4': {'1': 1, '2': 2, '3': 3, '4': 4},
272 '-4': {'-4': -4, '-3': -3, '-2': -2, '-1': -1}
Roger Meierbb267d42011-07-30 15:10:42 +0000273 };
274
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400275 const mapMapTestOutput = client.testMapMap(1);
Roger Meierbb267d42011-07-30 15:10:42 +0000276
277
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400278 for (let key in mapMapTestOutput) {
279 for (let key2 in mapMapTestOutput[key]) {
280 assert.equal(mapMapTestOutput[key][key2], mapMapTestExpectedResult[key][key2]);
Roger Meierbb267d42011-07-30 15:10:42 +0000281 }
282 }
Roger Meier08562732015-06-14 22:30:22 +0200283
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400284 checkRecursively(assert, mapMapTestOutput, mapMapTestExpectedResult);
Roger Meierbb267d42011-07-30 15:10:42 +0000285 });
286
287
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400288QUnit.module('Exception');
Roger Meierbb267d42011-07-30 15:10:42 +0000289
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400290 QUnit.test('Xception', function(assert) {
291 assert.expect(2);
292 const done = assert.async();
Kazuki Matsudab909a382016-02-13 19:36:09 +0900293 try {
294 client.testException('Xception');
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400295 assert.ok(false);
Kazuki Matsudab909a382016-02-13 19:36:09 +0900296 }catch (e) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400297 assert.equal(e.errorCode, 1001);
298 assert.equal(e.message, 'Xception');
299 done();
Roger Meierbb267d42011-07-30 15:10:42 +0000300 }
301 });
302
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400303 QUnit.test('no Exception', function(assert) {
304 assert.expect(1);
Kazuki Matsudab909a382016-02-13 19:36:09 +0900305 try {
306 client.testException('no Exception');
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400307 assert.ok(true);
Kazuki Matsudab909a382016-02-13 19:36:09 +0900308 }catch (e) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400309 assert.ok(false);
Roger Meierbb267d42011-07-30 15:10:42 +0000310 }
311 });
312
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400313 QUnit.test('TException', function(assert) {
henriquea2de4102014-02-07 14:12:56 +0100314 //ThriftTest does not list TException as a legal exception so it will
Roger Meier08562732015-06-14 22:30:22 +0200315 // generate an exception on the server that does not propagate back to
henriquea2de4102014-02-07 14:12:56 +0100316 // the client. This test has been modified to equate to "no exception"
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400317 assert.expect(1);
Kazuki Matsudab909a382016-02-13 19:36:09 +0900318 try {
319 client.testException('TException');
320 } catch (e) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400321 //assert.ok(false);
Roger Meierbb267d42011-07-30 15:10:42 +0000322 }
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400323 assert.ok(true);
Roger Meierbb267d42011-07-30 15:10:42 +0000324 });
325
326
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400327QUnit.module('Insanity');
Roger Meierbb267d42011-07-30 15:10:42 +0000328
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400329 const crazy = {
Kazuki Matsudab909a382016-02-13 19:36:09 +0900330 'userMap': { '5': 5, '8': 8 },
331 'xtructs': [{
332 'string_thing': 'Goodbye4',
333 'byte_thing': 4,
334 'i32_thing': 4,
335 'i64_thing': 4
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900336 },
337 {
Kazuki Matsudab909a382016-02-13 19:36:09 +0900338 'string_thing': 'Hello2',
339 'byte_thing': 2,
340 'i32_thing': 2,
341 'i64_thing': 2
Nobuaki Sukegawa01ede042015-09-29 02:16:53 +0900342 }]
343 };
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400344 QUnit.test('testInsanity', function(assert) {
345 const insanity = {
Kazuki Matsudab909a382016-02-13 19:36:09 +0900346 '1': {
347 '2': crazy,
348 '3': crazy
Roger Meierbb267d42011-07-30 15:10:42 +0000349 },
Kazuki Matsudab909a382016-02-13 19:36:09 +0900350 '2': { '6': { 'userMap': null, 'xtructs': null } }
Roger Meierbb267d42011-07-30 15:10:42 +0000351 };
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400352 const res = client.testInsanity(new ThriftTest.Insanity(crazy));
353 assert.ok(res, JSON.stringify(res));
354 assert.ok(insanity, JSON.stringify(insanity));
Roger Meier4f8a5232011-09-09 08:17:02 +0000355
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400356 checkRecursively(assert, res, insanity);
Roger Meierbb267d42011-07-30 15:10:42 +0000357 });
358
359
360//////////////////////////////////
361//Run same tests asynchronously
Roger Meierbb267d42011-07-30 15:10:42 +0000362
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400363QUnit.module('Async');
Roger Meierbb267d42011-07-30 15:10:42 +0000364
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400365 QUnit.test('Double', function(assert) {
366 assert.expect(1);
Roger Meierbb267d42011-07-30 15:10:42 +0000367
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400368 const done = assert.async();
Roger Meierbb267d42011-07-30 15:10:42 +0000369 client.testDouble(3.14159265, function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400370 assert.equal(result, 3.14159265);
371 done();
Roger Meierbb267d42011-07-30 15:10:42 +0000372 });
373 });
374
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400375 QUnit.test('Byte', function(assert) {
376 assert.expect(1);
Roger Meierbb267d42011-07-30 15:10:42 +0000377
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400378 const done = assert.async();
Roger Meierbb267d42011-07-30 15:10:42 +0000379 client.testByte(0x01, function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400380 assert.equal(result, 0x01);
381 done();
Roger Meierbb267d42011-07-30 15:10:42 +0000382 });
383 });
384
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400385 QUnit.test('I32', function(assert) {
386 assert.expect(2);
Roger Meierbb267d42011-07-30 15:10:42 +0000387
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400388 const done = assert.async(2);
Kazuki Matsudab909a382016-02-13 19:36:09 +0900389 client.testI32(Math.pow(2, 30), function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400390 assert.equal(result, Math.pow(2, 30));
391 done();
Roger Meierbb267d42011-07-30 15:10:42 +0000392 });
393
Kazuki Matsudab909a382016-02-13 19:36:09 +0900394 client.testI32(Math.pow(-2, 31), function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400395 assert.equal(result, Math.pow(-2, 31));
396 done();
Roger Meierbb267d42011-07-30 15:10:42 +0000397 });
398 });
399
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400400 QUnit.test('I64', function(assert) {
401 assert.expect(2);
Roger Meierbb267d42011-07-30 15:10:42 +0000402
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400403 const done = assert.async(2);
henriquea2de4102014-02-07 14:12:56 +0100404 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
Kazuki Matsudab909a382016-02-13 19:36:09 +0900405 client.testI64(Math.pow(2, 52), function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400406 assert.equal(result, Math.pow(2, 52));
407 done();
henriquea2de4102014-02-07 14:12:56 +0100408 });
409
henriquea2de4102014-02-07 14:12:56 +0100410 //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
Kazuki Matsudab909a382016-02-13 19:36:09 +0900411 client.testI64(Math.pow(-2, 52), function(result) {
Brian Forbisb5d6ea32018-08-25 23:39:29 -0400412 assert.equal(result, Math.pow(-2, 52));
413 done();
Roger Meierbb267d42011-07-30 15:10:42 +0000414 });
415 });