napi-rs/examples/napi-compat-mode/__test__/napi7/arraybuffer.spec.ts

32 lines
912 B
TypeScript
Raw Normal View History

2021-04-21 18:19:45 +09:00
import ava from 'ava'
import { napiVersion } from '../napi-version'
const bindings = require('../../index.node')
const test = napiVersion >= 7 ? ava : ava.skip
test('should be able to detach ArrayBuffer', (t) => {
const buf = Buffer.from('hello world')
const ab = buf.buffer.slice(0, buf.length)
try {
bindings.testDetachArrayBuffer(ab)
t.is(ab.byteLength, 0)
} catch (e) {
2021-11-19 15:58:21 +09:00
t.is((e as any).code, 'DetachableArraybufferExpected')
2021-04-21 18:19:45 +09:00
}
})
test('is detached arraybuffer should work fine', (t) => {
const buf = Buffer.from('hello world')
const ab = buf.buffer.slice(0, buf.length)
try {
bindings.testDetachArrayBuffer(ab)
const nonDetachedArrayBuffer = new ArrayBuffer(10)
t.true(bindings.testIsDetachedArrayBuffer(ab))
t.false(bindings.testIsDetachedArrayBuffer(nonDetachedArrayBuffer))
} catch (e) {
2021-11-19 15:58:21 +09:00
t.is((e as any).code, 'DetachableArraybufferExpected')
2021-04-21 18:19:45 +09:00
}
})