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