THRIFT-4406: Optionally generate ES6 compatible JavaScript where service methods return Promises
includes tests and TypeScript support
Client: js

This closes #1452
diff --git a/compiler/cpp/src/thrift/generate/t_js_generator.cc b/compiler/cpp/src/thrift/generate/t_js_generator.cc
index f45ef57..269e46a 100644
--- a/compiler/cpp/src/thrift/generate/t_js_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_js_generator.cc
@@ -58,6 +58,7 @@
     gen_node_ = false;
     gen_jquery_ = false;
     gen_ts_ = false;
+    gen_es6_ = false;
 
     bool with_ns_ = false;
 
@@ -70,6 +71,8 @@
         gen_ts_ = true;
       } else if( iter->first.compare("with_ns") == 0) {
         with_ns_ = true;
+      } else if( iter->first.compare("es6") == 0) {
+        gen_es6_ = true;
       } else {
         throw "unknown option js:" + iter->first;
       }
@@ -79,6 +82,10 @@
       throw "Invalid switch: [-gen js:node,ts] options not compatible";
     }
 
+    if (gen_es6_ && gen_jquery_) {
+      throw "Invalid switch: [-gen js:es6,jquery] options not compatible";
+    }
+
     if (gen_node_ && gen_jquery_) {
       throw "Invalid switch: [-gen js:node,jquery] options not compatible, try: [-gen js:node -gen "
             "js:jquery]";
@@ -328,6 +335,11 @@
   bool gen_ts_;
 
   /**
+   * True if we should generate ES6 code, i.e. with Promises
+   */
+  bool gen_es6_;
+
+  /**
    * The name of the defined module(s), for TypeScript Definition Files.
    */
   string ts_module_;
@@ -370,7 +382,7 @@
   // Print header
   f_types_ << autogen_comment();
 
-  if (gen_node_ && no_ns_) {
+  if ((gen_node_ || gen_es6_) && no_ns_) {
     f_types_ << "\"use strict\";" << endl << endl;
   }
 
@@ -408,10 +420,13 @@
  */
 string t_js_generator::js_includes() {
   if (gen_node_) {
-    return string(
+    string result = string(
         "var thrift = require('thrift');\n"
-        "var Thrift = thrift.Thrift;\n"
-        "var Q = thrift.Q;\n");
+        "var Thrift = thrift.Thrift;\n");
+    if (!gen_es6_) {
+      result += "var Q = thrift.Q;\n";
+    }
+    return result;
   }
 
   return "";
@@ -965,7 +980,7 @@
 
   f_service_ << autogen_comment();
 
-  if (gen_node_ && no_ns_) {
+  if ((gen_node_ || gen_es6_) && no_ns_) {
     f_service_ << "\"use strict\";" << endl << endl;
   }
 
@@ -1373,19 +1388,42 @@
 
     // Open function
     f_service_ << js_namespace(tservice->get_program()) << service_name_ << "Client.prototype."
-               << function_signature(*f_iter, "", true) << " {" << endl;
+               << function_signature(*f_iter, "", !gen_es6_) << " {" << endl;
 
     indent_up();
 
     if (gen_ts_) {
-      f_service_ts_ << ts_print_doc(*f_iter) <<
-          // function definition without callback
-          ts_indent() << ts_function_signature(*f_iter, false) << endl << ts_print_doc(*f_iter) <<
-          // overload with callback
-          ts_indent() << ts_function_signature(*f_iter, true) << endl;
+      // function definition without callback
+      f_service_ts_ << ts_print_doc(*f_iter) << ts_indent() << ts_function_signature(*f_iter, false) << endl;
+
+      if (!gen_es6_) {
+        // overload with callback
+        f_service_ts_ << ts_print_doc(*f_iter) << ts_indent() << ts_function_signature(*f_iter, true) << endl;
+      }
     }
 
-    if (gen_node_) { // Node.js output      ./gen-nodejs
+    if (gen_es6_ && gen_node_) {
+      f_service_ << indent() << "this._seqid = this.new_seqid();" << endl;
+      f_service_ << indent() << "var self = this;" << endl << indent()
+                 << "return new Promise(function(resolve, reject) {" << endl;
+      indent_up();
+      f_service_ << indent() << "self._reqs[self.seqid()] = function(error, result) {" << endl;
+      indent_up();
+      indent(f_service_) << "if (error) {" << endl;
+      indent_up();
+      indent(f_service_) << "reject(error);" << endl;
+      indent_down();
+      indent(f_service_) << "} else {" << endl;
+      indent_up();
+      indent(f_service_) << "resolve(result);" << endl;
+      indent_down();
+      indent(f_service_) << "}" << endl;
+      indent_down();
+      indent(f_service_) << "};" << endl;
+      f_service_ << indent() << "self.send_" << funname << "(" << arglist << ");" << endl;
+      indent_down();
+      f_service_ << indent() << "});" << endl;
+    } else if (gen_node_) { // Node.js output      ./gen-nodejs
       f_service_ << indent() << "this._seqid = this.new_seqid();" << endl << indent()
                  << "if (callback === undefined) {" << endl;
       indent_up();
@@ -1412,6 +1450,23 @@
                  << "this.send_" << funname << "(" << arglist << ");" << endl;
       indent_down();
       indent(f_service_) << "}" << endl;
+    } else if (gen_es6_) {
+      f_service_ << indent() << "var self = this;" << endl << indent()
+                 << "return new Promise(function(resolve, reject) {" << endl;
+      indent_up();
+      f_service_ << indent() << "self.send_" << funname << "(" << arglist
+                 << (arglist.empty() ? "" : ", ") << "function(error, result) {" << endl;
+      indent_up();
+      f_service_ << indent() << "if (error) {" << endl;
+      f_service_ << indent() << "  reject(error);" << endl;
+      f_service_ << indent() << "} else {" << endl;
+      f_service_ << indent() << "  resolve(result);" << endl;
+      f_service_ << indent() << "}" << endl;
+      indent_down();
+      f_service_ << indent() << "});" << endl;
+      indent_down();
+      f_service_ << indent() << "});" << endl;
+
     } else if (gen_jquery_) { // jQuery output       ./gen-js
       f_service_ << indent() << "if (callback === undefined) {" << endl;
       indent_up();
@@ -1507,10 +1562,29 @@
     } else {
       if (gen_jquery_) {
         f_service_ << indent() << "return this.output.getTransport().flush(callback);" << endl;
+      } else if (gen_es6_) {
+        f_service_ << indent() << "var self = this;" << endl;
+        if((*f_iter)->is_oneway()) {
+          f_service_ << indent() << "this.output.getTransport().flush(true, null);" << endl;
+          f_service_ << indent() << "callback();" << endl;
+        } else {
+          f_service_ << indent() << "this.output.getTransport().flush(true, function() {" << endl;
+          indent_up();
+          f_service_ << indent() << "var error = null, result = null;" << endl;
+          f_service_ << indent() << "try {" << endl;
+          f_service_ << indent() << "  result = self.recv_" << funname << "();" << endl;
+          f_service_ << indent() << "} catch (e) {" << endl;
+          f_service_ << indent() << "  error = e;" << endl;
+          f_service_ << indent() << "}" << endl;
+          f_service_ << indent() << "callback(error, result);" << endl;
+          indent_down();
+          f_service_ << indent() << "});";
+        }
       } else {
         f_service_ << indent() << "if (callback) {" << endl;
         if((*f_iter)->is_oneway()) {
           f_service_ << indent() << "  this.output.getTransport().flush(true, null);" << endl;
+          f_service_ << indent() << "  callback();" << endl;
         } else {
           f_service_ << indent() << "  var self = this;" << endl;
           f_service_ << indent() << "  this.output.getTransport().flush(true, function() {" << endl;
@@ -2238,7 +2312,12 @@
       str += "void;";
     }
   } else {
-    str += "): " + ts_get_type(tfunction->get_returntype()) + ";";
+    if (gen_es6_) {
+      str += "): Promise<" + ts_get_type(tfunction->get_returntype()) + ">;";
+    }
+    else {
+      str += "): " + ts_get_type(tfunction->get_returntype()) + ";";
+    }
   }
 
   return str;
@@ -2282,4 +2361,5 @@
                           "    jquery:          Generate jQuery compatible code.\n"
                           "    node:            Generate node.js compatible code.\n"
                           "    ts:              Generate TypeScript definition files.\n"
-                          "    with_ns:         Create global namespace objects when using node.js\n")
+                          "    with_ns:         Create global namespace objects when using node.js\n"
+                          "    es6:             Create ES6 code with Promises\n")
diff --git a/lib/js/Gruntfile.js b/lib/js/Gruntfile.js
index e678cd5..577a393 100644
--- a/lib/js/Gruntfile.js
+++ b/lib/js/Gruntfile.js
@@ -51,7 +51,10 @@
       },
       ThriftGenDeepConstructor: {
         command: '../../compiler/cpp/thrift -gen js -o test ../../test/JsDeepConstructorTest.thrift'
-      }
+      },
+      ThriftGenES6: {
+        command: '../../compiler/cpp/thrift -gen js -gen js:es6 -o test ../../test/ThriftTest.thrift'
+      },
     },
     external_daemon: {
       ThriftTestServer: {
@@ -133,6 +136,13 @@
             'http://localhost:8088/test-deep-constructor.html'
           ]
         }
+      },
+      ThriftWSES6: {
+        options: {
+          urls: [
+            'http://localhost:8088/test-es6.html'
+          ]
+        }
       }
     },
     jshint: {
@@ -162,13 +172,15 @@
                               'shell:ThriftGenDeepConstructor', 'qunit:ThriftDeepConstructor',
                               'qunit:ThriftJS', 'qunit:ThriftJS_TLS',
                               'qunit:ThriftWS',
-                              'shell:ThriftGenJQ', 'qunit:ThriftJSJQ', 'qunit:ThriftJSJQ_TLS'
+                              'shell:ThriftGenJQ', 'qunit:ThriftJSJQ', 'qunit:ThriftJSJQ_TLS',
+                              'shell:ThriftGenES6', 'qunit:ThriftWSES6'
                              ]);
   grunt.registerTask('default', ['jshint', 'shell:InstallThriftJS', 'shell:InstallThriftNodeJSDep', 'shell:ThriftGen',
                                  'external_daemon:ThriftTestServer', 'external_daemon:ThriftTestServer_TLS',
                                  'qunit:ThriftJS', 'qunit:ThriftJS_TLS',
                                  'qunit:ThriftWS',
                                  'shell:ThriftGenJQ', 'qunit:ThriftJSJQ', 'qunit:ThriftJSJQ_TLS',
+                                 'shell:ThriftGenES6', 'qunit:ThriftWSES6',
                                  'concat', 'uglify', 'jsdoc'
                                 ]);
 };
