...

Source file src/google.golang.org/protobuf/internal/strs/strings_test.go

Documentation: google.golang.org/protobuf/internal/strs

     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  		{"go2proto", "Go2Proto"},
    34  		{"世界", "世界"},
    35  		{"x世界", "X世界"},
    36  		{"foo_bar世界", "FooBar世界"},
    37  	}
    38  	for _, tc := range tests {
    39  		if got := GoCamelCase(tc.in); got != tc.want {
    40  			t.Errorf("GoCamelCase(%q) = %q, want %q", tc.in, got, tc.want)
    41  		}
    42  	}
    43  }
    44  
    45  func TestGoSanitized(t *testing.T) {
    46  	tests := []struct {
    47  		in, want string
    48  	}{
    49  		{"", "_"},
    50  		{"boo", "boo"},
    51  		{"Boo", "Boo"},
    52  		{"ßoo", "ßoo"},
    53  		{"default", "_default"},
    54  		{"hello", "hello"},
    55  		{"hello-world!!", "hello_world__"},
    56  		{"hello-\xde\xad\xbe\xef\x00", "hello_____"},
    57  		{"hello 世界", "hello_世界"},
    58  		{"世界", "世界"},
    59  	}
    60  	for _, tc := range tests {
    61  		if got := GoSanitized(tc.in); got != tc.want {
    62  			t.Errorf("GoSanitized(%q) = %q, want %q", tc.in, got, tc.want)
    63  		}
    64  	}
    65  }
    66  
    67  func TestName(t *testing.T) {
    68  	tests := []struct {
    69  		in                string
    70  		inEnumPrefix      string
    71  		wantMapEntry      string
    72  		wantEnumValue     string
    73  		wantTrimValue     string
    74  		wantJSONCamelCase string
    75  		wantJSONSnakeCase string
    76  	}{{
    77  		in:                "abc",
    78  		inEnumPrefix:      "",
    79  		wantMapEntry:      "AbcEntry",
    80  		wantEnumValue:     "Abc",
    81  		wantTrimValue:     "abc",
    82  		wantJSONCamelCase: "abc",
    83  		wantJSONSnakeCase: "abc",
    84  	}, {
    85  		in:                "foo_baR_",
    86  		inEnumPrefix:      "foo_bar",
    87  		wantMapEntry:      "FooBaREntry",
    88  		wantEnumValue:     "FooBar",
    89  		wantTrimValue:     "foo_baR_",
    90  		wantJSONCamelCase: "fooBaR",
    91  		wantJSONSnakeCase: "foo_ba_r_",
    92  	}, {
    93  		in:                "snake_caseCamelCase",
    94  		inEnumPrefix:      "snakecasecamel",
    95  		wantMapEntry:      "SnakeCaseCamelCaseEntry",
    96  		wantEnumValue:     "SnakeCasecamelcase",
    97  		wantTrimValue:     "Case",
    98  		wantJSONCamelCase: "snakeCaseCamelCase",
    99  		wantJSONSnakeCase: "snake_case_camel_case",
   100  	}, {
   101  		in:                "FiZz_BuZz",
   102  		inEnumPrefix:      "fizz",
   103  		wantMapEntry:      "FiZzBuZzEntry",
   104  		wantEnumValue:     "FizzBuzz",
   105  		wantTrimValue:     "BuZz",
   106  		wantJSONCamelCase: "FiZzBuZz",
   107  		wantJSONSnakeCase: "_fi_zz__bu_zz",
   108  	}}
   109  
   110  	for _, tt := range tests {
   111  		if got := MapEntryName(tt.in); got != tt.wantMapEntry {
   112  			t.Errorf("MapEntryName(%q) = %q, want %q", tt.in, got, tt.wantMapEntry)
   113  		}
   114  		if got := EnumValueName(tt.in); got != tt.wantEnumValue {
   115  			t.Errorf("EnumValueName(%q) = %q, want %q", tt.in, got, tt.wantEnumValue)
   116  		}
   117  		if got := TrimEnumPrefix(tt.in, tt.inEnumPrefix); got != tt.wantTrimValue {
   118  			t.Errorf("ErimEnumPrefix(%q, %q) = %q, want %q", tt.in, tt.inEnumPrefix, got, tt.wantTrimValue)
   119  		}
   120  		if got := JSONCamelCase(tt.in); got != tt.wantJSONCamelCase {
   121  			t.Errorf("JSONCamelCase(%q) = %q, want %q", tt.in, got, tt.wantJSONCamelCase)
   122  		}
   123  		if got := JSONSnakeCase(tt.in); got != tt.wantJSONSnakeCase {
   124  			t.Errorf("JSONSnakeCase(%q) = %q, want %q", tt.in, got, tt.wantJSONSnakeCase)
   125  		}
   126  	}
   127  }
   128  
   129  var (
   130  	srcString = "1234"
   131  	srcBytes  = []byte(srcString)
   132  	dst       uint64
   133  )
   134  
   135  func BenchmarkCast(b *testing.B) {
   136  	b.Run("Ideal", func(b *testing.B) {
   137  		b.ReportAllocs()
   138  		for i := 0; i < b.N; i++ {
   139  			dst, _ = strconv.ParseUint(srcString, 0, 64)
   140  		}
   141  		if dst != 1234 {
   142  			b.Errorf("got %d, want %s", dst, srcString)
   143  		}
   144  	})
   145  	b.Run("Copy", func(b *testing.B) {
   146  		b.ReportAllocs()
   147  		for i := 0; i < b.N; i++ {
   148  			dst, _ = strconv.ParseUint(string(srcBytes), 0, 64)
   149  		}
   150  		if dst != 1234 {
   151  			b.Errorf("got %d, want %s", dst, srcString)
   152  		}
   153  	})
   154  	b.Run("Cast", func(b *testing.B) {
   155  		b.ReportAllocs()
   156  		for i := 0; i < b.N; i++ {
   157  			dst, _ = strconv.ParseUint(UnsafeString(srcBytes), 0, 64)
   158  		}
   159  		if dst != 1234 {
   160  			b.Errorf("got %d, want %s", dst, srcString)
   161  		}
   162  	})
   163  }
   164  

View as plain text