scroll/src/common/strings_test.ts

56 lines
1.7 KiB
JavaScript
Raw Normal View History

import { importDefaultForRuntime, ITestBase } from './mod.ts';
2023-11-09 12:05:30 -05:00
import {
chars,
ctrl_key,
is_ascii,
is_control,
strlen,
truncate,
} from './strings.ts';
2023-11-02 13:06:48 -04:00
2023-11-03 11:59:58 -04:00
const t: ITestBase = await importDefaultForRuntime('test_base');
2023-11-02 13:06:48 -04:00
2023-11-03 11:59:58 -04:00
t.test('chars fn properly splits strings into unicode characters', () => {
t.assertEquals(chars('😺😸😹'), ['😺', '😸', '😹']);
});
2023-11-09 12:05:30 -05:00
t.test('ctrl_key fn returns expected values', () => {
const ctrl_a = ctrl_key('a');
t.assertTrue(is_control(ctrl_a));
t.assertEquals(ctrl_a, String.fromCodePoint(0x01));
const invalid = ctrl_key('😺');
t.assertFalse(is_control(invalid));
t.assertEquals(invalid, '😺');
});
t.test('is_ascii properly discerns ascii chars', () => {
2023-11-03 11:59:58 -04:00
t.assertTrue(is_ascii('asjyverkjhsdf1928374'));
t.assertFalse(is_ascii('😺acalskjsdf'));
2023-11-02 13:06:48 -04:00
});
2023-11-09 12:05:30 -05:00
t.test('is_control fn works as expected', () => {
t.assertFalse(is_control('abc'));
t.assertTrue(is_control(String.fromCodePoint(0x01)));
t.assertFalse(is_control('😺'));
});
t.test('strlen fn returns expected length for multibyte characters', () => {
t.assertEquals(strlen('😺😸😹'), 3);
t.assertNotEquals('😺😸😹'.length, strlen('😺😸😹'));
// Skin tone modifier + base character
t.assertEquals(strlen('🤰🏼'), 2);
t.assertNotEquals('🤰🏼'.length, strlen('🤰🏼'));
// This has 4 sub-characters, and 3 zero-width-joiners
t.assertEquals(strlen('👨‍👩‍👧‍👦'), 7);
t.assertNotEquals('👨‍👩‍👧‍👦'.length, strlen('👨‍👩‍👧‍👦'));
});
t.test('truncate shortens strings', () => {
t.assertEquals(truncate('😺😸😹', 1), '😺');
t.assertEquals(truncate('😺😸😹', 5), '😺😸😹');
t.assertEquals(truncate('👨‍👩‍👧‍👦', 5), '👨‍👩‍👧');
});