You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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