diff --git a/test_module/__test__/array.spec.ts b/test_module/__test__/array.spec.ts new file mode 100644 index 00000000..34962c51 --- /dev/null +++ b/test_module/__test__/array.spec.ts @@ -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) + } +}) diff --git a/test_module/src/array.rs b/test_module/src/array.rs new file mode 100644 index 00000000..08208394 --- /dev/null +++ b/test_module/src/array.rs @@ -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 { + env.create_array().map(Some) +} + +#[js_function(1)] +fn test_create_array_with_length(ctx: CallContext) -> Result { + ctx + .env + .create_array_with_length(ctx.get::(0)?.try_into()?) +} + +#[js_function(3)] +fn test_set_element(ctx: CallContext) -> Result { + let mut arr = ctx.get::(0)?; + let index = ctx.get::(1)?; + let ele = ctx.get::(2)?; + arr.set_element(index.try_into()?, ele)?; + + ctx.env.get_undefined() +} + +#[js_function(2)] +fn test_has_element(ctx: CallContext) -> Result { + let arr = ctx.get::(0)?; + let index = ctx.get::(1)?; + + ctx.env.get_boolean(arr.has_element(index.try_into()?)?) +} + +#[js_function(2)] +fn test_delete_element(ctx: CallContext) -> Result { + let mut arr = ctx.get::(0)?; + let index = ctx.get::(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(()) +} diff --git a/test_module/src/lib.rs b/test_module/src/lib.rs index 863c8365..48617ca2 100644 --- a/test_module/src/lib.rs +++ b/test_module/src/lib.rs @@ -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)?;