|
1 | 1 | import { describe, expect, it } from 'vitest';
|
2 |
| -import { convertAsciiBinaryToText, convertTextToAsciiBinary } from './text-to-binary.models'; |
| 2 | +import { convertTextToUtf8Binary, convertUtf8BinaryToText } from './text-to-binary.models'; |
3 | 3 |
|
4 | 4 | describe('text-to-binary', () => {
|
5 |
| - describe('convertTextToAsciiBinary', () => { |
6 |
| - it('a text string is converted to its ascii binary representation', () => { |
7 |
| - expect(convertTextToAsciiBinary('A')).toBe('01000001'); |
8 |
| - expect(convertTextToAsciiBinary('hello')).toBe('01101000 01100101 01101100 01101100 01101111'); |
9 |
| - expect(convertTextToAsciiBinary('')).toBe(''); |
| 5 | + const utf8Tests = [ |
| 6 | + { text: '文字', binary: '11100110 10010110 10000111 11100101 10101101 10010111' }, |
| 7 | + { text: '💩', binary: '11110000 10011111 10010010 10101001' }, |
| 8 | + ]; |
| 9 | + |
| 10 | + describe('convertTextToUtf8Binary', () => { |
| 11 | + it('a text string is converted to its UTF-8 binary representation', () => { |
| 12 | + expect(convertTextToUtf8Binary('A')).toBe('01000001'); |
| 13 | + expect(convertTextToUtf8Binary('hello')).toBe('01101000 01100101 01101100 01101100 01101111'); |
| 14 | + expect(convertTextToUtf8Binary('')).toBe(''); |
10 | 15 | });
|
11 | 16 | it('the separator between octets can be changed', () => {
|
12 |
| - expect(convertTextToAsciiBinary('hello', { separator: '' })).toBe('0110100001100101011011000110110001101111'); |
| 17 | + expect(convertTextToUtf8Binary('hello', { separator: '' })).toBe('0110100001100101011011000110110001101111'); |
| 18 | + expect(convertTextToUtf8Binary('hello', { separator: '-' })).toBe('01101000-01100101-01101100-01101100-01101111'); |
| 19 | + }); |
| 20 | + it('works with non-ASCII input', () => { |
| 21 | + for (const { text, binary } of utf8Tests) { |
| 22 | + const converted = convertTextToUtf8Binary(text); |
| 23 | + expect(converted).toBe(binary); |
| 24 | + } |
13 | 25 | });
|
14 | 26 | });
|
15 | 27 |
|
16 |
| - describe('convertAsciiBinaryToText', () => { |
| 28 | + describe('convertUtf8BinaryToText', () => { |
17 | 29 | it('an ascii binary string is converted to its text representation', () => {
|
18 |
| - expect(convertAsciiBinaryToText('01101000 01100101 01101100 01101100 01101111')).toBe('hello'); |
19 |
| - expect(convertAsciiBinaryToText('01000001')).toBe('A'); |
20 |
| - expect(convertTextToAsciiBinary('')).toBe(''); |
| 30 | + expect(convertUtf8BinaryToText('01101000 01100101 01101100 01101100 01101111')).toBe('hello'); |
| 31 | + expect(convertUtf8BinaryToText('01000001')).toBe('A'); |
| 32 | + expect(convertTextToUtf8Binary('')).toBe(''); |
21 | 33 | });
|
22 | 34 |
|
23 | 35 | it('the given binary string is cleaned before conversion', () => {
|
24 |
| - expect(convertAsciiBinaryToText(' 01000 001garbage')).toBe('A'); |
| 36 | + expect(convertUtf8BinaryToText(' 01000 001garbage')).toBe('A'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('throws an error if the given binary string is not an integer number of complete octets', () => { |
| 40 | + expect(() => convertUtf8BinaryToText('010000011')).toThrow('Invalid binary string'); |
| 41 | + expect(() => convertUtf8BinaryToText('010000011 010000011')).toThrow('Invalid binary string'); |
| 42 | + expect(() => convertUtf8BinaryToText('1')).toThrow('Invalid binary string'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('throws an error if the given binary string is not valid UTF-8', () => { |
| 46 | + expect(() => convertUtf8BinaryToText('11111111')).toThrow(); |
25 | 47 | });
|
26 | 48 |
|
27 |
| - it('throws an error if the given binary string as no complete octet', () => { |
28 |
| - expect(() => convertAsciiBinaryToText('010000011')).toThrow('Invalid binary string'); |
29 |
| - expect(() => convertAsciiBinaryToText('1')).toThrow('Invalid binary string'); |
| 49 | + it('works with non-ASCII input', () => { |
| 50 | + for (const { text, binary } of utf8Tests) { |
| 51 | + const reverted = convertUtf8BinaryToText(binary); |
| 52 | + expect(reverted).toBe(text); |
| 53 | + } |
30 | 54 | });
|
31 | 55 | });
|
32 | 56 | });
|
0 commit comments