Skip to content

Commit f80cf5a

Browse files
cjihrigMylesBorins
authored andcommitted
test: add coverage to tty module
PR-URL: #16959 Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 4e3aa9a commit f80cf5a

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
require('../common');
3+
constassert=require('assert');
4+
const{ ReadStream, WriteStream }=require('tty');
5+
6+
{
7+
// Verify that tty.ReadStream can be constructed without new.
8+
conststream=ReadStream(0);
9+
10+
stream.unref();
11+
assert(streaminstanceofReadStream);
12+
assert.strictEqual(stream.isTTY,true);
13+
}
14+
15+
{
16+
// Verify that tty.WriteStream can be constructed without new.
17+
conststream=WriteStream(1);
18+
19+
assert(streaminstanceofWriteStream);
20+
assert.strictEqual(stream.isTTY,true);
21+
}

‎test/pseudo-tty/test-tty-stream-constructors.out‎

Whitespace-only changes.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
const{ WriteStream }=require('tty');
5+
const{TTY}=process.binding('tty_wrap');
6+
constgetWindowSize=TTY.prototype.getWindowSize;
7+
8+
functionmonkeyPatchGetWindowSize(fn){
9+
TTY.prototype.getWindowSize=function(){
10+
TTY.prototype.getWindowSize=getWindowSize;
11+
returnfn.apply(this,arguments);
12+
};
13+
}
14+
15+
{
16+
// tty.WriteStream constructor does not define columns and rows if an error
17+
// occurs while retrieving the window size from the handle.
18+
monkeyPatchGetWindowSize(function(){
19+
return-1;
20+
});
21+
22+
conststream=WriteStream(1);
23+
24+
assert(streaminstanceofWriteStream);
25+
assert.strictEqual(stream.columns,undefined);
26+
assert.strictEqual(stream.rows,undefined);
27+
}
28+
29+
{
30+
// _refreshSize() emits an error if an error occurs while retrieving the
31+
// window size from the handle.
32+
conststream=WriteStream(1);
33+
34+
stream.on('error',common.mustCall((err)=>{
35+
assert.strictEqual(err.syscall,'getWindowSize');
36+
}));
37+
38+
monkeyPatchGetWindowSize(function(){
39+
return-1;
40+
});
41+
42+
stream._refreshSize();
43+
}
44+
45+
{
46+
// _refreshSize() emits a 'resize' event when the window size changes.
47+
monkeyPatchGetWindowSize(function(size){
48+
size[0]=80;
49+
size[1]=24;
50+
return0;
51+
});
52+
53+
conststream=WriteStream(1);
54+
55+
stream.on('resize',common.mustCall(()=>{
56+
assert.strictEqual(stream.columns,82);
57+
assert.strictEqual(stream.rows,26);
58+
}));
59+
60+
assert.strictEqual(stream.columns,80);
61+
assert.strictEqual(stream.rows,24);
62+
63+
monkeyPatchGetWindowSize(function(size){
64+
size[0]=82;
65+
size[1]=26;
66+
return0;
67+
});
68+
69+
stream._refreshSize();
70+
}
71+
72+
{
73+
// WriteStream.prototype.getWindowSize() returns the current columns and rows.
74+
monkeyPatchGetWindowSize(function(size){
75+
size[0]=99;
76+
size[1]=32;
77+
return0;
78+
});
79+
80+
conststream=WriteStream(1);
81+
82+
assert.strictEqual(stream.columns,99);
83+
assert.strictEqual(stream.rows,32);
84+
assert.deepStrictEqual(stream.getWindowSize(),[99,32]);
85+
}

‎test/pseudo-tty/test-tty-window-size.out‎

Whitespace-only changes.

0 commit comments

Comments
(0)