mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-01 13:31:37 +00:00
Add a test for JavaScript UTF-8 <=> UTF-16 conversion
JavaScript uses UTF-16 but FlatBuffers uses UTF-8. This commit tests the code that does the conversion between the two encodings. The last entry in the array is tricky because each code point actually requires two UTF-16 code units, unlike the other examples. The current JSON output of flatc actually handles this case incorrectly (it generates invalid JSON with UTF-8 code units). The generated JavaScript code passes these tests fine, however.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -45,6 +45,7 @@ tests/go_gen
|
|||||||
tests/monsterdata_java_wire.mon
|
tests/monsterdata_java_wire.mon
|
||||||
tests/monsterdata_go_wire.mon
|
tests/monsterdata_go_wire.mon
|
||||||
tests/monsterdata_javascript_wire.mon
|
tests/monsterdata_javascript_wire.mon
|
||||||
|
tests/unicode_test.mon
|
||||||
CMakeLists.txt.user
|
CMakeLists.txt.user
|
||||||
CMakeScripts/**
|
CMakeScripts/**
|
||||||
CTestTestfile.cmake
|
CTestTestfile.cmake
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// Run this using JavaScriptTest.sh
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
|
||||||
@@ -66,6 +67,8 @@ function main() {
|
|||||||
// Test it:
|
// Test it:
|
||||||
testBuffer(fbb.dataBuffer());
|
testBuffer(fbb.dataBuffer());
|
||||||
|
|
||||||
|
testUnicode();
|
||||||
|
|
||||||
console.log('FlatBuffers test: completed successfully');
|
console.log('FlatBuffers test: completed successfully');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,4 +116,29 @@ function testBuffer(bb) {
|
|||||||
assert.strictEqual(monster.testbool(), false);
|
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();
|
main();
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
pushd "$(dirname $0)" >/dev/null
|
pushd "$(dirname $0)" >/dev/null
|
||||||
test_dir="$(pwd)"
|
../flatc -b monster_test.fbs unicode_test.json
|
||||||
node ${test_dir}/JavaScriptTest
|
node JavaScriptTest
|
||||||
|
|||||||
11
tests/unicode_test.json
Normal file
11
tests/unicode_test.json
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"name": "unicode_test",
|
||||||
|
"testarrayofstring": [
|
||||||
|
"Цлїςσδε",
|
||||||
|
"フムアムカモケモ",
|
||||||
|
"フムヤムカモケモ",
|
||||||
|
"㊀㊁㊂㊃㊄",
|
||||||
|
"☳☶☲",
|
||||||
|
"𡇙𝌆"
|
||||||
|
]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user