github.heygears.com/openimsdk/tools@v0.0.49/utils/stringutil/strings_test.go (about) 1 // Copyright © 2023 OpenIM. All rights reserved. 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 stringutil 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 ) 23 24 func TestIntToString(t *testing.T) { 25 assert.Equal(t, "123", IntToString(123)) 26 } 27 28 func TestStringToInt(t *testing.T) { 29 assert.Equal(t, 123, StringToInt("123")) 30 } 31 32 func TestStringToInt64(t *testing.T) { 33 assert.Equal(t, int64(123), StringToInt64("123")) 34 } 35 36 func TestStringToInt32(t *testing.T) { 37 assert.Equal(t, int32(123), StringToInt32("123")) 38 } 39 40 func TestInt32ToString(t *testing.T) { 41 assert.Equal(t, "123", Int32ToString(123)) 42 } 43 44 func TestUint32ToString(t *testing.T) { 45 assert.Equal(t, "123", Uint32ToString(123)) 46 } 47 48 func TestIsContain(t *testing.T) { 49 list := []string{"apple", "banana", "cherry"} 50 assert.True(t, IsContain("banana", list)) 51 assert.False(t, IsContain("date", list)) 52 } 53 54 func TestIsContainInt32(t *testing.T) { 55 list := []int32{1, 2, 3} 56 assert.True(t, IsContainInt32(2, list)) 57 assert.False(t, IsContainInt32(4, list)) 58 } 59 60 func TestIsContainInt(t *testing.T) { 61 list := []int{1, 2, 3} 62 assert.True(t, IsContainInt(2, list)) 63 assert.False(t, IsContainInt(4, list)) 64 } 65 66 func TestRemoveDuplicateElement(t *testing.T) { 67 idList := []string{"a", "b", "a", "c", "b"} 68 expected := []string{"a", "b", "c"} 69 result := RemoveDuplicateElement(idList) 70 assert.ElementsMatch(t, expected, result) 71 } 72 73 func TestIsDuplicateStringSlice(t *testing.T) { 74 assert.False(t, IsDuplicateStringSlice([]string{"a", "b", "c"})) 75 assert.True(t, IsDuplicateStringSlice([]string{"a", "b", "a"})) 76 } 77 78 func TestFormatString(t *testing.T) { 79 cases := []struct { 80 name string 81 text string 82 length int 83 alignLeft bool 84 want string 85 }{ 86 {"LeftAlignShort", "hello", 10, true, "hello "}, 87 {"RightAlignShort", "hello", 10, false, " hello"}, 88 {"ExactLength", "hello", 5, true, "hello"}, 89 {"TruncateLong", "hello world", 5, true, "hello"}, 90 {"LeftAlignEmpty", "", 5, true, " "}, 91 {"RightAlignEmpty", "", 5, false, " "}, 92 {"NoLength", "hello", 0, true, ""}, 93 } 94 95 for _, tc := range cases { 96 t.Run(tc.name, func(t *testing.T) { 97 got := FormatString(tc.text, tc.length, tc.alignLeft) 98 if got != tc.want { 99 t.Errorf("FormatString(%q, %d, %t) = %q; want %q", tc.text, tc.length, tc.alignLeft, got, tc.want) 100 } 101 }) 102 } 103 } 104 105 func TestCamelCaseToSpaceSeparated(t *testing.T) { 106 inputs := []string{ 107 "HelloWorld,GoGo", 108 "hello world, go go", 109 } 110 for _, input := range inputs { 111 r := CamelCaseToSpaceSeparated(input) 112 fmt.Println(r) 113 } 114 }