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.

198 lines
5.1 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 TestToSnake(t *testing.T) {
  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. t.Error("'" + in + "'('" + result + "' != '" + out + "')")
  55. }
  56. }
  57. }
  58. func TestToSnakeWithIgnore(t *testing.T) {
  59. cases := [][]string{
  60. {"testCase", "test_case"},
  61. {"TestCase", "test_case"},
  62. {"Test Case", "test_case"},
  63. {" Test Case", "test_case"},
  64. {"Test Case ", "test_case"},
  65. {" Test Case ", "test_case"},
  66. {"test", "test"},
  67. {"test_case", "test_case"},
  68. {"Test", "test"},
  69. {"", ""},
  70. {"ManyManyWords", "many_many_words"},
  71. {"manyManyWords", "many_many_words"},
  72. {"AnyKind of_string", "any_kind_of_string"},
  73. {"numbers2and55with000", "numbers_2_and_55_with_000"},
  74. {"JSONData", "json_data"},
  75. {"AwesomeAcitvity.UserID", "awesome_acitvity.user_id", "."},
  76. {"AwesomeAcitvity.User.Id", "awesome_acitvity.user.id", "."},
  77. {"AwesomeUsername@Awesome.Com", "awesome_username@awesome._com", "@"},
  78. }
  79. for _, i := range cases {
  80. in := i[0]
  81. out := i[1]
  82. var ignore uint8
  83. if len(i) == 3 {
  84. ignore = i[2][0]
  85. }
  86. result := ToSnakeWithIgnore(in, ignore)
  87. if result != out {
  88. t.Error("'" + in + "'('" + result + "' != '" + out + "')")
  89. }
  90. }
  91. }
  92. func TestToDelimited(t *testing.T) {
  93. cases := [][]string{
  94. {"testCase", "test@case"},
  95. {"TestCase", "test@case"},
  96. {"Test Case", "test@case"},
  97. {" Test Case", "test@case"},
  98. {"Test Case ", "test@case"},
  99. {" Test Case ", "test@case"},
  100. {"test", "test"},
  101. {"test_case", "test@case"},
  102. {"Test", "test"},
  103. {"", ""},
  104. {"ManyManyWords", "many@many@words"},
  105. {"manyManyWords", "many@many@words"},
  106. {"AnyKind of_string", "any@kind@of@string"},
  107. {"numbers2and55with000", "numbers@2@and@55@with@000"},
  108. {"JSONData", "json@data"},
  109. {"userID", "user@id"},
  110. {"AAAbbb", "aa@abbb"},
  111. {"test-case", "test@case"},
  112. }
  113. for _, i := range cases {
  114. in := i[0]
  115. out := i[1]
  116. result := ToDelimited(in, '@')
  117. if result != out {
  118. t.Error("'" + in + "' ('" + result + "' != '" + out + "')")
  119. }
  120. }
  121. }
  122. func TestToScreamingSnake(t *testing.T) {
  123. cases := [][]string{
  124. {"testCase", "TEST_CASE"},
  125. }
  126. for _, i := range cases {
  127. in := i[0]
  128. out := i[1]
  129. result := ToScreamingSnake(in)
  130. if result != out {
  131. t.Error("'" + result + "' != '" + out + "'")
  132. }
  133. }
  134. }
  135. func TestToKebab(t *testing.T) {
  136. cases := [][]string{
  137. {"testCase", "test-case"},
  138. }
  139. for _, i := range cases {
  140. in := i[0]
  141. out := i[1]
  142. result := ToKebab(in)
  143. if result != out {
  144. t.Error("'" + result + "' != '" + out + "'")
  145. }
  146. }
  147. }
  148. func TestToScreamingKebab(t *testing.T) {
  149. cases := [][]string{
  150. {"testCase", "TEST-CASE"},
  151. }
  152. for _, i := range cases {
  153. in := i[0]
  154. out := i[1]
  155. result := ToScreamingKebab(in)
  156. if result != out {
  157. t.Error("'" + result + "' != '" + out + "'")
  158. }
  159. }
  160. }
  161. func TestToScreamingDelimited(t *testing.T) {
  162. cases := [][]string{
  163. {"testCase", "TEST.CASE"},
  164. }
  165. for _, i := range cases {
  166. in := i[0]
  167. out := i[1]
  168. result := ToScreamingDelimited(in, '.', 0, true)
  169. if result != out {
  170. t.Error("'" + result + "' != '" + out + "'")
  171. }
  172. }
  173. }
  174. func TestToScreamingDelimitedWithIgnore(t *testing.T) {
  175. cases := [][]string{
  176. {"AnyKind of_string", "ANY.KIND OF.STRING", ".", " "},
  177. }
  178. for _, i := range cases {
  179. in := i[0]
  180. out := i[1]
  181. delimiter := uint8(i[2][0])
  182. ignore := uint8(i[3][0])
  183. result := ToScreamingDelimited(in, delimiter, ignore, true)
  184. if result != out {
  185. t.Error("'" + result + "' != '" + out + "'")
  186. }
  187. }
  188. }