214 lines
5.8 KiB
JavaScript
214 lines
5.8 KiB
JavaScript
import { describe, test, expect } from 'vitest'
|
|
import { expectDataViewEqual } from '.'
|
|
import { filledDataView, sizedDataView } from '../src'
|
|
import { Int, isHeadless, parse, serialize, sizeof, sizeofHead, Struct } from '../src'
|
|
|
|
describe('README', () => {
|
|
test('header', () => {
|
|
const User = Struct({
|
|
id: Int(32, 'unsigned'),
|
|
name: String,
|
|
age: Number,
|
|
array: [Array, Boolean],
|
|
raw_data: DataView,
|
|
})
|
|
|
|
const user = {
|
|
id: 1,
|
|
name: 'john',
|
|
age: 20,
|
|
array: [true, false],
|
|
raw_data: filledDataView([0x00, 0x00, 0x00, 0xFF]),
|
|
}
|
|
|
|
const dv = sizedDataView(sizeof(user, User))
|
|
serialize(dv, user, User)
|
|
|
|
const user_copy = parse(dv, User)
|
|
|
|
expect(user_copy).toEqual(user)
|
|
expectDataViewEqual(user_copy.raw_data, user.raw_data)
|
|
})
|
|
test('unknown sizeof', () => {
|
|
expect(() => sizeof(Array, Boolean)).toThrowError('unknown size of Array,Boolean')
|
|
})
|
|
})
|
|
|
|
describe('serialize', () => {
|
|
test('Boolean', () => {
|
|
const expected = filledDataView([0x01])
|
|
let actual = sizedDataView(0)
|
|
expect(() => serialize(actual, true, Boolean)).toThrow()
|
|
|
|
actual = sizedDataView(1)
|
|
|
|
serialize(actual, true, Boolean)
|
|
expectDataViewEqual(actual, expected)
|
|
})
|
|
test('Number', () => {
|
|
const expected = filledDataView([0x40, 0x97, 0xF2, 0x80, 0x00, 0x00, 0x00, 0x00])
|
|
let dv = sizedDataView(7)
|
|
expect(() => serialize(dv, 0, Number)).toThrow()
|
|
|
|
dv = sizedDataView(sizeof(Number))
|
|
expect(8).toEqual(dv.byteLength)
|
|
|
|
serialize(dv, 1532.625, Number)
|
|
expectDataViewEqual(dv, expected)
|
|
})
|
|
test('String', () => {
|
|
const expected = filledDataView([
|
|
0x00, 0x00, 0x00, 0x05,
|
|
0x68, 0x65, 0x6C, 0x6C, 0x6F,
|
|
])
|
|
let dv = sizedDataView(8)
|
|
expect(() => serialize(dv, 'hello', String)).toThrow()
|
|
|
|
dv = sizedDataView(sizeof('hello', String))
|
|
expect(9).toEqual(dv.byteLength)
|
|
|
|
serialize(dv, 'hello', String)
|
|
expectDataViewEqual(dv, expected)
|
|
})
|
|
test('Array, Number', () => {
|
|
const expected = filledDataView([
|
|
0x00, 0x00, 0x00, 0x02,
|
|
0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
])
|
|
let dv = sizedDataView(19)
|
|
expect(() => serialize(dv, [1, 2], Array, Number)).toThrow()
|
|
|
|
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,
|
|
0x00, 0x01, 0x02, 0x03,
|
|
])
|
|
const dv = filledDataView([
|
|
0x00, 0x01, 0x02, 0x03,
|
|
])
|
|
let actual = sizedDataView(7)
|
|
expect(() => serialize(actual, dv, DataView)).toThrow()
|
|
|
|
actual = sizedDataView(8)
|
|
serialize(actual, dv, DataView)
|
|
expectDataViewEqual(actual, expected)
|
|
})
|
|
})
|
|
|
|
describe('parse', () => {
|
|
test('Boolean', () => {
|
|
expect(parse(filledDataView([0x00]), Boolean)).toBeFalsy()
|
|
expect(parse(filledDataView([0x01]), Boolean)).toBeTruthy()
|
|
})
|
|
test('Number', () => {
|
|
const expected = 1532.625
|
|
const dv = filledDataView([0x40, 0x97, 0xF2, 0x80, 0x00, 0x00, 0x00, 0x00])
|
|
const actual = parse(dv, Number)
|
|
expect(actual).toEqual(expected)
|
|
})
|
|
test('String', () => {
|
|
const expected = 'hello'
|
|
const dv = filledDataView([
|
|
0x00, 0x00, 0x00, 0x05,
|
|
0x68, 0x65, 0x6C, 0x6C, 0x6F
|
|
])
|
|
const actual = parse(dv, String)
|
|
expect(actual).toEqual(expected)
|
|
})
|
|
test('Array, Number', () => {
|
|
const expected = [1, 2]
|
|
const dv = filledDataView([
|
|
0x00, 0x00, 0x00, 0x02,
|
|
0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
])
|
|
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,
|
|
])
|
|
const dv = filledDataView([
|
|
0x00, 0x00, 0x00, 0x04,
|
|
0x00, 0x01, 0x02, 0x03,
|
|
])
|
|
const actual = parse(dv, DataView)
|
|
expectDataViewEqual(actual, expected)
|
|
})
|
|
})
|
|
|
|
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)
|
|
})
|
|
test('String', () => {
|
|
expect(sizeofHead(String)).toEqual(4)
|
|
})
|
|
test('Array', () => {
|
|
expect(sizeofHead(Array, Number)).toEqual(4)
|
|
})
|
|
test('DataView', () => {
|
|
expect(sizeofHead(DataView)).toEqual(4)
|
|
})
|
|
})
|
|
|
|
describe('sizeof', () => {
|
|
test('unknown', () => {
|
|
expect(() => sizeof(String)).toThrow()
|
|
expect(() => sizeof(Array, Number)).toThrow()
|
|
expect(() => sizeof(DataView)).toThrow()
|
|
})
|
|
})
|