Repository navigation
Expand file tree
/
Copy pathstring_test.go
More file actions
148 lines (136 loc) · 4.41 KB
/
Copy pathstring_test.go
File metadata and controls
148 lines (136 loc) · 4.41 KB
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
package stringx_test
import (
"testing"
"github.com/bycigo/stringx"
)
func TestLen(t *testing.T) {
tests := []struct {
name string
s string
want int
}{
{"empty string", "", 0},
{"ascii string", "hello", 5},
{"chinese characters", "你好世界", 4},
{"mixed ascii and chinese", "hello你好", 7},
{"emoji", "👍🎉", 2},
{"single byte", "a", 1},
{"single rune", "中", 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := stringx.Len(tt.s); got != tt.want {
t.Errorf("stringx.Len(%q) = %v, want %v", tt.s, got, tt.want)
}
})
}
}
func TestReverse(t *testing.T) {
tests := []struct {
name string
s string
want string
}{
{"empty string", "", ""},
{"single character", "a", "a"},
{"ascii string", "hello", "olleh"},
{"chinese characters", "你好世界", "界世好你"},
{"mixed ascii and chinese", "hello你好", "好你olleh"},
{"palindrome", "abcba", "abcba"},
{"emoji", "👍🎉", "🎉👍"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := stringx.Reverse(tt.s); got != tt.want {
t.Errorf("stringx.Reverse(%q) = %v, want %v", tt.s, got, tt.want)
}
})
}
}
func TestPadLeft(t *testing.T) {
tests := []struct {
name string
s string
length int
padding string
want string
}{
{"pad with single char", "hello", 10, "*", "*****hello"},
{"pad with multiple chars", "hello", 10, "ab", "ababahello"},
{"no padding needed", "hello", 5, "*", "hello"},
{"length less than string", "hello", 3, "*", "hello"},
{"empty padding", "hello", 10, "", "hello"},
{"empty string", "", 5, "*", "*****"},
{"unicode padding", "hi", 5, "你", "你你你hi"},
{"unicode string", "你好", 5, "*", "***你好"},
{"mixed unicode", "你好", 6, "ab", "abab你好"},
{"multi-byte padding partial", "hi", 4, "你好", "你好hi"},
{"multi-byte padding partial 2", "hi", 5, "你好世", "你好世hi"},
{"multi-byte padding partial 3", "hi", 6, "你好世", "你好世你hi"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := stringx.PadLeft(tt.s, tt.length, tt.padding); got != tt.want {
t.Errorf("stringx.PadLeft(%q, %v, %q) = %v, want %v", tt.s, tt.length, tt.padding, got, tt.want)
}
})
}
}
func TestPadRight(t *testing.T) {
tests := []struct {
name string
s string
length int
padding string
want string
}{
{"pad with single char", "hello", 10, "*", "hello*****"},
{"pad with multiple chars", "hello", 10, "ab", "helloababa"},
{"no padding needed", "hello", 5, "*", "hello"},
{"length less than string", "hello", 3, "*", "hello"},
{"empty padding", "hello", 10, "", "hello"},
{"empty string", "", 5, "*", "*****"},
{"unicode padding", "hi", 5, "你", "hi你你你"},
{"unicode string", "你好", 5, "*", "你好***"},
{"mixed unicode", "你好", 6, "ab", "你好abab"},
{"multi-byte padding partial", "hi", 4, "你好", "hi你好"},
{"multi-byte padding partial 2", "hi", 5, "你好世", "hi你好世"},
{"multi-byte padding partial 3", "hi", 6, "你好世", "hi你好世你"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := stringx.PadRight(tt.s, tt.length, tt.padding); got != tt.want {
t.Errorf("stringx.PadRight(%q, %v, %q) = %v, want %v", tt.s, tt.length, tt.padding, got, tt.want)
}
})
}
}
func TestPadBoth(t *testing.T) {
tests := []struct {
name string
s string
length int
padding string
want string
}{
{"pad with single char even", "hello", 11, "*", "***hello***"},
{"pad with single char odd", "hello", 10, "*", "**hello***"},
{"pad with multiple chars", "hello", 11, "ab", "abahelloaba"},
{"no padding needed", "hello", 5, "*", "hello"},
{"length less than string", "hello", 3, "*", "hello"},
{"empty padding", "hello", 10, "", "hello"},
{"empty string", "", 6, "*", "******"},
{"unicode padding", "hi", 6, "你", "你你hi你你"},
{"unicode string", "你好", 6, "*", "**你好**"},
{"mixed unicode", "你好", 8, "ab", "aba你好aba"},
{"multi-byte padding partial both", "hi", 6, "你好", "你好hi你好"},
{"multi-byte padding partial left", "hi", 5, "你好世", "你hi你好"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := stringx.PadBoth(tt.s, tt.length, tt.padding); got != tt.want {
t.Errorf("stringx.PadBoth(%q, %v, %q) = %v, want %v", tt.s, tt.length, tt.padding, got, tt.want)
}
})
}
}