test(napi): add specs for array

This commit is contained in:
LongYinan 2020-10-21 15:45:45 +08:00
parent e559908b76
commit 4dcf50ef06
No known key found for this signature in database
GPG key ID: A3FFE134A3E20881
3 changed files with 103 additions and 0 deletions

View file

@ -0,0 +1,47 @@
import test from 'ava'
const bindings = require('../index.node')
test('should be able to create array', (t) => {
const arr: number[] = bindings.testCreateArray()
t.true(arr instanceof Array)
t.true(Array.isArray(arr))
arr.push(1, 2, 3)
t.deepEqual(arr, [1, 2, 3])
})
test('should be able to create array with length', (t) => {
const len = 100
const arr: number[] = bindings.testCreateArrayWithLength(len)
t.true(arr instanceof Array)
t.true(Array.isArray(arr))
t.is(arr.length, len)
})
test('should be able to set element', (t) => {
const obj = {}
const index = 29
const arr: unknown[] = []
bindings.testSetElement(arr, index, obj)
t.is(arr[index], obj)
})
test('should be able to use has_element', (t) => {
const arr: any[] = [1, '3', undefined]
const index = 29
arr[index] = {}
t.true(bindings.testHasElement(arr, 0))
t.true(bindings.testHasElement(arr, 1))
t.true(bindings.testHasElement(arr, 2))
t.false(bindings.testHasElement(arr, 3))
t.false(bindings.testHasElement(arr, 10))
t.true(bindings.testHasElement(arr, index))
})
test('should be able to delete element', (t) => {
const arr: number[] = [0, 1, 2, 3]
for (const [index] of arr.entries()) {
t.true(bindings.testDeleteElement(arr, index))
t.true(arr[index] === undefined)
}
})

54
test_module/src/array.rs Normal file
View file

@ -0,0 +1,54 @@
use std::convert::TryInto;
use napi::{
CallContext, ContextlessResult, Env, JsBoolean, JsNumber, JsObject, JsUndefined, JsUnknown,
Module, Result,
};
#[contextless_function]
fn test_create_array(env: Env) -> ContextlessResult<JsObject> {
env.create_array().map(Some)
}
#[js_function(1)]
fn test_create_array_with_length(ctx: CallContext) -> Result<JsObject> {
ctx
.env
.create_array_with_length(ctx.get::<JsNumber>(0)?.try_into()?)
}
#[js_function(3)]
fn test_set_element(ctx: CallContext) -> Result<JsUndefined> {
let mut arr = ctx.get::<JsObject>(0)?;
let index = ctx.get::<JsNumber>(1)?;
let ele = ctx.get::<JsUnknown>(2)?;
arr.set_element(index.try_into()?, ele)?;
ctx.env.get_undefined()
}
#[js_function(2)]
fn test_has_element(ctx: CallContext) -> Result<JsBoolean> {
let arr = ctx.get::<JsObject>(0)?;
let index = ctx.get::<JsNumber>(1)?;
ctx.env.get_boolean(arr.has_element(index.try_into()?)?)
}
#[js_function(2)]
fn test_delete_element(ctx: CallContext) -> Result<JsBoolean> {
let mut arr = ctx.get::<JsObject>(0)?;
let index = ctx.get::<JsNumber>(1)?;
ctx.env.get_boolean(arr.delete_element(index.try_into()?)?)
}
pub fn register_js(module: &mut Module) -> Result<()> {
module.create_named_method("testCreateArray", test_create_array)?;
module.create_named_method("testCreateArrayWithLength", test_create_array_with_length)?;
module.create_named_method("testSetElement", test_set_element)?;
module.create_named_method("testHasElement", test_has_element)?;
module.create_named_method("testDeleteElement", test_delete_element)?;
Ok(())
}

View file

@ -20,6 +20,7 @@ mod napi6;
#[cfg(napi4)]
mod tokio_rt;
mod array;
mod arraybuffer;
mod buffer;
mod class;
@ -42,6 +43,7 @@ register_module!(test_module, init);
fn init(module: &mut Module) -> Result<()> {
module.create_named_method("getNapiVersion", get_napi_version)?;
array::register_js(module)?;
error::register_js(module)?;
string::register_js(module)?;
serde::register_js(module)?;