forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
padding_test.go
137 lines (121 loc) · 3.49 KB
/
padding_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
package strutil_test
import (
"testing"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestPadding(t *testing.T) {
tests := []struct {
want, give, pad string
len int
pos strutil.PosFlag
}{
{"ab000", "ab", "0", 5, strutil.PosRight},
{"000ab", "ab", "0", 5, strutil.PosLeft},
{"ab012", "ab012", "0", 4, strutil.PosLeft},
{"ab ", "ab", "", 5, strutil.PosRight},
{" ab", "ab", "", 5, strutil.PosLeft},
}
for _, tt := range tests {
assert.Eq(t, tt.want, strutil.Padding(tt.give, tt.pad, tt.len, tt.pos))
if tt.pos == strutil.PosRight {
assert.Eq(t, tt.want, strutil.PadRight(tt.give, tt.pad, tt.len))
} else {
assert.Eq(t, tt.want, strutil.PadLeft(tt.give, tt.pad, tt.len))
}
}
}
func TestResize(t *testing.T) {
tests := []struct {
want, give string
len int
pos strutil.PosFlag
}{
{"ab ", "ab", 5, strutil.PosRight},
{" ab", "ab", 5, strutil.PosLeft},
{"ab012", "ab012", 5, strutil.PosLeft},
{"ab", "ab", 2, strutil.PosLeft},
}
for _, tt := range tests {
assert.Eq(t, tt.want, strutil.Resize(tt.give, tt.len, tt.pos))
}
want := " title "
assert.Eq(t, want, strutil.Resize("title", 20, strutil.PosMiddle))
}
func TestRepeat(t *testing.T) {
assert.Eq(t, "aaa", strutil.Repeat("a", 3))
assert.Eq(t, "DD", strutil.Repeat("D", 2))
assert.Eq(t, "D", strutil.Repeat("D", 1))
assert.Eq(t, "A", strutil.Repeat("A", 0))
assert.Eq(t, "D", strutil.Repeat("D", -3))
}
func TestRepeatRune(t *testing.T) {
tests := []struct {
want []rune
give rune
times int
}{
{[]rune("bbb"), 'b', 3},
{[]rune("..."), '.', 3},
{[]rune(" "), ' ', 2},
}
for _, tt := range tests {
assert.Eq(t, tt.want, strutil.RepeatRune(tt.give, tt.times))
}
}
func TestRepeatBytes(t *testing.T) {
assert.Eq(t, []byte("aaa"), strutil.RepeatBytes('a', 3))
assert.Eq(t, []byte{}, strutil.RepeatBytes('a', 0))
assert.Eq(t, []byte{}, strutil.RepeatBytes(' ', 0))
assert.Eq(t, []byte{}, strutil.RepeatBytes(' ', -2))
assert.Eq(t, []byte{' '}, strutil.RepeatBytes(' ', 1))
assert.Len(t, strutil.RepeatBytes(' ', 20), 20)
}
func TestPadChars(t *testing.T) {
tests := []struct {
wt []byte
ls []byte
pad byte
pln int
}{
{
[]byte("aaaabc"), []byte("abc"), 'a', 6,
},
{
[]byte("abc"), []byte("abcd"), 'a', 3,
},
}
for _, item := range tests {
assert.Eq(t, item.wt, strutil.PadChars(item.ls, item.pad, item.pln, strutil.PosLeft))
assert.Eq(t, item.wt, strutil.PadBytes(item.ls, item.pad, item.pln, strutil.PosLeft))
assert.Eq(t, item.wt, strutil.PadBytesLeft(item.ls, item.pad, item.pln))
}
tests2 := []struct {
wt []byte
ls []byte
pad byte
pln int
}{
{
[]byte("abcaaa"), []byte("abc"), 'a', 6,
},
{
[]byte("abc"), []byte("abcd"), 'a', 3,
},
}
for _, item := range tests2 {
assert.Eq(t, item.wt, strutil.PadChars(item.ls, item.pad, item.pln, strutil.PosRight))
assert.Eq(t, item.wt, strutil.PadBytes(item.ls, item.pad, item.pln, strutil.PosRight))
assert.Eq(t, item.wt, strutil.PadBytesRight(item.ls, item.pad, item.pln))
}
}
func TestPadRunes(t *testing.T) {
assert.Eq(t, []rune("hi123aaa"), strutil.PadRunesRight([]rune("hi123"), 'a', 8))
assert.Eq(t, []rune("aaahi123"), strutil.PadRunesLeft([]rune("hi123"), 'a', 8))
assert.Eq(t, []rune("hi123aaa"), strutil.PadRunes([]rune("hi123"), 'a', 8, strutil.PosRight))
}
// runtime error: make slice: cap out of range #76
// https://github.com/gookit/goutil/issues/76
func TestIssues_76(t *testing.T) {
// TODO
}