]>
Commit | Line | Data |
---|---|---|
d76d5067 JB |
1 | var glob = require('glob'); |
2 | var fs = require('fs'); | |
bebe43e5 | 3 | var pty = require('node-pty'); |
d76d5067 | 4 | var sleep = require('sleep'); |
56ecc77d | 5 | var Terminal = require('../xterm'); |
d76d5067 JB |
6 | |
7 | var CONSOLE_LOG = console.log; | |
8 | ||
9 | // expect files need terminal at 80x25! | |
10 | var COLS = 80; | |
11 | var ROWS = 25; | |
12 | ||
dcf30b40 JB |
13 | /** some helpers for pty interaction */ |
14 | // we need a pty in between to get the termios decorations | |
15 | // for the basic test cases a raw pty device is enough | |
d76d5067 JB |
16 | var primitive_pty = pty.native.open(COLS, ROWS); |
17 | ||
18 | // fake sychronous pty write - read | |
dcf30b40 | 19 | // we just pipe the data from slave to master as a child program would do |
d76d5067 JB |
20 | // pty.js opens pipe fds with O_NONBLOCK |
21 | // just wait 10ms instead of setting fds to blocking mode | |
dcf30b40 JB |
22 | function pty_write_read(s) { |
23 | fs.writeSync(primitive_pty.slave, s); | |
24 | sleep.usleep(10000); | |
25 | var b = Buffer(64000); | |
26 | var bytes = fs.readSync(primitive_pty.master, b, 0, 64000); | |
27 | return b.toString('utf8', 0, bytes); | |
d76d5067 JB |
28 | } |
29 | ||
ed1a31d1 | 30 | // make sure raw pty is at x=0 and has no pending data |
dcf30b40 JB |
31 | function pty_reset() { |
32 | pty_write_read('\r\n'); | |
33 | } | |
34 | ||
35 | /* debug helpers */ | |
36 | // generate colorful noisy output to compare xterm and emulator cell states | |
d76d5067 | 37 | function formatError(in_, out_, expected) { |
dcf30b40 JB |
38 | function addLineNumber(start, color) { |
39 | var counter = start || 0; | |
40 | return function(s) { | |
41 | counter += 1; | |
42 | return '\x1b[33m' + (' ' + counter).slice(-2) + color + s; | |
d76d5067 | 43 | } |
dcf30b40 JB |
44 | } |
45 | var line80 = '12345678901234567890123456789012345678901234567890123456789012345678901234567890'; | |
46 | var s = ''; | |
47 | s += '\n\x1b[34m' + JSON.stringify(in_); | |
48 | s += '\n\x1b[33m ' + line80 + '\n'; | |
49 | s += out_.split('\n').map(addLineNumber(0, '\x1b[31m')).join('\n'); | |
50 | s += '\n\x1b[33m ' + line80 + '\n'; | |
51 | s += expected.split('\n').map(addLineNumber(0, '\x1b[32m')).join('\n'); | |
52 | return s; | |
d76d5067 JB |
53 | } |
54 | ||
55 | // simple debug output of terminal cells | |
56 | function terminalToString(term) { | |
dcf30b40 JB |
57 | var result = ''; |
58 | var line_s = ''; | |
d5f0fff5 | 59 | for (var line = term.ybase; line < term.ybase + term.rows; line++) { |
dcf30b40 JB |
60 | line_s = ''; |
61 | for (var cell=0; cell<term.cols; ++cell) { | |
607c8191 | 62 | line_s += term.lines.get(line)[cell][1]; |
d76d5067 | 63 | } |
dcf30b40 JB |
64 | // rtrim empty cells as xterm does |
65 | line_s = line_s.replace(/\s+$/, ''); | |
66 | result += line_s; | |
67 | result += '\n'; | |
68 | } | |
69 | return result; | |
d76d5067 JB |
70 | } |
71 | ||
dcf30b40 JB |
72 | /** tests */ |
73 | describe('xterm output comparison', function() { | |
74 | var xterm; | |
d76d5067 | 75 | |
dcf30b40 JB |
76 | beforeEach(function () { |
77 | xterm = new Terminal(COLS, ROWS); | |
78 | xterm.refresh = function() {}; | |
79 | }); | |
d76d5067 | 80 | |
dcf30b40 JB |
81 | // omit stack trace for escape sequence files |
82 | Error.stackTraceLimit = 0; | |
56ecc77d | 83 | var files = glob.sync('**/escape_sequence_files/*.in'); |
dcf30b40 | 84 | // only successful tests for now |
d5f0fff5 DI |
85 | var successful = [0, 2, 6, 12, 13, 18, 20, 22, 27, 28, 50]; |
86 | for (var i = 0; i < files.length; i++) { | |
87 | // if (i !== 1) continue; | |
88 | //for (var a in successful) { | |
89 | // var i = successful[a]; | |
90 | (function(filename) { | |
dcf30b40 JB |
91 | it(filename.split('/').slice(-1)[0], function () { |
92 | pty_reset(); | |
93 | var in_file = fs.readFileSync(filename, 'utf8'); | |
94 | var from_pty = pty_write_read(in_file); | |
95 | // uncomment this to get log from terminal | |
607c8191 | 96 | //console.log = function(){}; |
94c01ec3 DI |
97 | |
98 | // Perform a synchronous .write(data) | |
99 | xterm.writeBuffer.push(from_pty); | |
100 | xterm.innerWrite(); | |
101 | ||
dcf30b40 JB |
102 | var from_emulator = terminalToString(xterm); |
103 | console.log = CONSOLE_LOG; | |
104 | var expected = fs.readFileSync(filename.split('.')[0] + '.text', 'utf8'); | |
adaf604c DI |
105 | // Some of the tests have whitespace on the right of lines, we trim all the linex |
106 | // from xterm.js so ignore this for now at least. | |
107 | var expectedRightTrimmed = expected.split('\n').map(function (l) { | |
108 | return l.replace(/\s+$/, ''); | |
109 | }).join('\n'); | |
110 | if (from_emulator != expectedRightTrimmed) { | |
dcf30b40 | 111 | // uncomment to get noisy output |
d5f0fff5 DI |
112 | throw new Error(formatError(in_file, from_emulator, expected)); |
113 | // throw new Error('mismatch'); | |
dcf30b40 JB |
114 | } |
115 | }); | |
116 | })(files[i]); | |
117 | } | |
d76d5067 JB |
118 | }); |
119 |