feat(Struct)
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
import { describe, test, expect } from 'vitest'
|
||||
import { filledDataView, sizedDataView } from '.'
|
||||
import { parse, serialize, sizeof, sizeofHead } from '../src'
|
||||
import { ConstArray, ConstString } from '../src/type'
|
||||
|
||||
describe('serialize', () => {
|
||||
test('Number', () => {
|
||||
@ -15,16 +14,6 @@ describe('serialize', () => {
|
||||
serialize(dv, 1532.625, Number)
|
||||
expect(dv).toEqual(expected)
|
||||
})
|
||||
test('ConstString', () => {
|
||||
const expected = filledDataView([0x68, 0x65, 0x6C, 0x00, 0x00])
|
||||
let dv = sizedDataView(4)
|
||||
expect(() => serialize(dv, 'hello', ConstString(5))).toThrow()
|
||||
|
||||
dv = sizedDataView(5)
|
||||
|
||||
serialize(dv, 'hello', ConstString(3))
|
||||
expect(dv).toEqual(expected)
|
||||
})
|
||||
test('String', () => {
|
||||
const expected = filledDataView([
|
||||
0x00, 0x00, 0x00, 0x05,
|
||||
@ -39,20 +28,6 @@ describe('serialize', () => {
|
||||
serialize(dv, 'hello', String)
|
||||
expect(dv).toEqual(expected)
|
||||
})
|
||||
test('ConstArray, Number', () => {
|
||||
const expected = filledDataView([
|
||||
0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
])
|
||||
let dv = sizedDataView(15)
|
||||
expect(() => serialize(dv, [1, 2], ConstArray(2), Number)).toThrow()
|
||||
|
||||
dv = sizedDataView(sizeof(ConstArray(2), Number))
|
||||
expect(16).toEqual(dv.byteLength)
|
||||
|
||||
serialize(dv, [1, 2], ConstArray(2), Number)
|
||||
expect(dv).toEqual(expected)
|
||||
})
|
||||
test('Array, Number', () => {
|
||||
const expected = filledDataView([
|
||||
0x00, 0x00, 0x00, 0x02,
|
||||
@ -77,12 +52,6 @@ describe('parse', () => {
|
||||
const actual = parse(dv, Number)
|
||||
expect(actual).toEqual(expected)
|
||||
})
|
||||
test('ConstString', () => {
|
||||
const expected = 'hel'
|
||||
const dv = filledDataView([0x68, 0x65, 0x6C, 0x6C, 0x6F])
|
||||
const actual = parse(dv, ConstString(3))
|
||||
expect(actual).toEqual(expected)
|
||||
})
|
||||
test('String', () => {
|
||||
const expected = 'hello'
|
||||
const dv = filledDataView([
|
||||
@ -92,15 +61,6 @@ describe('parse', () => {
|
||||
const actual = parse(dv, String)
|
||||
expect(actual).toEqual(expected)
|
||||
})
|
||||
test('ConstArray, Number', () => {
|
||||
const expected = [1, 2]
|
||||
const dv = filledDataView([
|
||||
0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
])
|
||||
const actual = parse(dv, ConstArray(2), Number)
|
||||
expect(actual).toEqual(expected)
|
||||
})
|
||||
test('Array, Number', () => {
|
||||
const expected = [1, 2]
|
||||
const dv = filledDataView([
|
||||
@ -116,21 +76,15 @@ describe('parse', () => {
|
||||
describe('sizeofHead', () => {
|
||||
test('Number', () => {
|
||||
expect(sizeofHead(Number)).toEqual(8)
|
||||
expect(sizeofHead(1)).toEqual(8)
|
||||
})
|
||||
test('ConstString', () => {
|
||||
expect(sizeofHead(ConstString(2))).toEqual(2)
|
||||
expect(sizeofHead(1, Number)).toEqual(8)
|
||||
})
|
||||
test('String', () => {
|
||||
expect(sizeofHead(String)).toEqual(4)
|
||||
expect(sizeofHead('s')).toEqual(4)
|
||||
})
|
||||
test('ConstArray', () => {
|
||||
expect(sizeofHead(ConstArray(2), Number)).toEqual(16)
|
||||
expect(sizeofHead('s', String)).toEqual(4)
|
||||
})
|
||||
test('Array', () => {
|
||||
expect(sizeofHead(Array, Number)).toEqual(4)
|
||||
expect(sizeofHead([1])).toEqual(4)
|
||||
expect(sizeofHead([1], Array, Number)).toEqual(4)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user