aboutsummaryrefslogtreecommitdiff
path: root/test/js-native-api/test_array/test.js
diff options
context:
space:
mode:
authorGabriel Schulhof <gabriel.schulhof@intel.com>2018-11-17 12:34:54 -0800
committerGabriel Schulhof <gabriel.schulhof@intel.com>2018-12-04 13:58:17 -0800
commit938e11882b96e19b443477571455088baaa054d8 (patch)
treeeb828a60957a2881995ba9a83f44a32a18fbff16 /test/js-native-api/test_array/test.js
parent83ee137c4565112177f22f2c735b266b22262220 (diff)
downloadandroid-node-v8-938e11882b96e19b443477571455088baaa054d8.tar.gz
android-node-v8-938e11882b96e19b443477571455088baaa054d8.tar.bz2
android-node-v8-938e11882b96e19b443477571455088baaa054d8.zip
test: partition N-API tests
Partition test/addons-napi into test/js-native-api and test/node-api to isolate the Node.js-agnostic portion of the N-API tests from the Node.js-specific portion. PR-URL: https://github.com/nodejs/node/pull/24557 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Diffstat (limited to 'test/js-native-api/test_array/test.js')
-rw-r--r--test/js-native-api/test_array/test.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/test/js-native-api/test_array/test.js b/test/js-native-api/test_array/test.js
new file mode 100644
index 0000000000..75c181d9da
--- /dev/null
+++ b/test/js-native-api/test_array/test.js
@@ -0,0 +1,60 @@
+'use strict';
+const common = require('../../common');
+const assert = require('assert');
+
+// Testing api calls for arrays
+const test_array = require(`./build/${common.buildType}/test_array`);
+
+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);
+}