@@ -10,23 +10,20 @@ func TestWordWrap(t *testing.T) {
1010 Expected string
1111 Limit int
1212 KeepNewlines bool
13- HardWrap bool
1413 }{
1514 // No-op, should pass through, including trailing whitespace:
1615 {
1716 "foobar\n " ,
1817 "foobar\n " ,
1918 0 ,
2019 true ,
21- false ,
2220 },
2321 // Nothing to wrap here, should pass through:
2422 {
2523 "foo" ,
2624 "foo" ,
2725 4 ,
2826 true ,
29- false ,
3027 },
3128 // A single word that is too long passes through.
3229 // We do not break long words:
@@ -35,31 +32,27 @@ func TestWordWrap(t *testing.T) {
3532 "foobarfoo" ,
3633 4 ,
3734 true ,
38- false ,
3935 },
4036 // Lines are broken at whitespace:
4137 {
4238 "foo bar foo" ,
4339 "foo\n bar\n foo" ,
4440 4 ,
4541 true ,
46- false ,
4742 },
4843 // A hyphen is a valid breakpoint:
4944 {
5045 "foo-foobar" ,
5146 "foo-\n foobar" ,
5247 4 ,
5348 true ,
54- false ,
5549 },
5650 // Space buffer needs to be emptied before breakpoints:
5751 {
5852 "foo --bar" ,
5953 "foo --bar" ,
6054 9 ,
6155 true ,
62- false ,
6356 },
6457 // Lines are broken at whitespace, even if words
6558 // are too long. We do not break words:
@@ -68,15 +61,13 @@ func TestWordWrap(t *testing.T) {
6861 "foo\n bars\n foobars" ,
6962 4 ,
7063 true ,
71- false ,
7264 },
7365 // A word that would run beyond the limit is wrapped:
7466 {
7567 "foo bar" ,
7668 "foo\n bar" ,
7769 5 ,
7870 true ,
79- false ,
8071 },
8172 // Whitespace that trails a line and fits the width
8273 // passes through, as does whitespace prefixing an
@@ -86,7 +77,6 @@ func TestWordWrap(t *testing.T) {
8677 "foo\n b\t a\n bar" ,
8778 4 ,
8879 true ,
89- false ,
9080 },
9181 // Trailing whitespace is removed if it doesn't fit the width.
9282 // Runs of whitespace on which a line is broken are removed:
@@ -95,61 +85,53 @@ func TestWordWrap(t *testing.T) {
9585 "foo\n b\n ar" ,
9686 4 ,
9787 true ,
98- false ,
9988 },
10089 // An explicit line break at the end of the input is preserved:
10190 {
10291 "foo bar foo\n " ,
10392 "foo\n bar\n foo\n " ,
10493 4 ,
10594 true ,
106- false ,
10795 },
10896 // Explicit break are always preserved:
10997 {
11098 "\n foo bar\n \n \n foo\n " ,
11199 "\n foo\n bar\n \n \n foo\n " ,
112100 4 ,
113101 true ,
114- false ,
115102 },
116103 // Unless we ask them to be ignored:
117104 {
118105 "\n foo bar\n \n \n foo\n " ,
119106 "foo\n bar\n foo" ,
120107 4 ,
121108 false ,
122- false ,
123109 },
124110 // Complete example:
125111 {
126112 " This is a list: \n \n \t * foo\n \t * bar\n \n \n \t * foo \n bar " ,
127113 " This\n is a\n list: \n \n \t * foo\n \t * bar\n \n \n \t * foo\n bar" ,
128114 6 ,
129115 true ,
130- false ,
131116 },
132117 // ANSI sequence codes don't affect length calculation:
133118 {
134119 "\x1B [38;2;249;38;114mfoo\x1B [0m\x1B [38;2;248;248;242m \x1B [0m\x1B [38;2;230;219;116mbar\x1B [0m" ,
135120 "\x1B [38;2;249;38;114mfoo\x1B [0m\x1B [38;2;248;248;242m \x1B [0m\x1B [38;2;230;219;116mbar\x1B [0m" ,
136121 7 ,
137122 true ,
138- false ,
139123 },
140124 // ANSI control codes don't get wrapped, but get finished and again started at each line break:
141125 {
142126 "\x1B [38;2;249;38;114m(\x1B [0m\x1B [38;2;248;248;242mjust another test\x1B [38;2;249;38;114m)\x1B [0m" ,
143127 "\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" ,
144128 3 ,
145129 true ,
146- false ,
147130 },
148131 }
149132
150133 for i , tc := range tt {
151134 f := NewWriter (tc .Limit )
152- f .HardBreak = tc .HardWrap
153135 f .KeepNewlines = tc .KeepNewlines
154136
155137 _ , err := f .Write ([]byte (tc .Input ))
@@ -171,3 +153,107 @@ func TestWordWrapString(t *testing.T) {
171153 t .Errorf ("expected:\n \n `%s`\n \n Actual Output:\n \n `%s`" , expected , actual )
172154 }
173155}
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