github.com/wangyougui/gf/v2@v2.6.5/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/wangyougui/gf.
     6  
     7  package gstr_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/wangyougui/gf/v2/test/gtest"
    13  	"github.com/wangyougui/gf/v2/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  		t.Assert(gstr.CaseSnakeFirstUpper("RGBCodeMd5", "."), "rgb.code.md5")
   194  	})
   195  
   196  }
   197  func Test_CaseTypeMatch(t *testing.T) {
   198  	caseTypes := []gstr.CaseType{
   199  		gstr.Camel,
   200  		gstr.CamelLower,
   201  		gstr.Snake,
   202  		gstr.SnakeFirstUpper,
   203  		gstr.SnakeScreaming,
   204  		gstr.Kebab,
   205  		gstr.KebabScreaming,
   206  		gstr.Lower,
   207  		"test", // invalid case type
   208  	}
   209  	testCaseTypes := []string{
   210  		"camel",
   211  		"camelLower",
   212  		"snake",
   213  		"snakeFirstUpper",
   214  		"snakeScreaming",
   215  		"kebab",
   216  		"kebabScreaming",
   217  		"lower",
   218  		"test",
   219  	}
   220  	gtest.C(t, func(t *gtest.T) {
   221  		for i := 0; i < len(caseTypes); i++ {
   222  			t.Assert(gstr.CaseTypeMatch(testCaseTypes[i]), caseTypes[i])
   223  		}
   224  	})
   225  }
   226  
   227  func Test_CaseConvert(t *testing.T) {
   228  	caseTypes := []gstr.CaseType{
   229  		gstr.Camel,
   230  		gstr.CamelLower,
   231  		gstr.Snake,
   232  		gstr.SnakeFirstUpper,
   233  		gstr.SnakeScreaming,
   234  		gstr.Kebab,
   235  		gstr.KebabScreaming,
   236  		gstr.Lower,
   237  		"test", // invalid case type
   238  		"",     // invalid case type
   239  	}
   240  	testCaseTypes := []string{
   241  		"AnyKindOfString",    // Camel
   242  		"anyKindOfString",    // CamelLower
   243  		"any_kind_of_string", // Snake
   244  		"any_kind_of_string", // SnakeFirstUpper
   245  		"ANY_KIND_OF_STRING", // SnakeScreaming
   246  		"any-kind-of-string", // Kebab
   247  		"ANY-KIND-OF-STRING", // KebabScreaming
   248  		"any_kind_of_string", // Lower
   249  		"any_kind_of_string", // invalid case type
   250  		"any_kind_of_string", // invalid case type
   251  	}
   252  	gtest.C(t, func(t *gtest.T) {
   253  		for i := 0; i < len(caseTypes); i++ {
   254  			t.Assert(gstr.CaseConvert("any_kind_of_string", caseTypes[i]), testCaseTypes[i])
   255  			t.Logf("test case: %s success", caseTypes[i])
   256  		}
   257  	})
   258  }