2020-08-24 17:16:35 +09:00
|
|
|
import test from 'ava'
|
2020-06-21 20:10:06 +09:00
|
|
|
|
|
|
|
const bindings = require('../index.node')
|
|
|
|
|
|
|
|
test('should get buffer length', (t) => {
|
|
|
|
const fixture = Buffer.from('wow, hello')
|
|
|
|
t.is(bindings.getBufferLength(fixture), fixture.length)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should stringify buffer', (t) => {
|
|
|
|
const fixture = 'wow, hello'
|
|
|
|
t.is(bindings.bufferToString(Buffer.from(fixture)), fixture)
|
|
|
|
})
|
2020-10-11 22:54:23 +09:00
|
|
|
|
|
|
|
test('should copy', (t) => {
|
|
|
|
const fixture = Buffer.from('wow, hello')
|
|
|
|
const copyBuffer = bindings.copyBuffer(fixture)
|
|
|
|
t.deepEqual(copyBuffer, fixture)
|
|
|
|
t.not(fixture, copyBuffer)
|
|
|
|
})
|
2020-12-24 12:53:23 +09:00
|
|
|
|
|
|
|
test('should create borrowed buffer with noop finalize', (t) => {
|
|
|
|
t.deepEqual(
|
|
|
|
bindings.createBorrowedBufferWithNoopFinalize(),
|
|
|
|
Buffer.from([1, 2, 3]),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should create borrowed buffer with finalize', (t) => {
|
|
|
|
t.deepEqual(
|
|
|
|
bindings.createBorrowedBufferWithFinalize(),
|
|
|
|
Buffer.from([1, 2, 3]),
|
|
|
|
)
|
|
|
|
})
|
2020-12-25 01:21:37 +09:00
|
|
|
|
2022-04-23 17:43:36 +09:00
|
|
|
test('should create empty borrowed buffer with finalize', (t) => {
|
2022-04-26 16:14:37 +09:00
|
|
|
t.throws(() => bindings.createEmptyBorrowedBufferWithFinalize().toString(), {
|
|
|
|
message: 'Borrowed data should not be null',
|
|
|
|
})
|
|
|
|
t.throws(() => bindings.createEmptyBorrowedBufferWithFinalize().toString(), {
|
|
|
|
message: 'Borrowed data should not be null',
|
|
|
|
})
|
2022-04-23 17:43:36 +09:00
|
|
|
})
|
|
|
|
|
|
|
|
test('should create empty buffer', (t) => {
|
|
|
|
t.is(bindings.createEmptyBuffer().toString(), '')
|
|
|
|
t.is(bindings.createEmptyBuffer().toString(), '')
|
|
|
|
})
|
|
|
|
|
2020-12-25 01:21:37 +09:00
|
|
|
test('should be able to mutate buffer', (t) => {
|
|
|
|
const fixture = Buffer.from([0, 1])
|
|
|
|
bindings.mutateBuffer(fixture)
|
|
|
|
t.is(fixture[1], 42)
|
|
|
|
})
|