1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
'use strict';
const common = require('../common');
const assert = require('assert');
const repl = require('repl');
// \u001b[1G - Moves the cursor to 1st column
// \u001b[0J - Clear screen
// \u001b[3G - Moves the cursor to 3rd column
const terminalCode = '\u001b[1G\u001b[0J> \u001b[3G';
const terminalCodeRegex = new RegExp(terminalCode.replace(/\[/g, '\\['), 'g');
function run({ input, output, event, checkTerminalCodes = true }) {
const stream = new common.ArrayStream();
let found = '';
stream.write = (msg) => found += msg.replace('\r', '');
let expected = `${terminalCode}.editor\n` +
'// Entering editor mode (^D to finish, ^C to cancel)\n' +
`${input}${output}\n${terminalCode}`;
const replServer = repl.start({
prompt: '> ',
terminal: true,
input: stream,
output: stream,
useColors: false
});
stream.emit('data', '.editor\n');
stream.emit('data', input);
replServer.write('', event);
replServer.close();
if (!checkTerminalCodes) {
found = found.replace(terminalCodeRegex, '').replace(/\n/g, '');
expected = expected.replace(terminalCodeRegex, '').replace(/\n/g, '');
}
assert.strictEqual(found, expected);
}
const tests = [
{
input: '',
output: '\n(To exit, press ^C again or type .exit)',
event: { ctrl: true, name: 'c' }
},
{
input: 'var i = 1;',
output: '',
event: { ctrl: true, name: 'c' }
},
{
input: 'var i = 1;\ni + 3',
output: '\n4',
event: { ctrl: true, name: 'd' }
},
{
input: ' var i = 1;\ni + 3',
output: '\n4',
event: { ctrl: true, name: 'd' }
},
{
input: '',
output: '',
checkTerminalCodes: false,
event: null,
}
];
tests.forEach(run);
// Auto code alignment for .editor mode
function testCodeAligment({ input, cursor = 0, line = '' }) {
const stream = new common.ArrayStream();
const outputStream = new common.ArrayStream();
stream.write = () => { throw new Error('Writing not allowed!'); };
const replServer = repl.start({
prompt: '> ',
terminal: true,
input: stream,
output: outputStream,
useColors: false
});
stream.emit('data', '.editor\n');
input.split('').forEach((ch) => stream.emit('data', ch));
// Test the content of current line and the cursor position
assert.strictEqual(line, replServer.line);
assert.strictEqual(cursor, replServer.cursor);
replServer.write('', { ctrl: true, name: 'd' });
replServer.close();
// Ensure that empty lines are not saved in history
assert.notStrictEqual(replServer.history[0].trim(), '');
}
const codeAlignmentTests = [
{
input: 'var i = 1;\n'
},
{
input: ' var i = 1;\n',
cursor: 2,
line: ' '
},
{
input: ' var i = 1;\n',
cursor: 5,
line: ' '
},
{
input: ' var i = 1;\n var j = 2\n',
cursor: 2,
line: ' '
}
];
codeAlignmentTests.forEach(testCodeAligment);
|