forked from chalk/chalk
/
test.js
151 lines (126 loc) · 5.21 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict';
var assert = require('assert');
var requireUncached = require('require-uncached');
var resolveFrom = require('resolve-from');
var semver = require('semver');
var chalk = require('./');
describe('chalk', function () {
it('should style string', function () {
assert.equal(chalk.underline('foo'), '\u001b[4mfoo\u001b[24m');
assert.equal(chalk.red('foo'), '\u001b[31mfoo\u001b[39m');
assert.equal(chalk.bgRed('foo'), '\u001b[41mfoo\u001b[49m');
});
it('should support applying multiple styles at once', function () {
assert.equal(chalk.red.bgGreen.underline('foo'), '\u001b[31m\u001b[42m\u001b[4mfoo\u001b[24m\u001b[49m\u001b[39m');
assert.equal(chalk.underline.red.bgGreen('foo'), '\u001b[4m\u001b[31m\u001b[42mfoo\u001b[49m\u001b[39m\u001b[24m');
});
it('should support nesting styles', function () {
assert.equal(
chalk.red('foo' + chalk.underline.bgBlue('bar') + '!'),
'\u001b[31mfoo\u001b[4m\u001b[44mbar\u001b[49m\u001b[24m!\u001b[39m'
);
});
it('should support nesting styles of the same type (color, underline, bg)', function () {
assert.equal(
chalk.red('a' + chalk.yellow('b' + chalk.green('c') + 'b') + 'c'),
'\u001b[31ma\u001b[33mb\u001b[32mc\u001b[33mb\u001b[31mc\u001b[39m'
);
});
it('should reset all styles with `.reset()`', function () {
assert.equal(chalk.reset(chalk.red.bgGreen.underline('foo') + 'foo'), '\u001b[0m\u001b[31m\u001b[42m\u001b[4mfoo\u001b[24m\u001b[49m\u001b[39mfoo\u001b[0m');
});
it('should be able to cache multiple styles', function () {
var red = chalk.red;
var green = chalk.green;
var redBold = red.bold;
var greenBold = green.bold;
assert.notEqual(red('foo'), green('foo'));
assert.notEqual(redBold('bar'), greenBold('bar'));
assert.notEqual(green('baz'), greenBold('baz'));
});
it('should alias gray to grey', function () {
assert.equal(chalk.grey('foo'), '\u001b[90mfoo\u001b[39m');
});
it('should support variable number of arguments', function () {
assert.equal(chalk.red('foo', 'bar'), '\u001b[31mfoo bar\u001b[39m');
});
it('should support falsy values', function () {
assert.equal(chalk.red(0), '\u001b[31m0\u001b[39m');
});
it('shouldn\'t output escape codes if the input is empty', function () {
assert.equal(chalk.red(), '');
assert.equal(chalk.red.blue.black(), '');
});
it('should keep Function.prototype methods', function () {
assert.equal(chalk.grey.apply(null, ['foo']), '\u001b[90mfoo\u001b[39m');
assert.equal(chalk.reset(chalk.red.bgGreen.underline.bind(null)('foo') + 'foo'), '\u001b[0m\u001b[31m\u001b[42m\u001b[4mfoo\u001b[24m\u001b[49m\u001b[39mfoo\u001b[0m');
assert.equal(chalk.red.blue.black.call(null), '');
});
});
describe('chalk on windows', function () {
var originalEnv;
var originalPlatform;
// in node versions older than 0.12.x process.platform cannot be overridden
if (semver.lt(process.version, '0.12.0')) {
return;
}
before(function () {
originalEnv = process.env;
originalPlatform = process.platform;
});
after(function () {
process.env = originalEnv;
Object.defineProperty(process, 'platform', {value: originalPlatform});
});
beforeEach(function () {
process.env = {};
Object.defineProperty(process, 'platform', {value: 'win32'});
// since chalk internally modifies ansiStyles.blue.open, ansi-styles needs
// to be removed from the require cache for require-uncached to work
delete require.cache[resolveFrom(__dirname, 'ansi-styles')];
});
it('should replace blue foreground color in cmd.exe', function () {
process.env.TERM = 'dumb';
var chalkCtx = requireUncached('./');
assert.equal(chalkCtx.blue('foo'), '\u001b[94mfoo\u001b[39m');
});
it('shouldn\'t replace blue foreground color in xterm based terminals', function () {
process.env.TERM = 'xterm-256color';
var chalkCtx = requireUncached('./');
assert.equal(chalkCtx.blue('foo'), '\u001b[34mfoo\u001b[39m');
});
it('should not apply dimmed styling on gray strings, see https://github.com/chalk/chalk/issues/58', function () {
process.env.TERM = 'dumb';
var chalkCtx = requireUncached('./');
assert.equal(chalkCtx.gray.dim('foo'), '\u001b[90mfoo\u001b[22m\u001b[39m');
});
it('should apply dimmed styling on xterm compatible terminals', function () {
process.env.TERM = 'xterm';
var chalkCtx = requireUncached('./');
assert.equal(chalkCtx.gray.dim('foo'), '\u001b[90m\u001b[2mfoo\u001b[22m\u001b[39m');
});
it('should apply dimmed styling on strings of other colors', function () {
process.env.TERM = 'dumb';
var chalkCtx = requireUncached('./');
assert.equal(chalkCtx.blue.dim('foo'), '\u001b[94m\u001b[2mfoo\u001b[22m\u001b[39m');
});
});
describe('chalk.enabled', function () {
it('should not output colors when manually disabled', function () {
chalk.enabled = false;
assert.equal(chalk.red('foo'), 'foo');
chalk.enabled = true;
});
});
describe('chalk.constructor', function () {
it('should create a isolated context where colors can be disabled', function () {
var ctx = new chalk.constructor({enabled: false});
assert.equal(ctx.red('foo'), 'foo');
assert.equal(chalk.red('foo'), '\u001b[31mfoo\u001b[39m');
});
});
describe('chalk.styles', function () {
it('should expose the styles as ANSI escape codes', function () {
assert.equal(chalk.styles.red.open, '\u001b[31m');
});
});