Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

253 lines
6.4 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. }
  49. for _, i := range cases {
  50. in := i[0]
  51. out := i[1]
  52. result := ToSnake(in)
  53. if result != out {
  54. tb.Error("'" + in + "'('" + result + "' != '" + out + "')")
  55. }
  56. }
  57. }
  58. func TestToSnake(t *testing.T) { toSnake(t) }
  59. func BenchmarkToSnake(b *testing.B) {
  60. benchmarkSnakeTest(b, toSnake)
  61. }
  62. func toSnakeWithIgnore(tb testing.TB) {
  63. cases := [][]string{
  64. {"testCase", "test_case"},
  65. {"TestCase", "test_case"},
  66. {"Test Case", "test_case"},
  67. {" Test Case", "test_case"},
  68. {"Test Case ", "test_case"},
  69. {" Test Case ", "test_case"},
  70. {"test", "test"},
  71. {"test_case", "test_case"},
  72. {"Test", "test"},
  73. {"", ""},
  74. {"ManyManyWords", "many_many_words"},
  75. {"manyManyWords", "many_many_words"},
  76. {"AnyKind of_string", "any_kind_of_string"},
  77. {"numbers2and55with000", "numbers_2_and_55_with_000"},
  78. {"JSONData", "json_data"},
  79. {"AwesomeAcitvity.UserID", "awesome_acitvity.user_id", "."},
  80. {"AwesomeAcitvity.User.Id", "awesome_acitvity.user.id", "."},
  81. {"AwesomeUsername@Awesome.Com", "awesome_username@awesome._com", "@"},
  82. }
  83. for _, i := range cases {
  84. in := i[0]
  85. out := i[1]
  86. var ignore uint8
  87. if len(i) == 3 {
  88. ignore = i[2][0]
  89. }
  90. result := ToSnakeWithIgnore(in, ignore)
  91. if result != out {
  92. tb.Error("'" + in + "'('" + result + "' != '" + out + "')")
  93. }
  94. }
  95. }
  96. func TestToSnakeWithIgnore(t *testing.T) { toSnakeWithIgnore(t) }
  97. func BenchmarkToSnakeWithIgnore(b *testing.B) {
  98. benchmarkSnakeTest(b, toSnakeWithIgnore)
  99. }
  100. func toDelimited(tb testing.TB) {
  101. cases := [][]string{
  102. {"testCase", "test@case"},
  103. {"TestCase", "test@case"},
  104. {"Test Case", "test@case"},
  105. {" Test Case", "test@case"},
  106. {"Test Case ", "test@case"},
  107. {" Test Case ", "test@case"},
  108. {"test", "test"},
  109. {"test_case", "test@case"},
  110. {"Test", "test"},
  111. {"", ""},
  112. {"ManyManyWords", "many@many@words"},
  113. {"manyManyWords", "many@many@words"},
  114. {"AnyKind of_string", "any@kind@of@string"},
  115. {"numbers2and55with000", "numbers@2@and@55@with@000"},
  116. {"JSONData", "json@data"},
  117. {"userID", "user@id"},
  118. {"AAAbbb", "aa@abbb"},
  119. {"test-case", "test@case"},
  120. }
  121. for _, i := range cases {
  122. in := i[0]
  123. out := i[1]
  124. result := ToDelimited(in, '@')
  125. if result != out {
  126. tb.Error("'" + in + "' ('" + result + "' != '" + out + "')")
  127. }
  128. }
  129. }
  130. func TestToDelimited(t *testing.T) { toDelimited(t) }
  131. func BenchmarkToDelimited(b *testing.B) {
  132. benchmarkSnakeTest(b, toDelimited)
  133. }
  134. func toScreamingSnake(tb testing.TB) {
  135. cases := [][]string{
  136. {"testCase", "TEST_CASE"},
  137. }
  138. for _, i := range cases {
  139. in := i[0]
  140. out := i[1]
  141. result := ToScreamingSnake(in)
  142. if result != out {
  143. tb.Error("'" + result + "' != '" + out + "'")
  144. }
  145. }
  146. }
  147. func TestToScreamingSnake(t *testing.T) { toScreamingSnake(t) }
  148. func BenchmarkToScreamingSnake(b *testing.B) {
  149. benchmarkSnakeTest(b, toScreamingSnake)
  150. }
  151. func toKebab(tb testing.TB) {
  152. cases := [][]string{
  153. {"testCase", "test-case"},
  154. }
  155. for _, i := range cases {
  156. in := i[0]
  157. out := i[1]
  158. result := ToKebab(in)
  159. if result != out {
  160. tb.Error("'" + result + "' != '" + out + "'")
  161. }
  162. }
  163. }
  164. func TestToKebab(t *testing.T) { toKebab(t) }
  165. func BenchmarkToKebab(b *testing.B) {
  166. benchmarkSnakeTest(b, toKebab)
  167. }
  168. func toScreamingKebab(tb testing.TB) {
  169. cases := [][]string{
  170. {"testCase", "TEST-CASE"},
  171. }
  172. for _, i := range cases {
  173. in := i[0]
  174. out := i[1]
  175. result := ToScreamingKebab(in)
  176. if result != out {
  177. tb.Error("'" + result + "' != '" + out + "'")
  178. }
  179. }
  180. }
  181. func TestToScreamingKebab(t *testing.T) { toScreamingKebab(t) }
  182. func BenchmarkToScreamingKebab(b *testing.B) {
  183. benchmarkSnakeTest(b, toScreamingKebab)
  184. }
  185. func toScreamingDelimited(tb testing.TB) {
  186. cases := [][]string{
  187. {"testCase", "TEST.CASE"},
  188. }
  189. for _, i := range cases {
  190. in := i[0]
  191. out := i[1]
  192. result := ToScreamingDelimited(in, '.', 0, true)
  193. if result != out {
  194. tb.Error("'" + result + "' != '" + out + "'")
  195. }
  196. }
  197. }
  198. func TestToScreamingDelimited(t *testing.T) { toScreamingDelimited(t) }
  199. func BenchmarkToScreamingDelimited(b *testing.B) {
  200. benchmarkSnakeTest(b, toScreamingDelimited)
  201. }
  202. func toScreamingDelimitedWithIgnore(tb testing.TB) {
  203. cases := [][]string{
  204. {"AnyKind of_string", "ANY.KIND OF.STRING", ".", " "},
  205. }
  206. for _, i := range cases {
  207. in := i[0]
  208. out := i[1]
  209. delimiter := uint8(i[2][0])
  210. ignore := uint8(i[3][0])
  211. result := ToScreamingDelimited(in, delimiter, ignore, true)
  212. if result != out {
  213. tb.Error("'" + result + "' != '" + out + "'")
  214. }
  215. }
  216. }
  217. func TestToScreamingDelimitedWithIgnore(t *testing.T) { toScreamingDelimitedWithIgnore(t) }
  218. func BenchmarkToScreamingDelimitedWithIgnore(b *testing.B) {
  219. benchmarkSnakeTest(b, toScreamingDelimitedWithIgnore)
  220. }
  221. func benchmarkSnakeTest(b *testing.B, fn func(testing.TB)) {
  222. for n := 0; n < b.N; n++ {
  223. fn(b)
  224. }
  225. }