-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
184 lines (145 loc) · 6.35 KB
/
Copy pathtest.js
File metadata and controls
184 lines (145 loc) · 6.35 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const assert = require('assert');
const describe = require('mocha').describe;
const chalk = require('../chalk').default;
const C = chalk.native;
const test = 'test';
const format = 'Test %s';
const { create:S, StringExtra } = require('../index.js');
assert.stringExtra = (actual, expected) => {
assert.strictEqual(actual instanceof StringExtra, true);
if (expected !== undefined) {
assert.strictEqual(actual.s, expected);
assert.strictEqual(actual.toString(), expected);
assert.strictEqual(`${actual}`, expected);
}
return assert;
};
describe('StringExtra', function() {
it('should return a StringExtra object which can be typecast to a string', function() {
const actual = S(test);
assert.stringExtra(actual, test);
});
it('should allow the short hand sprintf array format', function() {
const actual = S([format, test]);
const expected = format.replace('%s', C.bold.whiteBright(test));
assert.stringExtra(actual, expected);
});
});
describe('StringExtra#chalk', function() {
const chalkProperties = [].concat(chalk.modifiers, chalk.fg, chalk.bg);
for (let i = 0, l = chalkProperties.length; i < l; i++) {
let prop = chalkProperties[i];
it(`should chain the "${prop}" chalk property`, function() {
const actual = S(test)[prop];
const expected = C[prop](test);
assert.stringExtra(actual, expected);
});
}
});
describe('StringExtra.style', function() {
it('should allow piped chalk styles', function() {
const actual = S(test).blue.dim.italic;
const expected = C.blue.dim.italic(test);
assert.stringExtra(actual, expected);
});
it('should allow piped chalk styles to be reset', function() {
let actual = S(test).blue.dim.italic;
let expected = C.blue.dim.italic(test);
assert.stringExtra(actual, expected);
// Ensure chalk's "reset" property works as expected.
actual = actual.reset.bgRed.whiteBright.bold;
expected = C.blue.dim.italic.reset.bgRed.whiteBright.bold(test);
assert.stringExtra(actual, expected);
// Ensure this module's "resetStyle" property works as expected.
actual = actual.resetStyle.bgRed.whiteBright.bold;
expected = C.bgRed.whiteBright.bold(test);
assert.stringExtra(actual, expected);
});
it('should allow piped chalk styles represented as a string', function() {
const actual = S(test).style('red.bold.underline');
const expected = C.red.bold.underline(test);
assert.stringExtra(actual, expected);
});
it('should allow a chained chalk function', function() {
const actual = S(test).style(C.red.bold.underline);
const expected = C.red.bold.underline(test);
assert.stringExtra(actual, expected);
});
it('should allow a custom function', function() {
const actual = S(test).style(v => `%${v}%`);
const expected = `%${test}%`;
assert.stringExtra(actual, expected);
});
it('should return lengths with and without ansi codes', function() {
const actual = S(test).red;
assert.stringExtra(actual);
assert.strictEqual(actual.length, test.length);
assert.strictEqual(actual.rawLength, C.red(test).length);
});
it('should throw an error when provided style is invalid', function() {
const actual = S(test);
const expected = new TypeError('Provided style must either be a string of chalk styles, piped using dot notation or a function that will be passed a single string value.');
assert.stringExtra(actual).throws(actual.style.bind(actual, {}), expected);
});
});
describe('StringExtra.prefix', function() {
it('should return a prefixed string', function() {
const actual = S(test).prefix('prefix');
const expected = `prefix ${test}`;
assert.stringExtra(actual, expected);
});
it('should return a styled prefixed string using piped chalk styles', function() {
const actual = S(test).prefix(S('prefix').red.bold.underline);
const expected = `${C.red.bold.underline('prefix')} ${test}`;
assert.stringExtra(actual, expected);
});
it('should return a double prefixed string', function() {
const actual = S(test).prefix('prefix').prefix(S('prefix').red.bold.underline);
const expected = `${C.red.bold.underline('prefix')} prefix ${test}`;
assert.stringExtra(actual, expected);
});
it('should return a prefixed string with a custom delimiter', function() {
const actual = S(test).prefix('prefix').setOption('prefixDelimiter', '-');
const expected = `prefix-${test}`;
assert.stringExtra(actual, expected);
});
it('should return a prefixed string with a custom styled delimiter', function() {
const actual = S(test).prefix('prefix').setOption('prefixDelimiter', S('-').red.bold.underline);
const expected = `prefix${C.red.bold.underline('-')}${test}`;
assert.stringExtra(actual, expected);
});
});
describe('StringExtra.suffix', function() {
it('should return a suffixed string', function() {
const actual = S(test).suffix('suffix');
const expected = `${test} suffix`;
assert.stringExtra(actual, expected);
});
it('should return a styled suffixed string using piped chalk styles', function() {
const actual = S(test).suffix(S('suffix').red.bold.underline);
const expected = `${test} ${C.red.bold.underline('suffix')}`;
assert.stringExtra(actual, expected);
});
it('should return a double suffixed string', function() {
const actual = S(test).suffix('suffix').suffix(S('suffix').red.bold.underline);
const expected = `${test} suffix ${C.red.bold.underline('suffix')}`;
assert.stringExtra(actual, expected);
});
it('should return a suffixed string with a custom delimiter', function() {
const actual = S(test).suffix('suffix').setOption('suffixDelimiter', '-');
const expected = `${test}-suffix`;
assert.stringExtra(actual, expected);
});
it('should return a suffixed string with a custom styled delimiter', function() {
const actual = S(test).suffix('suffix').setOption('suffixDelimiter', S('-').red.bold.underline);
const expected = `${test}${C.red.bold.underline('-')}suffix`;
assert.stringExtra(actual, expected);
});
});
describe('StringExtra.args', function() {
it('should set active styling to args', function() {
const actual = S([format, test]).magenta.argStyle.cyanBright.bold.underline;
const expected = C.magenta(format).replace('%s', chalk.wrap(C.cyanBright.bold.underline(test), C.magenta));
assert.stringExtra(actual, expected);
});
});