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