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 | |
Nobuaki Sukegawa | 72f8809 | 2015-05-10 23:35:29 +0900 | [diff] [blame] | 54 | // Work around for old API used by QUnitAdapter of jsTestDriver |
| 55 | if (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 Sukegawa | 6defea5 | 2015-11-14 17:36:29 +0900 | [diff] [blame] | 61 | if (details.message) console.log(details.message); |
Nobuaki Sukegawa | 72f8809 | 2015-05-10 23:35:29 +0900 | [diff] [blame] | 62 | console.log('Expected: ' + details.expected); |
| 63 | console.log('Actual : ' + details.actual); |
| 64 | console.log('======================'); |
| 65 | } |
| 66 | }); |
| 67 | } |
| 68 | |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 69 | // all Languages in UTF-8 |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 70 | 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] | 71 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 72 | function checkRecursively(assert, map1, map2) { |
Roger Meier | 4f8a523 | 2011-09-09 08:17:02 +0000 | [diff] [blame] | 73 | if (typeof map1 !== 'function' && typeof map2 !== 'function') { |
| 74 | if (!map1 || typeof map1 !== 'object') { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 75 | assert.equal(map1, map2); |
Roger Meier | 4f8a523 | 2011-09-09 08:17:02 +0000 | [diff] [blame] | 76 | } else { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 77 | for (let key in map1) { |
| 78 | checkRecursively(assert, map1[key], map2[key]); |
Roger Meier | 4f8a523 | 2011-09-09 08:17:02 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 83 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 84 | QUnit.module('Base Types'); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 85 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 86 | QUnit.test('Void', function(assert) { |
| 87 | assert.equal(client.testVoid(), undefined); |
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('Binary (String)', function(assert) { |
| 90 | let binary = ''; |
| 91 | for (let v = 255; v >= 0; --v) { |
Nobuaki Sukegawa | 6defea5 | 2015-11-14 17:36:29 +0900 | [diff] [blame] | 92 | binary += String.fromCharCode(v); |
| 93 | } |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 94 | assert.equal(client.testBinary(binary), binary); |
Nobuaki Sukegawa | 6defea5 | 2015-11-14 17:36:29 +0900 | [diff] [blame] | 95 | }); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 96 | QUnit.test('Binary (Uint8Array)', function(assert) { |
| 97 | let binary = ''; |
| 98 | for (let v = 255; v >= 0; --v) { |
Nobuaki Sukegawa | 6defea5 | 2015-11-14 17:36:29 +0900 | [diff] [blame] | 99 | binary += String.fromCharCode(v); |
| 100 | } |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 101 | const arr = new Uint8Array(binary.length); |
| 102 | for (let i = 0; i < binary.length; ++i) { |
Nobuaki Sukegawa | 6defea5 | 2015-11-14 17:36:29 +0900 | [diff] [blame] | 103 | arr[i] = binary[i].charCodeAt(); |
| 104 | } |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 105 | assert.equal(client.testBinary(arr), binary); |
Nobuaki Sukegawa | 6defea5 | 2015-11-14 17:36:29 +0900 | [diff] [blame] | 106 | }); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 107 | QUnit.test('String', function(assert) { |
| 108 | assert.equal(client.testString(''), ''); |
| 109 | assert.equal(client.testString(stringTest), stringTest); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 110 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 111 | const specialCharacters = 'quote: \" backslash:' + |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 112 | ' 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 Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 116 | assert.equal(client.testString(specialCharacters), specialCharacters); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 117 | }); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 118 | 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 Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 123 | }); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 124 | QUnit.test('Byte', function(assert) { |
| 125 | assert.equal(client.testByte(0), 0); |
| 126 | assert.equal(client.testByte(0x01), 0x01); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 127 | }); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 128 | 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 Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 132 | }); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 133 | QUnit.test('I64', function(assert) { |
| 134 | assert.equal(client.testI64(0), 0); |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 135 | //This is usually 2^60 but JS cannot represent anything over 2^52 accurately |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 136 | 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 Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 138 | }); |
| 139 | |
| 140 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 141 | QUnit.module('Structured Types'); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 142 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 143 | QUnit.test('Struct', function(assert) { |
| 144 | const structTestInput = new ThriftTest.Xtruct(); |
Roger Meier | edf0d1d | 2011-08-03 21:21:43 +0000 | [diff] [blame] | 145 | structTestInput.string_thing = 'worked'; |
| 146 | structTestInput.byte_thing = 0x01; |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 147 | structTestInput.i32_thing = Math.pow(2, 30); |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 148 | //This is usually 2^60 but JS cannot represent anything over 2^52 accurately |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 149 | structTestInput.i64_thing = Math.pow(2, 52); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 150 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 151 | const structTestOutput = client.testStruct(structTestInput); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 152 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 153 | 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 Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 157 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 158 | assert.equal(JSON.stringify(structTestOutput), JSON.stringify(structTestInput)); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 159 | }); |
| 160 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 161 | QUnit.test('Nest', function(assert) { |
| 162 | const xtrTestInput = new ThriftTest.Xtruct(); |
Roger Meier | edf0d1d | 2011-08-03 21:21:43 +0000 | [diff] [blame] | 163 | xtrTestInput.string_thing = 'worked'; |
| 164 | xtrTestInput.byte_thing = 0x01; |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 165 | xtrTestInput.i32_thing = Math.pow(2, 30); |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 166 | //This is usually 2^60 but JS cannot represent anything over 2^52 accurately |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 167 | xtrTestInput.i64_thing = Math.pow(2, 52); |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 168 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 169 | const nestTestInput = new ThriftTest.Xtruct2(); |
Roger Meier | edf0d1d | 2011-08-03 21:21:43 +0000 | [diff] [blame] | 170 | nestTestInput.byte_thing = 0x02; |
| 171 | nestTestInput.struct_thing = xtrTestInput; |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 172 | nestTestInput.i32_thing = Math.pow(2, 15); |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 173 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 174 | const nestTestOutput = client.testNest(nestTestInput); |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 175 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 176 | 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 Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 182 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 183 | assert.equal(JSON.stringify(nestTestOutput), JSON.stringify(nestTestInput)); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 184 | }); |
| 185 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 186 | QUnit.test('Map', function(assert) { |
| 187 | const mapTestInput = {7: 77, 8: 88, 9: 99}; |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 188 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 189 | const mapTestOutput = client.testMap(mapTestInput); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 190 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 191 | for (let key in mapTestOutput) { |
| 192 | assert.equal(mapTestOutput[key], mapTestInput[key]); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 193 | } |
| 194 | }); |
| 195 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 196 | QUnit.test('StringMap', function(assert) { |
| 197 | const mapTestInput = { |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 198 | 'a': '123', 'a b': 'with spaces ', 'same': 'same', '0': 'numeric key', |
| 199 | 'longValue': stringTest, stringTest: 'long key' |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 202 | const mapTestOutput = client.testStringMap(mapTestInput); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 203 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 204 | for (let key in mapTestOutput) { |
| 205 | assert.equal(mapTestOutput[key], mapTestInput[key]); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 206 | } |
| 207 | }); |
| 208 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 209 | QUnit.test('Set', function(assert) { |
| 210 | const setTestInput = [1, 2, 3]; |
| 211 | assert.ok(client.testSet(setTestInput), setTestInput); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 212 | }); |
| 213 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 214 | QUnit.test('List', function(assert) { |
| 215 | const listTestInput = [1, 2, 3]; |
| 216 | assert.ok(client.testList(listTestInput), listTestInput); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 217 | }); |
| 218 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 219 | QUnit.test('Enum', function(assert) { |
| 220 | assert.equal(client.testEnum(ThriftTest.Numberz.ONE), ThriftTest.Numberz.ONE); |
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('TypeDef', function(assert) { |
| 224 | assert.equal(client.testTypedef(69), 69); |
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('Skip', function(assert) { |
| 228 | const structTestInput = new ThriftTest.Xtruct(); |
| 229 | const modifiedClient = new ThriftTest.ThriftTestClient(protocol); |
Jens Geyer | 329d59a | 2014-06-19 22:11:53 +0200 | [diff] [blame] | 230 | |
| 231 | modifiedClient.recv_testStruct = function() { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 232 | const input = modifiedClient.input; |
| 233 | const xtruct3 = new ThriftTest.Xtruct3(); |
Jens Geyer | 329d59a | 2014-06-19 22:11:53 +0200 | [diff] [blame] | 234 | |
| 235 | input.readMessageBegin(); |
| 236 | input.readStructBegin(); |
| 237 | |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 238 | // read Xtruct data with Xtruct3 |
Jens Geyer | 329d59a | 2014-06-19 22:11:53 +0200 | [diff] [blame] | 239 | 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 Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 253 | structTestInput.byte_thing = 0x01; |
| 254 | structTestInput.i32_thing = Math.pow(2, 30); |
| 255 | structTestInput.i64_thing = Math.pow(2, 52); |
Jens Geyer | 329d59a | 2014-06-19 22:11:53 +0200 | [diff] [blame] | 256 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 257 | const structTestOutput = modifiedClient.testStruct(structTestInput); |
Jens Geyer | 329d59a | 2014-06-19 22:11:53 +0200 | [diff] [blame] | 258 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 259 | 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 Geyer | 329d59a | 2014-06-19 22:11:53 +0200 | [diff] [blame] | 264 | }); |
| 265 | |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 266 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 267 | QUnit.module('deeper!'); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 268 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 269 | QUnit.test('MapMap', function(assert) { |
| 270 | const mapMapTestExpectedResult = { |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 271 | '4': {'1': 1, '2': 2, '3': 3, '4': 4}, |
| 272 | '-4': {'-4': -4, '-3': -3, '-2': -2, '-1': -1} |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 275 | const mapMapTestOutput = client.testMapMap(1); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 276 | |
| 277 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 278 | for (let key in mapMapTestOutput) { |
| 279 | for (let key2 in mapMapTestOutput[key]) { |
| 280 | assert.equal(mapMapTestOutput[key][key2], mapMapTestExpectedResult[key][key2]); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 283 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 284 | checkRecursively(assert, mapMapTestOutput, mapMapTestExpectedResult); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 285 | }); |
| 286 | |
| 287 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 288 | QUnit.module('Exception'); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 289 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 290 | QUnit.test('Xception', function(assert) { |
| 291 | assert.expect(2); |
| 292 | const done = assert.async(); |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 293 | try { |
| 294 | client.testException('Xception'); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 295 | assert.ok(false); |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 296 | }catch (e) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 297 | assert.equal(e.errorCode, 1001); |
| 298 | assert.equal(e.message, 'Xception'); |
| 299 | done(); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 300 | } |
| 301 | }); |
| 302 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 303 | QUnit.test('no Exception', function(assert) { |
| 304 | assert.expect(1); |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 305 | try { |
| 306 | client.testException('no Exception'); |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 307 | assert.ok(true); |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 308 | }catch (e) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 309 | assert.ok(false); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 310 | } |
| 311 | }); |
| 312 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 313 | QUnit.test('TException', function(assert) { |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 314 | //ThriftTest does not list TException as a legal exception so it will |
Roger Meier | 0856273 | 2015-06-14 22:30:22 +0200 | [diff] [blame] | 315 | // generate an exception on the server that does not propagate back to |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 316 | // the client. This test has been modified to equate to "no exception" |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 317 | assert.expect(1); |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 318 | try { |
| 319 | client.testException('TException'); |
| 320 | } catch (e) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 321 | //assert.ok(false); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 322 | } |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 323 | assert.ok(true); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 324 | }); |
| 325 | |
| 326 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 327 | QUnit.module('Insanity'); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 328 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 329 | const crazy = { |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 330 | 'userMap': { '5': 5, '8': 8 }, |
| 331 | 'xtructs': [{ |
| 332 | 'string_thing': 'Goodbye4', |
| 333 | 'byte_thing': 4, |
| 334 | 'i32_thing': 4, |
| 335 | 'i64_thing': 4 |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 336 | }, |
| 337 | { |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 338 | 'string_thing': 'Hello2', |
| 339 | 'byte_thing': 2, |
| 340 | 'i32_thing': 2, |
| 341 | 'i64_thing': 2 |
Nobuaki Sukegawa | 01ede04 | 2015-09-29 02:16:53 +0900 | [diff] [blame] | 342 | }] |
| 343 | }; |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 344 | QUnit.test('testInsanity', function(assert) { |
| 345 | const insanity = { |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 346 | '1': { |
| 347 | '2': crazy, |
| 348 | '3': crazy |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 349 | }, |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 350 | '2': { '6': { 'userMap': null, 'xtructs': null } } |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 351 | }; |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 352 | const res = client.testInsanity(new ThriftTest.Insanity(crazy)); |
| 353 | assert.ok(res, JSON.stringify(res)); |
| 354 | assert.ok(insanity, JSON.stringify(insanity)); |
Roger Meier | 4f8a523 | 2011-09-09 08:17:02 +0000 | [diff] [blame] | 355 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 356 | checkRecursively(assert, res, insanity); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 357 | }); |
| 358 | |
| 359 | |
| 360 | ////////////////////////////////// |
| 361 | //Run same tests asynchronously |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 362 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 363 | QUnit.module('Async'); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 364 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 365 | QUnit.test('Double', function(assert) { |
| 366 | assert.expect(1); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 367 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 368 | const done = assert.async(); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 369 | client.testDouble(3.14159265, function(result) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 370 | assert.equal(result, 3.14159265); |
| 371 | done(); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 372 | }); |
| 373 | }); |
| 374 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 375 | QUnit.test('Byte', function(assert) { |
| 376 | assert.expect(1); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 377 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 378 | const done = assert.async(); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 379 | client.testByte(0x01, function(result) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 380 | assert.equal(result, 0x01); |
| 381 | done(); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 382 | }); |
| 383 | }); |
| 384 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 385 | QUnit.test('I32', function(assert) { |
| 386 | assert.expect(2); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 387 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 388 | const done = assert.async(2); |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 389 | client.testI32(Math.pow(2, 30), function(result) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 390 | assert.equal(result, Math.pow(2, 30)); |
| 391 | done(); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 392 | }); |
| 393 | |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 394 | client.testI32(Math.pow(-2, 31), function(result) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 395 | assert.equal(result, Math.pow(-2, 31)); |
| 396 | done(); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 397 | }); |
| 398 | }); |
| 399 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 400 | QUnit.test('I64', function(assert) { |
| 401 | assert.expect(2); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 402 | |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 403 | const done = assert.async(2); |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 404 | //This is usually 2^60 but JS cannot represent anything over 2^52 accurately |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 405 | client.testI64(Math.pow(2, 52), function(result) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 406 | assert.equal(result, Math.pow(2, 52)); |
| 407 | done(); |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 408 | }); |
| 409 | |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 410 | //This is usually 2^60 but JS cannot represent anything over 2^52 accurately |
Kazuki Matsuda | b909a38 | 2016-02-13 19:36:09 +0900 | [diff] [blame] | 411 | client.testI64(Math.pow(-2, 52), function(result) { |
Brian Forbis | b5d6ea3 | 2018-08-25 23:39:29 -0400 | [diff] [blame^] | 412 | assert.equal(result, Math.pow(-2, 52)); |
| 413 | done(); |
Roger Meier | bb267d4 | 2011-07-30 15:10:42 +0000 | [diff] [blame] | 414 | }); |
| 415 | }); |