diff --git a/lib/js/test/build.xml b/lib/js/test/build.xml
index a905fde..d10a2d1 100755
--- a/lib/js/test/build.xml
+++ b/lib/js/test/build.xml
@@ -96,6 +96,7 @@
     <get src="http://code.jquery.com/jquery-1.11.3.min.js" dest="${build}/js/lib/jquery.js" usetimestamp="true"/>
     <get src="http://code.jquery.com/qunit/qunit-1.18.0.js" dest="${build}/js/lib/qunit.js" usetimestamp="true"/>
     <get src="http://code.jquery.com/qunit/qunit-1.18.0.css" dest="${build}/js/lib/qunit.css" usetimestamp="true"/>
+    <get src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.js" dest="${build}/js/lib/es6-promise.js" usetimestamp="true"/>
   </target>
 
   <target name="compile" description="compile the test suite" depends="init, generate, resolve">
diff --git a/lib/js/test/test-async.js b/lib/js/test/test-async.js
index b56f2a2..b4e9854 100644
--- a/lib/js/test/test-async.js
+++ b/lib/js/test/test-async.js
@@ -345,4 +345,12 @@
     });
   });
 
+module('Oneway');
 
+  asyncTest('testOneway', function() {
+    expect(1);
+    client.testOneway(1, function(result) {
+      equal(result, undefined);
+      QUnit.start();
+    });
+  });
\ No newline at end of file
diff --git a/lib/js/test/test-es6.html b/lib/js/test/test-es6.html
new file mode 100644
index 0000000..92d0738
--- /dev/null
+++ b/lib/js/test/test-es6.html
@@ -0,0 +1,65 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements. See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership. The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License. You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied. See the License for the
+  specific language governing permissions and limitations
+  under the License.
+-->
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+  <title>Thrift Javascript Bindings: Unit Test</title>
+
+  <script src="build/js/thrift.js"         type="text/javascript" charset="utf-8"></script>
+  <script src="gen-js/ThriftTest_types.js" type="text/javascript" charset="utf-8"></script>
+  <script src="gen-js/ThriftTest.js"       type="text/javascript" charset="utf-8"></script>
+
+  <!-- ES6 Promise Polyfill -->
+  <script type="text/javascript" src="build/js/lib/es6-promise.js" charset="utf-8"></script>
+
+  <!-- jQuery -->
+  <script type="text/javascript" src="build/js/lib/jquery.js" charset="utf-8"></script>
+
+  <!-- QUnit Test framework-->
+  <script type="text/javascript" src="build/js/lib/qunit.js" charset="utf-8"></script>
+  <link rel="stylesheet" href="build/js/lib/qunit.css" type="text/css" media="screen" />
+
+  <!-- the Test Suite-->
+  <script>
+    var loc = window.location;
+    var ws_uri = ((loc.protocol === "https:") ? "wss://" : "ws://") +
+                   loc.hostname + ":" + loc.port + loc.pathname;
+    var transport = new Thrift.TWebSocketTransport(ws_uri);
+    var protocol  = new Thrift.Protocol(transport);
+    var client    = new ThriftTest.ThriftTestClient(protocol);
+    transport.open();
+  </script>
+  <script type="text/javascript" src="test-es6.js" charset="utf-8"></script>
+</head>
+<body>
+  <h1 id="qunit-header">Thrift Javascript Bindings: Unit Test (<a href="https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob;f=test/ThriftTest.thrift;hb=HEAD">ThriftTest.thrift</a>)</h1>
+  <h2 id="qunit-banner"></h2>
+  <div id="qunit-testrunner-toolbar"></div>
+  <h2 id="qunit-userAgent"></h2>
+  <ol id="qunit-tests"><li><!-- get valid xhtml strict--></li></ol>
+  <!-- Uncomment this to check the validity. This significantly slows down the test.
+  <p>
+      <a href="http://validator.w3.org/check/referer"><img
+          src="http://www.w3.org/Icons/valid-xhtml10"
+          alt="Valid XHTML 1.0!" height="31" width="88" /></a>
+  </p>
+  -->
+</body>
+</html>
diff --git a/lib/js/test/test-es6.js b/lib/js/test/test-es6.js
new file mode 100644
index 0000000..a3a31dc
--- /dev/null
+++ b/lib/js/test/test-es6.js
@@ -0,0 +1,354 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+ /* jshint -W100 */
+
+/*
+ * Fully Async JavaScript test suite for ThriftTest.thrift.
+ * These tests are designed to exercise the WebSocket transport
+ * (which is exclusively async).
+ *
+ * To compile client code for this test use:
+ *      $ thrift -gen js ThriftTest.thrift
+ */
+
+
+
+// all Languages in UTF-8
+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ú, 粵語";
+
+function checkRecursively(map1, map2) {
+  if (typeof map1 !== 'function' && typeof map2 !== 'function') {
+    if (!map1 || typeof map1 !== 'object') {
+        equal(map1, map2);
+    } else {
+      for (var key in map1) {
+        checkRecursively(map1[key], map2[key]);
+      }
+    }
+  }
+}
+
+module('Base Types');
+
+  asyncTest('Void', function() {
+    expect(1);
+    client.testVoid().then(function(result) {
+      equal(result, undefined);
+      QUnit.start();
+    });
+  });
+
+  asyncTest('String', function() {
+    expect(3);
+    QUnit.stop(2);
+    client.testString('').then(function(result) {
+       equal(result, '');
+       QUnit.start();
+    });
+    client.testString(stringTest).then(function(result) {
+       equal(result, stringTest);
+       QUnit.start();
+    });
+    var specialCharacters = 'quote: \" backslash:' +
+          ' forwardslash-escaped: \/ ' +
+          ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
+          ' now-all-of-them-together: "\\\/\b\n\r\t' +
+          ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><';
+    client.testString(specialCharacters).then(function(result) {
+       equal(result, specialCharacters);
+       QUnit.start();
+    });
+  });
+
+  asyncTest('Double', function() {
+    expect(4);
+    QUnit.stop(3);
+    client.testDouble(0).then(function(result) {
+       equal(result, 0);
+       QUnit.start();
+    });
+    client.testDouble(-1).then(function(result) {
+       equal(result, -1);
+       QUnit.start();
+    });
+    client.testDouble(3.14).then(function(result) {
+       equal(result, 3.14);
+       QUnit.start();
+    });
+    client.testDouble(Math.pow(2, 60)).then(function(result) {
+       equal(result, Math.pow(2, 60));
+       QUnit.start();
+    });
+  });
+  // TODO: add testBinary()
+  asyncTest('Byte', function() {
+    expect(2);
+    QUnit.stop();
+    client.testByte(0).then(function(result) {
+       equal(result, 0);
+       QUnit.start();
+    });
+    client.testByte(0x01).then(function(result) {
+       equal(result, 0x01);
+       QUnit.start();
+    });
+  });
+  asyncTest('I32', function() {
+    expect(3);
+    QUnit.stop(2);
+    client.testI32(0).then(function(result) {
+       equal(result, 0);
+       QUnit.start();
+    });
+    client.testI32(Math.pow(2, 30)).then(function(result) {
+       equal(result, Math.pow(2, 30));
+       QUnit.start();
+    });
+    client.testI32(-Math.pow(2, 30)).then(function(result) {
+       equal(result, -Math.pow(2, 30));
+       QUnit.start();
+    });
+  });
+  asyncTest('I64', function() {
+    expect(3);
+    QUnit.stop(2);
+    client.testI64(0).then(function(result) {
+       equal(result, 0);
+       QUnit.start();
+    });
+    //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
+    client.testI64(Math.pow(2, 52)).then(function(result) {
+       equal(result, Math.pow(2, 52));
+       QUnit.start();
+    });
+    client.testI64(-Math.pow(2, 52)).then(function(result) {
+       equal(result, -Math.pow(2, 52));
+       QUnit.start();
+    });
+  });
+
+
+module('Structured Types');
+
+  asyncTest('Struct', function() {
+    expect(5);
+    var structTestInput = new ThriftTest.Xtruct();
+    structTestInput.string_thing = 'worked';
+    structTestInput.byte_thing = 0x01;
+    structTestInput.i32_thing = Math.pow(2, 30);
+    //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
+    structTestInput.i64_thing = Math.pow(2, 52);
+
+    client.testStruct(structTestInput).then(function(result) {
+      equal(result.string_thing, structTestInput.string_thing);
+      equal(result.byte_thing, structTestInput.byte_thing);
+      equal(result.i32_thing, structTestInput.i32_thing);
+      equal(result.i64_thing, structTestInput.i64_thing);
+      equal(JSON.stringify(result), JSON.stringify(structTestInput));
+      QUnit.start();
+    });
+  });
+
+  asyncTest('Nest', function() {
+    expect(7);
+    var xtrTestInput = new ThriftTest.Xtruct();
+    xtrTestInput.string_thing = 'worked';
+    xtrTestInput.byte_thing = 0x01;
+    xtrTestInput.i32_thing = Math.pow(2, 30);
+    //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
+    xtrTestInput.i64_thing = Math.pow(2, 52);
+
+    var nestTestInput = new ThriftTest.Xtruct2();
+    nestTestInput.byte_thing = 0x02;
+    nestTestInput.struct_thing = xtrTestInput;
+    nestTestInput.i32_thing = Math.pow(2, 15);
+
+    client.testNest(nestTestInput).then(function(result) {
+      equal(result.byte_thing, nestTestInput.byte_thing);
+      equal(result.struct_thing.string_thing, nestTestInput.struct_thing.string_thing);
+      equal(result.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing);
+      equal(result.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing);
+      equal(result.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing);
+      equal(result.i32_thing, nestTestInput.i32_thing);
+      equal(JSON.stringify(result), JSON.stringify(nestTestInput));
+      QUnit.start();
+    });
+  });
+
+  asyncTest('Map', function() {
+    expect(3);
+    var mapTestInput = {7: 77, 8: 88, 9: 99};
+
+    client.testMap(mapTestInput).then(function(result) {
+      for (var key in result) {
+        equal(result[key], mapTestInput[key]);
+      }
+      QUnit.start();
+    });
+  });
+
+  asyncTest('StringMap', function() {
+    expect(6);
+    var mapTestInput = {
+      'a': '123', 'a b': 'with spaces ', 'same': 'same', '0': 'numeric key',
+      'longValue': stringTest, stringTest: 'long key'
+    };
+
+    client.testStringMap(mapTestInput).then(function(result) {
+      for (var key in result) {
+        equal(result[key], mapTestInput[key]);
+      }
+      QUnit.start();
+    });
+  });
+
+  asyncTest('Set', function() {
+    expect(1);
+    var setTestInput = [1, 2, 3];
+    client.testSet(setTestInput).then(function(result) {
+      ok(result, setTestInput);
+      QUnit.start();
+    });
+  });
+
+  asyncTest('List', function() {
+    expect(1);
+    var listTestInput = [1, 2, 3];
+    client.testList(listTestInput).then(function(result) {
+      ok(result, listTestInput);
+      QUnit.start();
+    });
+  });
+
+  asyncTest('Enum', function() {
+    expect(1);
+    client.testEnum(ThriftTest.Numberz.ONE).then(function(result) {
+      equal(result, ThriftTest.Numberz.ONE);
+      QUnit.start();
+    });
+  });
+
+  asyncTest('TypeDef', function() {
+    expect(1);
+    client.testTypedef(69).then(function(result) {
+      equal(result, 69);
+      QUnit.start();
+    });
+  });
+
+
+module('deeper!');
+
+  asyncTest('MapMap', function() {
+    expect(16);
+    var mapMapTestExpectedResult = {
+      '4': {'1': 1, '2': 2, '3': 3, '4': 4},
+      '-4': {'-4': -4, '-3': -3, '-2': -2, '-1': -1}
+    };
+
+    client.testMapMap(1).then(function(result) {
+      for (var key in result) {
+        for (var key2 in result[key]) {
+          equal(result[key][key2], mapMapTestExpectedResult[key][key2]);
+        }
+      }
+      checkRecursively(result, mapMapTestExpectedResult);
+      QUnit.start();
+    });
+  });
+
+
+module('Exception');
+
+  asyncTest('Xception', function() {
+    expect(2);
+    client.testException('Xception').then(function(res) {
+      ok(false);
+    }).catch(function(e) {
+      equal(e.errorCode, 1001);
+      equal(e.message, 'Xception');
+      QUnit.start();
+    });
+  });
+
+  asyncTest('no Exception', 0, function() {
+    expect(1);
+    client.testException('no Exception').then(function(e) {
+      ok(!e);
+      QUnit.start();
+    });
+  });
+
+module('Insanity');
+
+  asyncTest('testInsanity', function() {
+    expect(24);
+    var insanity = {
+      '1': {
+        '2': {
+          'userMap': { '5': 5, '8': 8 },
+          'xtructs': [{
+              'string_thing': 'Goodbye4',
+              'byte_thing': 4,
+              'i32_thing': 4,
+              'i64_thing': 4
+            },
+            {
+              'string_thing': 'Hello2',
+              'byte_thing': 2,
+              'i32_thing': 2,
+              'i64_thing': 2
+            }
+          ]
+        },
+        '3': {
+          'userMap': { '5': 5, '8': 8 },
+          'xtructs': [{
+              'string_thing': 'Goodbye4',
+              'byte_thing': 4,
+              'i32_thing': 4,
+              'i64_thing': 4
+            },
+            {
+              'string_thing': 'Hello2',
+              'byte_thing': 2,
+              'i32_thing': 2,
+              'i64_thing': 2
+            }
+          ]
+        }
+      },
+      '2': { '6': { 'userMap': null, 'xtructs': null } }
+    };
+    client.testInsanity(new ThriftTest.Insanity()).then(function(res) {
+      ok(res, JSON.stringify(res));
+      ok(insanity, JSON.stringify(insanity));
+      checkRecursively(res, insanity);
+      QUnit.start();
+    });
+  });
+
+module('Oneway');
+  asyncTest('testOneway', function() {
+    expect(1);
+    client.testOneway(1).then(function(result) {
+      equal(result, undefined);
+      QUnit.start();
+    });
+  });
\ No newline at end of file