@@ -10,23 +10,20 @@ func TestWordWrap(t *testing.T) {
10
10
Expected string
11
11
Limit int
12
12
KeepNewlines bool
13
- HardWrap bool
14
13
}{
15
14
// No-op, should pass through, including trailing whitespace:
16
15
{
17
16
"foobar\n " ,
18
17
"foobar\n " ,
19
18
0 ,
20
19
true ,
21
- false ,
22
20
},
23
21
// Nothing to wrap here, should pass through:
24
22
{
25
23
"foo" ,
26
24
"foo" ,
27
25
4 ,
28
26
true ,
29
- false ,
30
27
},
31
28
// A single word that is too long passes through.
32
29
// We do not break long words:
@@ -35,31 +32,27 @@ func TestWordWrap(t *testing.T) {
35
32
"foobarfoo" ,
36
33
4 ,
37
34
true ,
38
- false ,
39
35
},
40
36
// Lines are broken at whitespace:
41
37
{
42
38
"foo bar foo" ,
43
39
"foo\n bar\n foo" ,
44
40
4 ,
45
41
true ,
46
- false ,
47
42
},
48
43
// A hyphen is a valid breakpoint:
49
44
{
50
45
"foo-foobar" ,
51
46
"foo-\n foobar" ,
52
47
4 ,
53
48
true ,
54
- false ,
55
49
},
56
50
// Space buffer needs to be emptied before breakpoints:
57
51
{
58
52
"foo --bar" ,
59
53
"foo --bar" ,
60
54
9 ,
61
55
true ,
62
- false ,
63
56
},
64
57
// Lines are broken at whitespace, even if words
65
58
// are too long. We do not break words:
@@ -68,15 +61,13 @@ func TestWordWrap(t *testing.T) {
68
61
"foo\n bars\n foobars" ,
69
62
4 ,
70
63
true ,
71
- false ,
72
64
},
73
65
// A word that would run beyond the limit is wrapped:
74
66
{
75
67
"foo bar" ,
76
68
"foo\n bar" ,
77
69
5 ,
78
70
true ,
79
- false ,
80
71
},
81
72
// Whitespace that trails a line and fits the width
82
73
// passes through, as does whitespace prefixing an
@@ -86,7 +77,6 @@ func TestWordWrap(t *testing.T) {
86
77
"foo\n b\t a\n bar" ,
87
78
4 ,
88
79
true ,
89
- false ,
90
80
},
91
81
// Trailing whitespace is removed if it doesn't fit the width.
92
82
// Runs of whitespace on which a line is broken are removed:
@@ -95,61 +85,53 @@ func TestWordWrap(t *testing.T) {
95
85
"foo\n b\n ar" ,
96
86
4 ,
97
87
true ,
98
- false ,
99
88
},
100
89
// An explicit line break at the end of the input is preserved:
101
90
{
102
91
"foo bar foo\n " ,
103
92
"foo\n bar\n foo\n " ,
104
93
4 ,
105
94
true ,
106
- false ,
107
95
},
108
96
// Explicit break are always preserved:
109
97
{
110
98
"\n foo bar\n \n \n foo\n " ,
111
99
"\n foo\n bar\n \n \n foo\n " ,
112
100
4 ,
113
101
true ,
114
- false ,
115
102
},
116
103
// Unless we ask them to be ignored:
117
104
{
118
105
"\n foo bar\n \n \n foo\n " ,
119
106
"foo\n bar\n foo" ,
120
107
4 ,
121
108
false ,
122
- false ,
123
109
},
124
110
// Complete example:
125
111
{
126
112
" This is a list: \n \n \t * foo\n \t * bar\n \n \n \t * foo \n bar " ,
127
113
" This\n is a\n list: \n \n \t * foo\n \t * bar\n \n \n \t * foo\n bar" ,
128
114
6 ,
129
115
true ,
130
- false ,
131
116
},
132
117
// ANSI sequence codes don't affect length calculation:
133
118
{
134
119
"\x1B [38;2;249;38;114mfoo\x1B [0m\x1B [38;2;248;248;242m \x1B [0m\x1B [38;2;230;219;116mbar\x1B [0m" ,
135
120
"\x1B [38;2;249;38;114mfoo\x1B [0m\x1B [38;2;248;248;242m \x1B [0m\x1B [38;2;230;219;116mbar\x1B [0m" ,
136
121
7 ,
137
122
true ,
138
- false ,
139
123
},
140
124
// ANSI control codes don't get wrapped, but get finished and again started at each line break:
141
125
{
142
126
"\x1B [38;2;249;38;114m(\x1B [0m\x1B [38;2;248;248;242mjust another test\x1B [38;2;249;38;114m)\x1B [0m" ,
143
127
"\x1B [38;2;249;38;114m(\x1B [0m\x1B [38;2;248;248;242mjust\x1B [0m\n \x1B [38;2;248;248;242manother\x1B [0m\n \x1B [38;2;248;248;242mtest\x1B [38;2;249;38;114m)\x1B [0m" ,
144
128
3 ,
145
129
true ,
146
- false ,
147
130
},
148
131
}
149
132
150
133
for i , tc := range tt {
151
134
f := NewWriter (tc .Limit )
152
- f .HardBreak = tc .HardWrap
153
135
f .KeepNewlines = tc .KeepNewlines
154
136
155
137
_ , err := f .Write ([]byte (tc .Input ))
@@ -171,3 +153,107 @@ func TestWordWrapString(t *testing.T) {
171
153
t .Errorf ("expected:\n \n `%s`\n \n Actual Output:\n \n `%s`" , expected , actual )
172
154
}
173
155
}
156
+
157
+ func TestHardWrap (t * testing.T ) {
158
+ tt := []struct {
159
+ Input string
160
+ Expected string
161
+ Limit int
162
+ KeepNewlines bool
163
+ PreserveSpaces bool
164
+ TabReplace string
165
+ }{
166
+ // hardwrap wraps at limit and does not add newline if there is not more text
167
+ {
168
+ "foobarfoobar" ,
169
+ "foo\n bar\n foo\n bar" ,
170
+ 3 ,
171
+ true ,
172
+ true ,
173
+ "" ,
174
+ },
175
+ // With no TabReplace string tabs get ignored
176
+ {
177
+ "\t foo\t bar\t foo\t bar" ,
178
+ "foo\n bar\n foo\n bar" ,
179
+ 3 ,
180
+ true ,
181
+ true ,
182
+ "" ,
183
+ },
184
+ // limit of zero gets ignored
185
+ {
186
+ "foobar" ,
187
+ "foobar" ,
188
+ 0 ,
189
+ true ,
190
+ true ,
191
+ "" ,
192
+ },
193
+ // ANSI gets ended and restarted at each linebreak, so that the ansi is self contained within a line.
194
+ {
195
+ "\x1B [38;2;249;38;114m(\x1B [0m\x1B [38;2;248;248;242mjust an\n other test\x1B [38;2;249;38;114m)\x1B [0m" ,
196
+ `[38;2;249;38;114m([0m[38;2;248;248;242mju[0m
197
+ [38;2;248;248;242mst [0m
198
+ [38;2;248;248;242man[0m
199
+ [38;2;248;248;242moth[0m
200
+ [38;2;248;248;242mer [0m
201
+ [38;2;248;248;242mtes[0m
202
+ [38;2;248;248;242mt[38;2;249;38;114m)[0m` ,
203
+ 3 ,
204
+ true ,
205
+ true ,
206
+ "" ,
207
+ },
208
+ // if requested spaces are preserved as are Explicit linebreaks:
209
+ {
210
+ "\n foo bar\n \n \n foo\n " ,
211
+ "\n foo \n bar\n \n \n foo\n " ,
212
+ 4 ,
213
+ true ,
214
+ true ,
215
+ "" ,
216
+ },
217
+ // Unless we ask them to be ignored:
218
+ {
219
+ "\n foo bar\n \n \n foo\n " ,
220
+ "foo\n bar\n foo" ,
221
+ 4 ,
222
+ false ,
223
+ false ,
224
+ "" ,
225
+ },
226
+ {
227
+ "[38;2;248;248;242m[38;2;249;38;114mtest[0m" ,
228
+ "[38;2;248;248;242m[38;2;249;38;114mtest[0m" ,
229
+ 4 ,
230
+ true ,
231
+ true ,
232
+ "" ,
233
+ },
234
+ {
235
+ " " ,
236
+ " \n \n \n \n \n \n \n \n \n " ,
237
+ 4 ,
238
+ true ,
239
+ true ,
240
+ "" ,
241
+ },
242
+ }
243
+ for i , tc := range tt {
244
+ f := NewWriter (tc .Limit )
245
+ f .KeepNewlines = tc .KeepNewlines
246
+ f .HardWrap = true
247
+ f .PreserveSpaces = tc .PreserveSpaces
248
+
249
+ _ , err := f .Write ([]byte (tc .Input ))
250
+ if err != nil {
251
+ t .Error (err )
252
+ }
253
+ f .Close ()
254
+
255
+ if f .String () != tc .Expected {
256
+ t .Errorf ("Test %d, expected:\n \n `%s`\n \n Actual Output:\n \n `%s`" , i , tc .Expected , f .String ())
257
+ }
258
+ }
259
+ }
0 commit comments