Skip to content

Commit

Permalink
test: add arraybuffer tests
Browse files Browse the repository at this point in the history
- ArrayBuffer()
- ArrayBuffer(napi_env env, napi_value value)

PR-URL: #369
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
nadongguri authored and mhdawson committed Oct 22, 2018
1 parent 405f3e5 commit 729f6dc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
18 changes: 18 additions & 0 deletions test/arraybuffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ Value GetFinalizeCount(const CallbackInfo& info) {
return Number::New(info.Env(), finalizeCount);
}

Value CreateBufferWithConstructor(const CallbackInfo& info) {
ArrayBuffer buffer = ArrayBuffer::New(info.Env(), testLength);
if (buffer.ByteLength() != testLength) {
Error::New(info.Env(), "Incorrect buffer length.").ThrowAsJavaScriptException();
return Value();
}
InitData(static_cast<uint8_t*>(buffer.Data()), testLength);
ArrayBuffer buffer2(info.Env(), buffer);
return buffer2;
}

Value CheckEmptyBuffer(const CallbackInfo& info) {
ArrayBuffer buffer;
return Boolean::New(info.Env(), buffer.IsEmpty());
}

} // end anonymous namespace

Object InitArrayBuffer(Env env) {
Expand All @@ -148,6 +164,8 @@ Object InitArrayBuffer(Env env) {
Function::New(env, CreateExternalBufferWithFinalizeHint);
exports["checkBuffer"] = Function::New(env, CheckBuffer);
exports["getFinalizeCount"] = Function::New(env, GetFinalizeCount);
exports["createBufferWithConstructor"] = Function::New(env, CreateBufferWithConstructor);
exports["checkEmptyBuffer"] = Function::New(env, CheckEmptyBuffer);

return exports;
}
8 changes: 8 additions & 0 deletions test/arraybuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,13 @@ function test(binding) {
global.gc();
assert.strictEqual(1, binding.arraybuffer.getFinalizeCount());
},

'ArrayBuffer with constructor',
() => {
assert.strictEqual(true, binding.arraybuffer.checkEmptyBuffer());
const test = binding.arraybuffer.createBufferWithConstructor();
binding.arraybuffer.checkBuffer(test);
assert.ok(test instanceof ArrayBuffer);
},
]);
}

0 comments on commit 729f6dc

Please sign in to comment.