occulto/test/encoding.spec.ts

39 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

2024-07-29 16:14:12 +02:00
import { describe, expect, it } from 'vitest'
2022-10-18 16:59:33 +02:00
import { Base64, Bytes, Hex } from '../dist/index.js'
import { Precomputed } from './values.js'
describe('Encoding', () => {
2022-10-18 17:27:14 +02:00
describe('Bytes', () => {
for (const [input, output] of Object.entries(Precomputed.Encoding.Bytes)) {
it(`Should encode ${input} to ${output}`, async () => {
2024-07-29 16:14:12 +02:00
expect(Bytes.encode(input).buffer).toEqual(output.buffer)
2022-10-18 17:27:14 +02:00
})
it(`Should decode ${output} to ${input}`, async () => {
2024-07-29 16:14:12 +02:00
expect(Bytes.decode(output)).toEqual(input)
2022-10-18 17:27:14 +02:00
})
}
})
2022-10-18 16:59:33 +02:00
describe('Hex', () => {
for (const [input, output] of Object.entries(Precomputed.Encoding.Hex)) {
const buffer = Bytes.encode(input)
it(`Should encode ${input} to ${output}`, async () => {
2024-07-29 16:14:12 +02:00
expect(Hex.encode(buffer)).toEqual(output)
2022-10-18 16:59:33 +02:00
})
it(`Should decode ${output} to ${input}`, async () => {
2024-07-29 16:14:12 +02:00
expect(Hex.decode(output).buffer).toEqual(buffer.buffer)
2022-10-18 16:59:33 +02:00
})
}
})
describe('Base64', () => {
for (const [input, output] of Object.entries(Precomputed.Encoding.Base64)) {
const buffer = Bytes.encode(input)
it(`Should encode ${input} to ${output}`, async () => {
2024-07-29 16:14:12 +02:00
expect(await Base64.encode(buffer)).toEqual(output)
})
it(`Should decode ${output} to ${input}`, async () => {
2024-07-29 16:14:12 +02:00
expect((await Base64.decode(output)).buffer).toEqual(buffer.buffer)
})
}
})
})