napi-rs/examples/napi-compat-mode/__test__/array.spec.ts
forehalo 2467b7139b
Introduce #[napi] procedural macro to automation development boilerplate (#696)
* napi procedural macro for basic rust/JavaScript types
* introduce the `compat-mode` for `napi` and `napi-derive` crates for backward compatible
* remove #[inline] and let compiler to decide the inline behavior
* cli now can produce the `.d.ts` file for native binding
* many tests and example for the new procedural macro

Co-authored-by: LongYinan <lynweklm@gmail.com>
2021-09-23 01:29:09 +08:00

47 lines
1.3 KiB
TypeScript

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)
}
})