vitess.io/vitess@v0.16.2/go/textutil/strings_test.go (about) 1 /* 2 Copyright 2020 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package textutil 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func TestSplitDelimitedList(t *testing.T) { 26 defaultList := []string{"one", "two", "three"} 27 tt := []struct { 28 s string 29 list []string 30 }{ 31 {s: "one,two,three"}, 32 {s: "one, two, three"}, 33 {s: "one,two; three "}, 34 {s: "one two three"}, 35 {s: "one,,,two,three"}, 36 {s: " one, ,two, three "}, 37 } 38 39 for _, tc := range tt { 40 if tc.list == nil { 41 tc.list = defaultList 42 } 43 list := SplitDelimitedList(tc.s) 44 assert.Equal(t, tc.list, list) 45 } 46 } 47 48 func TestEscapeJoin(t *testing.T) { 49 elems := []string{"normal", "with space", "with,comma", "with?question"} 50 s := EscapeJoin(elems, ",") 51 assert.Equal(t, "normal,with+space,with%2Ccomma,with%3Fquestion", s) 52 } 53 54 func TestSplitUnescape(t *testing.T) { 55 { 56 s := "" 57 elems, err := SplitUnescape(s, ",") 58 assert.NoError(t, err) 59 assert.Nil(t, elems) 60 } 61 { 62 s := "normal,with+space,with%2Ccomma,with%3Fquestion" 63 expected := []string{"normal", "with space", "with,comma", "with?question"} 64 elems, err := SplitUnescape(s, ",") 65 assert.NoError(t, err) 66 assert.Equal(t, expected, elems) 67 } 68 } 69 70 func TestSingleWordCamel(t *testing.T) { 71 tt := []struct { 72 word string 73 expect string 74 }{ 75 { 76 word: "", 77 expect: "", 78 }, 79 { 80 word: "_", 81 expect: "_", 82 }, 83 { 84 word: "a", 85 expect: "A", 86 }, 87 { 88 word: "A", 89 expect: "A", 90 }, 91 { 92 word: "_A", 93 expect: "_a", 94 }, 95 { 96 word: "mysql", 97 expect: "Mysql", 98 }, 99 { 100 word: "mySQL", 101 expect: "Mysql", 102 }, 103 } 104 for _, tc := range tt { 105 t.Run(tc.word, func(t *testing.T) { 106 camel := SingleWordCamel(tc.word) 107 assert.Equal(t, tc.expect, camel) 108 }) 109 } 110 }