github.com/viant/toolbox@v0.34.5/text_test.go (about) 1 package toolbox 2 3 import ( 4 "bytes" 5 "fmt" 6 "github.com/stretchr/testify/assert" 7 "io" 8 "strings" 9 "testing" 10 ) 11 12 func TestIsASCIIText(t *testing.T) { 13 14 var useCases = []struct { 15 Description string 16 Candidate string 17 Expected bool 18 }{ 19 { 20 Description: "basic text", 21 Candidate: `abc`, 22 Expected: true, 23 }, 24 { 25 Description: "JSON object like text", 26 Candidate: `{"k1"}`, 27 Expected: true, 28 }, 29 { 30 Description: "JSON array like text", 31 Candidate: `["$k1"]`, 32 Expected: true, 33 }, 34 { 35 Description: "bin data", 36 Candidate: "\u0000", 37 Expected: false, 38 }, 39 { 40 Description: "JSON text", 41 Candidate: `{ 42 "RepositoryDatastore":"db1", 43 "Db": [ 44 { 45 "Name": "db1", 46 "Config": { 47 "PoolSize": 3, 48 "MaxPoolSize": 5, 49 "DriverName": "mysql", 50 "Descriptor": "[username]:[password]@tcp(127.0.0.1:3306)/db1?parseTime=true", 51 "Credentials": "$mysqlCredentials" 52 } 53 } 54 ] 55 } 56 `, 57 Expected: true, 58 }, 59 } 60 61 for _, useCase := range useCases { 62 assert.EqualValues(t, useCase.Expected, IsASCIIText(useCase.Candidate), useCase.Description) 63 } 64 } 65 66 func TestIsPrintText(t *testing.T) { 67 var useCases = []struct { 68 Description string 69 Candidate string 70 Expected bool 71 }{ 72 { 73 Description: "basic text", 74 Candidate: `abc`, 75 Expected: true, 76 }, 77 { 78 Description: "JSON object like text", 79 Candidate: `{"k1"}`, 80 Expected: true, 81 }, 82 { 83 Description: "JSON array like text", 84 Candidate: `["$k1"]`, 85 Expected: true, 86 }, 87 { 88 Description: "bin data", 89 Candidate: "\u0000", 90 Expected: false, 91 }, 92 { 93 Description: "JSON text", 94 Candidate: `{ 95 "RepositoryDatastore":"db1", 96 "Db": [ 97 { 98 "Name": "db1", 99 "Config": { 100 "PoolSize": 3, 101 "MaxPoolSize": 5, 102 "DriverName": "mysql", 103 "Descriptor": "[username]:[password]@tcp(127.0.0.1:3306)/db1?parseTime=true", 104 "Credentials": "mysql" 105 } 106 } 107 ] 108 } 109 `, 110 Expected: true, 111 }, 112 } 113 114 for _, useCase := range useCases { 115 assert.EqualValues(t, useCase.Expected, IsPrintText(useCase.Candidate), useCase.Description) 116 } 117 } 118 119 func TestTerminatedSplitN(t *testing.T) { 120 var data = make([]byte, 0) 121 for i := 0; i < 9; i++ { 122 data = append(data, []byte(fmt.Sprintf("%v %v\n", strings.Repeat("x", 32), i))...) 123 } 124 text := string(data) 125 126 useCases := []struct { 127 description string 128 fragmentCount int 129 expectedFragmentSizes []int 130 }{ 131 { 132 description: "one fragment case", 133 fragmentCount: 1, 134 expectedFragmentSizes: []int{len(data)}, 135 }, 136 { 137 description: "two fragments case", 138 fragmentCount: 2, 139 expectedFragmentSizes: []int{175, 140}, 140 }, 141 { 142 description: "3 fragments case", 143 fragmentCount: 3, 144 expectedFragmentSizes: []int{140, 140, 35}, 145 }, 146 { 147 description: "7 fragments case", 148 fragmentCount: 7, 149 expectedFragmentSizes: []int{70, 70, 70, 70, 35}, 150 }, 151 { 152 description: "10 fragments case", //no more fragments then lines, so only 9 fragments here 153 fragmentCount: 10, 154 expectedFragmentSizes: []int{35, 35, 35, 35, 35, 35, 35, 35, 35}, 155 }, 156 } 157 158 for _, useCase := range useCases { 159 fragments := TerminatedSplitN(text, useCase.fragmentCount, "\n") 160 var actualFragmentSizes = make([]int, len(fragments)) 161 for i, fragment := range fragments { 162 actualFragmentSizes[i] = len(fragment) 163 } 164 assert.EqualValues(t, useCase.expectedFragmentSizes, actualFragmentSizes, useCase.description) 165 } 166 } 167 168 type testWriter struct { 169 *bytes.Buffer 170 data *[]string 171 } 172 173 func (t *testWriter) Close() error { 174 *t.data = append(*t.data, t.String()) 175 return nil 176 } 177 178 func newTestWriter(data *[]string) io.WriteCloser { 179 return &testWriter{ 180 data: data, 181 Buffer: new(bytes.Buffer), 182 } 183 } 184 185 func TestSplitTextStream(t *testing.T) { 186 187 var data = make([]byte, 0) 188 for i := 0; i < 9; i++ { 189 data = append(data, []byte(fmt.Sprintf("%v %v\n", strings.Repeat("x", 2), i))...) 190 } 191 text := string(data) 192 193 useCases := []struct { 194 description string 195 elements int 196 expect []string 197 }{ 198 { 199 description: "no more then 4 lines case", 200 elements: 4, 201 expect: []string{ 202 "xx 0\nxx 1\nxx 2\nxx 3", "xx 4\nxx 5\nxx 6\nxx 7", "xx 8", 203 }, 204 }, 205 { 206 description: "3 elements each", 207 elements: 3, 208 expect: []string{ 209 "xx 0\nxx 1\nxx 2", "xx 3\nxx 4\nxx 5", "xx 6\nxx 7\nxx 8", 210 }, 211 }, 212 { 213 description: "9 elements", 214 elements: 1, 215 expect: []string{ 216 "xx 0", "xx 1", "xx 2", "xx 3", "xx 4", "xx 5", "xx 6", "xx 7", "xx 8", 217 }, 218 }, 219 { 220 description: "9 elements", 221 elements: 0, 222 expect: []string{ 223 "xx 0", "xx 1", "xx 2", "xx 3", "xx 4", "xx 5", "xx 6", "xx 7", "xx 8", 224 }, 225 }, 226 { 227 description: "1 elements", 228 elements: 10, 229 expect: []string{ 230 "xx 0\nxx 1\nxx 2\nxx 3\nxx 4\nxx 5\nxx 6\nxx 7\nxx 8", 231 }, 232 }, 233 { 234 description: "1 elements", 235 elements: 9, 236 expect: []string{ 237 "xx 0\nxx 1\nxx 2\nxx 3\nxx 4\nxx 5\nxx 6\nxx 7\nxx 8", 238 }, 239 }, 240 } 241 242 for _, useCase := range useCases { 243 var data = make([]string, 0) 244 err := SplitTextStream(strings.NewReader(text), func() io.WriteCloser { return newTestWriter(&data) }, useCase.elements) 245 assert.Nil(t, err) 246 assert.EqualValues(t, useCase.expect, data, useCase.description) 247 } 248 } 249 250 func Test_CaseFormat(t *testing.T) { 251 var useCases = []struct { 252 description string 253 caseFrom int 254 caseTo int 255 input string 256 expect string 257 }{ 258 { 259 description: "camel to uppercase", 260 input: "thisIsMyTest", 261 caseFrom: CaseLowerCamel, 262 caseTo: CaseUpper, 263 expect: "THISISMYTEST", 264 }, 265 { 266 description: "camel to lower underscore", 267 input: "thisIsMyTest", 268 caseFrom: CaseLowerCamel, 269 caseTo: CaseLowerUnderscore, 270 expect: "this_is_my_test", 271 }, 272 { 273 description: "camel to upper underscore", 274 input: "thisIsMyTest", 275 caseFrom: CaseLowerCamel, 276 caseTo: CaseUpperUnderscore, 277 expect: "THIS_IS_MY_TEST", 278 }, 279 { 280 description: "lower underscore to upper camel", 281 input: "this_is_my_test", 282 caseFrom: CaseLowerUnderscore, 283 caseTo: CaseUpperCamel, 284 expect: "ThisIsMyTest", 285 }, 286 { 287 description: "upper underscore to lower camel", 288 input: "THIS_IS_MY_TEST", 289 caseFrom: CaseUpperUnderscore, 290 caseTo: CaseLowerCamel, 291 expect: "thisIsMyTest", 292 }, 293 294 { 295 description: "upper camel to lower camel", 296 input: "ThisIsMyTest", 297 caseFrom: CaseUpperCamel, 298 caseTo: CaseLowerCamel, 299 expect: "thisIsMyTest", 300 }, 301 } 302 303 for _, useCase := range useCases { 304 actual := ToCaseFormat(useCase.input, useCase.caseFrom, useCase.caseTo) 305 assert.Equal(t, useCase.expect, actual, useCase.description) 306 } 307 308 }