bitbucket.org/ai69/amoy@v0.2.3/string_test.go (about) 1 package amoy 2 3 import "testing" 4 5 func TestSubstrAfterFirst(t *testing.T) { 6 tests := []struct { 7 name string 8 s string 9 sub string 10 want string 11 }{ 12 {"all empty", EmptyStr, EmptyStr, EmptyStr}, 13 {"s is empty", EmptyStr, "Hello", EmptyStr}, 14 {"sub is empty", "Hello", EmptyStr, EmptyStr}, 15 {"sub is not found", "Hello", "W", EmptyStr}, 16 {"s and sub is the same", "Hello", "Hello", EmptyStr}, 17 {"s contains sub", "ABCDE", "BC", "DE"}, 18 {"sub contains s", "BC", "ABCDE", EmptyStr}, 19 {"sub is prefix", "ABCDE", "AB", "CDE"}, 20 {"sub is suffix", "ABCDE", "DE", ""}, 21 {"multiple sub", "ABCBCBCBCBCDE", "BC", "BCBCBCBCDE"}, 22 {"cjk", "我爱你爱你哦", "爱你", "爱你哦"}, 23 } 24 for _, tt := range tests { 25 t.Run(tt.name, func(t *testing.T) { 26 if got := SubstrAfterFirst(tt.s, tt.sub); got != tt.want { 27 t.Errorf("SubstrAfterFirst(%q, %q) = %q, want %q", tt.s, tt.sub, got, tt.want) 28 } 29 }) 30 } 31 } 32 33 func TestSubstrAfterLast(t *testing.T) { 34 tests := []struct { 35 name string 36 s string 37 sub string 38 want string 39 }{ 40 {"all empty", EmptyStr, EmptyStr, EmptyStr}, 41 {"s is empty", EmptyStr, "Hello", EmptyStr}, 42 {"sub is empty", "Hello", EmptyStr, EmptyStr}, 43 {"sub is not found", "Hello", "W", EmptyStr}, 44 {"s and sub is the same", "Hello", "Hello", EmptyStr}, 45 {"s contains sub", "ABCDE", "BC", "DE"}, 46 {"sub contains s", "BC", "ABCDE", EmptyStr}, 47 {"sub is prefix", "ABCDE", "AB", "CDE"}, 48 {"sub is suffix", "ABCDE", "DE", ""}, 49 {"multiple sub", "ABCBCBCBCBCDE", "BC", "DE"}, 50 {"cjk", "我爱你爱你哦", "爱你", "哦"}, 51 } 52 for _, tt := range tests { 53 t.Run(tt.name, func(t *testing.T) { 54 if got := SubstrAfterLast(tt.s, tt.sub); got != tt.want { 55 t.Errorf("SubstrAfterLast(%q, %q) = %q, want %q", tt.s, tt.sub, got, tt.want) 56 } 57 }) 58 } 59 } 60 61 func TestSubstrBeforeFirst(t *testing.T) { 62 tests := []struct { 63 name string 64 s string 65 sub string 66 want string 67 }{ 68 {"all empty", EmptyStr, EmptyStr, EmptyStr}, 69 {"s is empty", EmptyStr, "Hello", EmptyStr}, 70 {"sub is empty", "Hello", EmptyStr, EmptyStr}, 71 {"sub is not found", "Hello", "W", EmptyStr}, 72 {"s and sub is the same", "Hello", "Hello", EmptyStr}, 73 {"s contains sub", "ABCDE", "BC", "A"}, 74 {"sub contains s", "BC", "ABCDE", EmptyStr}, 75 {"sub is prefix", "ABCDE", "AB", ""}, 76 {"sub is suffix", "ABCDE", "DE", "ABC"}, 77 {"multiple sub", "ABCBCBCBCBCDE", "BC", "A"}, 78 {"cjk", "我爱你爱你哦", "爱你", "我"}, 79 } 80 for _, tt := range tests { 81 t.Run(tt.name, func(t *testing.T) { 82 if got := SubstrBeforeFirst(tt.s, tt.sub); got != tt.want { 83 t.Errorf("SubstrBeforeFirst(%q, %q) = %q, want %q", tt.s, tt.sub, got, tt.want) 84 } 85 }) 86 } 87 } 88 89 func TestSubstrBeforeLast(t *testing.T) { 90 tests := []struct { 91 name string 92 s string 93 sub string 94 want string 95 }{ 96 {"all empty", EmptyStr, EmptyStr, EmptyStr}, 97 {"s is empty", EmptyStr, "Hello", EmptyStr}, 98 {"sub is empty", "Hello", EmptyStr, EmptyStr}, 99 {"sub is not found", "Hello", "W", EmptyStr}, 100 {"s and sub is the same", "Hello", "Hello", EmptyStr}, 101 {"s contains sub", "ABCDE", "BC", "A"}, 102 {"sub contains s", "BC", "ABCDE", EmptyStr}, 103 {"sub is prefix", "ABCDE", "AB", ""}, 104 {"sub is suffix", "ABCDE", "DE", "ABC"}, 105 {"multiple sub", "ABCBCBCBCBCDE", "BC", "ABCBCBCBC"}, 106 {"cjk", "我爱你爱你哦", "爱你", "我爱你"}, 107 } 108 for _, tt := range tests { 109 t.Run(tt.name, func(t *testing.T) { 110 if got := SubstrBeforeLast(tt.s, tt.sub); got != tt.want { 111 t.Errorf("SubstrBeforeLast(%q, %q) = %q, want %q", tt.s, tt.sub, got, tt.want) 112 } 113 }) 114 } 115 } 116 117 func TestReverseStr(t *testing.T) { 118 tests := []struct { 119 name string 120 s string 121 want string 122 }{ 123 {"empty", EmptyStr, EmptyStr}, 124 {"single", "A", "A"}, 125 {"single CJK", "悟", "悟"}, 126 {"normal", "ABC", "CBA"}, 127 {"CJK", "我爱你", "你爱我"}, 128 {"emoji", "👤🌋", "🌋👤"}, 129 } 130 for _, tt := range tests { 131 t.Run(tt.name, func(t *testing.T) { 132 if got := ReverseStr(tt.s); got != tt.want { 133 t.Errorf("ReverseStr() = %q, want %q", got, tt.want) 134 } 135 }) 136 } 137 } 138 139 func TestTruncateStr(t *testing.T) { 140 tests := []struct { 141 name string 142 s string 143 limit int 144 want string 145 }{ 146 {"uncut", "hello", 6, "hello"}, 147 {"exact", "hello", 5, "hello"}, 148 {"cut", "hello", 3, "hel"}, 149 {"zero", "hello", 0, EmptyStr}, 150 {"negative", "hello", -2, EmptyStr}, 151 {"empty", EmptyStr, 1, EmptyStr}, 152 {"chinese", "實屬要著,積之以久應有相當成就。", 16, "實屬要著,積之以久應有相當成就。"}, 153 {"chinese1", "實屬要著,積之以久應有相當成就。", 15, "實屬要著,積之以久應有相當成就"}, 154 {"chinese2", "實屬要著,積之以久應有相當成就。", 14, "實屬要著,積之以久應有相當成"}, 155 {"chinese3", "實屬要著,積之以久應有相當成就。", 13, "實屬要著,積之以久應有相當"}, 156 {"emoji", "🍔🥪🌮🌯🥠🥞🍪🥮", 8, "🍔🥪🌮🌯🥠🥞🍪🥮"}, 157 {"emoji1", "🍔🥪🌮🌯🥠🥞🍪🥮", 7, "🍔🥪🌮🌯🥠🥞🍪"}, 158 {"emoji2", "🍔🥪🌮🌯🥠🥞🍪🥮", 4, "🍔🥪🌮🌯"}, 159 } 160 for _, tt := range tests { 161 t.Run(tt.name, func(t *testing.T) { 162 if got := TruncateStr(tt.s, tt.limit); got != tt.want { 163 t.Errorf("TruncateStr(%q, %d) = %v, want %v", tt.s, tt.limit, got, tt.want) 164 } 165 }) 166 } 167 } 168 169 func BenchmarkContainsJapanese(b *testing.B) { 170 cases := []string{ 171 `c!ty'super`, 172 `𝐿𝑖𝑡𝑡𝑙𝑒𝐷𝑖𝑟𝑡𝑦𝑆𝑒𝑐𝑟𝑒𝑡`, 173 `柯桥街道鉴湖景园`, 174 `這個年過完會肥死`, 175 `ホノカアボーイ`, 176 `へんたいがな`, 177 `妖艶さと卑猥さの探究者`, 178 `抜群の美女`, 179 `내 소중이 이쁘지`, 180 `เลิฟวิลล่าโฮเทล`, 181 } 182 b.ResetTimer() 183 for i := 0; i < b.N; i++ { 184 for _, c := range cases { 185 _ = ContainsJapanese(c) 186 } 187 } 188 } 189 190 func TestContainsChinese(t *testing.T) { 191 tests := []struct { 192 s string 193 want bool 194 }{ 195 {`c!ty'super`, false}, 196 {`𝐿𝑖𝑡𝑡𝑙𝑒𝐷𝑖𝑟𝑡𝑦𝑆𝑒𝑐𝑟𝑒𝑡`, false}, 197 {`,,,。。。`, false}, 198 {`柯桥街道鉴湖景园`, true}, 199 {`這個年過完會肥死`, true}, 200 {`ホノカアボーイ`, false}, 201 {`へんたいがな`, false}, 202 {`妖艶さと卑猥さの探究者`, true}, 203 {`抜群の美女`, true}, 204 {`내 소중이 이쁘지`, false}, 205 {`เลิฟวิลล่าโฮเทล`, false}, 206 } 207 for _, tt := range tests { 208 t.Run(tt.s, func(t *testing.T) { 209 if got := ContainsChinese(tt.s); got != tt.want { 210 t.Errorf("ContainsChinese(%s) = %v, want %v", tt.s, got, tt.want) 211 } 212 }) 213 } 214 } 215 216 func TestContainsKorean(t *testing.T) { 217 tests := []struct { 218 s string 219 want bool 220 }{ 221 {`c!ty'super`, false}, 222 {`𝐿𝑖𝑡𝑡𝑙𝑒𝐷𝑖𝑟𝑡𝑦𝑆𝑒𝑐𝑟𝑒𝑡`, false}, 223 {`,,,。。。`, false}, 224 {`柯桥街道鉴湖景园`, false}, 225 {`這個年過完會肥死`, false}, 226 {`ホノカアボーイ`, false}, 227 {`へんたいがな`, false}, 228 {`妖艶さと卑猥さの探究者`, false}, 229 {`抜群の美女`, false}, 230 {`내 소중이 이쁘지`, true}, 231 {`เลิฟวิลล่าโฮเทล`, false}, 232 } 233 for _, tt := range tests { 234 t.Run(tt.s, func(t *testing.T) { 235 if got := ContainsKorean(tt.s); got != tt.want { 236 t.Errorf("ContainsKorean(%s) = %v, want %v", tt.s, got, tt.want) 237 } 238 }) 239 } 240 } 241 242 func TestContainsJapanese(t *testing.T) { 243 tests := []struct { 244 s string 245 want bool 246 }{ 247 {`c!ty'super`, false}, 248 {`𝐿𝑖𝑡𝑡𝑙𝑒𝐷𝑖𝑟𝑡𝑦𝑆𝑒𝑐𝑟𝑒𝑡`, false}, 249 {`,,,。。。`, false}, 250 {`柯桥街道鉴湖景园`, true}, 251 {`這個年過完會肥死`, true}, 252 {`ホノカアボーイ`, true}, 253 {`へんたいがな`, true}, 254 {`妖艶さと卑猥さの探究者`, true}, 255 {`抜群の美女`, true}, 256 {`내 소중이 이쁘지`, false}, 257 {`เลิฟวิลล่าโฮเทล`, false}, 258 } 259 for _, tt := range tests { 260 t.Run(tt.s, func(t *testing.T) { 261 if got := ContainsJapanese(tt.s); got != tt.want { 262 t.Errorf("ContainsJapanese(%s) = %v, want %v", tt.s, got, tt.want) 263 } 264 }) 265 } 266 } 267 268 func TestTrimLeftSpace(t *testing.T) { 269 tests := []struct { 270 name string 271 s string 272 want string 273 }{ 274 {"empty", "", ""}, 275 {"all spaces", " \t \n ", ""}, 276 {"left spaces", " \t \n 123", "123"}, 277 {"right spaces", "456 \t \n ", "456 \t \n "}, 278 {"left and right spaces", " \t \n 7 8 9 \t \n ", "7 8 9 \t \n "}, 279 {"no spaces", "abc", "abc"}, 280 } 281 for _, tt := range tests { 282 t.Run(tt.name, func(t *testing.T) { 283 if got := TrimLeftSpace(tt.s); got != tt.want { 284 t.Errorf("TrimLeftSpace(%q) = %q, want %q", tt.s, got, tt.want) 285 } 286 }) 287 } 288 } 289 290 func TestTrimRightSpace(t *testing.T) { 291 tests := []struct { 292 name string 293 s string 294 want string 295 }{ 296 {"empty", "", ""}, 297 {"all spaces", " \t \n ", ""}, 298 {"left spaces", " \t \n 123", " \t \n 123"}, 299 {"right spaces", "456 \t \n ", "456"}, 300 {"left and right spaces", " \t \n 7 8 9 \t \n ", " \t \n 7 8 9"}, 301 {"no spaces", "abc", "abc"}, 302 } 303 for _, tt := range tests { 304 t.Run(tt.name, func(t *testing.T) { 305 if got := TrimRightSpace(tt.s); got != tt.want { 306 t.Errorf("TrimRightSpace(%q) = %q, want %q", tt.s, got, tt.want) 307 } 308 }) 309 } 310 } 311 312 func TestTrimInnerSpace(t *testing.T) { 313 tests := []struct { 314 name string 315 s string 316 want string 317 }{ 318 {"empty", "", ""}, 319 {"all spaces", " \t \n ", ""}, 320 {"left spaces", " \t \n 123", "123"}, 321 {"right spaces", "456 \t \n ", "456"}, 322 {"left and right spaces", " \t \n 7 8 9 \t \n ", "789"}, 323 {"no spaces", "abc", "abc"}, 324 } 325 for _, tt := range tests { 326 t.Run(tt.name, func(t *testing.T) { 327 if got := TrimInnerSpace(tt.s); got != tt.want { 328 t.Errorf("TrimInnerSpace(%q) = %q, want %q", tt.s, got, tt.want) 329 } 330 }) 331 } 332 }