45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
import { describe, expect, test } from "vitest";
|
|
import { ConstString, Int, isHeadless, parse, Sequence, serialize, sizeof } from "../src";
|
|
import { expectDataViewEqual } from '.'
|
|
import { filledDataView, sizedDataView } from '../src'
|
|
|
|
describe(Sequence.name, () => {
|
|
const Type = Sequence([Int(8, 'unsigned'), String, Boolean])
|
|
const type = [255, 'hello', true]
|
|
const type_dv = filledDataView([
|
|
0xFF,
|
|
0x00, 0x00, 0x00, 0x05,
|
|
0x68, 0x65, 0x6C, 0x6C, 0x6F,
|
|
0x01,
|
|
])
|
|
|
|
test('serialize', () => {
|
|
let actual = sizedDataView(10)
|
|
expect(() => serialize(actual, type, Type)).toThrow()
|
|
|
|
actual = sizedDataView(sizeof(type, Type))
|
|
serialize(actual, type, Type)
|
|
|
|
expectDataViewEqual(actual, type_dv)
|
|
})
|
|
|
|
test('parse', () => {
|
|
expect(parse(type_dv, Type)).toEqual(type)
|
|
})
|
|
|
|
test('isHeadless', () => {
|
|
expect(isHeadless(Sequence([Number, String]))).toBeTruthy()
|
|
expect(isHeadless(Sequence([Number, ConstString(4)]))).toBeFalsy()
|
|
})
|
|
|
|
test('sizeof', () => {
|
|
expect(sizeof(Sequence([Number, ConstString(4)]))).toEqual(12)
|
|
|
|
expect(() => sizeof(Sequence([Number, String]))).toThrow()
|
|
expect(() => sizeof([1], Sequence([Number, String]))).toThrow()
|
|
expect(() => sizeof([1, 'hello', 3], Sequence([Number, String]))).toThrow()
|
|
|
|
expect(sizeof([1, 'hello'], Sequence([Number, String]))).toEqual(17)
|
|
})
|
|
})
|