henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 1 | /* |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 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 | */ |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 19 | /* jshint -W100 */ |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 20 | |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 21 | /* |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 22 | * 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 Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 26 | * Synchronous blocking calls should be identical in both |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 27 | * Normal and jQuery interfaces. All synchronous tests belong |
| 28 | * here. |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 29 | * |
| 30 | * Asynchronous success callbacks passed as the last parameter |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 31 | * of an RPC call should be identical in both Normal and jQuery |
| 32 | * interfaces. Async success tests belong here. |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 33 | * |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 34 | * 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 Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 37 | * tests also belong in test-jq.js. Do not create any jQuery |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 38 | * 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 Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 46 | * ++ test-nojq.js for "-gen js" only tests |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 47 | * ++ test-jq.js for "-gen js:jquery" only tests |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 48 | */ |
| 49 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 50 | const transport = new Thrift.Transport('/service'); |
| 51 | const protocol = new Thrift.Protocol(transport); |
| 52 | const client = new ThriftTest.ThriftTestClient(protocol); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 53 | |
Mustafa Senol Cosar | f86845e | 2018-12-05 17:50:18 +0300 | [diff] [blame] | 54 | const int64_2_pow_60 = new Int64('1000000000000000'); |
| 55 | const int64_minus_2_pow_60 = new Int64('f000000000000000'); |
| 56 | |
Nobuaki Sukegawa | 72f8809 | 2015-05-10 23:35:29 +0900 | [diff] [blame] | 57 | // Work around for old API used by QUnitAdapter of jsTestDriver |
| 58 | if (typeof QUnit.log == 'function') { |
| 59 | // When using real QUnit (fron PhantomJS) log failures to console |
| 60 | QUnit.log(function(details) { |
| 61 | if (!details.result) { |
| 62 | console.log('======== FAIL ========'); |
| 63 | console.log('TestName: ' + details.name); |
Nobuaki Sukegawa | 6defea5 | 2015-11-14 17:36:29 +0900 | [diff] [blame] | 64 | if (details.message) console.log(details.message); |
Nobuaki Sukegawa | 72f8809 | 2015-05-10 23:35:29 +0900 | [diff] [blame] | 65 | console.log('Expected: ' + details.expected); |
| 66 | console.log('Actual : ' + details.actual); |
| 67 | console.log('======================'); |
| 68 | } |
| 69 | }); |
| 70 | } |
| 71 | |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 72 | // all Languages in UTF-8 |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 73 | const 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 Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 74 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 75 | function checkRecursively(assert, map1, map2) { |
Roger Meier | 4f8a523 | 2011-09-09 08:17:02 +0000 | [diff] [blame] | 76 | if (typeof map1 !== 'function' && typeof map2 !== 'function') { |
| 77 | if (!map1 || typeof map1 !== 'object') { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 78 | assert.equal(map1, map2); |
Roger Meier | 4f8a523 | 2011-09-09 08:17:02 +0000 | [diff] [blame] | 79 | } else { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 80 | for (let key in map1) { |
| 81 | checkRecursively(assert, map1[key], map2[key]); |
Roger Meier | 4f8a523 | 2011-09-09 08:17:02 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 86 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 87 | QUnit.module('Base Types'); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 88 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 89 | QUnit.test('Void', function(assert) { |
| 90 | assert.equal(client.testVoid(), undefined); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 91 | }); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 92 | QUnit.test('Binary (String)', function(assert) { |
| 93 | let binary = ''; |
| 94 | for (let v = 255; v >= 0; --v) { |
Nobuaki Sukegawa | 6defea5 | 2015-11-14 17:36:29 +0900 | [diff] [blame] | 95 | binary += String.fromCharCode(v); |
| 96 | } |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 97 | assert.equal(client.testBinary(binary), binary); |
Nobuaki Sukegawa | 6defea5 | 2015-11-14 17:36:29 +0900 | [diff] [blame] | 98 | }); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 99 | QUnit.test('Binary (Uint8Array)', function(assert) { |
| 100 | let binary = ''; |
| 101 | for (let v = 255; v >= 0; --v) { |
Nobuaki Sukegawa | 6defea5 | 2015-11-14 17:36:29 +0900 | [diff] [blame] | 102 | binary += String.fromCharCode(v); |
| 103 | } |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 104 | const arr = new Uint8Array(binary.length); |
| 105 | for (let i = 0; i < binary.length; ++i) { |
Nobuaki Sukegawa | 6defea5 | 2015-11-14 17:36:29 +0900 | [diff] [blame] | 106 | arr[i] = binary[i].charCodeAt(); |
| 107 | } |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 108 | assert.equal(client.testBinary(arr), binary); |
Nobuaki Sukegawa | 6defea5 | 2015-11-14 17:36:29 +0900 | [diff] [blame] | 109 | }); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 110 | QUnit.test('String', function(assert) { |
| 111 | assert.equal(client.testString(''), ''); |
| 112 | assert.equal(client.testString(stringTest), stringTest); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 113 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 114 | const specialCharacters = 'quote: \" backslash:' + |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 115 | ' forwardslash-escaped: \/ ' + |
| 116 | ' backspace: \b formfeed: \f newline: \n return: \r tab: ' + |
| 117 | ' now-all-of-them-together: "\\\/\b\n\r\t' + |
| 118 | ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><'; |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 119 | assert.equal(client.testString(specialCharacters), specialCharacters); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 120 | }); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 121 | QUnit.test('Double', function(assert) { |
| 122 | assert.equal(client.testDouble(0), 0); |
| 123 | assert.equal(client.testDouble(-1), -1); |
| 124 | assert.equal(client.testDouble(3.14), 3.14); |
| 125 | assert.equal(client.testDouble(Math.pow(2, 60)), Math.pow(2, 60)); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 126 | }); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 127 | QUnit.test('Byte', function(assert) { |
| 128 | assert.equal(client.testByte(0), 0); |
| 129 | assert.equal(client.testByte(0x01), 0x01); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 130 | }); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 131 | QUnit.test('I32', function(assert) { |
| 132 | assert.equal(client.testI32(0), 0); |
| 133 | assert.equal(client.testI32(Math.pow(2, 30)), Math.pow(2, 30)); |
| 134 | assert.equal(client.testI32(-Math.pow(2, 30)), -Math.pow(2, 30)); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 135 | }); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 136 | QUnit.test('I64', function(assert) { |
| 137 | assert.equal(client.testI64(0), 0); |
Mustafa Senol Cosar | f86845e | 2018-12-05 17:50:18 +0300 | [diff] [blame] | 138 | |
| 139 | let int64_2_pow_60_result = client.testI64(int64_2_pow_60); |
| 140 | assert.ok(int64_2_pow_60.equals(int64_2_pow_60_result)); |
| 141 | |
| 142 | let int64_minus_2_pow_60_result = client.testI64(int64_minus_2_pow_60); |
| 143 | assert.ok(int64_minus_2_pow_60.equals(int64_minus_2_pow_60_result)); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 144 | }); |
| 145 | |
| 146 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 147 | QUnit.module('Structured Types'); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 148 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 149 | QUnit.test('Struct', function(assert) { |
| 150 | const structTestInput = new ThriftTest.Xtruct(); |
Roger Meier | edf0d1d | 2011-08-03 21:21:43 +0000 | [diff] [blame] | 151 | structTestInput.string_thing = 'worked'; |
| 152 | structTestInput.byte_thing = 0x01; |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 153 | structTestInput.i32_thing = Math.pow(2, 30); |
Mustafa Senol Cosar | f86845e | 2018-12-05 17:50:18 +0300 | [diff] [blame] | 154 | structTestInput.i64_thing = int64_2_pow_60; |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 155 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 156 | const structTestOutput = client.testStruct(structTestInput); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 157 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 158 | assert.equal(structTestOutput.string_thing, structTestInput.string_thing); |
| 159 | assert.equal(structTestOutput.byte_thing, structTestInput.byte_thing); |
| 160 | assert.equal(structTestOutput.i32_thing, structTestInput.i32_thing); |
Mustafa Senol Cosar | f86845e | 2018-12-05 17:50:18 +0300 | [diff] [blame] | 161 | assert.ok(structTestOutput.i64_thing.equals(structTestInput.i64_thing)); |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 162 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 163 | assert.equal(JSON.stringify(structTestOutput), JSON.stringify(structTestInput)); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 164 | }); |
| 165 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 166 | QUnit.test('Nest', function(assert) { |
| 167 | const xtrTestInput = new ThriftTest.Xtruct(); |
Roger Meier | edf0d1d | 2011-08-03 21:21:43 +0000 | [diff] [blame] | 168 | xtrTestInput.string_thing = 'worked'; |
| 169 | xtrTestInput.byte_thing = 0x01; |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 170 | xtrTestInput.i32_thing = Math.pow(2, 30); |
Mustafa Senol Cosar | f86845e | 2018-12-05 17:50:18 +0300 | [diff] [blame] | 171 | xtrTestInput.i64_thing = int64_2_pow_60; |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 172 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 173 | const nestTestInput = new ThriftTest.Xtruct2(); |
Roger Meier | edf0d1d | 2011-08-03 21:21:43 +0000 | [diff] [blame] | 174 | nestTestInput.byte_thing = 0x02; |
| 175 | nestTestInput.struct_thing = xtrTestInput; |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 176 | nestTestInput.i32_thing = Math.pow(2, 15); |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 177 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 178 | const nestTestOutput = client.testNest(nestTestInput); |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 179 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 180 | assert.equal(nestTestOutput.byte_thing, nestTestInput.byte_thing); |
| 181 | assert.equal(nestTestOutput.struct_thing.string_thing, nestTestInput.struct_thing.string_thing); |
| 182 | assert.equal(nestTestOutput.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing); |
| 183 | assert.equal(nestTestOutput.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing); |
Mustafa Senol Cosar | f86845e | 2018-12-05 17:50:18 +0300 | [diff] [blame] | 184 | assert.ok(nestTestOutput.struct_thing.i64_thing.equals(nestTestInput.struct_thing.i64_thing)); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 185 | assert.equal(nestTestOutput.i32_thing, nestTestInput.i32_thing); |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 186 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 187 | assert.equal(JSON.stringify(nestTestOutput), JSON.stringify(nestTestInput)); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 188 | }); |
| 189 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 190 | QUnit.test('Map', function(assert) { |
| 191 | const mapTestInput = {7: 77, 8: 88, 9: 99}; |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 192 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 193 | const mapTestOutput = client.testMap(mapTestInput); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 194 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 195 | for (let key in mapTestOutput) { |
| 196 | assert.equal(mapTestOutput[key], mapTestInput[key]); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 197 | } |
| 198 | }); |
| 199 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 200 | QUnit.test('StringMap', function(assert) { |
| 201 | const mapTestInput = { |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 202 | 'a': '123', 'a b': 'with spaces ', 'same': 'same', '0': 'numeric key', |
| 203 | 'longValue': stringTest, stringTest: 'long key' |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 206 | const mapTestOutput = client.testStringMap(mapTestInput); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 207 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 208 | for (let key in mapTestOutput) { |
| 209 | assert.equal(mapTestOutput[key], mapTestInput[key]); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 210 | } |
| 211 | }); |
| 212 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 213 | QUnit.test('Set', function(assert) { |
| 214 | const setTestInput = [1, 2, 3]; |
| 215 | assert.ok(client.testSet(setTestInput), setTestInput); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 216 | }); |
| 217 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 218 | QUnit.test('List', function(assert) { |
| 219 | const listTestInput = [1, 2, 3]; |
| 220 | assert.ok(client.testList(listTestInput), listTestInput); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 221 | }); |
| 222 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 223 | QUnit.test('Enum', function(assert) { |
| 224 | assert.equal(client.testEnum(ThriftTest.Numberz.ONE), ThriftTest.Numberz.ONE); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 225 | }); |
| 226 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 227 | QUnit.test('TypeDef', function(assert) { |
| 228 | assert.equal(client.testTypedef(69), 69); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 229 | }); |
| 230 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 231 | QUnit.test('Skip', function(assert) { |
| 232 | const structTestInput = new ThriftTest.Xtruct(); |
| 233 | const modifiedClient = new ThriftTest.ThriftTestClient(protocol); |
Jens Geyer | 329d59a | 2014-06-19 22:11:53 +0200 | [diff] [blame] | 234 | |
| 235 | modifiedClient.recv_testStruct = function() { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 236 | const input = modifiedClient.input; |
| 237 | const xtruct3 = new ThriftTest.Xtruct3(); |
Jens Geyer | 329d59a | 2014-06-19 22:11:53 +0200 | [diff] [blame] | 238 | |
| 239 | input.readMessageBegin(); |
| 240 | input.readStructBegin(); |
| 241 | |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 242 | // read Xtruct data with Xtruct3 |
Jens Geyer | 329d59a | 2014-06-19 22:11:53 +0200 | [diff] [blame] | 243 | input.readFieldBegin(); |
| 244 | xtruct3.read(input); |
| 245 | input.readFieldEnd(); |
| 246 | // read Thrift.Type.STOP message |
| 247 | input.readFieldBegin(); |
| 248 | input.readFieldEnd(); |
| 249 | |
| 250 | input.readStructEnd(); |
| 251 | input.readMessageEnd(); |
| 252 | |
| 253 | return xtruct3; |
| 254 | }; |
| 255 | |
| 256 | structTestInput.string_thing = 'worked'; |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 257 | structTestInput.byte_thing = 0x01; |
| 258 | structTestInput.i32_thing = Math.pow(2, 30); |
Mustafa Senol Cosar | f86845e | 2018-12-05 17:50:18 +0300 | [diff] [blame] | 259 | structTestInput.i64_thing = int64_2_pow_60; |
Jens Geyer | 329d59a | 2014-06-19 22:11:53 +0200 | [diff] [blame] | 260 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 261 | const structTestOutput = modifiedClient.testStruct(structTestInput); |
Jens Geyer | 329d59a | 2014-06-19 22:11:53 +0200 | [diff] [blame] | 262 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 263 | assert.equal(structTestOutput instanceof ThriftTest.Xtruct3, true); |
| 264 | assert.equal(structTestOutput.string_thing, structTestInput.string_thing); |
| 265 | assert.equal(structTestOutput.changed, null); |
| 266 | assert.equal(structTestOutput.i32_thing, structTestInput.i32_thing); |
Mustafa Senol Cosar | f86845e | 2018-12-05 17:50:18 +0300 | [diff] [blame] | 267 | assert.ok(structTestOutput.i64_thing.equals(structTestInput.i64_thing)); |
Jens Geyer | 329d59a | 2014-06-19 22:11:53 +0200 | [diff] [blame] | 268 | }); |
| 269 | |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 270 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 271 | QUnit.module('deeper!'); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 272 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 273 | QUnit.test('MapMap', function(assert) { |
| 274 | const mapMapTestExpectedResult = { |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 275 | '4': {'1': 1, '2': 2, '3': 3, '4': 4}, |
| 276 | '-4': {'-4': -4, '-3': -3, '-2': -2, '-1': -1} |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 277 | }; |
| 278 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 279 | const mapMapTestOutput = client.testMapMap(1); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 280 | |
| 281 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 282 | for (let key in mapMapTestOutput) { |
| 283 | for (let key2 in mapMapTestOutput[key]) { |
| 284 | assert.equal(mapMapTestOutput[key][key2], mapMapTestExpectedResult[key][key2]); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 287 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 288 | checkRecursively(assert, mapMapTestOutput, mapMapTestExpectedResult); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 289 | }); |
| 290 | |
| 291 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 292 | QUnit.module('Exception'); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 293 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 294 | QUnit.test('Xception', function(assert) { |
| 295 | assert.expect(2); |
| 296 | const done = assert.async(); |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 297 | try { |
| 298 | client.testException('Xception'); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 299 | assert.ok(false); |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 300 | }catch (e) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 301 | assert.equal(e.errorCode, 1001); |
| 302 | assert.equal(e.message, 'Xception'); |
| 303 | done(); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 304 | } |
| 305 | }); |
| 306 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 307 | QUnit.test('no Exception', function(assert) { |
| 308 | assert.expect(1); |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 309 | try { |
| 310 | client.testException('no Exception'); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 311 | assert.ok(true); |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 312 | }catch (e) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 313 | assert.ok(false); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 314 | } |
| 315 | }); |
| 316 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 317 | QUnit.test('TException', function(assert) { |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 318 | //ThriftTest does not list TException as a legal exception so it will |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 319 | // generate an exception on the server that does not propagate back to |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 320 | // the client. This test has been modified to equate to "no exception" |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 321 | assert.expect(1); |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 322 | try { |
| 323 | client.testException('TException'); |
| 324 | } catch (e) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 325 | //assert.ok(false); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 326 | } |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 327 | assert.ok(true); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 328 | }); |
| 329 | |
| 330 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 331 | QUnit.module('Insanity'); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 332 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 333 | const crazy = { |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 334 | 'userMap': { '5': 5, '8': 8 }, |
| 335 | 'xtructs': [{ |
| 336 | 'string_thing': 'Goodbye4', |
| 337 | 'byte_thing': 4, |
| 338 | 'i32_thing': 4, |
Mustafa Senol Cosar | f86845e | 2018-12-05 17:50:18 +0300 | [diff] [blame] | 339 | 'i64_thing': new Int64(4) |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 340 | }, |
| 341 | { |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 342 | 'string_thing': 'Hello2', |
| 343 | 'byte_thing': 2, |
| 344 | 'i32_thing': 2, |
Mustafa Senol Cosar | f86845e | 2018-12-05 17:50:18 +0300 | [diff] [blame] | 345 | 'i64_thing': new Int64(2) |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 346 | }] |
| 347 | }; |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 348 | QUnit.test('testInsanity', function(assert) { |
| 349 | const insanity = { |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 350 | '1': { |
| 351 | '2': crazy, |
| 352 | '3': crazy |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 353 | }, |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 354 | '2': { '6': { 'userMap': null, 'xtructs': null } } |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 355 | }; |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 356 | const res = client.testInsanity(new ThriftTest.Insanity(crazy)); |
| 357 | assert.ok(res, JSON.stringify(res)); |
| 358 | assert.ok(insanity, JSON.stringify(insanity)); |
Roger Meier | 4f8a523 | 2011-09-09 08:17:02 +0000 | [diff] [blame] | 359 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 360 | checkRecursively(assert, res, insanity); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 361 | }); |
| 362 | |
| 363 | |
| 364 | ////////////////////////////////// |
| 365 | //Run same tests asynchronously |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 366 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 367 | QUnit.module('Async'); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 368 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 369 | QUnit.test('Double', function(assert) { |
| 370 | assert.expect(1); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 371 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 372 | const done = assert.async(); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 373 | client.testDouble(3.14159265, function(result) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 374 | assert.equal(result, 3.14159265); |
| 375 | done(); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 376 | }); |
| 377 | }); |
| 378 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 379 | QUnit.test('Byte', function(assert) { |
| 380 | assert.expect(1); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 381 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 382 | const done = assert.async(); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 383 | client.testByte(0x01, function(result) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 384 | assert.equal(result, 0x01); |
| 385 | done(); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 386 | }); |
| 387 | }); |
| 388 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 389 | QUnit.test('I32', function(assert) { |
| 390 | assert.expect(2); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 391 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 392 | const done = assert.async(2); |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 393 | client.testI32(Math.pow(2, 30), function(result) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 394 | assert.equal(result, Math.pow(2, 30)); |
| 395 | done(); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 396 | }); |
| 397 | |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 398 | client.testI32(Math.pow(-2, 31), function(result) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 399 | assert.equal(result, Math.pow(-2, 31)); |
| 400 | done(); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 401 | }); |
| 402 | }); |
| 403 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 404 | QUnit.test('I64', function(assert) { |
| 405 | assert.expect(2); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 406 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 407 | const done = assert.async(2); |
Mustafa Senol Cosar | f86845e | 2018-12-05 17:50:18 +0300 | [diff] [blame] | 408 | client.testI64(int64_2_pow_60, function(result) { |
| 409 | assert.ok(int64_2_pow_60.equals(result)); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 410 | done(); |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 411 | }); |
| 412 | |
Mustafa Senol Cosar | f86845e | 2018-12-05 17:50:18 +0300 | [diff] [blame] | 413 | client.testI64(int64_minus_2_pow_60, function(result) { |
| 414 | assert.ok(int64_minus_2_pow_60.equals(result)); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame] | 415 | done(); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 416 | }); |
| 417 | }); |