Require Node.js 6
This commit is contained in:
parent
70f22d87ba
commit
0307f263cb
13 changed files with 204 additions and 216 deletions
|
|
@ -4,82 +4,82 @@ import test from 'ava';
|
|||
// Spoof supports-color
|
||||
require('./_supports-color')(__dirname);
|
||||
|
||||
const m = require('..');
|
||||
const chalk = require('..');
|
||||
|
||||
test('return an empty string for an empty literal', t => {
|
||||
const ctx = m.constructor();
|
||||
t.is(ctx``, '');
|
||||
const instance = chalk.constructor();
|
||||
t.is(instance``, '');
|
||||
});
|
||||
|
||||
test('return a regular string for a literal with no templates', t => {
|
||||
const ctx = m.constructor({level: 0});
|
||||
t.is(ctx`hello`, 'hello');
|
||||
const instance = chalk.constructor({level: 0});
|
||||
t.is(instance`hello`, 'hello');
|
||||
});
|
||||
|
||||
test('correctly perform template parsing', t => {
|
||||
const ctx = m.constructor({level: 0});
|
||||
t.is(ctx`{bold Hello, {cyan World!} This is a} test. {green Woo!}`,
|
||||
ctx.bold('Hello,', ctx.cyan('World!'), 'This is a') + ' test. ' + ctx.green('Woo!'));
|
||||
const instance = chalk.constructor({level: 0});
|
||||
t.is(instance`{bold Hello, {cyan World!} This is a} test. {green Woo!}`,
|
||||
instance.bold('Hello,', instance.cyan('World!'), 'This is a') + ' test. ' + instance.green('Woo!'));
|
||||
});
|
||||
|
||||
test('correctly perform template substitutions', t => {
|
||||
const ctx = m.constructor({level: 0});
|
||||
const instance = chalk.constructor({level: 0});
|
||||
const name = 'Sindre';
|
||||
const exclamation = 'Neat';
|
||||
t.is(ctx`{bold Hello, {cyan.inverse ${name}!} This is a} test. {green ${exclamation}!}`,
|
||||
ctx.bold('Hello,', ctx.cyan.inverse(name + '!'), 'This is a') + ' test. ' + ctx.green(exclamation + '!'));
|
||||
t.is(instance`{bold Hello, {cyan.inverse ${name}!} This is a} test. {green ${exclamation}!}`,
|
||||
instance.bold('Hello,', instance.cyan.inverse(name + '!'), 'This is a') + ' test. ' + instance.green(exclamation + '!'));
|
||||
});
|
||||
|
||||
test('correctly parse and evaluate color-convert functions', t => {
|
||||
const ctx = m.constructor({level: 3});
|
||||
t.is(ctx`{bold.rgb(144,10,178).inverse Hello, {~inverse there!}}`,
|
||||
const instance = chalk.constructor({level: 3});
|
||||
t.is(instance`{bold.rgb(144,10,178).inverse Hello, {~inverse there!}}`,
|
||||
'\u001B[1m\u001B[38;2;144;10;178m\u001B[7mHello, ' +
|
||||
'\u001B[27m\u001B[39m\u001B[22m\u001B[1m' +
|
||||
'\u001B[38;2;144;10;178mthere!\u001B[39m\u001B[22m');
|
||||
|
||||
t.is(ctx`{bold.bgRgb(144,10,178).inverse Hello, {~inverse there!}}`,
|
||||
t.is(instance`{bold.bgRgb(144,10,178).inverse Hello, {~inverse there!}}`,
|
||||
'\u001B[1m\u001B[48;2;144;10;178m\u001B[7mHello, ' +
|
||||
'\u001B[27m\u001B[49m\u001B[22m\u001B[1m' +
|
||||
'\u001B[48;2;144;10;178mthere!\u001B[49m\u001B[22m');
|
||||
});
|
||||
|
||||
test('properly handle escapes', t => {
|
||||
const ctx = m.constructor({level: 3});
|
||||
t.is(ctx`{bold hello \{in brackets\}}`,
|
||||
const instance = chalk.constructor({level: 3});
|
||||
t.is(instance`{bold hello \{in brackets\}}`,
|
||||
'\u001B[1mhello {in brackets}\u001B[22m');
|
||||
});
|
||||
|
||||
test('throw if there is an unclosed block', t => {
|
||||
const ctx = m.constructor({level: 3});
|
||||
const instance = chalk.constructor({level: 3});
|
||||
try {
|
||||
console.log(ctx`{bold this shouldn't appear ever\}`);
|
||||
console.log(instance`{bold this shouldn't appear ever\}`);
|
||||
t.fail();
|
||||
} catch (err) {
|
||||
t.is(err.message, 'Chalk template literal is missing 1 closing bracket (`}`)');
|
||||
} catch (error) {
|
||||
t.is(error.message, 'Chalk template literal is missing 1 closing bracket (`}`)');
|
||||
}
|
||||
|
||||
try {
|
||||
console.log(ctx`{bold this shouldn't {inverse appear {underline ever\} :) \}`);
|
||||
console.log(instance`{bold this shouldn't {inverse appear {underline ever\} :) \}`);
|
||||
t.fail();
|
||||
} catch (err) {
|
||||
t.is(err.message, 'Chalk template literal is missing 3 closing brackets (`}`)');
|
||||
} catch (error) {
|
||||
t.is(error.message, 'Chalk template literal is missing 3 closing brackets (`}`)');
|
||||
}
|
||||
});
|
||||
|
||||
test('throw if there is an invalid style', t => {
|
||||
const ctx = m.constructor({level: 3});
|
||||
const instance = chalk.constructor({level: 3});
|
||||
try {
|
||||
console.log(ctx`{abadstylethatdoesntexist this shouldn't appear ever}`);
|
||||
console.log(instance`{abadstylethatdoesntexist this shouldn't appear ever}`);
|
||||
t.fail();
|
||||
} catch (err) {
|
||||
t.is(err.message, 'Unknown Chalk style: abadstylethatdoesntexist');
|
||||
} catch (error) {
|
||||
t.is(error.message, 'Unknown Chalk style: abadstylethatdoesntexist');
|
||||
}
|
||||
});
|
||||
|
||||
test('properly style multiline color blocks', t => {
|
||||
const ctx = m.constructor({level: 3});
|
||||
const instance = chalk.constructor({level: 3});
|
||||
t.is(
|
||||
ctx`{bold
|
||||
instance`{bold
|
||||
Hello! This is a
|
||||
${'multiline'} block!
|
||||
:)
|
||||
|
|
@ -97,74 +97,74 @@ test('properly style multiline color blocks', t => {
|
|||
});
|
||||
|
||||
test('escape interpolated values', t => {
|
||||
const ctx = m.constructor({level: 0});
|
||||
t.is(ctx`Hello {bold hi}`, 'Hello hi');
|
||||
t.is(ctx`Hello ${'{bold hi}'}`, 'Hello {bold hi}');
|
||||
const instance = chalk.constructor({level: 0});
|
||||
t.is(instance`Hello {bold hi}`, 'Hello hi');
|
||||
t.is(instance`Hello ${'{bold hi}'}`, 'Hello {bold hi}');
|
||||
});
|
||||
|
||||
test('allow custom colors (themes) on custom contexts', t => {
|
||||
const ctx = m.constructor({level: 3});
|
||||
ctx.rose = ctx.hex('#F6D9D9');
|
||||
t.is(ctx`Hello, {rose Rose}.`, 'Hello, \u001B[38;2;246;217;217mRose\u001B[39m.');
|
||||
const instance = chalk.constructor({level: 3});
|
||||
instance.rose = instance.hex('#F6D9D9');
|
||||
t.is(instance`Hello, {rose Rose}.`, 'Hello, \u001B[38;2;246;217;217mRose\u001B[39m.');
|
||||
});
|
||||
|
||||
test('correctly parse newline literals (bug #184)', t => {
|
||||
const ctx = m.constructor({level: 0});
|
||||
t.is(ctx`Hello
|
||||
const instance = chalk.constructor({level: 0});
|
||||
t.is(instance`Hello
|
||||
{red there}`, 'Hello\nthere');
|
||||
});
|
||||
|
||||
test('correctly parse newline escapes (bug #177)', t => {
|
||||
const ctx = m.constructor({level: 0});
|
||||
t.is(ctx`Hello\nthere!`, `Hello\nthere!`);
|
||||
const instance = chalk.constructor({level: 0});
|
||||
t.is(instance`Hello\nthere!`, 'Hello\nthere!');
|
||||
});
|
||||
|
||||
test('correctly parse escape in parameters (bug #177 comment 318622809)', t => {
|
||||
const ctx = m.constructor({level: 0});
|
||||
const instance = chalk.constructor({level: 0});
|
||||
const str = '\\';
|
||||
t.is(ctx`{blue ${str}}`, '\\');
|
||||
t.is(instance`{blue ${str}}`, '\\');
|
||||
});
|
||||
|
||||
test('correctly parses unicode/hex escapes', t => {
|
||||
const ctx = m.constructor({level: 0});
|
||||
t.is(ctx`\u0078ylophones are fo\x78y! {magenta.inverse \u0078ylophones are fo\x78y!}`,
|
||||
const instance = chalk.constructor({level: 0});
|
||||
t.is(instance`\u0078ylophones are fo\x78y! {magenta.inverse \u0078ylophones are fo\x78y!}`,
|
||||
'xylophones are foxy! xylophones are foxy!');
|
||||
});
|
||||
|
||||
test('correctly parses string arguments', t => {
|
||||
const ctx = m.constructor({level: 3});
|
||||
t.is(ctx`{keyword('black').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
|
||||
t.is(ctx`{keyword('blac\x6B').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
|
||||
t.is(ctx`{keyword('blac\u006B').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
|
||||
const instance = chalk.constructor({level: 3});
|
||||
t.is(instance`{keyword('black').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
|
||||
t.is(instance`{keyword('blac\x6B').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
|
||||
t.is(instance`{keyword('blac\u006B').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
|
||||
});
|
||||
|
||||
test('throws if a bad argument is encountered', t => {
|
||||
const ctx = m.constructor({level: 3}); // Keep level at least 1 in case we optimize for disabled chalk instances
|
||||
const instance = chalk.constructor({level: 3}); // Keep level at least 1 in case we optimize for disabled chalk instances
|
||||
try {
|
||||
console.log(ctx`{keyword(????) hi}`);
|
||||
console.log(instance`{keyword(????) hi}`);
|
||||
t.fail();
|
||||
} catch (err) {
|
||||
t.is(err.message, 'Invalid Chalk template style argument: ???? (in style \'keyword\')');
|
||||
} catch (error) {
|
||||
t.is(error.message, 'Invalid Chalk template style argument: ???? (in style \'keyword\')');
|
||||
}
|
||||
});
|
||||
|
||||
test('throws if an extra unescaped } is found', t => {
|
||||
const ctx = m.constructor({level: 0});
|
||||
const instance = chalk.constructor({level: 0});
|
||||
try {
|
||||
console.log(ctx`{red hi!}}`);
|
||||
console.log(instance`{red hi!}}`);
|
||||
t.fail();
|
||||
} catch (err) {
|
||||
t.is(err.message, 'Found extraneous } in Chalk template literal');
|
||||
} catch (error) {
|
||||
t.is(error.message, 'Found extraneous } in Chalk template literal');
|
||||
}
|
||||
});
|
||||
|
||||
test('should not parse upper-case escapes', t => {
|
||||
const ctx = m.constructor({level: 0});
|
||||
t.is(ctx`\N\n\T\t\X07\x07\U000A\u000A\U000a\u000a`, 'N\nT\tX07\x07U000A\u000AU000a\u000A');
|
||||
const instance = chalk.constructor({level: 0});
|
||||
t.is(instance`\N\n\T\t\X07\x07\U000A\u000A\U000a\u000a`, 'N\nT\tX07\x07U000A\u000AU000a\u000A');
|
||||
});
|
||||
|
||||
test('should properly handle undefined template interpolated values', t => {
|
||||
const ctx = m.constructor({level: 0});
|
||||
t.is(ctx`hello ${undefined}`, 'hello undefined');
|
||||
t.is(ctx`hello ${null}`, 'hello null');
|
||||
const instance = chalk.constructor({level: 0});
|
||||
t.is(instance`hello ${undefined}`, 'hello undefined');
|
||||
t.is(instance`hello ${null}`, 'hello null');
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue