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
|
package wordwrap
import (
"testing"
)
func TestWordWrap(t *testing.T) {
tt := []struct {
Input string
Expected string
Limit int
KeepNewlines bool
}{
// No-op, should pass through, including trailing whitespace:
{
"foobar\n ",
"foobar\n ",
0,
true,
},
// Nothing to wrap here, should pass through:
{
"foo",
"foo",
4,
true,
},
// A single word that is too long passes through.
// We do not break long words:
{
"foobarfoo",
"foobarfoo",
4,
true,
},
// Lines are broken at whitespace:
{
"foo bar foo",
"foo\nbar\nfoo",
4,
true,
},
// A hyphen is a valid breakpoint:
{
"foo-foobar",
"foo-\nfoobar",
4,
true,
},
// Space buffer needs to be emptied before breakpoints:
{
"foo --bar",
"foo --bar",
9,
true,
},
// Lines are broken at whitespace, even if words
// are too long. We do not break words:
{
"foo bars foobars",
"foo\nbars\nfoobars",
4,
true,
},
// A word that would run beyond the limit is wrapped:
{
"foo bar",
"foo\nbar",
5,
true,
},
// Whitespace that trails a line and fits the width
// passes through, as does whitespace prefixing an
// explicit line break. A tab counts as one character:
{
"foo\nb\t a\n bar",
"foo\nb\t a\n bar",
4,
true,
},
// Trailing whitespace is removed if it doesn't fit the width.
// Runs of whitespace on which a line is broken are removed:
{
"foo \nb ar ",
"foo\nb\nar",
4,
true,
},
// An explicit line break at the end of the input is preserved:
{
"foo bar foo\n",
"foo\nbar\nfoo\n",
4,
true,
},
// Explicit break are always preserved:
{
"\nfoo bar\n\n\nfoo\n",
"\nfoo\nbar\n\n\nfoo\n",
4,
true,
},
// Unless we ask them to be ignored:
{
"\nfoo bar\n\n\nfoo\n",
"foo\nbar\nfoo",
4,
false,
},
// Complete example:
{
" This is a list: \n\n\t* foo\n\t* bar\n\n\n\t* foo \nbar ",
" This\nis a\nlist: \n\n\t* foo\n\t* bar\n\n\n\t* foo\nbar",
6,
true,
},
// ANSI sequence codes don't affect length calculation:
{
"\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m",
"\x1B[38;2;249;38;114mfoo\x1B[0m\x1B[38;2;248;248;242m \x1B[0m\x1B[38;2;230;219;116mbar\x1B[0m",
7,
true,
},
// ANSI control codes don't get wrapped:
{
"\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mjust another test\x1B[38;2;249;38;114m)\x1B[0m",
"\x1B[38;2;249;38;114m(\x1B[0m\x1B[38;2;248;248;242mjust\nanother\ntest\x1B[38;2;249;38;114m)\x1B[0m",
3,
true,
},
}
for i, tc := range tt {
f := NewWriter(tc.Limit)
f.KeepNewlines = tc.KeepNewlines
_, err := f.Write([]byte(tc.Input))
if err != nil {
t.Error(err)
}
f.Close()
if f.String() != tc.Expected {
t.Errorf("Test %d, expected:\n\n`%s`\n\nActual Output:\n\n`%s`", i, tc.Expected, f.String())
}
}
}
func TestWordWrapString(t *testing.T) {
actual := String("foo bar", 3)
expected := "foo\nbar"
if actual != expected {
t.Errorf("expected:\n\n`%s`\n\nActual Output:\n\n`%s`", expected, actual)
}
}
|