Philip Frank | 8fdd610 | 2017-12-06 12:38:05 +0100 | [diff] [blame] | 1 | /* |
| 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 |
| 33 | var 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 | |
| 35 | function 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 | |
| 47 | module('Base Types'); |
| 48 | |
| 49 | asyncTest('Void', function() { |
| 50 | expect(1); |
| 51 | client.testVoid().then(function(result) { |
| 52 | equal(result, undefined); |
| 53 | QUnit.start(); |
| 54 | }); |
| 55 | }); |
| 56 | |
| 57 | asyncTest('String', function() { |
| 58 | expect(3); |
| 59 | QUnit.stop(2); |
| 60 | client.testString('').then(function(result) { |
| 61 | equal(result, ''); |
| 62 | QUnit.start(); |
| 63 | }); |
| 64 | client.testString(stringTest).then(function(result) { |
| 65 | equal(result, stringTest); |
| 66 | QUnit.start(); |
| 67 | }); |
| 68 | var specialCharacters = 'quote: \" backslash:' + |
| 69 | ' forwardslash-escaped: \/ ' + |
| 70 | ' backspace: \b formfeed: \f newline: \n return: \r tab: ' + |
| 71 | ' now-all-of-them-together: "\\\/\b\n\r\t' + |
| 72 | ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><'; |
| 73 | client.testString(specialCharacters).then(function(result) { |
| 74 | equal(result, specialCharacters); |
| 75 | QUnit.start(); |
| 76 | }); |
| 77 | }); |
| 78 | |
| 79 | asyncTest('Double', function() { |
| 80 | expect(4); |
| 81 | QUnit.stop(3); |
| 82 | client.testDouble(0).then(function(result) { |
| 83 | equal(result, 0); |
| 84 | QUnit.start(); |
| 85 | }); |
| 86 | client.testDouble(-1).then(function(result) { |
| 87 | equal(result, -1); |
| 88 | QUnit.start(); |
| 89 | }); |
| 90 | client.testDouble(3.14).then(function(result) { |
| 91 | equal(result, 3.14); |
| 92 | QUnit.start(); |
| 93 | }); |
| 94 | client.testDouble(Math.pow(2, 60)).then(function(result) { |
| 95 | equal(result, Math.pow(2, 60)); |
| 96 | QUnit.start(); |
| 97 | }); |
| 98 | }); |
| 99 | // TODO: add testBinary() |
| 100 | asyncTest('Byte', function() { |
| 101 | expect(2); |
| 102 | QUnit.stop(); |
| 103 | client.testByte(0).then(function(result) { |
| 104 | equal(result, 0); |
| 105 | QUnit.start(); |
| 106 | }); |
| 107 | client.testByte(0x01).then(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).then(function(result) { |
| 116 | equal(result, 0); |
| 117 | QUnit.start(); |
| 118 | }); |
| 119 | client.testI32(Math.pow(2, 30)).then(function(result) { |
| 120 | equal(result, Math.pow(2, 30)); |
| 121 | QUnit.start(); |
| 122 | }); |
| 123 | client.testI32(-Math.pow(2, 30)).then(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).then(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)).then(function(result) { |
| 137 | equal(result, Math.pow(2, 52)); |
| 138 | QUnit.start(); |
| 139 | }); |
| 140 | client.testI64(-Math.pow(2, 52)).then(function(result) { |
| 141 | equal(result, -Math.pow(2, 52)); |
| 142 | QUnit.start(); |
| 143 | }); |
| 144 | }); |
| 145 | |
| 146 | |
| 147 | module('Structured Types'); |
| 148 | |
| 149 | asyncTest('Struct', function() { |
| 150 | expect(5); |
| 151 | var structTestInput = new ThriftTest.Xtruct(); |
| 152 | structTestInput.string_thing = 'worked'; |
| 153 | structTestInput.byte_thing = 0x01; |
| 154 | structTestInput.i32_thing = Math.pow(2, 30); |
| 155 | //This is usually 2^60 but JS cannot represent anything over 2^52 accurately |
| 156 | structTestInput.i64_thing = Math.pow(2, 52); |
| 157 | |
| 158 | client.testStruct(structTestInput).then(function(result) { |
| 159 | equal(result.string_thing, structTestInput.string_thing); |
| 160 | equal(result.byte_thing, structTestInput.byte_thing); |
| 161 | equal(result.i32_thing, structTestInput.i32_thing); |
| 162 | equal(result.i64_thing, structTestInput.i64_thing); |
| 163 | equal(JSON.stringify(result), JSON.stringify(structTestInput)); |
| 164 | QUnit.start(); |
| 165 | }); |
| 166 | }); |
| 167 | |
| 168 | asyncTest('Nest', function() { |
| 169 | expect(7); |
| 170 | var xtrTestInput = new ThriftTest.Xtruct(); |
| 171 | xtrTestInput.string_thing = 'worked'; |
| 172 | xtrTestInput.byte_thing = 0x01; |
| 173 | xtrTestInput.i32_thing = Math.pow(2, 30); |
| 174 | //This is usually 2^60 but JS cannot represent anything over 2^52 accurately |
| 175 | xtrTestInput.i64_thing = Math.pow(2, 52); |
| 176 | |
| 177 | var nestTestInput = new ThriftTest.Xtruct2(); |
| 178 | nestTestInput.byte_thing = 0x02; |
| 179 | nestTestInput.struct_thing = xtrTestInput; |
| 180 | nestTestInput.i32_thing = Math.pow(2, 15); |
| 181 | |
| 182 | client.testNest(nestTestInput).then(function(result) { |
| 183 | equal(result.byte_thing, nestTestInput.byte_thing); |
| 184 | equal(result.struct_thing.string_thing, nestTestInput.struct_thing.string_thing); |
| 185 | equal(result.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing); |
| 186 | equal(result.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing); |
| 187 | equal(result.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing); |
| 188 | equal(result.i32_thing, nestTestInput.i32_thing); |
| 189 | equal(JSON.stringify(result), JSON.stringify(nestTestInput)); |
| 190 | QUnit.start(); |
| 191 | }); |
| 192 | }); |
| 193 | |
| 194 | asyncTest('Map', function() { |
| 195 | expect(3); |
| 196 | var mapTestInput = {7: 77, 8: 88, 9: 99}; |
| 197 | |
| 198 | client.testMap(mapTestInput).then(function(result) { |
| 199 | for (var key in result) { |
| 200 | equal(result[key], mapTestInput[key]); |
| 201 | } |
| 202 | QUnit.start(); |
| 203 | }); |
| 204 | }); |
| 205 | |
| 206 | asyncTest('StringMap', function() { |
| 207 | expect(6); |
| 208 | var mapTestInput = { |
| 209 | 'a': '123', 'a b': 'with spaces ', 'same': 'same', '0': 'numeric key', |
| 210 | 'longValue': stringTest, stringTest: 'long key' |
| 211 | }; |
| 212 | |
| 213 | client.testStringMap(mapTestInput).then(function(result) { |
| 214 | for (var key in result) { |
| 215 | equal(result[key], mapTestInput[key]); |
| 216 | } |
| 217 | QUnit.start(); |
| 218 | }); |
| 219 | }); |
| 220 | |
| 221 | asyncTest('Set', function() { |
| 222 | expect(1); |
| 223 | var setTestInput = [1, 2, 3]; |
| 224 | client.testSet(setTestInput).then(function(result) { |
| 225 | ok(result, setTestInput); |
| 226 | QUnit.start(); |
| 227 | }); |
| 228 | }); |
| 229 | |
| 230 | asyncTest('List', function() { |
| 231 | expect(1); |
| 232 | var listTestInput = [1, 2, 3]; |
| 233 | client.testList(listTestInput).then(function(result) { |
| 234 | ok(result, listTestInput); |
| 235 | QUnit.start(); |
| 236 | }); |
| 237 | }); |
| 238 | |
| 239 | asyncTest('Enum', function() { |
| 240 | expect(1); |
| 241 | client.testEnum(ThriftTest.Numberz.ONE).then(function(result) { |
| 242 | equal(result, ThriftTest.Numberz.ONE); |
| 243 | QUnit.start(); |
| 244 | }); |
| 245 | }); |
| 246 | |
| 247 | asyncTest('TypeDef', function() { |
| 248 | expect(1); |
| 249 | client.testTypedef(69).then(function(result) { |
| 250 | equal(result, 69); |
| 251 | QUnit.start(); |
| 252 | }); |
| 253 | }); |
| 254 | |
| 255 | |
| 256 | module('deeper!'); |
| 257 | |
| 258 | asyncTest('MapMap', function() { |
| 259 | expect(16); |
| 260 | var mapMapTestExpectedResult = { |
| 261 | '4': {'1': 1, '2': 2, '3': 3, '4': 4}, |
| 262 | '-4': {'-4': -4, '-3': -3, '-2': -2, '-1': -1} |
| 263 | }; |
| 264 | |
| 265 | client.testMapMap(1).then(function(result) { |
| 266 | for (var key in result) { |
| 267 | for (var key2 in result[key]) { |
| 268 | equal(result[key][key2], mapMapTestExpectedResult[key][key2]); |
| 269 | } |
| 270 | } |
| 271 | checkRecursively(result, mapMapTestExpectedResult); |
| 272 | QUnit.start(); |
| 273 | }); |
| 274 | }); |
| 275 | |
| 276 | |
| 277 | module('Exception'); |
| 278 | |
| 279 | asyncTest('Xception', function() { |
| 280 | expect(2); |
| 281 | client.testException('Xception').then(function(res) { |
| 282 | ok(false); |
| 283 | }).catch(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').then(function(e) { |
| 293 | ok(!e); |
| 294 | QUnit.start(); |
| 295 | }); |
| 296 | }); |
| 297 | |
| 298 | module('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()).then(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 | module('Oneway'); |
| 348 | asyncTest('testOneway', function() { |
| 349 | expect(1); |
| 350 | client.testOneway(1).then(function(result) { |
| 351 | equal(result, undefined); |
| 352 | QUnit.start(); |
| 353 | }); |
| 354 | }); |