-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
164 lines (146 loc) · 5.83 KB
/
test.js
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
'use strict';
/*!
* justified <https://github.com/jonschlinkert/justified>
*
* Copyright (c) 2014-2017, Jon Schlinkert.
* Licensed under the MIT License.
*/
require('mocha');
var assert = require('assert');
var justify = require('./');
var text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
describe('justified', function() {
describe('default settings', function() {
it('should justify text using default settings', function() {
assert.equal(justify(text), [
'Lorem ipsum dolor sit amet, consectetur adipiscing',
'elit, sed do eiusmod tempor incididunt ut labore et',
'dolore magna aliqua. Ut enim ad minim veniam, quis',
'nostrud exercitation ullamco laboris nisi ut aliquip ex',
'ea commodo consequat. Duis aute irure dolor in',
'reprehenderit in voluptate velit esse cillum dolore eu',
'fugiat nulla pariatur. Excepteur sint occaecat',
'cupidatat non proident, sunt in culpa qui officia',
'deserunt mollit anim id est laborum.',
''
].join('\n'));
});
});
describe('options.width', function() {
it('should justify text to the specified width', function() {
assert.equal(justify(text.slice(0, 26), {width: 15}), [
'Lorem ipsum',
'dolor sit amet',
''
].join('\n'));
assert.equal(justify(text.slice(0, 50), {width: 12}), [
'Lorem ipsum',
'dolor sit',
'amet,',
'consectetur',
'adipiscing',
''
].join('\n'));
assert.equal(justify(text.slice(0, 50), {width: 30}), [
'Lorem ipsum dolor sit amet,',
'consectetur adipiscing',
''
].join('\n'));
assert.equal(justify(text.slice(0, 75), {width: 30}), [
'Lorem ipsum dolor sit amet,',
'consectetur adipiscing elit,',
'sed do eiusmod tem',
''
].join('\n'));
assert.equal(justify(text.slice(0, 150), {width: 30}), [
'Lorem ipsum dolor sit amet,',
'consectetur adipiscing elit,',
'sed do eiusmod tempor',
'incididunt ut labore et dolore',
'magna aliqua. Ut enim ad minim',
'veniam, q',
''
].join('\n'));
assert.equal(justify(text.slice(0, 150), {width: 40}), [
'Lorem ipsum dolor sit amet, consectetur',
'adipiscing elit, sed do eiusmod tempor',
'incididunt ut labore et dolore magna',
'aliqua. Ut enim ad minim veniam, q',
''
].join('\n'));
});
});
describe('options.justifyLastLine', function() {
it('should force the last line to be justified', function() {
assert.equal(justify(text.slice(0, 150), {width: 30, justifyLastLine: true}), [
'Lorem ipsum dolor sit amet,',
'consectetur adipiscing elit,',
'sed do eiusmod tempor',
'incididunt ut labore et dolore',
'magna aliqua. Ut enim ad minim',
'veniam, q',
''
].join('\n'));
});
it('should allow justifyLastLine to be a function', function() {
function justifyLastLine(line, maxWidth) {
return line.length > (maxWidth / 2);
}
var opts = {width: 30, justifyLastLine: justifyLastLine};
assert.equal(justify(text.slice(0, 150), opts), [
'Lorem ipsum dolor sit amet,',
'consectetur adipiscing elit,',
'sed do eiusmod tempor',
'incididunt ut labore et dolore',
'magna aliqua. Ut enim ad minim',
'veniam, q',
''
].join('\n'));
});
it('should allow justifyLastLine to be a function', function() {
function justifyLastLine(line, maxWidth) {
return line.length > 5;
}
var opts = {width: 30, justifyLastLine: justifyLastLine};
assert.equal(justify(text.slice(0, 150), opts), [
'Lorem ipsum dolor sit amet,',
'consectetur adipiscing elit,',
'sed do eiusmod tempor',
'incididunt ut labore et dolore',
'magna aliqua. Ut enim ad minim',
'veniam, q',
''
].join('\n'));
});
});
describe('options.indent', function() {
it('should indent text the specified number of spaces', function() {
assert.equal(justify(text, {indent: 4}), [
' Lorem ipsum dolor sit amet, consectetur adipiscing',
' elit, sed do eiusmod tempor incididunt ut labore et',
' dolore magna aliqua. Ut enim ad minim veniam, quis',
' nostrud exercitation ullamco laboris nisi ut aliquip ex',
' ea commodo consequat. Duis aute irure dolor in',
' reprehenderit in voluptate velit esse cillum dolore eu',
' fugiat nulla pariatur. Excepteur sint occaecat',
' cupidatat non proident, sunt in culpa qui officia',
' deserunt mollit anim id est laborum.',
''
].join('\n'));
});
});
describe('options.random', function() {
it('should use a custom random function', function() {
function random(max) {
return Math.floor(Math.random() * (max + 1));
}
var res = justify(text, {random: random});
// this will be randomly, by very infrequently, wrong :)
assert(res !== justify(text));
var lines = res.trim().split('\n');
lines.slice(0, -1).forEach(function(line, i) {
assert.equal(line.length, 55);
});
});
});
});