]> git.proxmox.com Git - mirror_xterm.js.git/blob - test/escape_sequences.js
adding xterm comparison tests
[mirror_xterm.js.git] / test / escape_sequences.js
1 var glob = require('glob');
2 var fs = require('fs');
3 var pty = require('pty.js');
4 var sleep = require('sleep');
5 var Terminal = require('../src/xterm');
6
7 var CONSOLE_LOG = console.log;
8
9 // expect files need terminal at 80x25!
10 var COLS = 80;
11 var ROWS = 25;
12
13 // primitive pty pipe is enough for the test cases
14 var primitive_pty = pty.native.open(COLS, ROWS);
15
16 // fake sychronous pty write - read
17 // pty.js opens pipe fds with O_NONBLOCK
18 // just wait 10ms instead of setting fds to blocking mode
19 function pty_write_read(t, s) {
20 fs.writeSync(t.slave, s);
21 sleep.usleep(10000);
22 var b = Buffer(64000);
23 var bytes = fs.readSync(t.master, b, 0, 64000);
24 return b.toString('utf8', 0, bytes);
25 }
26
27 // generate noisy output to compare xterm and emulator
28 function formatError(in_, out_, expected) {
29 function addLineNumber(start, color) {
30 var counter = start || 0;
31 return function(s) {
32 counter += 1;
33 return '\x1b[33m' + (' ' + counter).slice(-2) + color + s;
34 }
35 }
36 var line80 = '12345678901234567890123456789012345678901234567890123456789012345678901234567890';
37 var s = '';
38 s += '\n\x1b[34m' + JSON.stringify(in_);
39 s += '\n\x1b[33m ' + line80 + '\n';
40 s += out_.split('\n').map(addLineNumber(0, '\x1b[31m')).join('\n');
41 s += '\n\x1b[33m ' + line80 + '\n';
42 s += expected.split('\n').map(addLineNumber(0, '\x1b[32m')).join('\n');
43 return s;
44 }
45
46 // simple debug output of terminal cells
47 function terminalToString(term) {
48 var result = '';
49 var line_s = '';
50 for (var line=0; line<term.rows; ++line) {
51 line_s = '';
52 for (var cell=0; cell<term.cols; ++cell) {
53 line_s += term.lines[line][cell][1];
54 }
55 // rtrim empty cells as xterm does
56 line_s = line_s.replace(/\s+$/, '');
57 result += line_s;
58 result += '\n';
59 }
60 return result;
61 }
62
63 describe('Escape code files', function() {
64 var xterm;
65
66 beforeEach(function () {
67 xterm = new Terminal(80, 25);
68 });
69
70 // omit stack trace for escape sequence files
71 Error.stackTraceLimit = 0;
72 var files = glob.sync('test/escape_sequence_files/*.in');
73 // first 44 are the most basic escape sequences
74 for (var i=0; i<44; ++i) {
75 //for (var i=0; i<files.length; ++i) {
76 (function(filename){
77 it(filename.split('/').slice(-1)[0], function () {
78 var in_file = fs.readFileSync(filename, 'utf8');
79 var from_pty = pty_write_read(primitive_pty, in_file);
80 // uncomment this to get log from terminal
81 console.log = function(){};
82 xterm.write(from_pty);
83 var from_emulator = terminalToString(xterm);
84 console.log = CONSOLE_LOG;
85 var expected = fs.readFileSync(filename.split('.')[0] + '.text', 'utf8');
86 if (from_emulator != expected) {
87 // uncomment to get noisy output
88 //throw new Error(formatError(in_file, from_emulator, expected));
89 throw new Error('mismatch');
90 }
91 });
92 })(files[i]);
93 }
94 });
95