Browse Source

test: fixes test/disabled/test-readline.js

This test is still in test/disabled because it requires a tty, however
when executed directly this test now passes.

Signed-off-by: Fedor Indutny <fedor@indutny.com>
archived-io.js-v0.10
Dan Kaplun 11 years ago
committed by Fedor Indutny
parent
commit
52527e03d3
  1. 129
      test/disabled/test-readline.js

129
test/disabled/test-readline.js

@ -30,37 +30,40 @@ var readline = require('readline');
var key = { var key = {
xterm: { xterm: {
home: [27, 91, 72], home: ['\x1b[H', {ctrl: true, name: 'a'}],
end: [27, 91, 70], end: ['\x1b[F', {ctrl: true, name: 'e'}],
metab: [27, 98], metab: ['\x1bb', {meta: true, name: 'b'}],
metaf: [27, 102], metaf: ['\x1bf', {meta: true, name: 'f'}],
metad: [27, 100] metad: ['\x1bd', {meta: true, name: 'd'}]
}, },
gnome: { gnome: {
home: [27, 79, 72], home: ['\x1bOH', {ctrl: true, name: 'a'}],
end: [27, 79, 70] end: ['\x1bOF', {ctrl: true, name: 'e'}]
}, },
rxvt: { rxvt: {
home: [27, 91, 55], home: ['\x1b[7', {ctrl: true, name: 'a'}],
end: [27, 91, 56] end: ['\x1b[8', {ctrl: true, name: 'e'}]
}, },
putty: { putty: {
home: [27, 91, 49, 126], home: ['\x1b[1~', {ctrl: true, name: 'a'}],
end: [27, 91, 52, 126] end: ['\x1b[>~', {ctrl: true, name: 'e'}]
} }
}; };
var readlineFakeStream = function() { var readlineFakeStream = function() {
var written_bytes = []; var written_bytes = [];
var rl = readline.createInterface( var rl = readline.createInterface(
{ fd: 1, { input: process.stdin,
write: function(b) { output: process.stdout,
written_bytes.push(b); completer: function(text) {
return [[], ''];
} }
},
function(text) {
return [[], ''];
}); });
var _stdoutWrite = process.stdout.write;
process.stdout.write = function (data) {
data.split('').forEach(rl.written_bytes.push.bind(rl.written_bytes));
_stdoutWrite.apply(this, arguments);
}
rl.written_bytes = written_bytes; rl.written_bytes = written_bytes;
return rl; return rl;
}; };
@ -70,67 +73,43 @@ var written_bytes_length, refreshed;
rl.write('foo'); rl.write('foo');
assert.equal(3, rl.cursor); assert.equal(3, rl.cursor);
rl.write(key.xterm.home); [key.xterm, key.rxvt, key.gnome, key.putty].forEach(function (key) {
assert.equal(0, rl.cursor); rl.write.apply(rl, key.home);
rl.write(key.xterm.end); assert.equal(0, rl.cursor);
assert.equal(3, rl.cursor); rl.write.apply(rl, key.end);
rl.write(key.rxvt.home); assert.equal(3, rl.cursor);
assert.equal(0, rl.cursor); });
rl.write(key.rxvt.end);
assert.equal(3, rl.cursor);
rl.write(key.gnome.home);
assert.equal(0, rl.cursor);
rl.write(key.gnome.end);
assert.equal(3, rl.cursor);
rl.write(key.putty.home);
assert.equal(0, rl.cursor);
rl.write(key.putty.end);
assert.equal(3, rl.cursor);
rl = readlineFakeStream(); rl = readlineFakeStream();
rl.write('foo bar.hop/zoo'); rl.write('foo bar.hop/zoo');
rl.write(key.xterm.home); rl.write.apply(rl, key.xterm.home);
written_bytes_length = rl.written_bytes.length; [
rl.write(key.xterm.metaf); {cursor: 4, key: key.xterm.metaf},
assert.equal(3, rl.cursor); {cursor: 7, key: key.xterm.metaf},
refreshed = written_bytes_length !== rl.written_bytes.length; {cursor: 8, key: key.xterm.metaf},
assert.equal(true, refreshed); {cursor: 11, key: key.xterm.metaf},
rl.write(key.xterm.metaf); {cursor: 12, key: key.xterm.metaf},
assert.equal(7, rl.cursor); {cursor: 15, key: key.xterm.metaf},
rl.write(key.xterm.metaf); {cursor: 12, key: key.xterm.metab},
assert.equal(11, rl.cursor); {cursor: 11, key: key.xterm.metab},
written_bytes_length = rl.written_bytes.length; {cursor: 8, key: key.xterm.metab},
rl.write(key.xterm.metaf); {cursor: 7, key: key.xterm.metab},
assert.equal(15, rl.cursor); {cursor: 4, key: key.xterm.metab},
refreshed = written_bytes_length !== rl.written_bytes.length; {cursor: 0, key: key.xterm.metab},
assert.equal(true, refreshed); ].forEach(function (action) {
written_bytes_length = rl.written_bytes.length; written_bytes_length = rl.written_bytes.length;
rl.write(key.xterm.metab); rl.write.apply(rl, action.key);
assert.equal(12, rl.cursor); assert.equal(action.cursor, rl.cursor);
refreshed = written_bytes_length !== rl.written_bytes.length; refreshed = written_bytes_length !== rl.written_bytes.length;
assert.equal(true, refreshed); assert.equal(true, refreshed);
rl.write(key.xterm.metab); });
assert.equal(8, rl.cursor);
rl.write(key.xterm.metab);
assert.equal(4, rl.cursor);
written_bytes_length = rl.written_bytes.length;
rl.write(key.xterm.metab);
assert.equal(0, rl.cursor);
refreshed = written_bytes_length !== rl.written_bytes.length;
assert.equal(true, refreshed);
rl = readlineFakeStream(); rl = readlineFakeStream();
rl.write('foo bar.hop/zoo'); rl.write('foo bar.hop/zoo');
rl.write(key.xterm.home); rl.write.apply(rl, key.xterm.home);
rl.write(key.xterm.metad); ['bar.hop/zoo', '.hop/zoo', 'hop/zoo', '/zoo', 'zoo', ''].forEach(function (expectedLine) {
assert.equal(0, rl.cursor); rl.write.apply(rl, key.xterm.metad);
assert.equal(' bar.hop/zoo', rl.line); assert.equal(0, rl.cursor);
rl.write(key.xterm.metad); assert.equal(expectedLine, rl.line);
assert.equal(0, rl.cursor); });
assert.equal('.hop/zoo', rl.line); rl.close();
rl.write(key.xterm.metad);
assert.equal(0, rl.cursor);
assert.equal('/zoo', rl.line);
rl.write(key.xterm.metad);
assert.equal(0, rl.cursor);
assert.equal('', rl.line);

Loading…
Cancel
Save