forked from orbit-oss/chalk
green(a + blue(b) + c) will now look the same as green(a) + blue(b) + green(c), as expected. In the previous implementation the output would have been green(a) + blue(b) + c, because the reset code of the second expression would close all expressions around it as well. Now this reset code is replaced by a start code of the outer lying expression, both stopping the inner and re-starting the outer.
70 lines
2.3 KiB
JavaScript
70 lines
2.3 KiB
JavaScript
'use strict';
|
|
var assert = require('assert');
|
|
var chalk = require('./index');
|
|
|
|
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[4m\u001b[42m\u001b[31mfoo\u001b[39m\u001b[49m\u001b[24m');
|
|
assert.equal(chalk.underline.red.bgGreen('foo'), '\u001b[42m\u001b[31m\u001b[4mfoo\u001b[24m\u001b[39m\u001b[49m');
|
|
});
|
|
|
|
it('should support nesting styles', function () {
|
|
assert.equal(
|
|
chalk.red('foo' + chalk.underline.bgBlue('bar') + '!'),
|
|
'\u001b[31mfoo\u001b[44m\u001b[4mbar\u001b[24m\u001b[49m!\u001b[39m'
|
|
);
|
|
});
|
|
|
|
it('should support nesting styles of the same type (color, underline, bg)', function () {
|
|
assert.equal(
|
|
chalk.red('a' + chalk.blue('b' + chalk.green('c') + 'b') + 'c'),
|
|
'\u001b[31ma\u001b[34mb\u001b[32mc\u001b[34mb\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[4m\u001b[42m\u001b[31mfoo\u001b[39m\u001b[49m\u001b[24mfoo\u001b[0m');
|
|
});
|
|
|
|
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('don\'t output escape codes if the input is empty', function () {
|
|
assert.equal(chalk.red(), '');
|
|
});
|
|
});
|
|
|
|
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.styles', function () {
|
|
it('should expose the styles as ANSI escape codes', function () {
|
|
assert.equal(chalk.styles.red.open, '\u001b[31m');
|
|
});
|
|
});
|
|
|
|
describe('chalk.stripColor()', function () {
|
|
it('should strip color from string', function () {
|
|
assert.equal(chalk.stripColor(chalk.underline.red.bgGreen('foo')), 'foo');
|
|
});
|
|
});
|