| Keith Rarick | 952f0bd | 2012-04-25 01:45:14 | [diff] [blame] | 1 | package text |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | var text = "The quick brown fox jumps over the lazy dog." |
| 9 | |
| 10 | func TestWrap(t *testing.T) { |
| Keith Rarick | 055ed20 | 2013-01-31 14:12:41 | [diff] [blame] | 11 | exp := [][]string{ |
| 12 | {"The", "quick", "brown", "fox"}, |
| 13 | {"jumps", "over", "the", "lazy", "dog."}, |
| Keith Rarick | 952f0bd | 2012-04-25 01:45:14 | [diff] [blame] | 14 | } |
| 15 | words := bytes.Split([]byte(text), sp) |
| Keith Rarick | e605cd5 | 2012-05-25 22:35:05 | [diff] [blame] | 16 | got := WrapWords(words, 1, 24, defaultPenalty) |
| Keith Rarick | 952f0bd | 2012-04-25 01:45:14 | [diff] [blame] | 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] { |
| Keith Rarick | 055ed20 | 2013-01-31 14:12:41 | [diff] [blame] | 25 | if exp[i][j] != string(got[i][j]) { |
| Keith Rarick | 952f0bd | 2012-04-25 01:45:14 | [diff] [blame] | 26 | t.Fatal(i, exp[i][j], got[i][j]) |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func 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 | |
| 39 | func 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 | } |
| Keith Rarick | bb797dc | 2015-09-05 22:45:08 | [diff] [blame] | 45 | |
| 46 | func 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 | } |