github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/strcase/snake_test.go (about)

     1  package strcase
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  type SnakeTest struct {
     8  	input  string
     9  	output string
    10  }
    11  
    12  // nolint gochecknoglobals
    13  var tests = []SnakeTest{
    14  	{"woof0_woof1", "woof0_woof1"},
    15  	{"_woof0_woof1_2", "_woof0_woof1_2"},
    16  	{"woof0_WOOF1_2", "woof0_woof1_2"},
    17  
    18  	{"a", "a"},
    19  	{"snake", "snake"},
    20  	{"A", "a"},
    21  	{"ID", "id"},
    22  	{"MOTD", "motd"},
    23  	{"Snake", "snake"},
    24  	{"SnakeTest", "snake_test"},
    25  	{"APIResponse", "api_response"},
    26  	{"SnakeID", "snake_id"},
    27  	{"Snake_Id", "snake_id"},
    28  	{"Snake_ID", "snake_id"},
    29  	{"SnakeIDGoogle", "snake_id_google"},
    30  	{"LinuxMOTD", "linux_motd"},
    31  	{"OMGWTFBBQ", "omgwtfbbq"},
    32  	{"omg_wtf_bbq", "omg_wtf_bbq"},
    33  	{"woof_woof", "woof_woof"},
    34  	{"_woof_woof", "_woof_woof"},
    35  	{"woof_woof_", "woof_woof_"},
    36  	{"WOOF", "woof"},
    37  	{"Woof", "woof"},
    38  	{"woof", "woof"},
    39  
    40  	{"WOOF0", "woof0"},
    41  	{"Woof1", "woof1"},
    42  	{"woof2", "woof2"},
    43  	{"woofWoof", "woof_woof"},
    44  	{"woofWOOF", "woof_woof"},
    45  	{"woof_WOOF", "woof_woof"},
    46  	{"Woof_WOOF", "woof_woof"},
    47  	{"WOOFWoofWoofWOOFWoofWoof", "woof_woof_woof_woof_woof_woof"},
    48  	{"WOOF_Woof_woof_WOOF_Woof_woof", "woof_woof_woof_woof_woof_woof"},
    49  	{"Woof_W", "woof_w"},
    50  	{"Woof_w", "woof_w"},
    51  	{"WoofW", "woof_w"},
    52  	{"Woof_W_", "woof_w_"},
    53  	{"Woof_w_", "woof_w_"},
    54  	{"WoofW_", "woof_w_"},
    55  	{"WOOF_", "woof_"},
    56  	{"W_Woof", "w_woof"},
    57  	{"w_Woof", "w_woof"},
    58  	{"WWoof", "w_woof"},
    59  	{"_W_Woof", "_w_woof"},
    60  	{"_w_Woof", "_w_woof"},
    61  	{"_WWoof", "_w_woof"},
    62  	{"_WOOF", "_woof"},
    63  	{"_woof", "_woof"},
    64  	{"Load5", "load5"},
    65  	{"V1", "v1"},
    66  }
    67  
    68  func TestSnakeCase(t *testing.T) {
    69  	for _, test := range tests {
    70  		if ToSnake(test.input) != test.output {
    71  			t.Errorf("SnakeCase(%q) -> %q, want %q", test.input, ToSnake(test.input), test.output)
    72  		}
    73  	}
    74  }
    75  
    76  func BenchmarkSnakeCase(b *testing.B) {
    77  	for i := 0; i < b.N; i++ {
    78  		for _, test := range tests {
    79  			ToSnake(test.input)
    80  		}
    81  	}
    82  }
    83  
    84  func TestToSnake(t *testing.T) {
    85  	cases := [][]string{
    86  		{"v1", "v1"},
    87  		{"testCase", "test_case"},
    88  		{"TestCase", "test_case"},
    89  		{"Test Case", "test_case"},
    90  		{" Test Case", "test_case"},
    91  		{"Test Case ", "test_case"},
    92  		{" Test Case ", "test_case"},
    93  		{"test", "test"},
    94  		{"test_case", "test_case"},
    95  		{"Test", "test"},
    96  		{"", ""},
    97  		{"ManyManyWords", "many_many_words"},
    98  		{"manyManyWords", "many_many_words"},
    99  		{"AnyKind of_string", "any_kind_of_string"},
   100  		{"numbers2and55with000", "numbers2_and55_with000"},
   101  		{"JSONData", "json_data"},
   102  		{"userID", "user_id"},
   103  		{"AAAbbb", "aa_abbb"},
   104  	}
   105  	for _, i := range cases {
   106  		in := i[0]
   107  		out := i[1]
   108  		result := ToSnake(in)
   109  
   110  		if result != out {
   111  			t.Error("'" + in + "'('" + result + "' != '" + out + "')")
   112  		}
   113  	}
   114  }
   115  
   116  func TestToDelimited(t *testing.T) {
   117  	cases := [][]string{
   118  		{"testCase", "test@case"},
   119  		{"TestCase", "test@case"},
   120  		{"Test Case", "test@case"},
   121  		{" Test Case", "test@case"},
   122  		{"Test Case ", "test@case"},
   123  		{" Test Case ", "test@case"},
   124  		{"test", "test"},
   125  		{"test_case", "test@case"},
   126  		{"Test", "test"},
   127  		{"", ""},
   128  		{"ManyManyWords", "many@many@words"},
   129  		{"manyManyWords", "many@many@words"},
   130  		{"AnyKind of_string", "any@kind@of@string"},
   131  		{"numbers2and55with000", "numbers2@and55@with000"},
   132  		{"JSONData", "json@data"},
   133  		{"userID", "user@id"},
   134  		{"AAAbbb", "aa@abbb"},
   135  		{"test-case", "test@case"},
   136  	}
   137  	for _, i := range cases {
   138  		in := i[0]
   139  		out := i[1]
   140  
   141  		result := ToDelimited(in, '@')
   142  		if result != out {
   143  			t.Error("'" + in + "' ('" + result + "' != '" + out + "')")
   144  		}
   145  	}
   146  }
   147  
   148  func TestToSnakeUpper(t *testing.T) {
   149  	cases := [][]string{
   150  		{"testCase", "TEST_CASE"},
   151  	}
   152  	for _, i := range cases {
   153  		in := i[0]
   154  		out := i[1]
   155  
   156  		result := ToSnakeUpper(in)
   157  		if result != out {
   158  			t.Error("'" + result + "' != '" + out + "'")
   159  		}
   160  	}
   161  }
   162  
   163  func TestToKebab(t *testing.T) {
   164  	cases := [][]string{
   165  		{"v1", "v1"},
   166  		{"testCase", "test-case"},
   167  	}
   168  	for _, i := range cases {
   169  		in := i[0]
   170  		out := i[1]
   171  
   172  		result := ToKebab(in)
   173  		if result != out {
   174  			t.Error("'" + result + "' != '" + out + "'")
   175  		}
   176  	}
   177  }
   178  
   179  func TestToKebabUpper(t *testing.T) {
   180  	cases := [][]string{
   181  		{"testCase", "TEST-CASE"},
   182  	}
   183  	for _, i := range cases {
   184  		in := i[0]
   185  		out := i[1]
   186  		result := ToKebabUpper(in)
   187  
   188  		if result != out {
   189  			t.Error("'" + result + "' != '" + out + "'")
   190  		}
   191  	}
   192  }
   193  
   194  func TestToDelimitedUpper(t *testing.T) {
   195  	cases := [][]string{
   196  		{"testCase", "TEST.CASE"},
   197  	}
   198  	for _, i := range cases {
   199  		in := i[0]
   200  		out := i[1]
   201  		result := ToDelimitedUpper(in, '.')
   202  
   203  		if result != out {
   204  			t.Error("'" + result + "' != '" + out + "'")
   205  		}
   206  	}
   207  }