strings_test.go 4.3 KB

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