-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtext.test.ts
49 lines (40 loc) · 1.31 KB
/
text.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { decodeUtf8, hex, uint8Array, uint8ArrayToHex } from '../src/text'
describe('text', () => {
describe('decodeUtf8', () => {
test('decodes ascii bytes', () => {
expect(decodeUtf8('a')).toEqual('a')
})
test('decodes empty string', () => {
expect(decodeUtf8('')).toEqual('')
})
test('decodes null value', () => {
expect(decodeUtf8(null)).toEqual('')
})
test('decodes undefined value', () => {
expect(decodeUtf8(undefined)).toEqual('')
})
test('decodes multi-byte characters', () => {
expect(decodeUtf8('\xF0\x9F\xA4\x94')).toEqual('🤔')
})
})
describe('hex', () => {
test('encodes binary as hex', () => {
expect(hex('\0\0')).toEqual('0x0000')
})
test('encodes ascii as hex', () => {
expect(hex('aa')).toEqual('0x6161')
})
})
describe('uint8Array', () => {
test('converts to an array of 8-bit unsigned integers', () => {
expect(uint8Array('')).toEqual(new Uint8Array([]))
expect(uint8Array('Å')).toEqual(new Uint8Array([197]))
})
})
describe('uint8ArrayToHex', () => {
test('converts an array of 8-bit unsigned integers to hex', () => {
expect(uint8ArrayToHex(new Uint8Array([]))).toEqual("x''")
expect(uint8ArrayToHex(new Uint8Array([197]))).toEqual("x'c5'")
})
})
})