🌐 AI搜索 & 代理 主页
blob: 634b6e8ebb9235ad06627728425445d8f1e903b5 [file] [log] [blame]
Keith Rarick952f0bd2012-04-25 01:45:141package 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) {
Keith Rarick055ed202013-01-31 14:12:4111 exp := [][]string{
12 {"The", "quick", "brown", "fox"},
13 {"jumps", "over", "the", "lazy", "dog."},
Keith Rarick952f0bd2012-04-25 01:45:1414 }
15 words := bytes.Split([]byte(text), sp)
Keith Raricke605cd52012-05-25 22:35:0516 got := WrapWords(words, 1, 24, defaultPenalty)
Keith Rarick952f0bd2012-04-25 01:45:1417 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 Rarick055ed202013-01-31 14:12:4125 if exp[i][j] != string(got[i][j]) {
Keith Rarick952f0bd2012-04-25 01:45:1426 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}
Keith Rarickbb797dc2015-09-05 22:45:0845
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}