summaryrefslogtreecommitdiff
path: root/tests/JavaScriptTest.js
diff options
context:
space:
mode:
authorEvan Wallace <evan.exe@gmail.com>2015-10-14 23:07:40 -0700
committerEvan Wallace <evan.exe@gmail.com>2015-10-14 23:07:40 -0700
commit4bef5e89c4cc303fdcc985da66d5f2f81c3f18d6 (patch)
tree81f3d3f77a1647017edfe6e3f626caf56d628995 /tests/JavaScriptTest.js
parent79222bf100aed80940efab986a84904e2bdc8e0e (diff)
downloadflatbuffers-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/JavaScriptTest.js')
-rw-r--r--tests/JavaScriptTest.js17
1 files changed, 17 insertions, 0 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);