Skip to content

Commit

Permalink
Updated test cases for toArray
Browse files Browse the repository at this point in the history
  • Loading branch information
BraydenLangley committed Feb 27, 2025
1 parent 4f761fd commit cc4a753
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion src/primitives/__tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ describe('utils', () => {
expect(toArray('1234', 'hex')).toEqual([0x12, 0x34])
expect(toArray('1234')).toEqual([49, 50, 51, 52])
expect(toArray('1234', 'utf8')).toEqual([49, 50, 51, 52])
expect(toArray('\u1234234')).toEqual([18, 52, 50, 51, 52])
expect(toArray('\u1234', 'utf8')).toEqual([225, 136, 180])
expect(toArray('\u1234' + '234', 'utf8')).toEqual([225, 136, 180, 50, 51, 52])
expect(toArray([1, 2, 3, 4])).toEqual([1, 2, 3, 4])
})

Expand Down Expand Up @@ -156,4 +157,53 @@ describe('utils', () => {
})
})
})

test('should return an empty array for an empty string', () => {
expect(toArray("")).toEqual([])
})

test('should encode ASCII characters correctly', () => {
const input = "Hello, World!"
const expected = [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]
expect(toArray(input)).toEqual(expected)
})

test('should encode 2-byte characters correctly', () => {
// "é" (U+00E9) should encode to [0xC3, 0xA9]
expect(toArray("é")).toEqual([0xC3, 0xA9])
})

test('should encode 3-byte characters correctly', () => {
// "€" (U+20AC) should encode to [0xE2, 0x82, 0xAC]
expect(toArray("€")).toEqual([0xE2, 0x82, 0xAC])
})

test('should encode 4-byte characters correctly', () => {
// "😃" (U+1F603) should encode to [0xF0, 0x9F, 0x98, 0x83]
expect(toArray("😃")).toEqual([0xF0, 0x9F, 0x98, 0x83])
})

test('should encode mixed content correctly', () => {
// "Hello, 😃! €" contains ASCII, an emoji, and a 3-byte character.
const input = "Hello, 😃! €"
const expected = [
// "Hello, " => ASCII bytes:
72, 101, 108, 108, 111, 44, 32,
// "😃" => 4-byte sequence:
0xF0, 0x9F, 0x98, 0x83,
// "!" => ASCII, then space:
33, 32,
// "€" => 3-byte sequence:
0xE2, 0x82, 0xAC
]
expect(toArray(input)).toEqual(expected)
})

test('should replace lone surrogates with the replacement character', () => {
// An unpaired high surrogate "\uD800" should be replaced with U+FFFD,
// which is encoded in UTF-8 as [0xEF, 0xBF, 0xBD]
const input = "\uD800"
const expected = [0xEF, 0xBF, 0xBD]
expect(toArray(input)).toEqual(expected)
})
})

0 comments on commit cc4a753

Please sign in to comment.