diff options
author | Evan Wallace <evan.exe@gmail.com> | 2015-10-14 23:07:40 -0700 |
---|---|---|
committer | Evan Wallace <evan.exe@gmail.com> | 2015-10-14 23:07:40 -0700 |
commit | 4bef5e89c4cc303fdcc985da66d5f2f81c3f18d6 (patch) | |
tree | 81f3d3f77a1647017edfe6e3f626caf56d628995 /tests | |
parent | 79222bf100aed80940efab986a84904e2bdc8e0e (diff) | |
download | flatbuffers-4bef5e89c4cc303fdcc985da66d5f2f81c3f18d6.tar.gz flatbuffers-4bef5e89c4cc303fdcc985da66d5f2f81c3f18d6.tar.bz2 flatbuffers-4bef5e89c4cc303fdcc985da66d5f2f81c3f18d6.zip |
Add UTF-8 support to the JavaScript API
This adds an optional argument to generated getters for string fields to
specify the encoding type and overloads the createString() function. It's
now possible to use either JavaScript UTF-16 string objects or C-style
UTF-8 byte arrays (Uint8Array) for string data.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/JavaScriptTest.js | 17 | ||||
-rw-r--r-- | tests/monster_test_generated.js | 21 | ||||
-rw-r--r-- | tests/unicode_test.json | 8 |
3 files changed, 37 insertions, 9 deletions
diff --git a/tests/JavaScriptTest.js b/tests/JavaScriptTest.js index 7a5945b9..8eb50f3c 100644 --- a/tests/JavaScriptTest.js +++ b/tests/JavaScriptTest.js @@ -125,18 +125,35 @@ function testUnicode() { var bb = new flatbuffers.ByteBuffer(new Uint8Array(correct)); var monster = MyGame.Example.Monster.getRootAsMonster(bb); assert.strictEqual(monster.name(), json.name); + assert.deepEqual(new Buffer(monster.name(flatbuffers.Encoding.UTF8_BYTES)), new Buffer(json.name)); + assert.strictEqual(monster.testarrayoftablesLength(), json.testarrayoftables.length); + json.testarrayoftables.forEach(function(table, i) { + var value = monster.testarrayoftables(i); + assert.strictEqual(value.name(), table.name); + assert.deepEqual(new Buffer(value.name(flatbuffers.Encoding.UTF8_BYTES)), new Buffer(table.name)); + }); assert.strictEqual(monster.testarrayofstringLength(), json.testarrayofstring.length); json.testarrayofstring.forEach(function(string, i) { assert.strictEqual(monster.testarrayofstring(i), string); + assert.deepEqual(new Buffer(monster.testarrayofstring(i, flatbuffers.Encoding.UTF8_BYTES)), new Buffer(string)); }); // Test writing var fbb = new flatbuffers.Builder(); var name = fbb.createString(json.name); + var testarrayoftablesOffsets = json.testarrayoftables.map(function(table) { + var name = fbb.createString(new Uint8Array(new Buffer(table.name))); + MyGame.Example.Monster.startMonster(fbb); + MyGame.Example.Monster.addName(fbb, name); + return MyGame.Example.Monster.endMonster(fbb); + }); + var testarrayoftablesOffset = MyGame.Example.Monster.createTestarrayoftablesVector(fbb, + testarrayoftablesOffsets); var testarrayofstringOffset = MyGame.Example.Monster.createTestarrayofstringVector(fbb, json.testarrayofstring.map(function(string) { return fbb.createString(string); })); MyGame.Example.Monster.startMonster(fbb); MyGame.Example.Monster.addTestarrayofstring(fbb, testarrayofstringOffset); + MyGame.Example.Monster.addTestarrayoftables(fbb, testarrayoftablesOffset); MyGame.Example.Monster.addName(fbb, name); MyGame.Example.Monster.finishMonsterBuffer(fbb, MyGame.Example.Monster.endMonster(fbb)); assert.deepEqual(new Buffer(fbb.asUint8Array()), correct); diff --git a/tests/monster_test_generated.js b/tests/monster_test_generated.js index 5687308e..d46a9c68 100644 --- a/tests/monster_test_generated.js +++ b/tests/monster_test_generated.js @@ -287,11 +287,12 @@ MyGame.Example.Stat.getRootAsStat = function(bb, obj) { }; /** - * @returns {?string} + * @param {flatbuffers.Encoding=} optionalEncoding + * @returns {string|Uint8Array} */ -MyGame.Example.Stat.prototype.id = function() { +MyGame.Example.Stat.prototype.id = function(optionalEncoding) { var offset = this.bb.__offset(this.bb_pos, 4); - return offset ? this.bb.__string(this.bb_pos + offset) : null; + return offset ? this.bb.__string(this.bb_pos + offset, optionalEncoding) : null; }; /** @@ -419,11 +420,12 @@ MyGame.Example.Monster.prototype.hp = function() { }; /** - * @returns {?string} + * @param {flatbuffers.Encoding=} optionalEncoding + * @returns {string|Uint8Array} */ -MyGame.Example.Monster.prototype.name = function() { +MyGame.Example.Monster.prototype.name = function(optionalEncoding) { var offset = this.bb.__offset(this.bb_pos, 10); - return offset ? this.bb.__string(this.bb_pos + offset) : null; + return offset ? this.bb.__string(this.bb_pos + offset, optionalEncoding) : null; }; /** @@ -488,11 +490,12 @@ MyGame.Example.Monster.prototype.test4Length = function() { /** * @param {number} index - * @returns {?string} + * @param {flatbuffers.Encoding=} optionalEncoding + * @returns {string|Uint8Array} */ -MyGame.Example.Monster.prototype.testarrayofstring = function(index) { +MyGame.Example.Monster.prototype.testarrayofstring = function(index, optionalEncoding) { var offset = this.bb.__offset(this.bb_pos, 24); - return offset ? this.bb.__string(this.bb.__vector(this.bb_pos + offset) + index * 4) : null; + return offset ? this.bb.__string(this.bb.__vector(this.bb_pos + offset) + index * 4, optionalEncoding) : null; }; /** diff --git a/tests/unicode_test.json b/tests/unicode_test.json index a78fdf0d..75e467a5 100644 --- a/tests/unicode_test.json +++ b/tests/unicode_test.json @@ -1,5 +1,13 @@ { "name": "unicode_test", + "testarrayoftables": [ + { "name": "Цлїςσδε" }, + { "name": "フムアムカモケモ" }, + { "name": "フムヤムカモケモ" }, + { "name": "㊀㊁㊂㊃㊄" }, + { "name": "☳☶☲" }, + { "name": "𡇙𝌆" } + ], "testarrayofstring": [ "Цлїςσδε", "フムアムカモケモ", |