-
Notifications
You must be signed in to change notification settings - Fork 15
/
wrap_test.go
152 lines (148 loc) · 3.64 KB
/
wrap_test.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package clif
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"fmt"
"strings"
"os"
)
var testsWrapText = []struct{
from string
expect string
}{
/**/
{
from: "",
expect: "",
},
{
from: "foo",
expect: "foo",
},
{
from: "foo bar baz",
expect: "foo bar baz",
},
{
from: "foo bar baz",
expect: "foo bar baz",
},
{
from: "foobarbaz",
expect: "foobarbaz",
},
{
from: "foobarbaz foobarbaz",
expect: "foobarbaz\nfoobarbaz",
},
{
from: "foo bar baz foo bar baz",
expect: "foo bar baz\nfoo bar baz",
},
{
from: "foo bar baz\nfoo bar baz",
expect: "foo bar baz\nfoo bar baz",
},
{
from: "foo bar baz foo bar baz foo bar baz",
expect: "foo bar baz\nfoo bar baz\nfoo bar baz",
},
{
from: "foo bar baz\nfoo bar baz\nfoo bar baz",
expect: "foo bar baz\nfoo bar baz\nfoo bar baz",
},
{
from: "foobarbazfoobarbazfoobarbaz",
expect: "foobarbazfoo\nbarbazfoobar\nbaz",
},
{
from: "foobarbazfoobarbazfoobarbazfoobarbaz",
expect: "foobarbazfoo\nbarbazfoobar\nbazfoobarbaz",
},
{
from: "xx foobarbazfoobarbazfoobarbaz yy",
expect: "xx\nfoobarbazfoo\nbarbazfoobar\nbaz yy",
},
{
from: "foo bar baz foo bar baz",
expect: "foo bar baz\nfoo bar baz",
},
{
from: "\033[34mfoo\033[0m",
expect: "\033[34mfoo\033[0m",
},
{
from: "\033[34mfoo\033[0m bar baz foo bar baz",
expect: "\033[34mfoo\033[0m bar baz\nfoo bar baz",
},
{
from: "\033[34mfoo bar baz foo bar\033[0m baz",
expect: strings.Join([]string{
"\033[34mfoo bar baz\033[0m",
"\033[34mfoo bar\033[0m baz",
}, "\n"),
},
{
from: "\033[34mfoo \033[1mbar",
expect: "\033[34mfoo \033[1mbar\033[0m",
},
{
from: "\033[34mfoo \033[1mbar baz\nfoo bar \033[0mbaz",
expect: "\033[34mfoo \033[1mbar baz\033[0m\n\033[34m\033[1mfoo bar \033[0mbaz",
},
{
from: "\033[34mfoobarbazfoobarbazfoobarbazfoobarbaz",
expect: "\033[34mfoobarbazfoo\033[0m\n\033[34mbarbazfoobar\033[0m\n\033[34mbazfoobarbaz\033[0m",
},
}
func TestWrapText(t *testing.T) {
Convey("Wrapping text", t, func() {
wrapper := NewWrapper(12)
wrapper.WhitespaceMode = WRAP_WHITESPACE_CONTRACT
wrapper.TrimMode = WRAP_TRIM_RIGHT
wrapper.BreakWords = true
for idx, test := range testsWrapText {
out := _stringRenderDump(test.from)
Convey(fmt.Sprintf("%d) %s\033[0m", idx, out), func() {
to := wrapper.Wrap(test.from)
if dbg := os.Getenv("WRAP_DEBUG"); dbg == "yes" || dbg == "1" {
_stringCompareDump(to, test.expect)
}
So(to, ShouldEqual, test.expect)
})
}
})
}
func TestWrapTextTrim(t *testing.T) {
Convey("Wrap vs Trimming", t, func() {
wrapper := NewWrapper(8)
wrapper.WhitespaceMode = WRAP_WHITESPACE_KEEP
wrapper.BreakWords = true
Convey("Right trim none", func() {
wrapper.TrimMode = WRAP_TRIM_NONE
So(wrapper.Wrap(" Line 1 \n Line 2 "), ShouldEqual, " Line 1 \n Line 2 ")
})
Convey("Right trim", func() {
wrapper.TrimMode = WRAP_TRIM_RIGHT
So(wrapper.Wrap(" Line 1 \n Line 2 "), ShouldEqual, " Line 1\n Line 2")
})
Convey("Left trim", func() {
wrapper.TrimMode = WRAP_TRIM_LEFT
So(wrapper.Wrap(" Line 1 \n Line 2 "), ShouldEqual, "Line 1 \nLine 2 ")
})
Convey("Both trim", func() {
wrapper.TrimMode = WRAP_TRIM_BOTH
So(wrapper.Wrap(" Line 1 \n Line 2 "), ShouldEqual, "Line 1\nLine 2")
})
Convey("Trim without whitespace mode", func() {
wrapper.WhitespaceMode = WRAP_WHITESPACE_CONTRACT
wrapper.TrimMode = WRAP_TRIM_BOTH
wrapped := wrapper.Wrap(" Line 1 \n Line 2 ")
expect := "Line 1\nLine 2"
if dbg := os.Getenv("WRAP_DEBUG"); dbg == "yes" || dbg == "1" {
_stringCompareDump(wrapped, expect)
}
So(wrapped, ShouldEqual, expect)
})
})
}