|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { TextDecoder, TextEncoder } from './text-common'; |
| 3 | + |
| 4 | +describe('TextDecoder', () => { |
| 5 | + const decoder = new TextDecoder(); |
| 6 | + |
| 7 | + it('decodes an ArrayBuffer', () => { |
| 8 | + expect(decoder.decode(new Uint8Array([0x68, 0x69]).buffer)).toBe('hi'); |
| 9 | + }); |
| 10 | + |
| 11 | + it('decodes only the bytes inside a typed array view', () => { |
| 12 | + const bytes = new Uint8Array([0x78, 0x78, 0x68, 0x69, 0x78]); |
| 13 | + expect(decoder.decode(bytes.subarray(2, 4))).toBe('hi'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('decodes only the bytes inside a DataView', () => { |
| 17 | + const bytes = new Uint8Array([0x78, 0x68, 0x69, 0x78]); |
| 18 | + expect(decoder.decode(new DataView(bytes.buffer, 1, 2))).toBe('hi'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('round-trips multi-byte characters through TextEncoder', () => { |
| 22 | + const encoded = new TextEncoder().encode('héllo ✓'); |
| 23 | + expect(decoder.decode(encoded)).toBe('héllo ✓'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('rejects values that are not buffers', () => { |
| 27 | + expect(() => decoder.decode('abc' as any)).toThrow(); |
| 28 | + }); |
| 29 | +}); |
0 commit comments