Skip to content

Commit

Permalink
test: write tests for Array class
Browse files Browse the repository at this point in the history
add tests for array class.

PR-URL: nodejs/node-addon-api#363
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
  • Loading branch information
Marlyfleitas committed Oct 22, 2018
1 parent afbcbaf commit 89e7a0d
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
41 changes: 41 additions & 0 deletions test/basic_types/array.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#define NAPI_EXPERIMENTAL
#include "napi.h"

using namespace Napi;

Value CreateArray(const CallbackInfo& info) {
if (info.Length() > 0) {
size_t length = info[0].As<Number>().Uint32Value();
return Array::New(info.Env(), length);
} else {
return Array::New(info.Env());
}
}

Value GetLength(const CallbackInfo& info) {
Array array = info[0].As<Array>();
return Number::New(info.Env(), static_cast<uint32_t>(array.Length()));
}

Value GetElement(const CallbackInfo& info) {
Array array = info[0].As<Array>();
size_t index = info[1].As<Number>().Uint32Value();
return array[index];
}

void SetElement(const CallbackInfo& info) {
Array array = info[0].As<Array>();
size_t index = info[1].As<Number>().Uint32Value();
array[index] = info[2].As<Value>();
}

Object InitBasicTypesArray(Env env) {
Object exports = Object::New(env);

exports["createArray"] = Function::New(env, CreateArray);
exports["getLength"] = Function::New(env, GetLength);
exports["get"] = Function::New(env, GetElement);
exports["set"] = Function::New(env, SetElement);

return exports;
}
37 changes: 37 additions & 0 deletions test/basic_types/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');

test(require(`../build/${buildType}/binding.node`));
test(require(`../build/${buildType}/binding_noexcept.node`));

function test(binding) {

// create empty array
const array = binding.basic_types_array.createArray();
assert.strictEqual(binding.basic_types_array.getLength(array), 0);

// create array with length
const arrayWithLength = binding.basic_types_array.createArray(10);
assert.strictEqual(binding.basic_types_array.getLength(arrayWithLength), 10);

// set function test
binding.basic_types_array.set(array, 0, 10);
binding.basic_types_array.set(array, 1, "test");
binding.basic_types_array.set(array, 2, 3.0);

// check length after set data
assert.strictEqual(binding.basic_types_array.getLength(array), 3);

// get function test
assert.strictEqual(binding.basic_types_array.get(array, 0), 10);
assert.strictEqual(binding.basic_types_array.get(array, 1), "test");
assert.strictEqual(binding.basic_types_array.get(array, 2), 3.0);

// overwrite test
binding.basic_types_array.set(array, 0, 5);
assert.strictEqual(binding.basic_types_array.get(array, 0), 5);

// out of index test
assert.strictEqual(binding.basic_types_array.get(array, 5), undefined);
}
2 changes: 2 additions & 0 deletions test/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using namespace Napi;
Object InitArrayBuffer(Env env);
Object InitAsyncContext(Env env);
Object InitAsyncWorker(Env env);
Object InitBasicTypesArray(Env env);
Object InitBasicTypesBoolean(Env env);
Object InitBasicTypesNumber(Env env);
Object InitBasicTypesValue(Env env);
Expand Down Expand Up @@ -39,6 +40,7 @@ Object Init(Env env, Object exports) {
exports.Set("arraybuffer", InitArrayBuffer(env));
exports.Set("asynccontext", InitAsyncContext(env));
exports.Set("asyncworker", InitAsyncWorker(env));
exports.Set("basic_types_array", InitBasicTypesArray(env));
exports.Set("basic_types_boolean", InitBasicTypesBoolean(env));
exports.Set("basic_types_number", InitBasicTypesNumber(env));
exports.Set("basic_types_value", InitBasicTypesValue(env));
Expand Down
1 change: 1 addition & 0 deletions test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'arraybuffer.cc',
'asynccontext.cc',
'asyncworker.cc',
'basic_types/array.cc',
'basic_types/boolean.cc',
'basic_types/number.cc',
'basic_types/value.cc',
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ let testModules = [
'arraybuffer',
'asynccontext',
'asyncworker',
'basic_types/array',
'basic_types/boolean',
'basic_types/number',
'basic_types/value',
Expand Down

0 comments on commit 89e7a0d

Please sign in to comment.