github.com/whiteCcinn/protobuf-go@v1.0.9/internal/strs/strings_test.go (about)

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package strs
     6  
     7  import (
     8  	"strconv"
     9  	"testing"
    10  )
    11  
    12  func TestGoCamelCase(t *testing.T) {
    13  	tests := []struct {
    14  		in, want string
    15  	}{
    16  		{"", ""},
    17  		{"one", "One"},
    18  		{"one_two", "OneTwo"},
    19  		{"_my_field_name_2", "XMyFieldName_2"},
    20  		{"Something_Capped", "Something_Capped"},
    21  		{"my_Name", "My_Name"},
    22  		{"OneTwo", "OneTwo"},
    23  		{"_", "X"},
    24  		{"_a_", "XA_"},
    25  		{"one.two", "OneTwo"},
    26  		{"one.Two", "One_Two"},
    27  		{"one_two.three_four", "OneTwoThreeFour"},
    28  		{"one_two.Three_four", "OneTwo_ThreeFour"},
    29  		{"_one._two", "XOne_XTwo"},
    30  		{"SCREAMING_SNAKE_CASE", "SCREAMING_SNAKE_CASE"},
    31  		{"double__underscore", "Double_Underscore"},
    32  		{"camelCase", "CamelCase"},
    33  		{"deeplink", "Deeplink"},
    34  		{"deep_link", "DeepLink"},
    35  		{"go2proto", "Go2Proto"},
    36  		{"世界", "世界"},
    37  		{"x世界", "X世界"},
    38  		{"foo_bar世界", "FooBar世界"},
    39  	}
    40  	for _, tc := range tests {
    41  		if got := GoCamelCase(tc.in); got != tc.want {
    42  			t.Errorf("GoCamelCase(%q) = %q, want %q", tc.in, got, tc.want)
    43  		}
    44  	}
    45  }
    46  
    47  func TestGoSanitized(t *testing.T) {
    48  	tests := []struct {
    49  		in, want string
    50  	}{
    51  		{"", "_"},
    52  		{"boo", "boo"},
    53  		{"Boo", "Boo"},
    54  		{"ßoo", "ßoo"},
    55  		{"default", "_default"},
    56  		{"hello", "hello"},
    57  		{"hello-world!!", "hello_world__"},
    58  		{"hello-\xde\xad\xbe\xef\x00", "hello_____"},
    59  		{"hello 世界", "hello_世界"},
    60  		{"世界", "世界"},
    61  	}
    62  	for _, tc := range tests {
    63  		if got := GoSanitized(tc.in); got != tc.want {
    64  			t.Errorf("GoSanitized(%q) = %q, want %q", tc.in, got, tc.want)
    65  		}
    66  	}
    67  }
    68  
    69  func TestName(t *testing.T) {
    70  	tests := []struct {
    71  		in                string
    72  		inEnumPrefix      string
    73  		wantMapEntry      string
    74  		wantEnumValue     string
    75  		wantTrimValue     string
    76  		wantJSONCamelCase string
    77  		wantJSONSnakeCase string
    78  	}{{
    79  		in:                "abc",
    80  		inEnumPrefix:      "",
    81  		wantMapEntry:      "AbcEntry",
    82  		wantEnumValue:     "Abc",
    83  		wantTrimValue:     "abc",
    84  		wantJSONCamelCase: "abc",
    85  		wantJSONSnakeCase: "abc",
    86  	}, {
    87  		in:                "foo_baR_",
    88  		inEnumPrefix:      "foo_bar",
    89  		wantMapEntry:      "FooBaREntry",
    90  		wantEnumValue:     "FooBar",
    91  		wantTrimValue:     "foo_baR_",
    92  		wantJSONCamelCase: "fooBaR",
    93  		wantJSONSnakeCase: "foo_ba_r_",
    94  	}, {
    95  		in:                "snake_caseCamelCase",
    96  		inEnumPrefix:      "snakecasecamel",
    97  		wantMapEntry:      "SnakeCaseCamelCaseEntry",
    98  		wantEnumValue:     "SnakeCasecamelcase",
    99  		wantTrimValue:     "Case",
   100  		wantJSONCamelCase: "snakeCaseCamelCase",
   101  		wantJSONSnakeCase: "snake_case_camel_case",
   102  	}, {
   103  		in:                "FiZz_BuZz",
   104  		inEnumPrefix:      "fizz",
   105  		wantMapEntry:      "FiZzBuZzEntry",
   106  		wantEnumValue:     "FizzBuzz",
   107  		wantTrimValue:     "BuZz",
   108  		wantJSONCamelCase: "FiZzBuZz",
   109  		wantJSONSnakeCase: "_fi_zz__bu_zz",
   110  	}}
   111  
   112  	for _, tt := range tests {
   113  		if got := MapEntryName(tt.in); got != tt.wantMapEntry {
   114  			t.Errorf("MapEntryName(%q) = %q, want %q", tt.in, got, tt.wantMapEntry)
   115  		}
   116  		if got := EnumValueName(tt.in); got != tt.wantEnumValue {
   117  			t.Errorf("EnumValueName(%q) = %q, want %q", tt.in, got, tt.wantEnumValue)
   118  		}
   119  		if got := TrimEnumPrefix(tt.in, tt.inEnumPrefix); got != tt.wantTrimValue {
   120  			t.Errorf("ErimEnumPrefix(%q, %q) = %q, want %q", tt.in, tt.inEnumPrefix, got, tt.wantTrimValue)
   121  		}
   122  		if got := JSONCamelCase(tt.in); got != tt.wantJSONCamelCase {
   123  			t.Errorf("JSONCamelCase(%q) = %q, want %q", tt.in, got, tt.wantJSONCamelCase)
   124  		}
   125  		if got := JSONSnakeCase(tt.in); got != tt.wantJSONSnakeCase {
   126  			t.Errorf("JSONSnakeCase(%q) = %q, want %q", tt.in, got, tt.wantJSONSnakeCase)
   127  		}
   128  	}
   129  }
   130  
   131  var (
   132  	srcString = "1234"
   133  	srcBytes  = []byte(srcString)
   134  	dst       uint64
   135  )
   136  
   137  func BenchmarkCast(b *testing.B) {
   138  	b.Run("Ideal", func(b *testing.B) {
   139  		b.ReportAllocs()
   140  		for i := 0; i < b.N; i++ {
   141  			dst, _ = strconv.ParseUint(srcString, 0, 64)
   142  		}
   143  		if dst != 1234 {
   144  			b.Errorf("got %d, want %s", dst, srcString)
   145  		}
   146  	})
   147  	b.Run("Copy", func(b *testing.B) {
   148  		b.ReportAllocs()
   149  		for i := 0; i < b.N; i++ {
   150  			dst, _ = strconv.ParseUint(string(srcBytes), 0, 64)
   151  		}
   152  		if dst != 1234 {
   153  			b.Errorf("got %d, want %s", dst, srcString)
   154  		}
   155  	})
   156  	b.Run("Cast", func(b *testing.B) {
   157  		b.ReportAllocs()
   158  		for i := 0; i < b.N; i++ {
   159  			dst, _ = strconv.ParseUint(UnsafeString(srcBytes), 0, 64)
   160  		}
   161  		if dst != 1234 {
   162  			b.Errorf("got %d, want %s", dst, srcString)
   163  		}
   164  	})
   165  }