github.com/gogf/gf@v1.16.9/text/gstr/gstr_z_unit_case_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gstr_test 8 9 import ( 10 "github.com/gogf/gf/test/gtest" 11 "testing" 12 13 "github.com/gogf/gf/text/gstr" 14 ) 15 16 func Test_CaseCamel(t *testing.T) { 17 cases := [][]string{ 18 {"test_case", "TestCase"}, 19 {"test", "Test"}, 20 {"TestCase", "TestCase"}, 21 {" test case ", "TestCase"}, 22 {"userLogin_log.bak", "UserLoginLogBak"}, 23 {"", ""}, 24 {"many_many_words", "ManyManyWords"}, 25 {"AnyKind of_string", "AnyKindOfString"}, 26 {"odd-fix", "OddFix"}, 27 {"numbers2And55with000", "Numbers2And55With000"}, 28 } 29 for _, i := range cases { 30 in := i[0] 31 out := i[1] 32 result := gstr.CaseCamel(in) 33 if result != out { 34 t.Error("'" + result + "' != '" + out + "'") 35 } 36 } 37 } 38 39 func Test_CaseCamelLower(t *testing.T) { 40 cases := [][]string{ 41 {"foo-bar", "fooBar"}, 42 {"TestCase", "testCase"}, 43 {"", ""}, 44 {"AnyKind of_string", "anyKindOfString"}, 45 } 46 for _, i := range cases { 47 in := i[0] 48 out := i[1] 49 result := gstr.CaseCamelLower(in) 50 if result != out { 51 t.Error("'" + result + "' != '" + out + "'") 52 } 53 } 54 } 55 56 func Test_CaseSnake(t *testing.T) { 57 cases := [][]string{ 58 {"testCase", "test_case"}, 59 {"TestCase", "test_case"}, 60 {"Test Case", "test_case"}, 61 {" Test Case", "test_case"}, 62 {"Test Case ", "test_case"}, 63 {" Test Case ", "test_case"}, 64 {"test", "test"}, 65 {"test_case", "test_case"}, 66 {"Test", "test"}, 67 {"", ""}, 68 {"ManyManyWords", "many_many_words"}, 69 {"manyManyWords", "many_many_words"}, 70 {"AnyKind of_string", "any_kind_of_string"}, 71 {"numbers2and55with000", "numbers_2_and_55_with_000"}, 72 {"JSONData", "json_data"}, 73 {"userID", "user_id"}, 74 {"AAAbbb", "aa_abbb"}, 75 } 76 for _, i := range cases { 77 in := i[0] 78 out := i[1] 79 result := gstr.CaseSnake(in) 80 if result != out { 81 t.Error("'" + in + "'('" + result + "' != '" + out + "')") 82 } 83 } 84 } 85 86 func Test_CaseDelimited(t *testing.T) { 87 cases := [][]string{ 88 {"testCase", "test@case"}, 89 {"TestCase", "test@case"}, 90 {"Test Case", "test@case"}, 91 {" Test Case", "test@case"}, 92 {"Test Case ", "test@case"}, 93 {" Test Case ", "test@case"}, 94 {"test", "test"}, 95 {"test_case", "test@case"}, 96 {"Test", "test"}, 97 {"", ""}, 98 {"ManyManyWords", "many@many@words"}, 99 {"manyManyWords", "many@many@words"}, 100 {"AnyKind of_string", "any@kind@of@string"}, 101 {"numbers2and55with000", "numbers@2@and@55@with@000"}, 102 {"JSONData", "json@data"}, 103 {"userID", "user@id"}, 104 {"AAAbbb", "aa@abbb"}, 105 {"test-case", "test@case"}, 106 } 107 for _, i := range cases { 108 in := i[0] 109 out := i[1] 110 result := gstr.CaseDelimited(in, '@') 111 if result != out { 112 t.Error("'" + in + "' ('" + result + "' != '" + out + "')") 113 } 114 } 115 } 116 117 func Test_CaseSnakeScreaming(t *testing.T) { 118 cases := [][]string{ 119 {"testCase", "TEST_CASE"}, 120 } 121 for _, i := range cases { 122 in := i[0] 123 out := i[1] 124 result := gstr.CaseSnakeScreaming(in) 125 if result != out { 126 t.Error("'" + result + "' != '" + out + "'") 127 } 128 } 129 } 130 131 func Test_CaseKebab(t *testing.T) { 132 cases := [][]string{ 133 {"testCase", "test-case"}, 134 {"optimization1.0.0", "optimization-1-0-0"}, 135 } 136 for _, i := range cases { 137 in := i[0] 138 out := i[1] 139 result := gstr.CaseKebab(in) 140 if result != out { 141 t.Error("'" + result + "' != '" + out + "'") 142 } 143 } 144 } 145 146 func Test_CaseKebabScreaming(t *testing.T) { 147 cases := [][]string{ 148 {"testCase", "TEST-CASE"}, 149 } 150 for _, i := range cases { 151 in := i[0] 152 out := i[1] 153 result := gstr.CaseKebabScreaming(in) 154 if result != out { 155 t.Error("'" + result + "' != '" + out + "'") 156 } 157 } 158 } 159 160 func Test_CaseDelimitedScreaming(t *testing.T) { 161 cases := [][]string{ 162 {"testCase", "TEST.CASE"}, 163 } 164 for _, i := range cases { 165 in := i[0] 166 out := i[1] 167 result := gstr.CaseDelimitedScreaming(in, '.', true) 168 if result != out { 169 t.Error("'" + result + "' != '" + out + "'") 170 } 171 } 172 } 173 174 func Test_CaseSnakeFirstUpper(t *testing.T) { 175 cases := [][]string{ 176 {"RGBCodeMd5", "rgb_code_md5"}, 177 {"testCase", "test_case"}, 178 {"Md5", "md5"}, 179 {"userID", "user_id"}, 180 {"RGB", "rgb"}, 181 {"RGBCode", "rgb_code"}, 182 {"_ID", "id"}, 183 {"User_ID", "user_id"}, 184 {"user_id", "user_id"}, 185 {"md5", "md5"}, 186 {"Numbers2And55With000", "numbers2_and55_with000"}, 187 } 188 gtest.C(t, func(t *gtest.T) { 189 for _, item := range cases { 190 t.Assert(gstr.CaseSnakeFirstUpper(item[0]), item[1]) 191 } 192 }) 193 194 }