diff --git a/.gitignore b/.gitignore index 69e7586a1..ca18d2049 100755 --- a/.gitignore +++ b/.gitignore @@ -45,6 +45,7 @@ tests/go_gen tests/monsterdata_java_wire.mon tests/monsterdata_go_wire.mon tests/monsterdata_javascript_wire.mon +tests/unicode_test.mon CMakeLists.txt.user CMakeScripts/** CTestTestfile.cmake diff --git a/tests/JavaScriptTest.js b/tests/JavaScriptTest.js index af62a4ee6..8e23d8366 100644 --- a/tests/JavaScriptTest.js +++ b/tests/JavaScriptTest.js @@ -1,3 +1,4 @@ +// Run this using JavaScriptTest.sh var assert = require('assert'); var fs = require('fs'); @@ -66,6 +67,8 @@ function main() { // Test it: testBuffer(fbb.dataBuffer()); + testUnicode(); + console.log('FlatBuffers test: completed successfully'); } @@ -113,4 +116,29 @@ function testBuffer(bb) { assert.strictEqual(monster.testbool(), false); } +function testUnicode() { + var correct = fs.readFileSync('unicode_test.mon'); + var json = JSON.parse(fs.readFileSync('unicode_test.json', 'utf8')); + + // Test reading + var bb = new flatbuffers.ByteBuffer(new Uint8Array(correct)); + var monster = MyGame.Example.Monster.getRootAsMonster(bb); + assert.strictEqual(monster.name(), json.name); + assert.strictEqual(monster.testarrayofstringLength(), json.testarrayofstring.length); + json.testarrayofstring.forEach(function(string, i) { + assert.strictEqual(monster.testarrayofstring(i), string); + }); + + // Test writing + var fbb = new flatbuffers.Builder(); + var name = fbb.createString(json.name); + 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.addName(fbb, name); + MyGame.Example.Monster.finishMonsterBuffer(fbb, MyGame.Example.Monster.endMonster(fbb)); + assert.deepEqual(new Buffer(fbb.asUint8Array()), correct); +} + main(); diff --git a/tests/JavaScriptTest.sh b/tests/JavaScriptTest.sh index 40dba2be1..3d3100756 100755 --- a/tests/JavaScriptTest.sh +++ b/tests/JavaScriptTest.sh @@ -1,5 +1,5 @@ #!/bin/sh pushd "$(dirname $0)" >/dev/null -test_dir="$(pwd)" -node ${test_dir}/JavaScriptTest +../flatc -b monster_test.fbs unicode_test.json +node JavaScriptTest diff --git a/tests/unicode_test.json b/tests/unicode_test.json new file mode 100644 index 000000000..a78fdf0d2 --- /dev/null +++ b/tests/unicode_test.json @@ -0,0 +1,11 @@ +{ + "name": "unicode_test", + "testarrayofstring": [ + "Цлїςσδε", + "フムアムカモケモ", + "フムヤムカモケモ", + "㊀㊁㊂㊃㊄", + "☳☶☲", + "𡇙𝌆" + ] +}