From afbcbaf281fd4b04ea87b2504140d0efe997e867 Mon Sep 17 00:00:00 2001 From: Marly Fleitas Date: Tue, 16 Oct 2018 09:46:48 +0900 Subject: [PATCH] test: add arraybuffer tests - ArrayBuffer() - ArrayBuffer(napi_env env, napi_value value) PR-URL: https://github.com/nodejs/node-addon-api/pull/369 Reviewed-By: Michael Dawson --- test/arraybuffer.cc | 18 ++++++++++++++++++ test/arraybuffer.js | 8 ++++++++ 2 files changed, 26 insertions(+) diff --git a/test/arraybuffer.cc b/test/arraybuffer.cc index 27bb993..e46f8cb 100644 --- a/test/arraybuffer.cc +++ b/test/arraybuffer.cc @@ -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(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) { @@ -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; } diff --git a/test/arraybuffer.js b/test/arraybuffer.js index d284fe8..4360461 100644 --- a/test/arraybuffer.js +++ b/test/arraybuffer.js @@ -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); + }, ]); }