Roger Meier | b3b07d6 | 2011-02-22 20:55:21 +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); |
Roger Meier | c991270 | 2010-10-15 13:16:05 +0000 | [diff] [blame] | 56 | |
| 57 | var specialCharacters = 'quote: \" backslash:' + |
| 58 | ' forwardslash-escaped: \/ ' + |
| 59 | ' backspace: \b formfeed: \f newline: \n return: \r tab: ' + |
| 60 | ' now-all-of-them-together: "\\\/\b\n\r\t' + |
| 61 | ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><'; |
| 62 | equals(client.testString(specialCharacters),specialCharacters); |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 63 | }); |
| 64 | test("Double", function() { |
| 65 | equals(client.testDouble(3.14), 3.14); |
| 66 | }); |
| 67 | test("Byte", function() { |
| 68 | equals(client.testByte(0x01), 0x01); |
| 69 | }); |
| 70 | test("I32", function() { |
| 71 | equals(client.testI32(Math.pow(2,30)), Math.pow(2,30)); |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | how to test things like that? |
| 75 | equals(client.testI32(Math.pow(2,60)), Math.pow(2,60)); |
| 76 | */ |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 77 | }); |
| 78 | test("I64", function() { |
| 79 | equals(client.testI64(Math.pow(2,60)), Math.pow(2,60)); |
| 80 | }); |
| 81 | |
| 82 | |
| 83 | module("Structured Types"); |
| 84 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 85 | test("Struct", function() { |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 86 | var structTestInput = new ThriftTest.Xtruct() |
| 87 | structTestInput.string_thing = 'worked' |
| 88 | structTestInput.byte_thing = 0x01 |
| 89 | structTestInput.i32_thing = Math.pow(2,30) |
| 90 | structTestInput.i64_thing = Math.pow(2,60) |
| 91 | |
| 92 | var structTestOutput = client.testStruct(structTestInput); |
| 93 | |
| 94 | equals(structTestOutput.string_thing, structTestInput.string_thing); |
| 95 | equals(structTestOutput.byte_thing, structTestInput.byte_thing); |
| 96 | equals(structTestOutput.i32_thing, structTestInput.i32_thing); |
| 97 | equals(structTestOutput.i64_thing, structTestInput.i64_thing); |
| 98 | |
| 99 | equals(JSON.stringify(structTestOutput), JSON.stringify(structTestInput)) |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 100 | }); |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 101 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 102 | test("Nest", function() { |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 103 | var xtrTestInput = new ThriftTest.Xtruct() |
| 104 | xtrTestInput.string_thing = 'worked' |
| 105 | xtrTestInput.byte_thing = 0x01 |
| 106 | xtrTestInput.i32_thing = Math.pow(2,30) |
| 107 | xtrTestInput.i64_thing = Math.pow(2,60) |
| 108 | |
| 109 | var nestTestInput = new ThriftTest.Xtruct2() |
| 110 | nestTestInput.byte_thing = 0x02 |
| 111 | nestTestInput.struct_thing = xtrTestInput |
| 112 | nestTestInput.i32_thing = Math.pow(2,15) |
| 113 | |
| 114 | var nestTestOutput = client.testNest(nestTestInput); |
| 115 | |
| 116 | equals(nestTestOutput.byte_thing, nestTestInput.byte_thing); |
| 117 | equals(nestTestOutput.struct_thing.string_thing, nestTestInput.struct_thing.string_thing); |
| 118 | equals(nestTestOutput.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing); |
| 119 | equals(nestTestOutput.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing); |
| 120 | equals(nestTestOutput.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing); |
| 121 | equals(nestTestOutput.i32_thing, nestTestInput.i32_thing); |
| 122 | |
| 123 | equals(JSON.stringify(nestTestOutput), JSON.stringify(nestTestInput)) |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 124 | }); |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 125 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 126 | test("Map", function() { |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 127 | var mapTestInput = {7:77, 8:88, 9:99}; |
| 128 | |
| 129 | var mapTestOutput = client.testMap(mapTestInput) |
| 130 | |
| 131 | for (var key in mapTestOutput) { |
| 132 | equals(mapTestOutput[key], mapTestInput[key]); |
| 133 | } |
| 134 | |
| 135 | equals(JSON.stringify(mapTestOutput), JSON.stringify(mapTestInput)) |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 136 | }); |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 137 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 138 | test("Set", function() { |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 139 | var setTestInput = new Array(1,2,3) |
| 140 | ok(client.testSet(setTestInput), setTestInput); |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 141 | }); |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 142 | |
| 143 | test("List", function() { |
| 144 | var listTestInput = new Array(1,2,3) |
| 145 | ok(client.testList(listTestInput), listTestInput); |
| 146 | }); |
| 147 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 148 | test("Enum", function() { |
| 149 | equals(client.testEnum(ThriftTest.Numberz.ONE), ThriftTest.Numberz.ONE); |
| 150 | }); |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 151 | |
| 152 | test("TypeDef", function() { |
| 153 | equals(client.testTypedef(69), 69); |
| 154 | }); |
| 155 | |
| 156 | module("deeper!"); |
| 157 | |
| 158 | test("MapMap", function() { |
| 159 | var mapMapTestExpectedResult = { |
| 160 | "-4":{"-4":-4, "-3":-3, "-2":-2, "-1":-1}, |
| 161 | "4":{"1":1,"2":2,"3":3,"4":4} |
| 162 | }; |
| 163 | |
| 164 | var mapMapTestOutput = client.testMapMap(1); |
| 165 | |
| 166 | equals(JSON.stringify(mapMapTestOutput), JSON.stringify(mapMapTestExpectedResult)) |
| 167 | }); |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 168 | |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 169 | test("testMulti", function() { |
| 170 | //var res = client.testInsanity(insanity); |
| 171 | //equals(res["1"]["1"].xtructs[0].string_thing, "worked"); |
| 172 | }); |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 173 | |
| 174 | |
| 175 | module("Exception"); |
| 176 | |
| 177 | test("Xception", function() { |
| 178 | expect(2); |
| 179 | try{ |
| 180 | client.testException("Xception"); |
| 181 | }catch(e){ |
Roger Meier | 37b5bf8 | 2010-10-24 21:41:24 +0000 | [diff] [blame] | 182 | equals(e.errorCode, 1001); |
| 183 | equals(e.message, "This is an Xception"); |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 184 | } |
| 185 | }); |
| 186 | |
| 187 | test("no Exception", function() { |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 188 | try{ |
| 189 | client.testException("no Exception"); |
| 190 | }catch(e){ |
Roger Meier | b3b07d6 | 2011-02-22 20:55:21 +0000 | [diff] [blame] | 191 | ok(false); |
| 192 | } |
| 193 | }); |
| 194 | |
| 195 | test("unexpected Application Exception", function() { |
| 196 | expect(1); |
| 197 | try{ |
| 198 | client.testException("ApplicationException"); |
| 199 | } catch(e) { |
| 200 | ok(true); |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 201 | } |
| 202 | }); |
| 203 | |
| 204 | |
| 205 | module("Insanity"); |
| 206 | |
| 207 | test("testInsanity", function() { |
Roger Meier | b3b07d6 | 2011-02-22 20:55:21 +0000 | [diff] [blame] | 208 | var insanity; |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 209 | var res = client.testInsanity(insanity); |
Roger Meier | b3b07d6 | 2011-02-22 20:55:21 +0000 | [diff] [blame] | 210 | equals(res["1"]["2"].xtructs[0].string_thing, "Goodbye4"); |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 211 | }); |
| 212 | |
| 213 | }); |
| 214 | //]]> |
| 215 | </script> |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 216 | |
| 217 | </head> |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 218 | <body> |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 219 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 220 | <script type="text/javascript" charset="utf-8"> |
| 221 | //<![CDATA[ |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 222 | |
| 223 | ////////////////////////////////// |
| 224 | //Run same tests asynchronously |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 225 | /* |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 226 | var transport = new Thrift.Transport() |
| 227 | var protocol = new Thrift.Protocol(transport) |
| 228 | var client = new ThriftTest.ThriftTestClient(protocol) |
| 229 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 230 | document.write("<h2>Asynchronous Example<\/h2>") |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 231 | jQuery.ajax({ |
| 232 | url: "/service", |
| 233 | data: client.send_testI32(Math.pow(2,30)), |
| 234 | type: "POST", |
| 235 | cache: false, |
| 236 | success: function(res){ |
| 237 | var _transport = new Thrift.Transport() |
| 238 | var _protocol = new Thrift.Protocol(_transport) |
| 239 | var _client = new ThriftTest.ThriftTestClient(_protocol) |
| 240 | |
| 241 | _transport.setRecvBuffer( res ) |
| 242 | |
| 243 | var v = _client.recv_testI32() |
| 244 | $("#body").append("client.testI32() => "+(v == Math.pow(2,30))+"<br/>") |
| 245 | |
| 246 | } |
| 247 | }) |
| 248 | |
| 249 | jQuery.ajax({ |
| 250 | url: "/service", |
| 251 | data: client.send_testI64(Math.pow(2,60)), |
| 252 | type: "POST", |
| 253 | cache: false, |
| 254 | success: function(res){ |
| 255 | var _transport = new Thrift.Transport() |
| 256 | var _protocol = new Thrift.Protocol(_transport) |
| 257 | var _client = new ThriftTest.ThriftTestClient(_protocol) |
| 258 | |
| 259 | _transport.setRecvBuffer( res ) |
| 260 | |
| 261 | var v = _client.recv_testI64() |
| 262 | $("#body").append("client.testI64() => "+(v == Math.pow(2,60))+"<br/>") |
| 263 | |
| 264 | } |
| 265 | }) |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 266 | */ |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 267 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 268 | //]]> |
| 269 | </script> |
Roger Meier | 5aaa021 | 2010-11-04 12:20:05 +0000 | [diff] [blame] | 270 | <h1 id="qunit-header">Thrift Javascript Bindings: Unit Test (<a href="https://svn.apache.org/repos/asf/thrift/trunk/test/ThriftTest.thrift">ThriftTest.thrift</a>)</h1> |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 271 | <h2 id="qunit-banner"></h2> |
Bryan Duxbury | 3ff373e | 2010-08-15 23:19:06 +0000 | [diff] [blame] | 272 | <div id="qunit-testrunner-toolbar"></div> |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 273 | <h2 id="qunit-userAgent"></h2> |
| 274 | <ol id="qunit-tests"><li><!-- get valid xhtml strict--></li></ol> |
| 275 | <p> |
| 276 | <a href="http://validator.w3.org/check/referer"><img |
| 277 | src="http://www.w3.org/Icons/valid-xhtml10" |
| 278 | alt="Valid XHTML 1.0!" height="31" width="88" /></a> |
| 279 | </p> |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 280 | </body> |
| 281 | </html> |