fix(Array, {HeadlessType})
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
import { describe, test, expect } from 'vitest'
|
||||
import { expectDataViewEqual, filledDataView, sizedDataView } from '.'
|
||||
import { parse, serialize, sizeof, sizeofHead } from '../src'
|
||||
import { isHeadless, parse, serialize, sizeof, sizeofHead } from '../src'
|
||||
|
||||
describe('serialize', () => {
|
||||
test('Boolean', () => {
|
||||
@ -32,7 +32,7 @@ describe('serialize', () => {
|
||||
let dv = sizedDataView(8)
|
||||
expect(() => serialize(dv, 'hello', String)).toThrow()
|
||||
|
||||
dv = sizedDataView(sizeof('hello'))
|
||||
dv = sizedDataView(sizeof('hello', String))
|
||||
expect(9).toEqual(dv.byteLength)
|
||||
|
||||
serialize(dv, 'hello', String)
|
||||
@ -47,12 +47,30 @@ describe('serialize', () => {
|
||||
let dv = sizedDataView(19)
|
||||
expect(() => serialize(dv, [1, 2], Array, Number)).toThrow()
|
||||
|
||||
dv = sizedDataView(sizeof([1, 2]))
|
||||
dv = sizedDataView(sizeof([1, 2], Array, Number))
|
||||
expect(20).toEqual(dv.byteLength)
|
||||
|
||||
serialize(dv, [1, 2], Array, Number)
|
||||
expectDataViewEqual(dv, expected)
|
||||
})
|
||||
test('Array, String', () => {
|
||||
const expected = filledDataView([
|
||||
0x00, 0x00, 0x00, 0x02,
|
||||
0x00, 0x00, 0x00, 0x0C,
|
||||
0x00, 0x00, 0x00, 0x15,
|
||||
// hello
|
||||
0x00, 0x00, 0x00, 0x05,
|
||||
0x68, 0x65, 0x6C, 0x6C, 0x6F,
|
||||
// sek1ro
|
||||
0x00, 0x00, 0x00, 0x06,
|
||||
0x73, 0x65, 0x6b, 0x31, 0x72, 0x6f,
|
||||
])
|
||||
const array = ['hello', 'sek1ro']
|
||||
const actual = sizedDataView(sizeof(array, Array, String))
|
||||
|
||||
serialize(actual, array, Array, String)
|
||||
expectDataViewEqual(actual, expected)
|
||||
})
|
||||
test('DataView', () => {
|
||||
const expected = filledDataView([
|
||||
0x00, 0x00, 0x00, 0x04,
|
||||
@ -100,6 +118,22 @@ describe('parse', () => {
|
||||
const actual = parse(dv, Array, Number)
|
||||
expect(actual).toEqual(expected)
|
||||
})
|
||||
test('Array, String', () => {
|
||||
const expected = ['hello', 'sek1ro']
|
||||
const dv = filledDataView([
|
||||
0x00, 0x00, 0x00, 0x02,
|
||||
0x00, 0x00, 0x00, 0x0C,
|
||||
0x00, 0x00, 0x00, 0x15,
|
||||
// hello
|
||||
0x00, 0x00, 0x00, 0x05,
|
||||
0x68, 0x65, 0x6C, 0x6C, 0x6F,
|
||||
// sek1ro
|
||||
0x00, 0x00, 0x00, 0x06,
|
||||
0x73, 0x65, 0x6b, 0x31, 0x72, 0x6f,
|
||||
])
|
||||
const actual = parse(dv, Array, String)
|
||||
expect(actual).toEqual(expected)
|
||||
})
|
||||
test('DataView', () => {
|
||||
const expected = filledDataView([
|
||||
0x00, 0x01, 0x02, 0x03,
|
||||
@ -113,22 +147,28 @@ describe('parse', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('isHeadless', () => {
|
||||
test('Array', () => {
|
||||
expect(isHeadless(Array, Number)).toBeTruthy()
|
||||
expect(isHeadless(Array, String)).toBeTruthy()
|
||||
})
|
||||
})
|
||||
|
||||
describe('sizeofHead', () => {
|
||||
test('Boolean', () => {
|
||||
expect(sizeofHead(Boolean)).toEqual(1)
|
||||
})
|
||||
test('Number', () => {
|
||||
expect(sizeofHead(Number)).toEqual(8)
|
||||
expect(sizeofHead(1, Number)).toEqual(8)
|
||||
})
|
||||
test('String', () => {
|
||||
expect(sizeofHead(String)).toEqual(4)
|
||||
expect(sizeofHead('s', String)).toEqual(4)
|
||||
})
|
||||
test('Array', () => {
|
||||
expect(sizeofHead(Array, Number)).toEqual(4)
|
||||
expect(sizeofHead([1], Array, Number)).toEqual(4)
|
||||
})
|
||||
test('DataView', () => {
|
||||
expect(sizeofHead(DataView)).toEqual(4)
|
||||
expect(sizeofHead(filledDataView([0x01]), DataView)).toEqual(4)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user