T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 2 | <!-- |
| 3 | Licensed to the Apache Software Foundation (ASF) under one |
| 4 | or more contributor license agreements. See the NOTICE file |
| 5 | distributed with this work for additional information |
| 6 | regarding copyright ownership. The ASF licenses this file |
| 7 | to you under the Apache License, Version 2.0 (the |
| 8 | "License"); you may not use this file except in compliance |
| 9 | with the License. You may obtain a copy of the License at |
| 10 | |
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | |
| 13 | Unless required by applicable law or agreed to in writing, |
| 14 | software distributed under the License is distributed on an |
| 15 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | KIND, either express or implied. See the License for the |
| 17 | specific language governing permissions and limitations |
| 18 | under the License. |
| 19 | --> |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 20 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 21 | <head> |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 22 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 23 | <title>Thrift Javascript Bindings: Unit Test</title> |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 24 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 25 | <script src="/thrift.js" type="text/javascript" charset="utf-8"></script> |
| 26 | <script src="gen-js/ThriftTest_types.js" type="text/javascript" charset="utf-8"></script> |
| 27 | <script src="gen-js/ThriftTest.js" type="text/javascript" charset="utf-8"></script> |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 28 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 29 | <!-- jQuery --> |
| 30 | <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" charset="utf-8"></script> |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 31 | |
| 32 | <!-- json2 --> |
| 33 | <script type="text/javascript" src="json2.js" charset="utf-8"></script> |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 34 | |
| 35 | <!-- QUnit Test framework--> |
| 36 | <script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js" charset="utf-8"></script> |
| 37 | <link rel="stylesheet" href="http://github.com/jquery/qunit/raw/master/qunit/qunit.css" type="text/css" media="screen" /> |
| 38 | |
| 39 | <script type="text/javascript" charset="utf-8"> |
| 40 | //<![CDATA[ |
| 41 | $(document).ready(function(){ |
| 42 | var transport = new Thrift.Transport("/service") |
| 43 | var protocol = new Thrift.Protocol(transport) |
| 44 | var client = new ThriftTest.ThriftTestClient(protocol) |
| 45 | |
| 46 | |
| 47 | module("Base Types"); |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 48 | |
| 49 | test("Void", function() { |
| 50 | equals(client.testVoid(), undefined); |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 51 | }); |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 52 | test("String", function() { |
| 53 | // all Languages in UTF-8 |
| 54 | 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ú, 粵語"; |
| 55 | equals(client.testString(stringTest), stringTest); |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 56 | }); |
| 57 | test("Double", function() { |
| 58 | equals(client.testDouble(3.14), 3.14); |
| 59 | }); |
| 60 | test("Byte", function() { |
| 61 | equals(client.testByte(0x01), 0x01); |
| 62 | }); |
| 63 | test("I32", function() { |
| 64 | equals(client.testI32(Math.pow(2,30)), Math.pow(2,30)); |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | how to test things like that? |
| 68 | equals(client.testI32(Math.pow(2,60)), Math.pow(2,60)); |
| 69 | */ |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 70 | }); |
| 71 | test("I64", function() { |
| 72 | equals(client.testI64(Math.pow(2,60)), Math.pow(2,60)); |
| 73 | }); |
| 74 | |
| 75 | |
| 76 | module("Structured Types"); |
| 77 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 78 | test("Struct", function() { |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 79 | var structTestInput = new ThriftTest.Xtruct() |
| 80 | structTestInput.string_thing = 'worked' |
| 81 | structTestInput.byte_thing = 0x01 |
| 82 | structTestInput.i32_thing = Math.pow(2,30) |
| 83 | structTestInput.i64_thing = Math.pow(2,60) |
| 84 | |
| 85 | var structTestOutput = client.testStruct(structTestInput); |
| 86 | |
| 87 | equals(structTestOutput.string_thing, structTestInput.string_thing); |
| 88 | equals(structTestOutput.byte_thing, structTestInput.byte_thing); |
| 89 | equals(structTestOutput.i32_thing, structTestInput.i32_thing); |
| 90 | equals(structTestOutput.i64_thing, structTestInput.i64_thing); |
| 91 | |
| 92 | equals(JSON.stringify(structTestOutput), JSON.stringify(structTestInput)) |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 93 | }); |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 94 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 95 | test("Nest", function() { |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 96 | var xtrTestInput = new ThriftTest.Xtruct() |
| 97 | xtrTestInput.string_thing = 'worked' |
| 98 | xtrTestInput.byte_thing = 0x01 |
| 99 | xtrTestInput.i32_thing = Math.pow(2,30) |
| 100 | xtrTestInput.i64_thing = Math.pow(2,60) |
| 101 | |
| 102 | var nestTestInput = new ThriftTest.Xtruct2() |
| 103 | nestTestInput.byte_thing = 0x02 |
| 104 | nestTestInput.struct_thing = xtrTestInput |
| 105 | nestTestInput.i32_thing = Math.pow(2,15) |
| 106 | |
| 107 | var nestTestOutput = client.testNest(nestTestInput); |
| 108 | |
| 109 | equals(nestTestOutput.byte_thing, nestTestInput.byte_thing); |
| 110 | equals(nestTestOutput.struct_thing.string_thing, nestTestInput.struct_thing.string_thing); |
| 111 | equals(nestTestOutput.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing); |
| 112 | equals(nestTestOutput.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing); |
| 113 | equals(nestTestOutput.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing); |
| 114 | equals(nestTestOutput.i32_thing, nestTestInput.i32_thing); |
| 115 | |
| 116 | equals(JSON.stringify(nestTestOutput), JSON.stringify(nestTestInput)) |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 117 | }); |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 118 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 119 | test("Map", function() { |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 120 | var mapTestInput = {7:77, 8:88, 9:99}; |
| 121 | |
| 122 | var mapTestOutput = client.testMap(mapTestInput) |
| 123 | |
| 124 | for (var key in mapTestOutput) { |
| 125 | equals(mapTestOutput[key], mapTestInput[key]); |
| 126 | } |
| 127 | |
| 128 | equals(JSON.stringify(mapTestOutput), JSON.stringify(mapTestInput)) |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 129 | }); |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 130 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 131 | test("Set", function() { |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 132 | var setTestInput = new Array(1,2,3) |
| 133 | ok(client.testSet(setTestInput), setTestInput); |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 134 | }); |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 135 | |
| 136 | test("List", function() { |
| 137 | var listTestInput = new Array(1,2,3) |
| 138 | ok(client.testList(listTestInput), listTestInput); |
| 139 | }); |
| 140 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 141 | test("Enum", function() { |
| 142 | equals(client.testEnum(ThriftTest.Numberz.ONE), ThriftTest.Numberz.ONE); |
| 143 | }); |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 144 | |
| 145 | test("TypeDef", function() { |
| 146 | equals(client.testTypedef(69), 69); |
| 147 | }); |
| 148 | |
| 149 | module("deeper!"); |
| 150 | |
| 151 | test("MapMap", function() { |
| 152 | var mapMapTestExpectedResult = { |
| 153 | "-4":{"-4":-4, "-3":-3, "-2":-2, "-1":-1}, |
| 154 | "4":{"1":1,"2":2,"3":3,"4":4} |
| 155 | }; |
| 156 | |
| 157 | var mapMapTestOutput = client.testMapMap(1); |
| 158 | |
| 159 | equals(JSON.stringify(mapMapTestOutput), JSON.stringify(mapMapTestExpectedResult)) |
| 160 | }); |
| 161 | |
| 162 | test("testInsanity", function() { |
| 163 | var insanityTestOutput; |
| 164 | var insanityTestInput; |
| 165 | |
| 166 | // map<UserId, map<Numberz,Insanity>> testInsanity(1: Insanity argument), |
| 167 | |
| 168 | var insanity = new ThriftTest.Insanity() |
| 169 | insanity.userMap[ThriftTest.Numberz.ONE] = 1 |
| 170 | insanity.userMap[ThriftTest.Numberz.TWO] = 2 |
| 171 | |
| 172 | |
| 173 | insanityTestOutput = client.testInsanity(insanity); |
| 174 | equals(JSON.stringify(insanityTestOutput), JSON.stringify(insanityTestInput)) |
| 175 | /* |
| 176 | request: |
| 177 | [1,"testInsanity",1,0,{"1":{"rec":{"1":{"map":["i32","i64",2,{"1":1,"2":2}]},"2":{"lst":["rec",0]}}}}] |
| 178 | response: |
| 179 | [1,"testInsanity",2,0,{"0":{"map":["i64","map",2,{"1":["i32","rec",2,{"2":{"1":{"map":["i32","i64",2,{"5":5,"8":8}]},"2":{"lst":["rec",2,{"1":{"str":"Goodbye4"},"4":{"i8":4},"9":{"i32":4},"11":{"i64":4}},{"1":{"str":"Hello2"},"4":{"i8":2},"9":{"i32":2},"11":{"i64":2}}]}},"3":{"1":{"map":["i32","i64",2,{"5":5,"8":8}]},"2":{"lst":["rec",2,{"1":{"str":"Goodbye4"},"4":{"i8":4},"9":{"i32":4},"11":{"i64":4}},{"1":{"str":"Hello2"},"4":{"i8":2},"9":{"i32":2},"11":{"i64":2}}]}}}],"2":["i32","rec",1,{"6":{"1":{"map":["i32","i64",0,{}]},"2":{"lst":["rec",0]}}}]}]}}] |
| 180 | */ |
| 181 | //equals(res["1"]["1"].xtructs[0].string_thing, "worked"); |
| 182 | }); |
| 183 | test("testMulti", function() { |
| 184 | //var res = client.testInsanity(insanity); |
| 185 | //equals(res["1"]["1"].xtructs[0].string_thing, "worked"); |
| 186 | }); |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 187 | |
| 188 | |
| 189 | module("Exception"); |
| 190 | |
| 191 | test("Xception", function() { |
| 192 | expect(2); |
| 193 | try{ |
| 194 | client.testException("Xception"); |
| 195 | }catch(e){ |
| 196 | equals(e.errorCode, 1); |
| 197 | equals(e.message, "Xception"); |
| 198 | } |
| 199 | }); |
| 200 | |
| 201 | test("ApplicationException", function() { |
| 202 | expect(1); |
| 203 | try{ |
| 204 | client.testException("ApplicationException"); |
| 205 | }catch(e){ |
| 206 | equals(e.message, "ApplicationException"); |
| 207 | } |
| 208 | }); |
| 209 | |
| 210 | test("no Exception", function() { |
| 211 | expect(1); |
| 212 | try{ |
| 213 | client.testException("no Exception"); |
| 214 | }catch(e){ |
| 215 | equals(e.message, "no Exception"); |
| 216 | } |
| 217 | }); |
| 218 | |
| 219 | |
| 220 | module("Insanity"); |
| 221 | |
| 222 | test("testInsanity", function() { |
| 223 | var res = client.testInsanity(insanity); |
| 224 | equals(res["1"]["1"].xtructs[0].string_thing, "worked"); |
| 225 | }); |
| 226 | |
| 227 | }); |
| 228 | //]]> |
| 229 | </script> |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 230 | |
| 231 | </head> |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 232 | <body> |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 233 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 234 | <script type="text/javascript" charset="utf-8"> |
| 235 | //<![CDATA[ |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 236 | |
| 237 | ////////////////////////////////// |
| 238 | //Run same tests asynchronously |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 239 | /* |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 240 | var transport = new Thrift.Transport() |
| 241 | var protocol = new Thrift.Protocol(transport) |
| 242 | var client = new ThriftTest.ThriftTestClient(protocol) |
| 243 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 244 | document.write("<h2>Asynchronous Example<\/h2>") |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 245 | jQuery.ajax({ |
| 246 | url: "/service", |
| 247 | data: client.send_testI32(Math.pow(2,30)), |
| 248 | type: "POST", |
| 249 | cache: false, |
| 250 | success: function(res){ |
| 251 | var _transport = new Thrift.Transport() |
| 252 | var _protocol = new Thrift.Protocol(_transport) |
| 253 | var _client = new ThriftTest.ThriftTestClient(_protocol) |
| 254 | |
| 255 | _transport.setRecvBuffer( res ) |
| 256 | |
| 257 | var v = _client.recv_testI32() |
| 258 | $("#body").append("client.testI32() => "+(v == Math.pow(2,30))+"<br/>") |
| 259 | |
| 260 | } |
| 261 | }) |
| 262 | |
| 263 | jQuery.ajax({ |
| 264 | url: "/service", |
| 265 | data: client.send_testI64(Math.pow(2,60)), |
| 266 | type: "POST", |
| 267 | cache: false, |
| 268 | success: function(res){ |
| 269 | var _transport = new Thrift.Transport() |
| 270 | var _protocol = new Thrift.Protocol(_transport) |
| 271 | var _client = new ThriftTest.ThriftTestClient(_protocol) |
| 272 | |
| 273 | _transport.setRecvBuffer( res ) |
| 274 | |
| 275 | var v = _client.recv_testI64() |
| 276 | $("#body").append("client.testI64() => "+(v == Math.pow(2,60))+"<br/>") |
| 277 | |
| 278 | } |
| 279 | }) |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 280 | */ |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 281 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 282 | //]]> |
| 283 | </script> |
| 284 | <h1 id="qunit-header">Thrift Javascript Bindings: Unit Test (<a href="https://svn.apache.org/repos/asf/incubator/thrift/trunk/test/ThriftTest.thrift">ThriftTest.thrift</a>)</h1> |
| 285 | <h2 id="qunit-banner"></h2> |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 286 | <div id="qunit-testrunner-toolbar"></div> |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 287 | <h2 id="qunit-userAgent"></h2> |
| 288 | <ol id="qunit-tests"><li><!-- get valid xhtml strict--></li></ol> |
| 289 | <p> |
| 290 | <a href="http://validator.w3.org/check/referer"><img |
| 291 | src="http://www.w3.org/Icons/valid-xhtml10" |
| 292 | alt="Valid XHTML 1.0!" height="31" width="88" /></a> |
| 293 | </p> |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 294 | </body> |
| 295 | </html> |