File: wrap_test.go

package info (click to toggle)
golang-text 0.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-proposed-updates, sid, trixie
  • size: 128 kB
  • sloc: makefile: 5
file content (62 lines) | stat: -rw-r--r-- 1,177 bytes parent folder | download | duplicates (4)
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
package text

import (
	"bytes"
	"testing"
)

var text = "The quick brown fox jumps over the lazy dog."

func TestWrap(t *testing.T) {
	exp := [][]string{
		{"The", "quick", "brown", "fox"},
		{"jumps", "over", "the", "lazy", "dog."},
	}
	words := bytes.Split([]byte(text), sp)
	got := WrapWords(words, 1, 24, defaultPenalty)
	if len(exp) != len(got) {
		t.Fail()
	}
	for i := range exp {
		if len(exp[i]) != len(got[i]) {
			t.Fail()
		}
		for j := range exp[i] {
			if exp[i][j] != string(got[i][j]) {
				t.Fatal(i, exp[i][j], got[i][j])
			}
		}
	}
}

func TestWrapNarrow(t *testing.T) {
	exp := "The\nquick\nbrown\nfox\njumps\nover\nthe\nlazy\ndog."
	if Wrap(text, 5) != exp {
		t.Fail()
	}
}

func TestWrapOneLine(t *testing.T) {
	exp := "The quick brown fox jumps over the lazy dog."
	if Wrap(text, 500) != exp {
		t.Fail()
	}
}

func TestWrapBug1(t *testing.T) {
	cases := []struct {
		limit int
		text  string
		want  string
	}{
		{4, "aaaaa", "aaaaa"},
		{4, "a aaaaa", "a\naaaaa"},
	}

	for _, test := range cases {
		got := Wrap(test.text, test.limit)
		if got != test.want {
			t.Errorf("Wrap(%q, %d) = %q want %q", test.text, test.limit, got, test.want)
		}
	}
}