github.com/lingyao2333/mo-zero@v1.4.1/core/stringx/strings_test.go (about) 1 package stringx 2 3 import ( 4 "path" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestNotEmpty(t *testing.T) { 11 cases := []struct { 12 args []string 13 expect bool 14 }{ 15 { 16 args: []string{"a", "b", "c"}, 17 expect: true, 18 }, 19 { 20 args: []string{"a", "", "c"}, 21 expect: false, 22 }, 23 { 24 args: []string{"a"}, 25 expect: true, 26 }, 27 { 28 args: []string{""}, 29 expect: false, 30 }, 31 { 32 args: []string{}, 33 expect: true, 34 }, 35 } 36 37 for _, each := range cases { 38 t.Run(path.Join(each.args...), func(t *testing.T) { 39 assert.Equal(t, each.expect, NotEmpty(each.args...)) 40 }) 41 } 42 } 43 44 func TestContainsString(t *testing.T) { 45 cases := []struct { 46 slice []string 47 value string 48 expect bool 49 }{ 50 {[]string{"1"}, "1", true}, 51 {[]string{"1"}, "2", false}, 52 {[]string{"1", "2"}, "1", true}, 53 {[]string{"1", "2"}, "3", false}, 54 {nil, "3", false}, 55 {nil, "", false}, 56 } 57 58 for _, each := range cases { 59 t.Run(path.Join(each.slice...), func(t *testing.T) { 60 actual := Contains(each.slice, each.value) 61 assert.Equal(t, each.expect, actual) 62 }) 63 } 64 } 65 66 func TestFilter(t *testing.T) { 67 cases := []struct { 68 input string 69 ignores []rune 70 expect string 71 }{ 72 {``, nil, ``}, 73 {`abcd`, nil, `abcd`}, 74 {`ab,cd,ef`, []rune{','}, `abcdef`}, 75 {`ab, cd,ef`, []rune{',', ' '}, `abcdef`}, 76 {`ab, cd, ef`, []rune{',', ' '}, `abcdef`}, 77 {`ab, cd, ef, `, []rune{',', ' '}, `abcdef`}, 78 } 79 80 for _, each := range cases { 81 t.Run(each.input, func(t *testing.T) { 82 actual := Filter(each.input, func(r rune) bool { 83 for _, x := range each.ignores { 84 if x == r { 85 return true 86 } 87 } 88 return false 89 }) 90 assert.Equal(t, each.expect, actual) 91 }) 92 } 93 } 94 95 func TestFirstN(t *testing.T) { 96 tests := []struct { 97 name string 98 input string 99 n int 100 ellipsis string 101 expect string 102 }{ 103 { 104 name: "english string", 105 input: "anything that we use", 106 n: 8, 107 expect: "anything", 108 }, 109 { 110 name: "english string with ellipsis", 111 input: "anything that we use", 112 n: 8, 113 ellipsis: "...", 114 expect: "anything...", 115 }, 116 { 117 name: "english string more", 118 input: "anything that we use", 119 n: 80, 120 expect: "anything that we use", 121 }, 122 { 123 name: "chinese string", 124 input: "我是中国人", 125 n: 2, 126 expect: "我是", 127 }, 128 { 129 name: "chinese string with ellipsis", 130 input: "我是中国人", 131 n: 2, 132 ellipsis: "...", 133 expect: "我是...", 134 }, 135 { 136 name: "chinese string", 137 input: "我是中国人", 138 n: 10, 139 expect: "我是中国人", 140 }, 141 } 142 143 for _, test := range tests { 144 t.Run(test.name, func(t *testing.T) { 145 assert.Equal(t, test.expect, FirstN(test.input, test.n, test.ellipsis)) 146 }) 147 } 148 } 149 150 func TestRemove(t *testing.T) { 151 cases := []struct { 152 input []string 153 remove []string 154 expect []string 155 }{ 156 { 157 input: []string{"a", "b", "a", "c"}, 158 remove: []string{"a", "b"}, 159 expect: []string{"c"}, 160 }, 161 { 162 input: []string{"b", "c"}, 163 remove: []string{"a"}, 164 expect: []string{"b", "c"}, 165 }, 166 { 167 input: []string{"b", "a", "c"}, 168 remove: []string{"a"}, 169 expect: []string{"b", "c"}, 170 }, 171 { 172 input: []string{}, 173 remove: []string{"a"}, 174 expect: []string{}, 175 }, 176 } 177 178 for _, each := range cases { 179 t.Run(path.Join(each.input...), func(t *testing.T) { 180 assert.ElementsMatch(t, each.expect, Remove(each.input, each.remove...)) 181 }) 182 } 183 } 184 185 func TestReverse(t *testing.T) { 186 cases := []struct { 187 input string 188 expect string 189 }{ 190 { 191 input: "abcd", 192 expect: "dcba", 193 }, 194 { 195 input: "", 196 expect: "", 197 }, 198 { 199 input: "我爱中国", 200 expect: "国中爱我", 201 }, 202 } 203 204 for _, each := range cases { 205 t.Run(each.input, func(t *testing.T) { 206 assert.Equal(t, each.expect, Reverse(each.input)) 207 }) 208 } 209 } 210 211 func TestSubstr(t *testing.T) { 212 cases := []struct { 213 input string 214 start int 215 stop int 216 err error 217 expect string 218 }{ 219 { 220 input: "abcdefg", 221 start: 1, 222 stop: 4, 223 expect: "bcd", 224 }, 225 { 226 input: "我爱中国3000遍,even more", 227 start: 1, 228 stop: 9, 229 expect: "爱中国3000遍", 230 }, 231 { 232 input: "abcdefg", 233 start: -1, 234 stop: 4, 235 err: ErrInvalidStartPosition, 236 expect: "", 237 }, 238 { 239 input: "abcdefg", 240 start: 100, 241 stop: 4, 242 err: ErrInvalidStartPosition, 243 expect: "", 244 }, 245 { 246 input: "abcdefg", 247 start: 1, 248 stop: -1, 249 err: ErrInvalidStopPosition, 250 expect: "", 251 }, 252 { 253 input: "abcdefg", 254 start: 1, 255 stop: 100, 256 err: ErrInvalidStopPosition, 257 expect: "", 258 }, 259 } 260 261 for _, each := range cases { 262 t.Run(each.input, func(t *testing.T) { 263 val, err := Substr(each.input, each.start, each.stop) 264 assert.Equal(t, each.err, err) 265 if err == nil { 266 assert.Equal(t, each.expect, val) 267 } 268 }) 269 } 270 } 271 272 func TestTakeOne(t *testing.T) { 273 cases := []struct { 274 valid string 275 or string 276 expect string 277 }{ 278 {"", "", ""}, 279 {"", "1", "1"}, 280 {"1", "", "1"}, 281 {"1", "2", "1"}, 282 } 283 284 for _, each := range cases { 285 t.Run(each.valid, func(t *testing.T) { 286 actual := TakeOne(each.valid, each.or) 287 assert.Equal(t, each.expect, actual) 288 }) 289 } 290 } 291 292 func TestTakeWithPriority(t *testing.T) { 293 tests := []struct { 294 fns []func() string 295 expect string 296 }{ 297 { 298 fns: []func() string{ 299 func() string { 300 return "first" 301 }, 302 func() string { 303 return "second" 304 }, 305 func() string { 306 return "third" 307 }, 308 }, 309 expect: "first", 310 }, 311 { 312 fns: []func() string{ 313 func() string { 314 return "" 315 }, 316 func() string { 317 return "second" 318 }, 319 func() string { 320 return "third" 321 }, 322 }, 323 expect: "second", 324 }, 325 { 326 fns: []func() string{ 327 func() string { 328 return "" 329 }, 330 func() string { 331 return "" 332 }, 333 func() string { 334 return "third" 335 }, 336 }, 337 expect: "third", 338 }, 339 { 340 fns: []func() string{ 341 func() string { 342 return "" 343 }, 344 func() string { 345 return "" 346 }, 347 func() string { 348 return "" 349 }, 350 }, 351 expect: "", 352 }, 353 } 354 355 for _, test := range tests { 356 t.Run(RandId(), func(t *testing.T) { 357 val := TakeWithPriority(test.fns...) 358 assert.Equal(t, test.expect, val) 359 }) 360 } 361 } 362 363 func TestUnion(t *testing.T) { 364 first := []string{ 365 "one", 366 "two", 367 "three", 368 } 369 second := []string{ 370 "zero", 371 "two", 372 "three", 373 "four", 374 } 375 union := Union(first, second) 376 contains := func(v string) bool { 377 for _, each := range union { 378 if v == each { 379 return true 380 } 381 } 382 383 return false 384 } 385 assert.Equal(t, 5, len(union)) 386 assert.True(t, contains("zero")) 387 assert.True(t, contains("one")) 388 assert.True(t, contains("two")) 389 assert.True(t, contains("three")) 390 assert.True(t, contains("four")) 391 }