henrique | 2a7dccc | 2014-03-07 22:16:51 +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(function(result) { |
| 52 | equal(result, undefined); |
| 53 | QUnit.start(); |
| 54 | }); |
| 55 | }); |
| 56 | |
| 57 | |
| 58 | asyncTest("String", function() { |
| 59 | expect( 3 ); |
| 60 | QUnit.stop(2); |
| 61 | client.testString('', function(result){ |
| 62 | equal(result, ''); |
| 63 | QUnit.start(); |
| 64 | }); |
| 65 | client.testString(stringTest, function(result){ |
| 66 | equal(result, stringTest); |
| 67 | QUnit.start(); |
| 68 | }); |
| 69 | |
| 70 | var specialCharacters = 'quote: \" backslash:' + |
| 71 | ' forwardslash-escaped: \/ ' + |
| 72 | ' backspace: \b formfeed: \f newline: \n return: \r tab: ' + |
| 73 | ' now-all-of-them-together: "\\\/\b\n\r\t' + |
| 74 | ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><'; |
| 75 | client.testString(specialCharacters, function(result){ |
| 76 | equal(result, specialCharacters); |
| 77 | QUnit.start(); |
| 78 | }); |
| 79 | }); |
| 80 | asyncTest("Double", function() { |
| 81 | expect( 4 ); |
| 82 | QUnit.stop(3); |
| 83 | client.testDouble(0, function(result){ |
| 84 | equal(result, 0); |
| 85 | QUnit.start(); |
| 86 | }); |
| 87 | client.testDouble(-1, function(result){ |
| 88 | equal(result, -1); |
| 89 | QUnit.start(); |
| 90 | }); |
| 91 | client.testDouble(3.14, function(result){ |
| 92 | equal(result, 3.14); |
| 93 | QUnit.start(); |
| 94 | }); |
| 95 | client.testDouble(Math.pow(2,60), function(result){ |
| 96 | equal(result, Math.pow(2,60)); |
| 97 | QUnit.start(); |
| 98 | }); |
| 99 | }); |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 100 | // TODO: add testBinary() |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 101 | asyncTest("Byte", function() { |
| 102 | expect( 2 ); |
| 103 | QUnit.stop(); |
| 104 | client.testByte(0, function(result) { |
| 105 | equal(result, 0); |
| 106 | QUnit.start(); |
| 107 | }); |
| 108 | client.testByte(0x01, function(result) { |
| 109 | equal(result, 0x01); |
| 110 | QUnit.start(); |
| 111 | }); |
| 112 | }); |
| 113 | asyncTest("I32", function() { |
| 114 | expect( 3 ); |
| 115 | QUnit.stop(2); |
| 116 | client.testI32(0, function(result){ |
| 117 | equal(result, 0); |
| 118 | QUnit.start(); |
| 119 | }); |
| 120 | client.testI32(Math.pow(2,30), function(result){ |
| 121 | equal(result, Math.pow(2,30)); |
| 122 | QUnit.start(); |
| 123 | }); |
| 124 | client.testI32(-Math.pow(2,30), function(result){ |
| 125 | equal(result, -Math.pow(2,30)); |
| 126 | QUnit.start(); |
| 127 | }); |
| 128 | }); |
| 129 | asyncTest("I64", function() { |
| 130 | expect( 3 ); |
| 131 | QUnit.stop(2); |
| 132 | client.testI64(0, function(result){ |
| 133 | equal(result, 0); |
| 134 | QUnit.start(); |
| 135 | }); |
| 136 | //This is usually 2^60 but JS cannot represent anything over 2^52 accurately |
| 137 | client.testI64(Math.pow(2,52), function(result){ |
| 138 | equal(result, Math.pow(2,52)); |
| 139 | QUnit.start(); |
| 140 | }); |
| 141 | client.testI64(-Math.pow(2,52), function(result){ |
| 142 | equal(result, -Math.pow(2,52)); |
| 143 | QUnit.start(); |
| 144 | }); |
| 145 | }); |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | module("Structured Types"); |
| 151 | |
| 152 | asyncTest("Struct", function() { |
| 153 | expect( 5 ); |
| 154 | var structTestInput = new ThriftTest.Xtruct(); |
| 155 | structTestInput.string_thing = 'worked'; |
| 156 | structTestInput.byte_thing = 0x01; |
| 157 | structTestInput.i32_thing = Math.pow(2,30); |
| 158 | //This is usually 2^60 but JS cannot represent anything over 2^52 accurately |
| 159 | structTestInput.i64_thing = Math.pow(2,52); |
| 160 | |
| 161 | client.testStruct(structTestInput, function(result){ |
| 162 | equal(result.string_thing, structTestInput.string_thing); |
| 163 | equal(result.byte_thing, structTestInput.byte_thing); |
| 164 | equal(result.i32_thing, structTestInput.i32_thing); |
| 165 | equal(result.i64_thing, structTestInput.i64_thing); |
| 166 | equal(JSON.stringify(result), JSON.stringify(structTestInput)); |
| 167 | QUnit.start(); |
| 168 | }); |
| 169 | }); |
| 170 | |
| 171 | asyncTest("Nest", function() { |
| 172 | expect( 7 ); |
| 173 | var xtrTestInput = new ThriftTest.Xtruct(); |
| 174 | xtrTestInput.string_thing = 'worked'; |
| 175 | xtrTestInput.byte_thing = 0x01; |
| 176 | xtrTestInput.i32_thing = Math.pow(2,30); |
| 177 | //This is usually 2^60 but JS cannot represent anything over 2^52 accurately |
| 178 | xtrTestInput.i64_thing = Math.pow(2,52); |
| 179 | |
| 180 | var nestTestInput = new ThriftTest.Xtruct2(); |
| 181 | nestTestInput.byte_thing = 0x02; |
| 182 | nestTestInput.struct_thing = xtrTestInput; |
| 183 | nestTestInput.i32_thing = Math.pow(2,15); |
| 184 | |
| 185 | client.testNest(nestTestInput, function(result){ |
| 186 | equal(result.byte_thing, nestTestInput.byte_thing); |
| 187 | equal(result.struct_thing.string_thing, nestTestInput.struct_thing.string_thing); |
| 188 | equal(result.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing); |
| 189 | equal(result.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing); |
| 190 | equal(result.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing); |
| 191 | equal(result.i32_thing, nestTestInput.i32_thing); |
| 192 | equal(JSON.stringify(result), JSON.stringify(nestTestInput)); |
| 193 | QUnit.start(); |
| 194 | }); |
| 195 | }); |
| 196 | |
| 197 | asyncTest("Map", function() { |
| 198 | expect( 3 ); |
| 199 | var mapTestInput = {7:77, 8:88, 9:99}; |
| 200 | |
| 201 | client.testMap(mapTestInput, function(result){ |
| 202 | for (var key in result) { |
| 203 | equal(result[key], mapTestInput[key]); |
| 204 | } |
| 205 | QUnit.start(); |
| 206 | }); |
| 207 | }); |
| 208 | |
| 209 | asyncTest("StringMap", function() { |
| 210 | expect( 6 ); |
| 211 | var mapTestInput = { |
| 212 | "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key", |
| 213 | "longValue":stringTest, stringTest:"long key" |
| 214 | }; |
| 215 | |
| 216 | client.testStringMap(mapTestInput, function(result){ |
| 217 | for (var key in result) { |
| 218 | equal(result[key], mapTestInput[key]); |
| 219 | } |
| 220 | QUnit.start(); |
| 221 | }); |
| 222 | }); |
| 223 | |
| 224 | asyncTest("Set", function() { |
| 225 | expect( 1 ); |
| 226 | var setTestInput = [1,2,3]; |
| 227 | client.testSet(setTestInput, function(result){ |
| 228 | ok(result, setTestInput); |
| 229 | QUnit.start(); |
| 230 | }); |
| 231 | }); |
| 232 | |
| 233 | asyncTest("List", function() { |
| 234 | expect( 1 ); |
| 235 | var listTestInput = [1,2,3]; |
| 236 | client.testList(listTestInput, function(result){ |
| 237 | ok(result, listTestInput); |
| 238 | QUnit.start(); |
| 239 | }); |
| 240 | }); |
| 241 | |
| 242 | asyncTest("Enum", function() { |
| 243 | expect( 1 ); |
| 244 | client.testEnum(ThriftTest.Numberz.ONE, function(result){ |
| 245 | equal(result, ThriftTest.Numberz.ONE); |
| 246 | QUnit.start(); |
| 247 | }); |
| 248 | }); |
| 249 | |
| 250 | asyncTest("TypeDef", function() { |
| 251 | expect( 1 ); |
| 252 | client.testTypedef(69, function(result){ |
| 253 | equal(result, 69); |
| 254 | QUnit.start(); |
| 255 | }); |
| 256 | }); |
| 257 | |
| 258 | |
| 259 | module("deeper!"); |
| 260 | |
| 261 | asyncTest("MapMap", function() { |
| 262 | expect( 16 ); |
| 263 | var mapMapTestExpectedResult = { |
| 264 | "4":{"1":1,"2":2,"3":3,"4":4}, |
| 265 | "-4":{"-4":-4, "-3":-3, "-2":-2, "-1":-1} |
| 266 | }; |
| 267 | |
| 268 | client.testMapMap(1, function(result){ |
| 269 | for (var key in result) { |
| 270 | for (var key2 in result[key]) { |
| 271 | equal(result[key][key2], mapMapTestExpectedResult[key][key2]); |
| 272 | } |
| 273 | } |
| 274 | checkRecursively(result, mapMapTestExpectedResult); |
| 275 | QUnit.start(); |
| 276 | }); |
| 277 | }); |
| 278 | |
| 279 | |
| 280 | module("Exception"); |
| 281 | |
| 282 | asyncTest("Xception", function() { |
| 283 | expect(2); |
| 284 | client.testException("Xception", function(e){ |
| 285 | equal(e.errorCode, 1001); |
| 286 | equal(e.message, "Xception"); |
| 287 | QUnit.start(); |
| 288 | }); |
| 289 | }); |
| 290 | |
| 291 | asyncTest("no Exception", 0, function() { |
| 292 | expect( 1 ); |
| 293 | client.testException("no Exception", function(e){ |
| 294 | ok(!e); |
| 295 | QUnit.start(); |
| 296 | }); |
| 297 | }); |
| 298 | |
| 299 | module("Insanity"); |
| 300 | |
| 301 | asyncTest("testInsanity", function() { |
| 302 | expect( 24 ); |
| 303 | var insanity = { |
| 304 | "1":{ |
| 305 | "2":{ |
| 306 | "userMap":{ "5":5, "8":8 }, |
| 307 | "xtructs":[{ |
| 308 | "string_thing":"Goodbye4", |
| 309 | "byte_thing":4, |
| 310 | "i32_thing":4, |
| 311 | "i64_thing":4 |
| 312 | }, |
| 313 | { |
| 314 | "string_thing":"Hello2", |
| 315 | "byte_thing":2, |
| 316 | "i32_thing":2, |
| 317 | "i64_thing":2 |
| 318 | } |
| 319 | ] |
| 320 | }, |
| 321 | "3":{ |
| 322 | "userMap":{ "5":5, "8":8 }, |
| 323 | "xtructs":[{ |
| 324 | "string_thing":"Goodbye4", |
| 325 | "byte_thing":4, |
| 326 | "i32_thing":4, |
| 327 | "i64_thing":4 |
| 328 | }, |
| 329 | { |
| 330 | "string_thing":"Hello2", |
| 331 | "byte_thing":2, |
| 332 | "i32_thing":2, |
| 333 | "i64_thing":2 |
| 334 | } |
| 335 | ] |
| 336 | } |
| 337 | }, |
| 338 | "2":{ "6":{ "userMap":null, "xtructs":null } } |
| 339 | }; |
| 340 | client.testInsanity(new ThriftTest.Insanity(), function(res){ |
| 341 | ok(res, JSON.stringify(res)); |
| 342 | ok(insanity, JSON.stringify(insanity)); |
| 343 | checkRecursively(res, insanity); |
| 344 | QUnit.start(); |
| 345 | }); |
| 346 | }); |
| 347 | |
| 348 | |