-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
99 lines (79 loc) · 2.41 KB
/
test.js
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
'use strict';
const {execFile} = require('child_process');
const {join} = require('path');
const {promisify} = require('util');
const {unlink, writeFile} = require('fs').promises;
Object.defineProperties(process.stdout, {
isTTY: {
value: true
},
columns: {
value: 15
}
});
const ttyWidthFrame = require('.');
const test = require('tape');
test('ttyWidthFrame()', async t => {
const contentWidth = process.stdout.columns - ' ┌'.length - '┐ '.length;
const lines = ttyWidthFrame('A\nB').split('\n');
t.equal(
lines.shift(),
` ┌${'─'.repeat(contentWidth)}┐`,
'should starts with an upper frame.'
);
t.equal(
lines.shift(),
` │${' '.repeat(contentWidth)}│`,
'should add 1 vertical padding to the top of contents.'
);
t.equal(
lines.shift(),
` │ A${' '.repeat(contentWidth - ' A'.length)}│`,
'should add 1 horizontal padding to both ends of each line.'
);
t.equal(
lines.shift(),
` │ B${' '.repeat(contentWidth - ' B'.length)}│`,
'should support multiline contents.'
);
t.equal(
lines.shift(),
` │${' '.repeat(contentWidth)}│`,
'should add 1 vertical padding to the bottom of contents.'
);
t.equal(
lines.shift(),
` └${'─'.repeat(contentWidth)}┘`,
'should starts with an lower frame.'
);
t.throws(
() => ttyWidthFrame(Buffer.from('a')),
/TypeError.*Expected a string to be framed with box-drawing characters, but got <Buffer 61>\./u,
'should throw an error when it takes a non-string argument.'
);
const tmp = join(__dirname, 'tmp.js');
await writeFile(tmp, 'try {require(".")()} catch ({message}) {console.error(message)}');
t.equal(
(await promisify(execFile)('node', [tmp])).stderr.replace(/\n$/u, ''),
'tty-width-frame only supports TTY environments, but the program is running under a non-TTY environment.',
'should throw an error when it\'s run in a non-TTY environment.'
);
await unlink(tmp);
Object.defineProperty(process.stdout, 'columns', {value: 14});
t.equal(
ttyWidthFrame('a'.repeat(100)),
'a'.repeat(100),
'should starts with an upper frame.'
);
t.throws(
() => ttyWidthFrame(),
/^RangeError.*Expected 1 argument \(<string>\), but got no arguments\./u,
'should throw an error when it takes no arguments.'
);
t.throws(
() => ttyWidthFrame('a', 'b'),
/^RangeError.*Expected 1 argument \(<string>\), but got 2 arguments\./u,
'should throw an error when it takes too many arguments.'
);
t.end();
});