github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/linter/strs/strs_test.go (about)

     1  package strs_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/yoheimuta/protolint/linter/strs"
     8  )
     9  
    10  func TestIsUpperCamelCase(t *testing.T) {
    11  	tests := []struct {
    12  		name  string
    13  		input string
    14  		want  bool
    15  	}{
    16  		{
    17  			name:  "the first letter is not an uppercase character",
    18  			input: "hello",
    19  		},
    20  		{
    21  			name:  "_ is included",
    22  			input: "Hello_world",
    23  		},
    24  		{
    25  			name:  ". is included",
    26  			input: "Hello.world",
    27  		},
    28  		{
    29  			name:  "the first letter is an uppercase character",
    30  			input: "Hello",
    31  			want:  true,
    32  		},
    33  		{
    34  			name:  "the first letter is an uppercase character and rest is a camel case",
    35  			input: "HelloWorld",
    36  			want:  true,
    37  		},
    38  	}
    39  
    40  	for _, test := range tests {
    41  		test := test
    42  		t.Run(test.name, func(t *testing.T) {
    43  			got := strs.IsUpperCamelCase(test.input)
    44  			if got != test.want {
    45  				t.Errorf("got %v, but want %v", got, test.want)
    46  			}
    47  		})
    48  	}
    49  }
    50  
    51  func TestIsLowerCamelCase(t *testing.T) {
    52  	tests := []struct {
    53  		name  string
    54  		input string
    55  		want  bool
    56  	}{
    57  		{
    58  			name:  "the first letter is an uppercase character",
    59  			input: "Hello",
    60  		},
    61  		{
    62  			name:  "_ is included",
    63  			input: "hello_world",
    64  		},
    65  		{
    66  			name:  ". is included",
    67  			input: "hello.world",
    68  		},
    69  		{
    70  			name:  "the first letter is a lowercase character",
    71  			input: "hello",
    72  			want:  true,
    73  		},
    74  		{
    75  			name:  "the first letter is a lowercase character and rest is a camel case",
    76  			input: "helloWorld",
    77  			want:  true,
    78  		},
    79  	}
    80  
    81  	for _, test := range tests {
    82  		test := test
    83  		t.Run(test.name, func(t *testing.T) {
    84  			got := strs.IsLowerCamelCase(test.input)
    85  			if got != test.want {
    86  				t.Errorf("got %v, but want %v", got, test.want)
    87  			}
    88  		})
    89  	}
    90  }
    91  func TestIsUpperSnakeCase(t *testing.T) {
    92  	tests := []struct {
    93  		name  string
    94  		input string
    95  		want  bool
    96  	}{
    97  		{
    98  			name: "empty is not uppercase",
    99  		},
   100  		{
   101  			name:  "includes lowercase characters",
   102  			input: "hello",
   103  		},
   104  		{
   105  			name:  "includes a lowercase character",
   106  			input: "hELLO",
   107  		},
   108  		{
   109  			name:  "all uppercase",
   110  			input: "HELLO",
   111  			want:  true,
   112  		},
   113  		{
   114  			name:  "all uppercase with underscore",
   115  			input: "FIRST_VALUE",
   116  			want:  true,
   117  		},
   118  	}
   119  
   120  	for _, test := range tests {
   121  		test := test
   122  		t.Run(test.name, func(t *testing.T) {
   123  			got := strs.IsUpperSnakeCase(test.input)
   124  			if got != test.want {
   125  				t.Errorf("got %v, but want %v", got, test.want)
   126  			}
   127  		})
   128  	}
   129  }
   130  
   131  func TestIsLowerSnakeCase(t *testing.T) {
   132  	tests := []struct {
   133  		name  string
   134  		input string
   135  		want  bool
   136  	}{
   137  		{
   138  			name: "empty is not lowercase",
   139  		},
   140  		{
   141  			name:  "includes uppercase characters",
   142  			input: "HELLO",
   143  		},
   144  		{
   145  			name:  "includes a uppercase character",
   146  			input: "Hello",
   147  		},
   148  		{
   149  			name:  "all lowercase",
   150  			input: "hello",
   151  			want:  true,
   152  		},
   153  		{
   154  			name:  "all lowercase with underscore",
   155  			input: "song_name",
   156  			want:  true,
   157  		},
   158  	}
   159  
   160  	for _, test := range tests {
   161  		test := test
   162  		t.Run(test.name, func(t *testing.T) {
   163  			got := strs.IsLowerSnakeCase(test.input)
   164  			if got != test.want {
   165  				t.Errorf("got %v, but want %v", got, test.want)
   166  			}
   167  		})
   168  	}
   169  }
   170  
   171  func TestSplitCamelCaseWord(t *testing.T) {
   172  	tests := []struct {
   173  		name  string
   174  		input string
   175  		want  []string
   176  	}{
   177  		{
   178  			name: "if s is empty, returns nil",
   179  		},
   180  		{
   181  			name:  "if s is not camel_case, returns nil",
   182  			input: "not_camel",
   183  		},
   184  		{
   185  			name:  "input consists of one word",
   186  			input: "Account",
   187  			want: []string{
   188  				"Account",
   189  			},
   190  		},
   191  		{
   192  			name:  "input consists of words with an initial capital",
   193  			input: "AccountStatus",
   194  			want: []string{
   195  				"Account",
   196  				"Status",
   197  			},
   198  		},
   199  		{
   200  			name:  "input consists of words without an initial capital",
   201  			input: "accountStatus",
   202  			want: []string{
   203  				"account",
   204  				"Status",
   205  			},
   206  		},
   207  		{
   208  			name:  "input consists of words with continuous upper letters",
   209  			input: "ACCOUNTStatusException",
   210  			want: []string{
   211  				"ACCOUNT",
   212  				"Status",
   213  				"Exception",
   214  			},
   215  		},
   216  	}
   217  
   218  	for _, test := range tests {
   219  		test := test
   220  		t.Run(test.name, func(t *testing.T) {
   221  			got := strs.SplitCamelCaseWord(test.input)
   222  			if !reflect.DeepEqual(got, test.want) {
   223  				t.Errorf("got %v, but want %v", got, test.want)
   224  			}
   225  		})
   226  	}
   227  }
   228  
   229  func TestToUpperSnakeCase(t *testing.T) {
   230  	tests := []struct {
   231  		name  string
   232  		input string
   233  		want  string
   234  	}{
   235  		{
   236  			name:  "s is not camel_case",
   237  			input: "not_camel",
   238  			want:  "NOT_CAMEL",
   239  		},
   240  		{
   241  			name:  "input consists of one word",
   242  			input: "Account",
   243  			want:  "ACCOUNT",
   244  		},
   245  		{
   246  			name:  "input consists of words with an initial capital",
   247  			input: "AccountStatus",
   248  			want:  "ACCOUNT_STATUS",
   249  		},
   250  		{
   251  			name:  "input consists of words without an initial capital",
   252  			input: "accountStatus",
   253  			want:  "ACCOUNT_STATUS",
   254  		},
   255  		{
   256  			name:  "convert from camel-case strings starting with a 2-letter abbreviation #341",
   257  			input: "ITDepartmentRegion",
   258  			want:  "IT_DEPARTMENT_REGION",
   259  		},
   260  		{
   261  			name:  "convert from camel-case strings includes OAuth #351",
   262  			input: "ListAccountPipedriveOAuthsEnabledFilter",
   263  			want:  "LIST_ACCOUNT_PIPEDRIVE_OAUTHS_ENABLED_FILTER",
   264  		},
   265  		{
   266  			name:  "convert from camel-case strings starting with OAuth #351",
   267  			input: "OAuthsEnabledFilter",
   268  			want:  "OAUTHS_ENABLED_FILTER",
   269  		},
   270  		{
   271  			name:  "convert from camel-case strings includes with a 2-letter abbreviation #341",
   272  			input: "ListITDepartmentRegion",
   273  			want:  "LIST_IT_DEPARTMENT_REGION",
   274  		},
   275  		{
   276  			name:  "convert from camel-case strings starting with a 3-letter abbreviation",
   277  			input: "ITCDepartmentRegion",
   278  			want:  "ITC_DEPARTMENT_REGION",
   279  		},
   280  		{
   281  			name:  "convert from camel-case strings includes a 3-letter abbreviation",
   282  			input: "ListITCDepartmentRegion",
   283  			want:  "LIST_ITC_DEPARTMENT_REGION",
   284  		},
   285  		{
   286  			name:  "input consists of kebab case",
   287  			input: "account-status",
   288  			want:  "ACCOUNT_STATUS",
   289  		},
   290  		{
   291  			name:  "input consists of .",
   292  			input: "account.status",
   293  			want:  "ACCOUNT_STATUS",
   294  		},
   295  	}
   296  
   297  	for _, test := range tests {
   298  		test := test
   299  		t.Run(test.name, func(t *testing.T) {
   300  			got := strs.ToUpperSnakeCase(test.input)
   301  			if !reflect.DeepEqual(got, test.want) {
   302  				t.Errorf("got %v, but want %v", got, test.want)
   303  			}
   304  		})
   305  	}
   306  }
   307  
   308  func TestToLowerSnakeCase(t *testing.T) {
   309  	tests := []struct {
   310  		name  string
   311  		input string
   312  		want  string
   313  	}{
   314  		{
   315  			name:  "input consists of one word",
   316  			input: "Account",
   317  			want:  "account",
   318  		},
   319  		{
   320  			name:  "input consists of words with an initial capital",
   321  			input: "AccountStatus",
   322  			want:  "account_status",
   323  		},
   324  		{
   325  			name:  "input consists of words without an initial capital",
   326  			input: "accountStatus",
   327  			want:  "account_status",
   328  		},
   329  		{
   330  			name:  "input consists of kebab case",
   331  			input: "account-status",
   332  			want:  "account_status",
   333  		},
   334  		{
   335  			name:  "input consists of .",
   336  			input: "account.status",
   337  			want:  "account_status",
   338  		},
   339  	}
   340  
   341  	for _, test := range tests {
   342  		test := test
   343  		t.Run(test.name, func(t *testing.T) {
   344  			got := strs.ToLowerSnakeCase(test.input)
   345  
   346  			if !reflect.DeepEqual(got, test.want) {
   347  				t.Errorf("got %v, but want %v", got, test.want)
   348  			}
   349  		})
   350  	}
   351  }
   352  
   353  func TestToUpperCamelCase(t *testing.T) {
   354  	tests := []struct {
   355  		name  string
   356  		input string
   357  		want  string
   358  	}{
   359  		{
   360  			name:  "input consists of one word",
   361  			input: "account",
   362  			want:  "Account",
   363  		},
   364  		{
   365  			name:  "input consists of words with an initial capital",
   366  			input: "AccountStatus",
   367  			want:  "AccountStatus",
   368  		},
   369  		{
   370  			name:  "input consists of words without an initial capital",
   371  			input: "accountStatus",
   372  			want:  "AccountStatus",
   373  		},
   374  		{
   375  			name:  "input consists of words without capital letters",
   376  			input: "accountstatus",
   377  			want:  "Accountstatus",
   378  		},
   379  		{
   380  			name:  "input lower_snake_case",
   381  			input: "account_status",
   382  			want:  "AccountStatus",
   383  		},
   384  		{
   385  			name:  "input UPPER_SNAKE_CASE",
   386  			input: "ACCOUNT_STATUS",
   387  			want:  "AccountStatus",
   388  		},
   389  	}
   390  
   391  	for _, test := range tests {
   392  		test := test
   393  		t.Run(test.name, func(t *testing.T) {
   394  			got := strs.ToUpperCamelCase(test.input)
   395  
   396  			if !reflect.DeepEqual(got, test.want) {
   397  				t.Errorf("got %v, but want %v", got, test.want)
   398  			}
   399  		})
   400  	}
   401  }
   402  
   403  func TestToLowerCamelCase(t *testing.T) {
   404  	tests := []struct {
   405  		name  string
   406  		input string
   407  		want  string
   408  	}{
   409  		{
   410  			name:  "input consists of one word",
   411  			input: "account",
   412  			want:  "account",
   413  		},
   414  		{
   415  			name:  "input consists of words with an initial capital",
   416  			input: "AccountStatus",
   417  			want:  "accountStatus",
   418  		},
   419  		{
   420  			name:  "input consists of words without an initial capital",
   421  			input: "accountStatus",
   422  			want:  "accountStatus",
   423  		},
   424  		{
   425  			name:  "input consists of words without capital letters",
   426  			input: "accountstatus",
   427  			want:  "accountstatus",
   428  		},
   429  		{
   430  			name:  "input lower_snake_case",
   431  			input: "account_status",
   432  			want:  "accountStatus",
   433  		},
   434  		{
   435  			name:  "input UPPER_SNAKE_CASE",
   436  			input: "ACCOUNT_STATUS",
   437  			want:  "accountStatus",
   438  		},
   439  	}
   440  
   441  	for _, test := range tests {
   442  		test := test
   443  		t.Run(test.name, func(t *testing.T) {
   444  			got := strs.ToLowerCamelCase(test.input)
   445  
   446  			if !reflect.DeepEqual(got, test.want) {
   447  				t.Errorf("got %v, but want %v", got, test.want)
   448  			}
   449  		})
   450  	}
   451  }
   452  
   453  func TestSplitSnakeCaseWord(t *testing.T) {
   454  	tests := []struct {
   455  		name  string
   456  		input string
   457  		want  []string
   458  	}{
   459  		{
   460  			name: "if s is empty, returns nil",
   461  		},
   462  		{
   463  			name:  "if s is not snake_case, returns nil",
   464  			input: "_not_snake",
   465  		},
   466  		{
   467  			name:  "input consists of one word",
   468  			input: "HELLO",
   469  			want: []string{
   470  				"HELLO",
   471  			},
   472  		},
   473  		{
   474  			name:  "input consists of multiple upper case words",
   475  			input: "REASON_FOR_ERROR",
   476  			want: []string{
   477  				"REASON",
   478  				"FOR",
   479  				"ERROR",
   480  			},
   481  		},
   482  		{
   483  			name:  "input consists of multiple lower case words",
   484  			input: "reason_for_error",
   485  			want: []string{
   486  				"reason",
   487  				"for",
   488  				"error",
   489  			},
   490  		},
   491  	}
   492  
   493  	for _, test := range tests {
   494  		test := test
   495  		t.Run(test.name, func(t *testing.T) {
   496  			got := strs.SplitSnakeCaseWord(test.input)
   497  			if !reflect.DeepEqual(got, test.want) {
   498  				t.Errorf("got %v, but want %v", got, test.want)
   499  			}
   500  		})
   501  	}
   502  }