github.com/gogf/gf/v2@v2.7.4/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  	"testing"
    11  
    12  	"github.com/gogf/gf/v2/test/gtest"
    13  	"github.com/gogf/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  		{"testCase", "TestCase"},
    22  		{" test  case ", "TestCase"},
    23  		{"userLogin_log.bak", "UserLoginLogBak"},
    24  		{"", ""},
    25  		{"many_many_words", "ManyManyWords"},
    26  		{"AnyKind of_string", "AnyKindOfString"},
    27  		{"odd-fix", "OddFix"},
    28  		{"numbers2And55with000", "Numbers2And55With000"},
    29  	}
    30  	for _, i := range cases {
    31  		in := i[0]
    32  		out := i[1]
    33  		result := gstr.CaseCamel(in)
    34  		if result != out {
    35  			t.Error("'" + result + "' != '" + out + "'")
    36  		}
    37  	}
    38  }
    39  
    40  func Test_CaseCamelLower(t *testing.T) {
    41  	cases := [][]string{
    42  		{"foo-bar", "fooBar"},
    43  		{"TestCase", "testCase"},
    44  		{"", ""},
    45  		{"AnyKind of_string", "anyKindOfString"},
    46  	}
    47  	for _, i := range cases {
    48  		in := i[0]
    49  		out := i[1]
    50  		result := gstr.CaseCamelLower(in)
    51  		if result != out {
    52  			t.Error("'" + result + "' != '" + out + "'")
    53  		}
    54  	}
    55  }
    56  
    57  func Test_CaseSnake(t *testing.T) {
    58  	cases := [][]string{
    59  		{"testCase", "test_case"},
    60  		{"TestCase", "test_case"},
    61  		{"Test Case", "test_case"},
    62  		{" Test Case", "test_case"},
    63  		{"Test Case ", "test_case"},
    64  		{" Test Case ", "test_case"},
    65  		{"test", "test"},
    66  		{"test_case", "test_case"},
    67  		{"Test", "test"},
    68  		{"", ""},
    69  		{"ManyManyWords", "many_many_words"},
    70  		{"manyManyWords", "many_many_words"},
    71  		{"AnyKind of_string", "any_kind_of_string"},
    72  		{"numbers2and55with000", "numbers_2_and_55_with_000"},
    73  		{"JSONData", "json_data"},
    74  		{"userID", "user_id"},
    75  		{"AAAbbb", "aa_abbb"},
    76  	}
    77  	for _, i := range cases {
    78  		in := i[0]
    79  		out := i[1]
    80  		result := gstr.CaseSnake(in)
    81  		if result != out {
    82  			t.Error("'" + in + "'('" + result + "' != '" + out + "')")
    83  		}
    84  	}
    85  }
    86  
    87  func Test_CaseDelimited(t *testing.T) {
    88  	cases := [][]string{
    89  		{"testCase", "test@case"},
    90  		{"TestCase", "test@case"},
    91  		{"Test Case", "test@case"},
    92  		{" Test Case", "test@case"},
    93  		{"Test Case ", "test@case"},
    94  		{" Test Case ", "test@case"},
    95  		{"test", "test"},
    96  		{"test_case", "test@case"},
    97  		{"Test", "test"},
    98  		{"", ""},
    99  		{"ManyManyWords", "many@many@words"},
   100  		{"manyManyWords", "many@many@words"},
   101  		{"AnyKind of_string", "any@kind@of@string"},
   102  		{"numbers2and55with000", "numbers@2@and@55@with@000"},
   103  		{"JSONData", "json@data"},
   104  		{"userID", "user@id"},
   105  		{"AAAbbb", "aa@abbb"},
   106  		{"test-case", "test@case"},
   107  	}
   108  	for _, i := range cases {
   109  		in := i[0]
   110  		out := i[1]
   111  		result := gstr.CaseDelimited(in, '@')
   112  		if result != out {
   113  			t.Error("'" + in + "' ('" + result + "' != '" + out + "')")
   114  		}
   115  	}
   116  }
   117  
   118  func Test_CaseSnakeScreaming(t *testing.T) {
   119  	cases := [][]string{
   120  		{"testCase", "TEST_CASE"},
   121  	}
   122  	for _, i := range cases {
   123  		in := i[0]
   124  		out := i[1]
   125  		result := gstr.CaseSnakeScreaming(in)
   126  		if result != out {
   127  			t.Error("'" + result + "' != '" + out + "'")
   128  		}
   129  	}
   130  }
   131  
   132  func Test_CaseKebab(t *testing.T) {
   133  	cases := [][]string{
   134  		{"testCase", "test-case"},
   135  		{"optimization1.0.0", "optimization-1-0-0"},
   136  	}
   137  	for _, i := range cases {
   138  		in := i[0]
   139  		out := i[1]
   140  		result := gstr.CaseKebab(in)
   141  		if result != out {
   142  			t.Error("'" + result + "' != '" + out + "'")
   143  		}
   144  	}
   145  }
   146  
   147  func Test_CaseKebabScreaming(t *testing.T) {
   148  	cases := [][]string{
   149  		{"testCase", "TEST-CASE"},
   150  	}
   151  	for _, i := range cases {
   152  		in := i[0]
   153  		out := i[1]
   154  		result := gstr.CaseKebabScreaming(in)
   155  		if result != out {
   156  			t.Error("'" + result + "' != '" + out + "'")
   157  		}
   158  	}
   159  }
   160  
   161  func Test_CaseDelimitedScreaming(t *testing.T) {
   162  	cases := [][]string{
   163  		{"testCase", "TEST.CASE"},
   164  	}
   165  	for _, i := range cases {
   166  		in := i[0]
   167  		out := i[1]
   168  		result := gstr.CaseDelimitedScreaming(in, '.', true)
   169  		if result != out {
   170  			t.Error("'" + result + "' != '" + out + "'")
   171  		}
   172  	}
   173  }
   174  
   175  func Test_CaseSnakeFirstUpper(t *testing.T) {
   176  	cases := [][]string{
   177  		{"RGBCodeMd5", "rgb_code_md5"},
   178  		{"testCase", "test_case"},
   179  		{"Md5", "md5"},
   180  		{"userID", "user_id"},
   181  		{"RGB", "rgb"},
   182  		{"RGBCode", "rgb_code"},
   183  		{"_ID", "id"},
   184  		{"User_ID", "user_id"},
   185  		{"user_id", "user_id"},
   186  		{"md5", "md5"},
   187  		{"Numbers2And55With000", "numbers2_and55_with000"},
   188  	}
   189  	gtest.C(t, func(t *gtest.T) {
   190  		for _, item := range cases {
   191  			t.Assert(gstr.CaseSnakeFirstUpper(item[0]), item[1])
   192  		}
   193  
   194  		t.Assert(gstr.CaseSnakeFirstUpper("RGBCodeMd5", "."), "rgb.code.md5")
   195  	})
   196  
   197  }
   198  func Test_CaseTypeMatch(t *testing.T) {
   199  	caseTypes := []gstr.CaseType{
   200  		gstr.Camel,
   201  		gstr.CamelLower,
   202  		gstr.Snake,
   203  		gstr.SnakeFirstUpper,
   204  		gstr.SnakeScreaming,
   205  		gstr.Kebab,
   206  		gstr.KebabScreaming,
   207  		gstr.Lower,
   208  		"test", // invalid case type
   209  	}
   210  	testCaseTypes := []string{
   211  		"camel",
   212  		"camelLower",
   213  		"snake",
   214  		"snakeFirstUpper",
   215  		"snakeScreaming",
   216  		"kebab",
   217  		"kebabScreaming",
   218  		"lower",
   219  		"test",
   220  	}
   221  	gtest.C(t, func(t *gtest.T) {
   222  		for i := 0; i < len(caseTypes); i++ {
   223  			t.Assert(gstr.CaseTypeMatch(testCaseTypes[i]), caseTypes[i])
   224  		}
   225  	})
   226  }
   227  
   228  func Test_CaseConvert(t *testing.T) {
   229  	caseTypes := []gstr.CaseType{
   230  		gstr.Camel,
   231  		gstr.CamelLower,
   232  		gstr.Snake,
   233  		gstr.SnakeFirstUpper,
   234  		gstr.SnakeScreaming,
   235  		gstr.Kebab,
   236  		gstr.KebabScreaming,
   237  		gstr.Lower,
   238  		"test", // invalid case type
   239  		"",     // invalid case type
   240  	}
   241  	testCaseTypes := []string{
   242  		"AnyKindOfString",    // Camel
   243  		"anyKindOfString",    // CamelLower
   244  		"any_kind_of_string", // Snake
   245  		"any_kind_of_string", // SnakeFirstUpper
   246  		"ANY_KIND_OF_STRING", // SnakeScreaming
   247  		"any-kind-of-string", // Kebab
   248  		"ANY-KIND-OF-STRING", // KebabScreaming
   249  		"any_kind_of_string", // Lower
   250  		"any_kind_of_string", // invalid case type
   251  		"any_kind_of_string", // invalid case type
   252  	}
   253  	gtest.C(t, func(t *gtest.T) {
   254  		for i := 0; i < len(caseTypes); i++ {
   255  			t.Assert(gstr.CaseConvert("any_kind_of_string", caseTypes[i]), testCaseTypes[i])
   256  			t.Logf("test case: %s success", caseTypes[i])
   257  		}
   258  	})
   259  }