github.com/kaydxh/golang@v0.0.131/go/strings/strings_test.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package strings_test 23 24 import ( 25 "fmt" 26 "strings" 27 "testing" 28 29 strconv_ "github.com/kaydxh/golang/go/strconv" 30 strings_ "github.com/kaydxh/golang/go/strings" 31 "github.com/stretchr/testify/assert" 32 ) 33 34 func TestReplace(t *testing.T) { 35 testCases := []struct { 36 s string 37 old string 38 news []interface{} 39 n int 40 expected string 41 }{ 42 { 43 s: "task_id in (?)", 44 old: "?", 45 news: []interface{}{"a"}, 46 n: 1, 47 expected: `task_id in ("a")`, 48 }, 49 { 50 s: "task_id in (?, ?, ?, ?, ?)", 51 old: "?", 52 news: []interface{}{"a", "a", "a", "a"}, 53 n: 5, 54 expected: `task_id in ("a", "a", "a", "a", "a")`, 55 }, 56 } 57 58 for _, testCase := range testCases { 59 t.Run(testCase.s, func(t *testing.T) { 60 newStr := strings_.Replace(testCase.s, testCase.old, testCase.news, true, testCase.n) 61 t.Logf("newStr: %v", newStr) 62 assert.Equal(t, testCase.expected, newStr) 63 64 }) 65 66 } 67 } 68 69 func TestReplaceAll(t *testing.T) { 70 testCases := []struct { 71 s string 72 old string 73 news []interface{} 74 expected string 75 }{ 76 { 77 s: "task_id in (?)", 78 old: "?", 79 news: []interface{}{"a"}, 80 expected: `task_id in ("a")`, 81 }, 82 { 83 s: "task_id in (?, ?, ?, ?, ?)", 84 old: "?", 85 news: []interface{}{"a", "a", "a", "a"}, 86 expected: `task_id in ("a", "a", "a", "a", "a")`, 87 }, 88 } 89 90 for _, testCase := range testCases { 91 t.Run(testCase.s, func(t *testing.T) { 92 newStr := strings_.ReplaceAll(testCase.s, testCase.old, testCase.news, true) 93 t.Logf("newStr: %v", newStr) 94 assert.Equal(t, testCase.expected, newStr) 95 96 }) 97 98 } 99 } 100 101 func TestSplit(t *testing.T) { 102 testCases := []struct { 103 s string 104 sep string 105 }{ 106 { 107 s: "1", 108 sep: ",", 109 }, 110 { 111 s: "a,b,c", 112 sep: ",", 113 }, 114 { 115 s: "a,b,c,,,,,", 116 sep: ",", 117 }, 118 } 119 120 for _, testCase := range testCases { 121 t.Run(testCase.s, func(t *testing.T) { 122 ss := strings_.SplitOmitEmpty(testCase.s, testCase.sep) 123 t.Logf("ss: %v. len(ss): %v", ss, len(ss)) 124 125 }) 126 127 } 128 129 } 130 131 func TestSplitToNums(t *testing.T) { 132 testCases := []struct { 133 s string 134 sep string 135 }{ 136 { 137 s: "1,2,3,4", 138 sep: ",", 139 }, 140 { 141 s: "1,2,3,4,", 142 sep: ",", 143 }, 144 { 145 s: "1,2,3,4,a", 146 sep: ",", 147 }, 148 } 149 150 for _, testCase := range testCases { 151 t.Run(testCase.s, func(t *testing.T) { 152 nums, err := strings_.SplitToNums(testCase.s, testCase.sep, strconv_.ToInt64) 153 if err != nil { 154 t.Errorf("failed to split to nums, err: %v", err) 155 return 156 } 157 t.Logf("got nums %v", nums) 158 159 }) 160 161 } 162 163 } 164 165 func TestGetStringOrFallback(t *testing.T) { 166 type args struct { 167 value string 168 defaultValue string 169 } 170 tests := []struct { 171 name string 172 args args 173 want string 174 }{ 175 // TODO: Add test cases. 176 { 177 name: "test1", 178 args: args{ 179 value: "abc", 180 defaultValue: "default", 181 }, 182 want: "abc", 183 }, 184 { 185 name: "test2", 186 args: args{ 187 value: "", 188 defaultValue: "default", 189 }, 190 want: "default", 191 }, 192 } 193 for _, tt := range tests { 194 t.Run(tt.name, func(t *testing.T) { 195 if got := strings_.GetStringOrFallback(tt.args.value, tt.args.defaultValue); got != tt.want { 196 t.Errorf("GetStringOrFallback() = %v, want %v", got, tt.want) 197 } 198 }) 199 } 200 } 201 202 func TestLastIndex(t *testing.T) { 203 tests := []struct { 204 value string 205 tag string 206 }{ 207 // TODO: Add test cases. 208 { 209 value: "abc_fashion_0", 210 tag: "_fashion", 211 }, 212 } 213 for i, tt := range tests { 214 t.Run(fmt.Sprintf("%v", i), func(t *testing.T) { 215 index := strings.LastIndex(tt.value, tt.tag) 216 if index != -1 { 217 t.Logf("result: %v", tt.value[:index]) 218 } else { 219 t.Logf("result: %v", tt.value) 220 } 221 }) 222 } 223 }