alignString.js
3.96 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
/* eslint-disable max-nested-callbacks */
import {
expect
} from 'chai';
import chalk from 'chalk';
import alignString from './../src/alignString';
describe('alignString', () => {
context('subject parameter value is not a string', () => {
it('throws an error', () => {
expect(() => {
alignString();
}).to.throw(Error, 'Subject parameter value must be a string.');
});
});
context('container width parameter value is not a string', () => {
it('throws an error', () => {
expect(() => {
alignString('');
}).to.throw(Error, 'Container width parameter value must be a number.');
});
});
context('subject parameter value width is greater than the container width', () => {
it('throws an error', () => {
expect(() => {
alignString('aa', 1, 'left');
}).to.throw(Error, 'Subject parameter value width cannot be greater than the container width.');
});
});
context('container alignment parameter value is not a string', () => {
it('throws an error', () => {
expect(() => {
alignString('', 1);
}).to.throw(Error, 'Alignment parameter value must be a string.');
});
});
context('container alignment parameter value is not a known alignment parameter value', () => {
it('throws an error', () => {
expect(() => {
alignString('', 1, 'foo');
}).to.throw(Error, 'Alignment parameter value must be a known alignment parameter value (left, right, center).');
});
});
context('subject parameter value', () => {
context('0 width', () => {
it('produces a string consisting of container width number of whitespace characters', () => {
expect(alignString('', 5, 'left')).to.equal(' ', 'left');
expect(alignString('', 5, 'center')).to.equal(' ', 'center');
expect(alignString('', 5, 'right')).to.equal(' ', 'right');
});
});
context('plain text', () => {
context('alignment', () => {
context('left', () => {
it('pads the string on the right side using a whitespace character', () => {
expect(alignString('aa', 6, 'left')).to.equal('aa ');
});
});
context('right', () => {
it('pads the string on the left side using a whitespace character', () => {
expect(alignString('aa', 6, 'right')).to.equal(' aa');
});
});
context('center', () => {
it('pads the string on both sides using a whitespace character', () => {
expect(alignString('aa', 6, 'center')).to.equal(' aa ');
});
context('uneven number of available with', () => {
it('floors the available width; adds extra space to the end of the string', () => {
expect(alignString('aa', 7, 'center')).to.equal(' aa ');
});
});
});
});
});
context('text containing ANSI escape codes', () => {
context('alignment', () => {
context('left', () => {
it('pads the string on the right side using a whitespace character', () => {
expect(alignString(chalk.red('aa'), 6, 'left')).to.equal(chalk.red('aa') + ' ');
});
});
context('right', () => {
it('pads the string on the left side using a whitespace character', () => {
expect(alignString(chalk.red('aa'), 6, 'right')).to.equal(' ' + chalk.red('aa'));
});
});
context('center', () => {
it('pads the string on both sides using a whitespace character', () => {
expect(alignString(chalk.red('aa'), 6, 'center')).to.equal(' ' + chalk.red('aa') + ' ');
});
context('uneven number of available with', () => {
it('floors the available width; adds extra space to the end of the string', () => {
expect(alignString(chalk.red('aa'), 7, 'center')).to.equal(' ' + chalk.red('aa') + ' ');
});
});
});
});
});
});
});