Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write tests for Array class #363

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -34,6 +35,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 @@ -7,6 +7,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