1package wordwrap
2
3import (
4	"testing"
5)
6
7func TestWrapString(t *testing.T) {
8	cases := []struct {
9		Input, Output string
10		Lim           uint
11	}{
12		// A simple word passes through.
13		{
14			"foo",
15			"foo",
16			4,
17		},
18		// A single word that is too long passes through.
19		// We do not break words.
20		{
21			"foobarbaz",
22			"foobarbaz",
23			4,
24		},
25		// Lines are broken at whitespace.
26		{
27			"foo bar baz",
28			"foo\nbar\nbaz",
29			4,
30		},
31		// Lines are broken at whitespace, even if words
32		// are too long. We do not break words.
33		{
34			"foo bars bazzes",
35			"foo\nbars\nbazzes",
36			4,
37		},
38		// A word that would run beyond the width is wrapped.
39		{
40			"fo sop",
41			"fo\nsop",
42			4,
43		},
44		// Whitespace that trails a line and fits the width
45		// passes through, as does whitespace prefixing an
46		// explicit line break. A tab counts as one character.
47		{
48			"foo\nb\t r\n baz",
49			"foo\nb\t r\n baz",
50			4,
51		},
52		// Trailing whitespace is removed if it doesn't fit the width.
53		// Runs of whitespace on which a line is broken are removed.
54		{
55			"foo    \nb   ar   ",
56			"foo\nb\nar",
57			4,
58		},
59		// An explicit line break at the end of the input is preserved.
60		{
61			"foo bar baz\n",
62			"foo\nbar\nbaz\n",
63			4,
64		},
65		// Explicit break are always preserved.
66		{
67			"\nfoo bar\n\n\nbaz\n",
68			"\nfoo\nbar\n\n\nbaz\n",
69			4,
70		},
71		// Complete example:
72		{
73			" This is a list: \n\n\t* foo\n\t* bar\n\n\n\t* baz  \nBAM    ",
74			" This\nis a\nlist: \n\n\t* foo\n\t* bar\n\n\n\t* baz\nBAM",
75			6,
76		},
77	}
78
79	for i, tc := range cases {
80		actual := WrapString(tc.Input, tc.Lim)
81		if actual != tc.Output {
82			t.Fatalf("Case %d Input:\n\n`%s`\n\nActual Output:\n\n`%s`", i, tc.Input, actual)
83		}
84	}
85}
86