File: test.js

package info (click to toggle)
node-ansi-styles 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 156 kB
  • sloc: makefile: 6
file content (113 lines) | stat: -rw-r--r-- 3,526 bytes parent folder | download
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
const test = require('tape');
const style = require('..');

test('return ANSI escape codes', t => {
	t.is(style.green.open, '\u001B[32m');
	t.is(style.bgGreen.open, '\u001B[42m');
	t.is(style.green.close, '\u001B[39m');
	t.is(style.gray.open, style.grey.open);
	t.end();
});

test('group related codes into categories', t => {
	t.is(style.color.magenta, style.magenta);
	t.is(style.bgColor.bgYellow, style.bgYellow);
	t.is(style.modifier.bold, style.bold);
	t.end();
});

test('groups should not be enumerable', t => {
	t.not(Object.getOwnPropertyDescriptor(style, 'modifier'), undefined);
	t.false(Object.keys(style).includes('modifier'));
	t.end();
});

test('don\'t pollute other objects', t => {
	const obj1 = require('..');
	const obj2 = require('..');

	obj1.foo = true;
	t.not(obj1.foo, obj2.foo);
	t.end();
});

test('all color types are always available', t => {
	const {ansi, ansi256, ansi16m} = style.color;

	t.true(ansi);
	t.true(ansi.ansi);
	t.true(ansi.ansi256);

	t.true(ansi256);
	t.true(ansi256.ansi);
	t.true(ansi256.ansi256);

	t.true(ansi16m);
	t.true(ansi16m.ansi);
	t.true(ansi16m.ansi256);

	// There are no such things as ansi16m source colors
	t.false(ansi.ansi16m);
	t.false(ansi256.ansi16m);
	t.false(ansi16m.ansi16m);
	t.end();
});

test('support conversion to ansi (16 colors)', t => {
	t.is(style.color.ansi.rgb(255, 255, 255), '\u001B[97m');
	t.is(style.color.ansi.hsl(140, 100, 50), '\u001B[92m');
	t.is(style.color.ansi.hex('#990099'), '\u001B[35m');
	t.is(style.color.ansi.hex('#FF00FF'), '\u001B[95m');

	t.is(style.bgColor.ansi.rgb(255, 255, 255), '\u001B[107m');
	t.is(style.bgColor.ansi.hsl(140, 100, 50), '\u001B[102m');
	t.is(style.bgColor.ansi.hex('#990099'), '\u001B[45m');
	t.is(style.bgColor.ansi.hex('#FF00FF'), '\u001B[105m');
	t.end();
});

test('support conversion to ansi (256 colors)', t => {
	t.is(style.color.ansi256.rgb(255, 255, 255), '\u001B[38;5;231m');
	t.is(style.color.ansi256.hsl(140, 100, 50), '\u001B[38;5;48m');
	t.is(style.color.ansi256.hex('#990099'), '\u001B[38;5;127m');
	t.is(style.color.ansi256.hex('#FF00FF'), '\u001B[38;5;201m');

	t.is(style.bgColor.ansi256.rgb(255, 255, 255), '\u001B[48;5;231m');
	t.is(style.bgColor.ansi256.hsl(140, 100, 50), '\u001B[48;5;48m');
	t.is(style.bgColor.ansi256.hex('#990099'), '\u001B[48;5;127m');
	t.is(style.bgColor.ansi256.hex('#FF00FF'), '\u001B[48;5;201m');
	t.end();
});

test('support conversion to ansi (16 million colors)', t => {
	t.is(style.color.ansi16m.rgb(255, 255, 255), '\u001B[38;2;255;255;255m');
	t.is(style.color.ansi16m.hsl(140, 100, 50), '\u001B[38;2;0;255;85m');
	t.is(style.color.ansi16m.hex('#990099'), '\u001B[38;2;153;0;153m');
	t.is(style.color.ansi16m.hex('#FF00FF'), '\u001B[38;2;255;0;255m');

	t.is(style.bgColor.ansi16m.rgb(255, 255, 255), '\u001B[48;2;255;255;255m');
	t.is(style.bgColor.ansi16m.hsl(140, 100, 50), '\u001B[48;2;0;255;85m');
	t.is(style.bgColor.ansi16m.hex('#990099'), '\u001B[48;2;153;0;153m');
	t.is(style.bgColor.ansi16m.hex('#FF00FF'), '\u001B[48;2;255;0;255m');
	t.end();
});

test('16/256/16m color close escapes', t => {
	t.is(style.color.close, '\u001B[39m');
	t.is(style.bgColor.close, '\u001B[49m');
	t.end();
});

test('export raw ANSI escape codes', t => {
	t.is(style.codes.get(0), 0);
	t.is(style.codes.get(1), 22);
	t.is(style.codes.get(91), 39);
	t.is(style.codes.get(40), 49);
	t.is(style.codes.get(100), 49);
	t.end();
});

test('rgb -> truecolor is stubbed', t => {
	t.is(style.color.ansi16m.rgb(123, 45, 67), '\u001B[38;2;123;45;67m');
	t.end();
});