scroll/src/common/all_test.ts

178 lines
4.7 KiB
JavaScript
Raw Normal View History

import { Ansi, KeyCommand, readKey } from './ansi.ts';
import Buffer from './buffer.ts';
2023-11-21 15:14:08 -05:00
import Document from './document.ts';
import Editor from './editor.ts';
import Row from './row.ts';
import { getTestRunner } from './runtime.ts';
import { defaultTerminalSize } from './termios.ts';
2023-11-21 15:14:08 -05:00
import * as Util from './utils.ts';
const {
assertEquals,
assertExists,
assertInstanceOf,
assertNotEquals,
assertFalse,
assertTrue,
testSuite,
} = await getTestRunner();
2023-11-21 15:14:08 -05:00
testSuite({
'ANSI::ANSI utils': {
'Ansi.moveCursor': () => {
assertEquals(Ansi.moveCursor(1, 2), '\x1b[2;3H');
},
'Ansi.moveCursorForward': () => {
assertEquals(Ansi.moveCursorForward(2), '\x1b[2C');
},
'Ansi.moveCursorDown': () => {
assertEquals(Ansi.moveCursorDown(7), '\x1b[7B');
},
},
'ANSI::readKey()': {
'readKey passthrough': () => {
// Ignore unhandled escape sequences
assertEquals(readKey('\x1b[]'), '\x1b[]');
// Pass explicitly mapped values right through
assertEquals(readKey(KeyCommand.ArrowUp), KeyCommand.ArrowUp);
assertEquals(readKey(KeyCommand.Home), KeyCommand.Home);
assertEquals(readKey(KeyCommand.Delete), KeyCommand.Delete);
},
'readKey Home': () => {
['\x1b[1~', '\x1b[7~', '\x1b[H', '\x1bOH'].forEach((code) => {
assertEquals(readKey(code), KeyCommand.Home);
});
},
'readKey End': () => {
['\x1b[4~', '\x1b[8~', '\x1b[F', '\x1bOF'].forEach((code) => {
assertEquals(readKey(code), KeyCommand.End);
});
},
},
Buffer: {
'Buffer exists': () => {
const b = new Buffer();
assertInstanceOf(b, Buffer);
assertEquals(b.strlen(), 0);
},
'Buffer.appendLine': () => {
const b = new Buffer();
// Carriage return and line feed
b.appendLine();
assertEquals(b.strlen(), 2);
b.clear();
assertEquals(b.strlen(), 0);
b.appendLine('foo');
assertEquals(b.strlen(), 5);
},
'Buffer.append': () => {
const b = new Buffer();
b.append('foobar');
assertEquals(b.strlen(), 6);
b.clear();
b.append('foobar', 3);
assertEquals(b.strlen(), 3);
},
'Buffer.flush': async () => {
const b = new Buffer();
b.appendLine('foobarbaz' + Ansi.ClearLine);
assertEquals(b.strlen(), 14);
await b.flush();
assertEquals(b.strlen(), 0);
},
},
Document: {
'Document.empty': () => {
const doc = Document.empty();
assertEquals(doc.numRows, 0);
assertTrue(doc.isEmpty());
assertEquals(doc.row(0), null);
},
'Document.appendRow': () => {
const doc = Document.empty();
doc.appendRow('foobar');
assertEquals(doc.numRows, 1);
assertFalse(doc.isEmpty());
assertInstanceOf(doc.row(0), Row);
},
},
Editor: {
'new Editor': () => {
const e = new Editor(defaultTerminalSize);
assertInstanceOf(e, Editor);
},
},
2023-11-21 15:14:08 -05:00
Row: {
'new Row': () => {
const row = new Row();
assertEquals(row.toString(), '');
},
},
'Util::Misc fns': {
'noop fn': () => {
2023-11-21 15:14:08 -05:00
assertExists(Util.noop);
assertEquals(Util.noop(), undefined);
},
},
'Util::String fns': {
'ord()': () => {
// Invalid output
2023-11-21 15:14:08 -05:00
assertEquals(Util.ord(''), 256);
// Valid output
2023-11-21 15:14:08 -05:00
assertEquals(Util.ord('a'), 97);
},
'chars() properly splits strings into unicode characters': () => {
2023-11-21 15:14:08 -05:00
assertEquals(Util.chars('😺😸😹'), ['😺', '😸', '😹']);
},
'ctrl_key()': () => {
2023-11-21 15:14:08 -05:00
const ctrl_a = Util.ctrlKey('a');
assertTrue(Util.isControl(ctrl_a));
assertEquals(ctrl_a, String.fromCodePoint(0x01));
2023-11-21 15:14:08 -05:00
const invalid = Util.ctrlKey('😺');
assertFalse(Util.isControl(invalid));
assertEquals(invalid, '😺');
},
'is_ascii()': () => {
2023-11-21 15:14:08 -05:00
assertTrue(Util.isAscii('asjyverkjhsdf1928374'));
assertFalse(Util.isAscii('😺acalskjsdf'));
assertFalse(Util.isAscii('ab😺ac'));
},
'is_control()': () => {
2023-11-21 15:14:08 -05:00
assertFalse(Util.isControl('abc'));
assertTrue(Util.isControl(String.fromCodePoint(0x01)));
assertFalse(Util.isControl('😺'));
},
'strlen()': () => {
// Ascii length
2023-11-21 15:14:08 -05:00
assertEquals(Util.strlen('abc'), 'abc'.length);
// Get number of visible unicode characters
2023-11-21 15:14:08 -05:00
assertEquals(Util.strlen('😺😸😹'), 3);
assertNotEquals('😺😸😹'.length, Util.strlen('😺😸😹'));
// Skin tone modifier + base character
2023-11-21 15:14:08 -05:00
assertEquals(Util.strlen('🤰🏼'), 2);
assertNotEquals('🤰🏼'.length, Util.strlen('🤰🏼'));
// This has 4 sub-characters, and 3 zero-width-joiners
2023-11-21 15:14:08 -05:00
assertEquals(Util.strlen('👨‍👩‍👧‍👦'), 7);
assertNotEquals('👨‍👩‍👧‍👦'.length, Util.strlen('👨‍👩‍👧‍👦'));
},
'truncate()': () => {
2023-11-21 15:14:08 -05:00
assertEquals(Util.truncate('😺😸😹', 1), '😺');
assertEquals(Util.truncate('😺😸😹', 5), '😺😸😹');
assertEquals(Util.truncate('👨‍👩‍👧‍👦', 5), '👨‍👩‍👧');
},
},
2023-11-21 15:14:08 -05:00
});