node/test/addons-napi/test_array/test.js
Ben Noordhuis d9b59def72
build,test: make building addon tests less fragile
* Get rid of recursive `make` when building the node binary.  An earlier
  commit makes GYP write out rules that we can use for proper dependency
  tracking.

* Use module name 'binding' in addons.md and addons-napi/*/binding.gyp.
  This massively simplifies the logic for generating the build rules.

* Check in auto-generated add-on tests from `doc/api/addons.md`.  The
  files change rarely and generating them dynamically causes no end of
  race conditions and special-casing during the build.

PR-URL: https://github.com/nodejs/node/pull/17407
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
2018-01-21 02:19:46 +01:00

61 lines
1.4 KiB
JavaScript

'use strict';
const common = require('../../common');
const assert = require('assert');
// Testing api calls for arrays
const test_array = require(`./build/${common.buildType}/binding`);
const array = [
1,
9,
48,
13493,
9459324,
{ name: 'hello' },
[
'world',
'node',
'abi'
]
];
assert.throws(
() => {
test_array.TestGetElement(array, array.length + 1);
},
/^Error: assertion \(\(\(uint32_t\)index < length\)\) failed: Index out of bounds!$/
);
assert.throws(
() => {
test_array.TestGetElement(array, -2);
},
/^Error: assertion \(index >= 0\) failed: Invalid index\. Expects a positive integer\.$/
);
array.forEach(function(element, index) {
assert.strictEqual(test_array.TestGetElement(array, index), element);
});
assert.deepStrictEqual(test_array.New(array), array);
assert(test_array.TestHasElement(array, 0));
assert.strictEqual(test_array.TestHasElement(array, array.length + 1), false);
assert(test_array.NewWithLength(0) instanceof Array);
assert(test_array.NewWithLength(1) instanceof Array);
// check max allowed length for an array 2^32 -1
assert(test_array.NewWithLength(4294967295) instanceof Array);
{
// Verify that array elements can be deleted.
const arr = ['a', 'b', 'c', 'd'];
assert.strictEqual(arr.length, 4);
assert.strictEqual(2 in arr, true);
assert.strictEqual(test_array.TestDeleteElement(arr, 2), true);
assert.strictEqual(arr.length, 4);
assert.strictEqual(2 in arr, false);
}