1package text 2 3import ( 4 "bytes" 5 "testing" 6) 7 8var text = "The quick brown fox jumps over the lazy dog." 9 10func TestWrap(t *testing.T) { 11 exp := [][]string{ 12 {"The", "quick", "brown", "fox"}, 13 {"jumps", "over", "the", "lazy", "dog."}, 14 } 15 words := bytes.Split([]byte(text), sp) 16 got := WrapWords(words, 1, 24, defaultPenalty) 17 if len(exp) != len(got) { 18 t.Fail() 19 } 20 for i := range exp { 21 if len(exp[i]) != len(got[i]) { 22 t.Fail() 23 } 24 for j := range exp[i] { 25 if exp[i][j] != string(got[i][j]) { 26 t.Fatal(i, exp[i][j], got[i][j]) 27 } 28 } 29 } 30} 31 32func TestWrapNarrow(t *testing.T) { 33 exp := "The\nquick\nbrown\nfox\njumps\nover\nthe\nlazy\ndog." 34 if Wrap(text, 5) != exp { 35 t.Fail() 36 } 37} 38 39func TestWrapOneLine(t *testing.T) { 40 exp := "The quick brown fox jumps over the lazy dog." 41 if Wrap(text, 500) != exp { 42 t.Fail() 43 } 44} 45 46func TestWrapBug1(t *testing.T) { 47 cases := []struct { 48 limit int 49 text string 50 want string 51 }{ 52 {4, "aaaaa", "aaaaa"}, 53 {4, "a aaaaa", "a\naaaaa"}, 54 } 55 56 for _, test := range cases { 57 got := Wrap(test.text, test.limit) 58 if got != test.want { 59 t.Errorf("Wrap(%q, %d) = %q want %q", test.text, test.limit, got, test.want) 60 } 61 } 62} 63