-
Notifications
You must be signed in to change notification settings - Fork 9
/
WordWrapTest.java
553 lines (456 loc) · 15.8 KB
/
WordWrapTest.java
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package org.davidmoten.text.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.github.davidmoten.junit.Asserts;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class WordWrapTest {
////////////////////////////////////////////
// Word wrap tests
////////////////////////////////////////////
@Test
public void longLineSplitsOnWhiteSpace() {
check("hello there", "hello\nthere");
}
@Test
public void longLineWithTwoSpacesInMiddle() {
check("hello there", "hello\nthere");
}
@Test
public void longLineALotOfWhiteSpaceInMiddle() {
check("hello there", "hello\nthere");
}
@Test
public void precedingWhitespaceConserved() {
check(" he", " he");
}
@Test
public void precedingWhitespaceLongWord() {
check(" helloyou", " hel-\nloyou");
}
@Test
public void whitespacePreservedAfterNewLine() {
check("hello\n the", "hello\n the");
}
@Test
public void shortLineNoWhitespace() {
check("hello", "hello");
}
@Test
public void shortLineHasWhitespace() {
check("hi bo", "hi bo");
}
@Test
public void emptyText() {
check("", "");
}
@Test
public void oneLetter() {
check("a", "a");
}
@Test
public void spaceThenOneLetter() {
check(" a", " a");
}
@Test
public void newLineCharacterPreserved() {
check("hello\nthere", "hello\nthere");
}
@Test
public void carriageReturnRemoved() {
check("hello\r\nthere", "hello\nthere");
}
@Test
public void whitespaceConservedAfterNewLine() {
check("hello\n there", "hello\n there");
}
@Test
public void wrapRightTrimsWhitespaceBeforeNewLine() {
check("abc \ncde ", "abc\ncde ");
}
@Test
public void longWordForcesBreak() {
check("hellothere", "hello-\nthere");
}
@Test
public void longWordForcesBreakNoHyphens() {
assertEquals("hellot\nhere",
WordWrap.from("hellothere").maxWidth(6).insertHyphens(false).wrap());
}
@Test
public void breakOnCommaDoesNotHappenWithoutSpaceAfter() {
check("hi,there", "hi,th-\nere");
}
@Test
public void noHyphenAfterDigits() {
check("1234567890", "123456\n7890");
}
@Test
public void breakOnCommasWithDigits() {
check("1,2,3,4,5,6,7,8,9", "1,2,3,\n4,5,6,\n7,8,9");
}
@Test
public void longThenShort() {
check("hellothere\n boo", "hello-\nthere\n boo");
}
@Test
public void longThenShortWithMoreLines() {
check("hellothere\n boo\n hi", "hello-\nthere\n boo\n hi");
}
@Test
public void endWithNewLine() {
check("a\n", "a\n");
}
@Test
public void spaceAndQuestionMark() {
check(" ?", " ?");
}
@Test
public void dontBreakOnQuestionMark() {
check("ab cde?", "ab\ncde?");
}
@Test
public void breakOnQuote() {
check("says 'helo'", "says\n'helo'");
}
@Test
public void breakQuoteInMiddle() {
check("why he's nasty", "why\nhe's\nnasty");
}
@Test
public void shortThenLong() {
check("hi mygoodnessme", "hi\nmygoo-\ndness-\nme");
}
@Test
public void longWhitespaceThenWord() {
check(" a", "\na");
}
@Test
public void longWhitespaceLastLine() {
check(" ", "");
}
@Test
public void longWhitespaceThenNewLine() {
check(" \n", "\n");
}
@Test
public void conserveWhitespace() {
check(" ab\n cd\n ef\n\nhi", " ab\n cd\n ef\n\nhi");
}
////////////////////////////////////////////
// Left trim tests
////////////////////////////////////////////
@Test
public void testLeftTrimLeadingEmpty() {
StringBuilder2 s = new StringBuilder2("");
WordWrap.leftTrim(s);
assertEquals("", s.toString());
}
@Test
public void testLeftTrimLeadingSpaces0() {
StringBuilder2 s = new StringBuilder2("abc");
WordWrap.leftTrim(s);
assertEquals("abc", s.toString());
}
@Test
public void testLeftTrimLeadingSpaces1() {
StringBuilder2 s = new StringBuilder2(" abc");
WordWrap.leftTrim(s);
assertEquals("abc", s.toString());
}
@Test
public void testLeftTrimLeadingSpaces3() {
StringBuilder2 s = new StringBuilder2(" abc");
WordWrap.leftTrim(s);
assertEquals("abc", s.toString());
}
////////////////////////////////////////////
// Right trim tests
////////////////////////////////////////////
@Test
public void testRightTrimAtEnd() {
assertEquals("abc", WordWrap.rightTrim("abc "));
}
@Test
public void testRightTrimNoSpace() {
assertEquals("abc", WordWrap.rightTrim("abc"));
}
@Test
public void testRightTrimEmpty() {
assertEquals("", WordWrap.rightTrim(""));
}
@Test
public void testRightTrimOnlySpace() {
assertEquals("", WordWrap.rightTrim(" "));
}
////////////////////////////////////////////
// Builder tests
////////////////////////////////////////////
@Test
public void testStringWidth() {
String text = WordWrap.from("abc").maxWidth(4).stringWidth(s -> s.length() * 2).wrap();
assertEquals("a-\nbc", text);
}
@Test
public void testNewLineOverride() {
String text = WordWrap.from("abc").maxWidth(2).newLine("\r\n").wrap();
assertEquals("a-\r\nbc", text);
}
@Test
public void testSetWordChars() {
String text = WordWrap.from("abc").maxWidth(2).extraWordChars("abc").wrap();
assertEquals("a-\nbc", text);
}
@Test
public void testIncludeWordChars() {
String text = WordWrap.from("abc").maxWidth(2).extraWordChars("")
.includeExtraWordChars("abc").wrap();
assertEquals("a-\nbc", text);
}
@Test
public void testExcludeWordChars() {
String text = WordWrap.from("abc").maxWidth(2).extraWordChars("abc")
.excludeExtraWordChars("abc").wrap();
assertEquals("a-\nbc", text);
}
@Test
public void testDontBreakLongWords() {
String s = WordWrap.from("hello jonathon").maxWidth(6).breakWords(false).wrap();
assertEquals("hello\njonathon", s);
}
@Test
public void testDontBreakLongWords2() {
String s = WordWrap.from("hell jonathon").maxWidth(6).breakWords(false).wrap();
assertEquals("hell\njonathon", s);
}
@Test
public void testFromInputStream() {
ByteArrayInputStream bytes = new ByteArrayInputStream(
"hi".getBytes(StandardCharsets.UTF_8));
assertEquals("hi", WordWrap.fromUtf8(bytes).maxWidth(6).wrap());
}
@Test
public void testFromFile() throws IOException {
Files.write(new File("target/test1.txt").toPath(), "hi".getBytes(StandardCharsets.UTF_8));
assertEquals("hi", WordWrap.from(new File("target/test1.txt"), StandardCharsets.UTF_8)
.maxWidth(6).wrap());
}
@Test(expected = IORuntimeException.class)
public void testWriterThrows() {
WordWrap.from("abc").wrap(new Writer() {
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
throw new IOException("boo");
}
@Override
public void flush() throws IOException {
}
@Override
public void close() throws IOException {
}
});
}
@Test(expected = IORuntimeException.class)
public void testWrapToFileThrows() {
WordWrap.from("abc").wrap(new File("target/doesNoExist/temp.txt"), StandardCharsets.UTF_8);
}
@Test(expected = IORuntimeException.class)
public void testCloseReaderThrows() {
WordWrap.close(new Reader() {
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
return 0;
}
@Override
public void close() throws IOException {
throw new IOException("boo");
}
});
}
@Test(expected = IORuntimeException.class)
public void testFromFileDoesNotExist() throws IOException {
WordWrap.from(new File("target/doesNotExist"), StandardCharsets.UTF_8).maxWidth(6).wrap();
}
@Test
public void testToList() {
List<String> list = WordWrap.from("hello there how are you").maxWidth(10).wrapToList();
assertEquals(Arrays.asList("hello", "there how", "are you"), list);
}
@Test
public void testToListWithNewLines() {
List<String> list = WordWrap.from("hello\n\n\n\nhow how\n").maxWidth(10).wrapToList();
assertEquals(Arrays.asList("hello", "", "","", "how how"), list);
}
@Test
public void testToListFinishWithNewLines() {
List<String> list = WordWrap.from("hello\n\n\n\n").maxWidth(10).wrapToList();
assertEquals(Arrays.asList("hello", "", "",""), list);
}
@Test(expected=IORuntimeException.class)
public void testLineConsumerThrows() {
WordWrap.from("hello there how are you").maxWidth(10).wrap(new LineConsumer() {
@Override
public void write(char[] chars, int offset, int length) throws IOException {
throw new IOException("problem");
}
@Override
public void writeNewLine() throws IOException {
throw new IOException("problem");
}});
}
@Test(expected=IORuntimeException.class)
public void testLineConsumerThrowsDontCloseReader() {
StringReader reader = new StringReader("hello there how are you");
WordWrap.from(reader, false).maxWidth(10).wrap(new LineConsumer() {
@Override
public void write(char[] chars, int offset, int length) throws IOException {
throw new IOException("problem");
}
@Override
public void writeNewLine() throws IOException {
throw new IOException("problem");
}});
}
@Test(expected = IllegalArgumentException.class)
public void testMaxWidthZero() {
WordWrap.from("abc").maxWidth(0);
}
@Test
public void testWrapToFile() {
File file = new File("target/testWrap.txt");
WordWrap.from("abc").wrap(file.getAbsolutePath(), StandardCharsets.UTF_8);
assertTrue(file.exists());
}
@Test
public void testDontWrapNumbers() {
assertEquals("hello\n123", WordWrap.from("hello 123").extraWordChars("0123456789").breakWords(false).maxWidth(8).wrap());
}
@Test
public void testNumbersWrapByDefault() {
assertEquals("hello 12\n3", WordWrap.from("hello 123").breakWords(false).maxWidth(8).wrap());
}
@Test
public void testStatelessness() {
assertEquals("hello super-\ncool", WordWrap.from("hello super-cool").breakWords(false).maxWidth(12).wrap());
assertEquals("hello\nsuper-cool", WordWrap.from("hello super-cool").breakWords(false).maxWidth(12).includeExtraWordChars("-").wrap());
assertEquals("hello super-\ncool", WordWrap.from("hello super-cool").breakWords(false).maxWidth(12).wrap());
}
@Test
public void testIssue49() {
assertEquals("CELESTAN Depot\n2,7mg/ml", //
WordWrap //
.from("CELESTAN Depot 2,7mg/ml") //
.includeExtraWordChars("0123456789") //
.breakWords(false) //
.maxWidth(17) //
.wrap());
}
////////////////////////////////////////////
// Novel wrapping tests
////////////////////////////////////////////
@Test
public void testImportanceOfBeingEarnest() throws IOException {
WordWrap.fromClasspathUtf8("/the-importance-of-being-earnest.txt") //
.maxWidth(20) //
.wrapUtf8("target/the-importance-of-being-earnest.txt");
}
@Test
public void testTheBlackGang() throws IOException {
WordWrap.fromClasspathUtf8("/the-black-gang.txt") //
.maxWidth(20) //
.wrapUtf8("target/the-black-gang.txt");
}
@Test
public void testTreasureIsland() throws IOException {
WordWrap.fromClasspathUtf8("/treasure-island-fragment.txt") //
.maxWidth(80) //
.wrapUtf8("target/treasure-island-fragment.txt");
}
@Test
public void testIsUtilityClass() {
Asserts.assertIsUtilityClass(WordWrap.class);
}
private static final class Check {
final String input;
final String output;
final String name;
Check(String input, String output, String name) {
this.input = input;
this.output = output;
this.name = name;
}
}
private static List<Check> checks = new ArrayList<>();
private static void check(String text, String expected) {
String s = WordWrap.from(text).maxWidth(6).wrap();
// System.out.println(s.replace(" ","\u2423"));
assertEquals(expected, s);
String name = Thread.currentThread().getStackTrace()[2].getMethodName();
checks.add(new Check(text, expected, name));
int i = 0;
try (FileWriter out = new FileWriter("src/docs/rules.md")) {
out.append("## Wrapping rules\n");
out.append(
"The rules below are generated from the unit tests in `WordWrapTest.java`. The default wrapping configuration used is a line length of 6 characters. The $ symbol is used to represent a new line character.\n\n");
for (Check check : checks) {
i++;
out.append(i + ". **" + check.name + "**\n\n");
out.append("Input:\n");
out.append("```\n");
out.append(prettify(check.input));
out.append("\n```\n");
out.append("Output:\n");
out.append("```\n");
out.append(prettify(check.output));
out.append("\n```\n\n");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static String prettify(String s) {
return s.replace(" ", "\u2423").replace("\n", "$\n");
}
public static void main(String[] args) throws IOException {
int i = 0;
long t = 0;
ByteArrayOutputStream b = new ByteArrayOutputStream(128 * 1024);
byte[] bytes = Files
.readAllBytes(new File("src/test/resources/treasure-island-fragment.txt").toPath());
String text = new String(bytes, StandardCharsets.UTF_8);
while (true) {
b.reset();
Writer out = new OutputStreamWriter(b, StandardCharsets.UTF_8);
WordWrap.from(text) //
.maxWidth(80) //
.wrap(out);
i++;
if (i % 100 == 0) {
if (i > 1000 && t == 0) {
t = System.currentTimeMillis();
i = 0;
}
long dt = System.currentTimeMillis() - t;
if (dt != 0) {
System.out.println("avgPerSecond=" + (i * 1000 / dt));
}
}
}
}
}