Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

267 linhas
6.7 KiB

  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2015 Ian Coleman
  5. * Copyright (c) 2018 Ma_124, <github.com/Ma124>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, Subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or Substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. package strcase
  26. import (
  27. "testing"
  28. )
  29. func toSnake(tb testing.TB) {
  30. cases := [][]string{
  31. {"testCase", "test_case"},
  32. {"TestCase", "test_case"},
  33. {"Test Case", "test_case"},
  34. {" Test Case", "test_case"},
  35. {"Test Case ", "test_case"},
  36. {" Test Case ", "test_case"},
  37. {"test", "test"},
  38. {"test_case", "test_case"},
  39. {"Test", "test"},
  40. {"", ""},
  41. {"ManyManyWords", "many_many_words"},
  42. {"manyManyWords", "many_many_words"},
  43. {"AnyKind of_string", "any_kind_of_string"},
  44. {"numbers2and55with000", "numbers_2_and_55_with_000"},
  45. {"JSONData", "json_data"},
  46. {"userID", "user_id"},
  47. {"AAAbbb", "aa_abbb"},
  48. {"1A2", "1_a_2"},
  49. {"A1B", "a_1_b"},
  50. {"A1A2A3", "a_1_a_2_a_3"},
  51. {"A1 A2 A3", "a_1_a_2_a_3"},
  52. {"AB1AB2AB3", "ab_1_ab_2_ab_3"},
  53. {"AB1 AB2 AB3", "ab_1_ab_2_ab_3"},
  54. }
  55. for _, i := range cases {
  56. in := i[0]
  57. out := i[1]
  58. result := ToSnake(in)
  59. if result != out {
  60. tb.Errorf("%q (%q != %q)", in, result, out)
  61. }
  62. }
  63. }
  64. func TestToSnake(t *testing.T) { toSnake(t) }
  65. func BenchmarkToSnake(b *testing.B) {
  66. benchmarkSnakeTest(b, toSnake)
  67. }
  68. func toSnakeWithIgnore(tb testing.TB) {
  69. cases := [][]string{
  70. {"testCase", "test_case"},
  71. {"TestCase", "test_case"},
  72. {"Test Case", "test_case"},
  73. {" Test Case", "test_case"},
  74. {"Test Case ", "test_case"},
  75. {" Test Case ", "test_case"},
  76. {"test", "test"},
  77. {"test_case", "test_case"},
  78. {"Test", "test"},
  79. {"", ""},
  80. {"ManyManyWords", "many_many_words"},
  81. {"manyManyWords", "many_many_words"},
  82. {"AnyKind of_string", "any_kind_of_string"},
  83. {"numbers2and55with000", "numbers_2_and_55_with_000"},
  84. {"JSONData", "json_data"},
  85. {"AwesomeActivity.UserID", "awesome_activity.user_id", "."},
  86. {"AwesomeActivity.User.Id", "awesome_activity.user.id", "."},
  87. {"AwesomeUsername@Awesome.Com", "awesome_username@awesome.com", "@"},
  88. }
  89. for _, i := range cases {
  90. in := i[0]
  91. out := i[1]
  92. var ignore uint8
  93. if len(i) == 3 {
  94. ignore = i[2][0]
  95. }
  96. result := ToSnakeWithIgnore(in, ignore)
  97. if result != out {
  98. istr := ""
  99. if len(i) == 3 {
  100. istr = " ignoring '" + i[2] + "'"
  101. }
  102. tb.Errorf("%q (%q != %q%s)", in, result, out, istr)
  103. }
  104. }
  105. }
  106. func TestToSnakeWithIgnore(t *testing.T) { toSnakeWithIgnore(t) }
  107. func BenchmarkToSnakeWithIgnore(b *testing.B) {
  108. benchmarkSnakeTest(b, toSnakeWithIgnore)
  109. }
  110. func toDelimited(tb testing.TB) {
  111. cases := [][]string{
  112. {"testCase", "test@case"},
  113. {"TestCase", "test@case"},
  114. {"Test Case", "test@case"},
  115. {" Test Case", "test@case"},
  116. {"Test Case ", "test@case"},
  117. {" Test Case ", "test@case"},
  118. {"test", "test"},
  119. {"test_case", "test@case"},
  120. {"Test", "test"},
  121. {"", ""},
  122. {"ManyManyWords", "many@many@words"},
  123. {"manyManyWords", "many@many@words"},
  124. {"AnyKind of_string", "any@kind@of@string"},
  125. {"numbers2and55with000", "numbers@2@and@55@with@000"},
  126. {"JSONData", "json@data"},
  127. {"userID", "user@id"},
  128. {"AAAbbb", "aa@abbb"},
  129. {"test-case", "test@case"},
  130. }
  131. for _, i := range cases {
  132. in := i[0]
  133. out := i[1]
  134. result := ToDelimited(in, '@')
  135. if result != out {
  136. tb.Errorf("%q (%q != %q)", in, result, out)
  137. }
  138. }
  139. }
  140. func TestToDelimited(t *testing.T) { toDelimited(t) }
  141. func BenchmarkToDelimited(b *testing.B) {
  142. benchmarkSnakeTest(b, toDelimited)
  143. }
  144. func toScreamingSnake(tb testing.TB) {
  145. cases := [][]string{
  146. {"testCase", "TEST_CASE"},
  147. }
  148. for _, i := range cases {
  149. in := i[0]
  150. out := i[1]
  151. result := ToScreamingSnake(in)
  152. if result != out {
  153. tb.Errorf("%q (%q != %q)", in, result, out)
  154. }
  155. }
  156. }
  157. func TestToScreamingSnake(t *testing.T) { toScreamingSnake(t) }
  158. func BenchmarkToScreamingSnake(b *testing.B) {
  159. benchmarkSnakeTest(b, toScreamingSnake)
  160. }
  161. func toKebab(tb testing.TB) {
  162. cases := [][]string{
  163. {"testCase", "test-case"},
  164. }
  165. for _, i := range cases {
  166. in := i[0]
  167. out := i[1]
  168. result := ToKebab(in)
  169. if result != out {
  170. tb.Errorf("%q (%q != %q)", in, result, out)
  171. }
  172. }
  173. }
  174. func TestToKebab(t *testing.T) { toKebab(t) }
  175. func BenchmarkToKebab(b *testing.B) {
  176. benchmarkSnakeTest(b, toKebab)
  177. }
  178. func toScreamingKebab(tb testing.TB) {
  179. cases := [][]string{
  180. {"testCase", "TEST-CASE"},
  181. }
  182. for _, i := range cases {
  183. in := i[0]
  184. out := i[1]
  185. result := ToScreamingKebab(in)
  186. if result != out {
  187. tb.Errorf("%q (%q != %q)", in, result, out)
  188. }
  189. }
  190. }
  191. func TestToScreamingKebab(t *testing.T) { toScreamingKebab(t) }
  192. func BenchmarkToScreamingKebab(b *testing.B) {
  193. benchmarkSnakeTest(b, toScreamingKebab)
  194. }
  195. func toScreamingDelimited(tb testing.TB) {
  196. cases := [][]string{
  197. {"testCase", "TEST.CASE"},
  198. }
  199. for _, i := range cases {
  200. in := i[0]
  201. out := i[1]
  202. result := ToScreamingDelimited(in, '.', 0, true)
  203. if result != out {
  204. tb.Errorf("%q (%q != %q)", in, result, out)
  205. }
  206. }
  207. }
  208. func TestToScreamingDelimited(t *testing.T) { toScreamingDelimited(t) }
  209. func BenchmarkToScreamingDelimited(b *testing.B) {
  210. benchmarkSnakeTest(b, toScreamingDelimited)
  211. }
  212. func toScreamingDelimitedWithIgnore(tb testing.TB) {
  213. cases := [][]string{
  214. {"AnyKind of_string", "ANY.KIND OF.STRING", ".", " "},
  215. }
  216. for _, i := range cases {
  217. in := i[0]
  218. out := i[1]
  219. delimiter := i[2][0]
  220. ignore := i[3][0]
  221. result := ToScreamingDelimited(in, delimiter, ignore, true)
  222. if result != out {
  223. istr := ""
  224. if len(i) == 4 {
  225. istr = " ignoring '" + i[3] + "'"
  226. }
  227. tb.Errorf("%q (%q != %q%s)", in, result, out, istr)
  228. }
  229. }
  230. }
  231. func TestToScreamingDelimitedWithIgnore(t *testing.T) { toScreamingDelimitedWithIgnore(t) }
  232. func BenchmarkToScreamingDelimitedWithIgnore(b *testing.B) {
  233. benchmarkSnakeTest(b, toScreamingDelimitedWithIgnore)
  234. }
  235. func benchmarkSnakeTest(b *testing.B, fn func(testing.TB)) {
  236. for n := 0; n < b.N; n++ {
  237. fn(b)
  238. }
  239. }