-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwordwrap_test.go
169 lines (159 loc) · 3.26 KB
/
wordwrap_test.go
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
package wordwrap_test
import (
"testing"
"github.com/eidolon/wordwrap"
)
func TestIndent(t *testing.T) {
tests := []struct {
input string
prefix string
prefixAll bool
expected string
}{
// When testing with no input
{
"",
"",
false,
"",
},
// When not prefixing all lines
// Should apply the prefix to the first line
{
"Test text\nTest text\nTest text",
"First line",
false,
"First lineTest text\n Test text\n Test text",
},
// Should allow prefixes to simply be spaces
{
"Test text\nTest text",
" ",
false,
" Test text\n Test text",
},
// When prefixing all lines
// Should apply the prefix to all lines
{
"Test text\nTest text\nTest text",
"First line",
true,
"First lineTest text\nFirst lineTest text\nFirst lineTest text",
},
}
for i, test := range tests {
actual := wordwrap.Indent(test.input, test.prefix, test.prefixAll)
if actual != test.expected {
t.Fatalf(
"Result for case %d did not match expected result.\nExpected:\n%s\nActual:\n%s\n",
i+1,
test.expected,
actual,
)
}
}
}
func TestWrapper(t *testing.T) {
tests := []struct {
limit int
breakWords bool
input string
expected string
}{
// When testing with no input
{
4,
false,
"",
"",
},
// When not breaking words
// Should wrap text so words fit on lines with the given limit
{
10,
false,
"Test text Test text Test text Test text",
"Test text\nTest text\nTest text\nTest text",
},
// Should remove additional whitespace
{
10,
false,
"Test text Test text Test text Test text",
"Test text\nTest text\nTest text\nTest text",
},
// Should trim text
{
10,
false,
" Test text Test text Test text Test text ",
"Test text\nTest text\nTest text\nTest text",
},
// Should not break words if breakWords is false
{
4,
false,
"Testtext",
"Testtext",
},
// When breaking words
// Should break words, and insert a hyphen
{
4,
true,
"Testtext",
"Test\ntext",
},
// Should break words into tiny pieces
{
1,
true,
"Testtext",
"T\ne\ns\nt\nt\ne\nx\nt",
},
// Should not break words if no words are too long
{
4,
true,
"Test text",
"Test\ntext",
},
// When given slightly more realistic data
// Should be broken properly
{
40,
false,
"This is a bunch of text that should be split at somewhere near 40 characters.",
"This is a bunch of text that should be\nsplit at somewhere near 40 characters.",
},
// When given text with a line break in it
// It should rebuild the string with spaces in, remember, we're intentionally being simple
{
20,
true,
"Test\n\ntext",
"Test text",
},
}
for i, test := range tests {
wrapper := wordwrap.Wrapper(test.limit, test.breakWords)
actual := wrapper(test.input)
if actual != test.expected {
t.Fatalf(
"Result for case %d did not match expected result.\nExpected:\n%s\nActual:\n%s\n",
i+1,
test.expected,
actual,
)
}
}
}
func TestWrapperPanicsWithInvalidLimit(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatalf("Wrapper did not panic on invalid input.")
}
}()
// 0 is an invalid limit
wordwrap.Wrapper(0, false)
}