istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/util/strcase/camelcase_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package strcase
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/onsi/gomega"
    21  )
    22  
    23  func TestCamelCase(t *testing.T) {
    24  	cases := map[string]string{
    25  		"":              "",
    26  		"foo":           "Foo",
    27  		"foobar":        "Foobar",
    28  		"fooBar":        "FooBar",
    29  		"foo_bar":       "FooBar",
    30  		"foo-bar":       "FooBar",
    31  		"foo_Bar":       "FooBar",
    32  		"foo9bar":       "Foo9Bar",
    33  		"HTTP-API-Spec": "HTTPAPISpec",
    34  		"http-api-spec": "HttpApiSpec",
    35  		"_foo":          "XFoo",
    36  		"-foo":          "XFoo",
    37  		"_Foo":          "XFoo",
    38  		"-Foo":          "XFoo",
    39  	}
    40  
    41  	for k, v := range cases {
    42  		t.Run(k, func(t *testing.T) {
    43  			g := NewWithT(t)
    44  
    45  			a := CamelCase(k)
    46  			g.Expect(a).To(Equal(v))
    47  		})
    48  	}
    49  }
    50  
    51  func TestCamelCaseToKebabCase(t *testing.T) {
    52  	cases := map[string]string{
    53  		"":                   "",
    54  		"Foo":                "foo",
    55  		"FooBar":             "foo-bar",
    56  		"foo9bar":            "foo9bar",
    57  		"HTTPAPISpec":        "http-api-spec",
    58  		"HTTPAPISpecBinding": "http-api-spec-binding",
    59  	}
    60  
    61  	for k, v := range cases {
    62  		t.Run(k, func(t *testing.T) {
    63  			g := NewWithT(t)
    64  
    65  			a := CamelCaseToKebabCase(k)
    66  			g.Expect(a).To(Equal(v))
    67  		})
    68  	}
    69  }
    70  
    71  func TestCamelCaseWithSeparator(t *testing.T) {
    72  	type args struct {
    73  		n   string
    74  		sep string
    75  	}
    76  	tests := []struct {
    77  		name string
    78  		args args
    79  		want string
    80  	}{
    81  		{
    82  			name: "foo_bar",
    83  			args: args{
    84  				n:   "foo_bar",
    85  				sep: "_",
    86  			},
    87  			want: "FooBar",
    88  		},
    89  		{
    90  			name: "foo-bar",
    91  			args: args{
    92  				n:   "foo-bar",
    93  				sep: "-",
    94  			},
    95  			want: "FooBar",
    96  		},
    97  		{
    98  			name: "foo9bar",
    99  			args: args{
   100  				n:   "foo9bar",
   101  				sep: "9",
   102  			},
   103  			want: "FooBar",
   104  		},
   105  		{
   106  			name: "foo/bar",
   107  			args: args{
   108  				n:   "foo/bar",
   109  				sep: "/",
   110  			},
   111  			want: "FooBar",
   112  		},
   113  		{
   114  			name: "foobar",
   115  			args: args{
   116  				n:   "foobar",
   117  				sep: "",
   118  			},
   119  			want: "FOOBAR",
   120  		},
   121  	}
   122  	for _, tt := range tests {
   123  		t.Run(tt.name, func(t *testing.T) {
   124  			if got := CamelCaseWithSeparator(tt.args.n, tt.args.sep); got != tt.want {
   125  				t.Errorf("CamelCaseWithSeparator() = %v, want %v", got, tt.want)
   126  			}
   127  		})
   128  	}
   129  }
   130  
   131  func Test_isWordSeparator(t *testing.T) {
   132  	tests := []struct {
   133  		name string
   134  		c    byte
   135  		want bool
   136  	}{
   137  		{
   138  			name: "_foo",
   139  			c:    '_',
   140  			want: true,
   141  		},
   142  		{
   143  			name: "-foo",
   144  			c:    '-',
   145  			want: true,
   146  		},
   147  		{
   148  			name: "foo",
   149  			c:    'f',
   150  			want: false,
   151  		},
   152  	}
   153  	for _, tt := range tests {
   154  		t.Run(tt.name, func(t *testing.T) {
   155  			if got := isWordSeparator(tt.c); got != tt.want {
   156  				t.Errorf("isWordSeparator() = %v, want %v", got, tt.want)
   157  			}
   158  		})
   159  	}
   160  }
   161  
   162  func Test_isASCIILower(t *testing.T) {
   163  	tests := []struct {
   164  		name string
   165  		c    byte
   166  		want bool
   167  	}{
   168  		{
   169  			name: "A",
   170  			c:    'A',
   171  			want: false,
   172  		},
   173  		{
   174  			name: "b",
   175  			c:    'b',
   176  			want: true,
   177  		},
   178  	}
   179  	for _, tt := range tests {
   180  		t.Run(tt.name, func(t *testing.T) {
   181  			if got := isASCIILower(tt.c); got != tt.want {
   182  				t.Errorf("isASCIILower() = %v, want %v", got, tt.want)
   183  			}
   184  		})
   185  	}
   186  }
   187  
   188  func Test_isASCIIDigit(t *testing.T) {
   189  	tests := []struct {
   190  		name string
   191  		c    byte
   192  		want bool
   193  	}{
   194  		{
   195  			name: "A",
   196  			c:    'A',
   197  			want: false,
   198  		},
   199  		{
   200  			name: "1",
   201  			c:    '1',
   202  			want: true,
   203  		},
   204  	}
   205  	for _, tt := range tests {
   206  		t.Run(tt.name, func(t *testing.T) {
   207  			if got := isASCIIDigit(tt.c); got != tt.want {
   208  				t.Errorf("isASCIIDigit() = %v, want %v", got, tt.want)
   209  			}
   210  		})
   211  	}
   212  }