Skip to content

Commit aca706d

Browse files
author
treilik
committed
added some more test cases
to test space preserving and HardWrap shortcut function
1 parent 7457b5f commit aca706d

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

wordwrap/wordwrap.go

-3
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,6 @@ func (w *WordWrap) Write(b []byte) (int, error) {
215215
// Close will finish the word-wrap operation. Always call it before trying to
216216
// retrieve the final result.
217217
func (w *WordWrap) Close() error {
218-
if w.HardWrap && w.word.PrintableRuneWidth()+w.lineLen > w.Limit {
219-
w.addNewLine()
220-
}
221218
if w.PreserveSpaces {
222219
w.addSpace()
223220
}

wordwrap/wordwrap_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ func TestHardWrap(t *testing.T) {
223223
false,
224224
"",
225225
},
226+
// If the text fits -> passing through
226227
{
227228
"test",
228229
"test",
@@ -231,6 +232,7 @@ func TestHardWrap(t *testing.T) {
231232
true,
232233
"",
233234
},
235+
// If requested preserve spaces, no matter how much.
234236
{
235237
" ",
236238
" \n \n \n \n \n \n \n \n \n ",
@@ -239,6 +241,15 @@ func TestHardWrap(t *testing.T) {
239241
true,
240242
"",
241243
},
244+
// If requested preserve spaces, no matter how much and wrap them accordingly
245+
{
246+
" ",
247+
" \n \n \n \n \n \n \n \n \n \n ",
248+
4,
249+
true,
250+
true,
251+
"",
252+
},
242253
}
243254
for i, tc := range tt {
244255
f := NewWriter(tc.Limit)
@@ -257,3 +268,18 @@ func TestHardWrap(t *testing.T) {
257268
}
258269
}
259270
}
271+
272+
func TestHarWrapShort(t *testing.T) {
273+
testCase := "\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mjust an\nother test\x1B[38;2;249;38;114m)\x1B[0m"
274+
expected := `(ju
275+
st
276+
an
277+
oth
278+
er
279+
tes
280+
t)`
281+
out := HardWrap(testCase, 3, " ")
282+
if out != expected {
283+
t.Errorf("From input expected:\n\n`%s`\n\nActual Output:\n\n`%s`", expected, out)
284+
}
285+
}

0 commit comments

Comments
 (0)