-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWrapperTest.java
More file actions
41 lines (32 loc) · 1.19 KB
/
Copy pathWrapperTest.java
File metadata and controls
41 lines (32 loc) · 1.19 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
package com.example.cleancoder.tdd.wordwrap;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
public class WrapperTest {
@Test
void shouldWrap() {
assertWraps(null, 1, "");
assertWraps("", 1, "");
assertWraps("x", 1, "x");
assertWraps("xx", 1, "x\nx");
assertWraps("xxx", 1, "x\nx\nx");
assertWraps("x x", 1, "x\nx");
assertWraps("x xx", 3, "x\nxx");
assertWraps("four score and seven years ago our fathers brought forth upon this continent",
7,
"four\nscore\nand\nseven\nyears\nago our\nfathers\nbrought\nforth\nupon\nthis\ncontine\nnt");
}
private static void assertWraps(String s, int width, String expected) {
assertThat(wrap(s, width)).isEqualTo(expected);
}
private static String wrap(String s, int width) {
if(s == null)
return "";
if(s.length() <= width)
return s;
int breakPoint = s.lastIndexOf(" ", width);
if(breakPoint == -1) {
breakPoint = width;
}
return s.substring(0, breakPoint) + "\n" + wrap(s.substring(breakPoint).trim(), width);
}
}