forked from bramstein/hypher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhypher-test.js
214 lines (200 loc) · 7.44 KB
/
hypher-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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var Hypher = require('../lib/hypher'),
vows = require('vows'),
assert = require('assert'),
data = require('./data.js'),
words = data.words,
language = data.language,
german = require('./de.js'),
finnish = require('./fi.js'),
dictionary = {};
function assertHyphenation(hyphenation) {
return function (topic) {
assert.deepEqual(topic, hyphenation);
};
}
function hyphenate(word) {
return function (h) {
return h.hyphenate(word);
};
}
function hyphenatesTextTo(hyphenation) {
var context = {
topic: function (h) {
return h.hyphenateText(this.context.name).split('\u00AD');
}
};
context['Should hyphenate to: ' + hyphenation.join('\u2022')] = assertHyphenation(hyphenation);
return context;
}
function hyphenatesTo(hyphenation) {
var context = {
topic: function (h) {
return h.hyphenate(this.context.name);
}
};
context['Should hyphenate to: ' + hyphenation.join('\u2022')] = assertHyphenation(hyphenation);
return context;
}
words.forEach(function (word) {
var w = word.split('-');
dictionary[w.join('')] = hyphenatesTo(w);
});
vows.describe('Hypher').addBatch({
'hyphenate ': {
topic: function () {
return new Hypher(language);
},
'word: ': dictionary
},
'trie': {
topic: function () {
return new Hypher({
patterns: {
"2": "a1b2",
"3": "a2bb3c"
}
});
},
'trie is correctly generated': function (h) {
assert.deepEqual(h.trie, {
97: {
_points: [0, 1],
98: {
_points: [0, 2]
}
},
98: {
_points: [0, 2],
99: {
_points: [0, 3]
}
},
_points: []
});
}
},
'hyphenate with soft hyphens': {
topic: function () {
return new Hypher(language);
},
'hyph\u00ADen': hyphenatesTo(['hyph\u00ADen'])
},
'hyphenate text with soft hyphens': {
topic: function () {
return new Hypher(language);
},
'hyph\u00ADen charact\u00ADer': hyphenatesTextTo(['hyph', 'en charact', 'er']),
},
'hyphenate with en-dash, hyphen-minus, hyphen, or ZWNJ': {
topic: function () {
return new Hypher(language);
},
// The resulting hyphenation might look odd, but a minus-hyphen, en-dash, or hyphen will
// be broken by the browser, so we don't need to insert a soft hyphen in that position.
'bootstrapping-brainstorm-victories': hyphenatesTextTo(['boot', 'strap', 'ping-brain', 'storm-vic', 'to', 'ries']),
// hyphen-minus
'bootstrapping\u002Dbrainstorm-victories': hyphenatesTextTo(['boot', 'strap', 'ping\u002Dbrain', 'storm-vic', 'to', 'ries']),
// hyphen
'bootstrapping\u2010brainstorm-victories': hyphenatesTextTo(['boot', 'strap', 'ping\u2010brain', 'storm-vic', 'to', 'ries']),
// en-dash
'bootstrapping\u2013brainstorm-victories': hyphenatesTextTo(['boot', 'strap', 'ping\u2013brain', 'storm-vic', 'to', 'ries']),
// ZWNJ
'bootstrapping\u200Cbrainstorm-victories': hyphenatesTextTo(['boot', 'strap', 'ping\u200Cbrain', 'storm-vic', 'to', 'ries'])
},
'hyphenate, preserve case and punctuation': {
topic: function () {
return new Hypher(language);
},
'Hyphenation': hyphenatesTo(['Hy', 'phen', 'ation']),
'!!!!!!!!': hyphenatesTo(['!!!!!!!!']),
'Hyphenation!': hyphenatesTo(['Hy', 'phen', 'ation!'])
},
'hyphenate with exceptions': {
topic: function () {
var l = Object.create(language);
l.exceptions = 'bootstrapping, brainstorm';
return new Hypher(l);
},
'bootstrapping': hyphenatesTo(['bootstrapping']),
'brainstorm': hyphenatesTo(['brainstorm'])
},
'hyphenate with exceptions (mixed case)': {
topic: function () {
var l = Object.create(language);
l.exceptions = 'bootstrapping, brainstorm';
return new Hypher(l);
},
'BoOtstrApPing': hyphenatesTo(['BoOtstrApPing']),
'BrainStorm': hyphenatesTo(['BrainStorm'])
},
'hyphenate with exceptions (without space)': {
topic: function () {
var l = Object.create(language);
l.exceptions = 'bootstrapping,brainstorm';
return new Hypher(l);
},
'bootstrapping': hyphenatesTo(['bootstrapping']),
'brainstorm': hyphenatesTo(['brainstorm'])
},
'hyphenate with custom points': {
topic: function () {
var l = Object.create(language);
l.exceptions = 'bo\u2027otstr\u2027apping, brai\u2027nstorm';
return new Hypher(l);
},
'bootstrapping': hyphenatesTo(['bo', 'otstr', 'apping']),
'brainstorm': hyphenatesTo(['brai', 'nstorm'])
},
'returns the correct exception': {
topic: function () {
var l = Object.create(language);
l.exceptions = 'inspi\u2027re\u2027rend'
return new Hypher(l);
},
'inspirerend': hyphenatesTo(['inspi', 're', 'rend'])
},
'hyphenate with custom points and mixed case': {
topic: function () {
var l = Object.create(language);
l.exceptions = 'bo\u2027otstr\u2027apping, brai\u2027nstorm';
return new Hypher(l);
},
'bOotsTrapPing': hyphenatesTo(['bO', 'otsTr', 'apPing']),
'BrainStorm': hyphenatesTo(['Brai', 'nStorm'])
},
'hyphenates uppercase': {
topic: function () {
var l = Object.create(finnish);
return new Hypher(l);
},
'LÄHETÄ SÄHKÖPOSTI': hyphenatesTextTo(['LÄ', 'HE', 'TÄ SÄH', 'KÖ', 'POS', 'TI'])
},
'hyphenate path like strings': {
topic: function () {
return new Hypher(language);
},
'http://www.ex.com/': hyphenatesTextTo(['http://\u200Bwww.ex.com/']),
'http://www.ex.com/some/file.txt': hyphenatesTextTo(['http://\u200Bwww.ex.com/\u200Bsome/\u200Bfile.txt']),
'some/path/to/some/where': hyphenatesTextTo(['some/\u200Bpath/\u200Bto/\u200Bsome/\u200Bwhere']),
'1234567/7654321': hyphenatesTextTo(['1234567/\u200B7654321']),
'/root/for/some/path': hyphenatesTextTo(['/root/\u200Bfor/\u200Bsome/\u200Bpath']),
'a text with a /path/in/it/': hyphenatesTextTo(['a text with a /path/\u200Bin/\u200Bit/']),
'a text with a /path/in/it/ and more text': hyphenatesTextTo(['a text with a /path/\u200Bin/\u200Bit/ and more text'])
},
'hyphenate with special characters': {
topic: function () {
return new Hypher(german);
},
'müsse': hyphenatesTo(['müs', 'se']),
'sozioökonomisch': hyphenatesTo(['so', 'zio', 'öko', 'no', 'misch']),
'kostenschätzungen': hyphenatesTo(['kos', 'ten', 'schät', 'zun', 'gen'])
},
'hyphenate text with special characters': {
topic: function () {
return new Hypher(german);
},
'müsse': hyphenatesTextTo(['müs', 'se']),
'sozioökonomisch': hyphenatesTextTo(['so', 'zio', 'öko', 'no', 'misch']),
'kostenschätzungen': hyphenatesTextTo(['kos', 'ten', 'schät', 'zun', 'gen'])
}
}).export(module);