github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/lang/stringx/stringx_test.go (about) 1 // Copyright 2021 ByteDance Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package stringx 16 17 import ( 18 "sort" 19 "strings" 20 "testing" 21 "unicode/utf8" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestPad(t *testing.T) { 27 type testData struct { 28 input string 29 padChar rune 30 size int 31 leftExpected string 32 leftExpectedSpace string 33 34 rightExpected string 35 rightExpectedSpace string 36 37 centerExpected string 38 centerExpectedSpace string 39 } 40 41 testCases := []testData{ 42 { 43 "", '-', 4, 44 "----", " ", 45 "----", " ", 46 "----", " ", 47 }, 48 { 49 "abc", '-', 0, 50 "abc", "abc", 51 "abc", "abc", 52 "abc", "abc", 53 }, 54 { 55 "abc", '-', 2, 56 "abc", "abc", 57 "abc", "abc", 58 "abc", "abc", 59 }, 60 { 61 "abc", '-', 4, 62 "-abc", " abc", 63 "abc-", "abc ", 64 "abc-", "abc ", 65 }, 66 { 67 "abc", '-', 5, 68 "--abc", " abc", 69 "abc--", "abc ", 70 "-abc-", " abc ", 71 }, 72 { 73 "abc", '-', 6, 74 "---abc", " abc", 75 "abc---", "abc ", 76 "-abc--", " abc ", 77 }, 78 { 79 "abc", '-', 7, 80 "----abc", " abc", 81 "abc----", "abc ", 82 "--abc--", " abc ", 83 }, 84 85 { 86 "abcd", '-', 7, 87 "---abcd", " abcd", 88 "abcd---", "abcd ", 89 "-abcd--", " abcd ", 90 }, 91 } 92 93 is := assert.New(t) 94 for _, testCase := range testCases { 95 is.Equal(testCase.leftExpected, PadLeftChar(testCase.input, testCase.size, testCase.padChar)) 96 is.Equal(testCase.leftExpectedSpace, PadLeftSpace(testCase.input, testCase.size)) 97 98 is.Equal(testCase.rightExpected, PadRightChar(testCase.input, testCase.size, testCase.padChar)) 99 is.Equal(testCase.rightExpectedSpace, PadRightSpace(testCase.input, testCase.size)) 100 101 is.Equal(testCase.centerExpected, PadCenterChar(testCase.input, testCase.size, testCase.padChar)) 102 is.Equal(testCase.centerExpectedSpace, PadCenterSpace(testCase.input, testCase.size)) 103 } 104 } 105 106 func TestRemove(t *testing.T) { 107 is := assert.New(t) 108 is.Equal("", RemoveChar("", 'h')) 109 is.Equal("z英文un排", RemoveChar("zh英文hunh排", 'h')) 110 is.Equal("zh英hun排", RemoveChar("zh英文hun文排", '文')) 111 112 is.Equal("", RemoveString("", "文hun")) 113 is.Equal("zh英文hun排", RemoveString("zh英文hun排", "")) 114 is.Equal("zh英排", RemoveString("zh英文hun排", "文hun")) 115 is.Equal("zh英文hun排", RemoveString("zh英文hun排", "")) 116 } 117 118 func TestRepeat(t *testing.T) { 119 is := assert.New(t) 120 is.Equal("", RepeatChar('-', 0)) 121 is.Equal("----", RepeatChar('-', 4)) 122 is.Equal(" ", RepeatChar(' ', 3)) 123 } 124 125 func TestRotate(t *testing.T) { 126 is := assert.New(t) 127 128 is.Equal("", Rotate("", 2)) 129 130 is.Equal("abc", Rotate("abc", 0)) 131 is.Equal("abc", Rotate("abc", 3)) 132 is.Equal("abc", Rotate("abc", 6)) 133 134 is.Equal("cab", Rotate("abc", 1)) 135 is.Equal("bca", Rotate("abc", -1)) 136 } 137 138 func TestReverse(t *testing.T) { 139 tests := []struct { 140 input string 141 expected string 142 }{ 143 {"", ""}, 144 {"abc", "cba"}, 145 {"a", "a"}, 146 {"çınar", "ranıç"}, 147 {" yağmur", "rumğay "}, 148 {"επαγγελματίες", "ςείταμλεγγαπε"}, 149 } 150 for _, test := range tests { 151 output := MustReverse(test.input) 152 assert.Equalf(t, test.expected, output, "Test case %s is not successful\n", test.input) 153 } 154 155 assert.Equal(t, MustReverse(""), "") 156 assert.Equal(t, MustReverse("X"), "X") 157 assert.Equal(t, MustReverse("\u0301b"), "b\u0301") 158 assert.Equal(t, MustReverse("😎⚽"), "⚽😎") 159 assert.Equal(t, MustReverse("Les Mise\u0301rables"), "selbar\u0301esiM seL") 160 assert.Equal(t, MustReverse("ab\u0301cde"), "edc\u0301ba") 161 assert.Equal(t, MustReverse("The quick bròwn 狐 jumped over the lazy 犬"), "犬 yzal eht revo depmuj 狐 nwòrb kciuq ehT") 162 _, err := Reverse(string([]byte{128, 128, 128, 128, 0})) 163 assert.Equal(t, ErrDecodeRune, err) 164 } 165 166 func TestSub(t *testing.T) { 167 type testData struct { 168 input string 169 start int 170 end int 171 expected string 172 } 173 174 newTestCase := func(intput string, start, end int, expected string) testData { 175 return testData{ 176 input: intput, 177 start: start, 178 end: end, 179 expected: expected, 180 } 181 } 182 183 testCases := []testData{ 184 newTestCase("", 0, 100, ""), 185 newTestCase("facgbheidjk", 3, 9, "gbheid"), 186 newTestCase("facgbheidjk", -50, 100, "facgbheidjk"), 187 newTestCase("facgbheidjk", -3, utf8.RuneCountInString("facgbheidjk"), "djk"), 188 newTestCase("facgbheidjk", -3, -1, "dj"), 189 newTestCase("zh英文hun排", 2, 5, "英文h"), 190 newTestCase("zh英文hun排", 2, -1, "英文hun"), 191 newTestCase("zh英文hun排", -100, -1, "zh英文hun"), 192 newTestCase("zh英文hun排", -100, -90, ""), 193 newTestCase("zh英文hun排", -10, -90, ""), 194 } 195 196 is := assert.New(t) 197 for _, testCase := range testCases { 198 is.Equal(testCase.expected, Sub(testCase.input, testCase.start, testCase.end)) 199 } 200 } 201 202 func TestContainsAnySubstrings(t *testing.T) { 203 is := assert.New(t) 204 is.True(ContainsAnySubstrings("abcdefg", []string{"a", "b"})) 205 is.True(ContainsAnySubstrings("abcdefg", []string{"a", "z"})) 206 is.False(ContainsAnySubstrings("abcdefg", []string{"ac", "z"})) 207 is.False(ContainsAnySubstrings("abcdefg", []string{"x", "z"})) 208 } 209 210 func TestShuffle(t *testing.T) { 211 is := assert.New(t) 212 213 shuffleAndSort := func(str string) string { 214 s := Shuffle(str) 215 slice := sort.StringSlice(strings.Split(s, "")) 216 slice.Sort() 217 return strings.Join(slice, "") 218 } 219 220 strMap := map[string]string{ 221 "": "", 222 "facgbheidjk": "abcdefghijk", 223 "尝试中文": "中尝文试", 224 "zh英文hun排": "hhnuz排文英", 225 } 226 for input, expected := range strMap { 227 actual := shuffleAndSort(input) 228 is.Equal(expected, actual) 229 } 230 }