strings_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package stringx
  2. import (
  3. "path"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestNotEmpty(t *testing.T) {
  8. cases := []struct {
  9. args []string
  10. expect bool
  11. }{
  12. {
  13. args: []string{"a", "b", "c"},
  14. expect: true,
  15. },
  16. {
  17. args: []string{"a", "", "c"},
  18. expect: false,
  19. },
  20. {
  21. args: []string{"a"},
  22. expect: true,
  23. },
  24. {
  25. args: []string{""},
  26. expect: false,
  27. },
  28. {
  29. args: []string{},
  30. expect: true,
  31. },
  32. }
  33. for _, each := range cases {
  34. t.Run(path.Join(each.args...), func(t *testing.T) {
  35. assert.Equal(t, each.expect, NotEmpty(each.args...))
  36. })
  37. }
  38. }
  39. func TestContainsString(t *testing.T) {
  40. cases := []struct {
  41. slice []string
  42. value string
  43. expect bool
  44. }{
  45. {[]string{"1"}, "1", true},
  46. {[]string{"1"}, "2", false},
  47. {[]string{"1", "2"}, "1", true},
  48. {[]string{"1", "2"}, "3", false},
  49. {nil, "3", false},
  50. {nil, "", false},
  51. }
  52. for _, each := range cases {
  53. t.Run(path.Join(each.slice...), func(t *testing.T) {
  54. actual := Contains(each.slice, each.value)
  55. assert.Equal(t, each.expect, actual)
  56. })
  57. }
  58. }
  59. func TestFilter(t *testing.T) {
  60. cases := []struct {
  61. input string
  62. ignores []rune
  63. expect string
  64. }{
  65. {``, nil, ``},
  66. {`abcd`, nil, `abcd`},
  67. {`ab,cd,ef`, []rune{','}, `abcdef`},
  68. {`ab, cd,ef`, []rune{',', ' '}, `abcdef`},
  69. {`ab, cd, ef`, []rune{',', ' '}, `abcdef`},
  70. {`ab, cd, ef, `, []rune{',', ' '}, `abcdef`},
  71. }
  72. for _, each := range cases {
  73. t.Run(each.input, func(t *testing.T) {
  74. actual := Filter(each.input, func(r rune) bool {
  75. for _, x := range each.ignores {
  76. if x == r {
  77. return true
  78. }
  79. }
  80. return false
  81. })
  82. assert.Equal(t, each.expect, actual)
  83. })
  84. }
  85. }
  86. func TestRemove(t *testing.T) {
  87. cases := []struct {
  88. input []string
  89. remove []string
  90. expect []string
  91. }{
  92. {
  93. input: []string{"a", "b", "a", "c"},
  94. remove: []string{"a", "b"},
  95. expect: []string{"c"},
  96. },
  97. {
  98. input: []string{"b", "c"},
  99. remove: []string{"a"},
  100. expect: []string{"b", "c"},
  101. },
  102. {
  103. input: []string{"b", "a", "c"},
  104. remove: []string{"a"},
  105. expect: []string{"b", "c"},
  106. },
  107. {
  108. input: []string{},
  109. remove: []string{"a"},
  110. expect: []string{},
  111. },
  112. }
  113. for _, each := range cases {
  114. t.Run(path.Join(each.input...), func(t *testing.T) {
  115. assert.ElementsMatch(t, each.expect, Remove(each.input, each.remove...))
  116. })
  117. }
  118. }
  119. func TestReverse(t *testing.T) {
  120. cases := []struct {
  121. input string
  122. expect string
  123. }{
  124. {
  125. input: "abcd",
  126. expect: "dcba",
  127. },
  128. {
  129. input: "",
  130. expect: "",
  131. },
  132. {
  133. input: "我爱中国",
  134. expect: "国中爱我",
  135. },
  136. }
  137. for _, each := range cases {
  138. t.Run(each.input, func(t *testing.T) {
  139. assert.Equal(t, each.expect, Reverse(each.input))
  140. })
  141. }
  142. }
  143. func TestSubstr(t *testing.T) {
  144. cases := []struct {
  145. input string
  146. start int
  147. stop int
  148. err error
  149. expect string
  150. }{
  151. {
  152. input: "abcdefg",
  153. start: 1,
  154. stop: 4,
  155. expect: "bcd",
  156. },
  157. {
  158. input: "我爱中国3000遍,even more",
  159. start: 1,
  160. stop: 9,
  161. expect: "爱中国3000遍",
  162. },
  163. {
  164. input: "abcdefg",
  165. start: -1,
  166. stop: 4,
  167. err: ErrInvalidStartPosition,
  168. expect: "",
  169. },
  170. {
  171. input: "abcdefg",
  172. start: 100,
  173. stop: 4,
  174. err: ErrInvalidStartPosition,
  175. expect: "",
  176. },
  177. {
  178. input: "abcdefg",
  179. start: 1,
  180. stop: -1,
  181. err: ErrInvalidStopPosition,
  182. expect: "",
  183. },
  184. {
  185. input: "abcdefg",
  186. start: 1,
  187. stop: 100,
  188. err: ErrInvalidStopPosition,
  189. expect: "",
  190. },
  191. }
  192. for _, each := range cases {
  193. t.Run(each.input, func(t *testing.T) {
  194. val, err := Substr(each.input, each.start, each.stop)
  195. assert.Equal(t, each.err, err)
  196. if err == nil {
  197. assert.Equal(t, each.expect, val)
  198. }
  199. })
  200. }
  201. }
  202. func TestTakeOne(t *testing.T) {
  203. cases := []struct {
  204. valid string
  205. or string
  206. expect string
  207. }{
  208. {"", "", ""},
  209. {"", "1", "1"},
  210. {"1", "", "1"},
  211. {"1", "2", "1"},
  212. }
  213. for _, each := range cases {
  214. t.Run(each.valid, func(t *testing.T) {
  215. actual := TakeOne(each.valid, each.or)
  216. assert.Equal(t, each.expect, actual)
  217. })
  218. }
  219. }
  220. func TestTakeWithPriority(t *testing.T) {
  221. tests := []struct {
  222. fns []func() string
  223. expect string
  224. }{
  225. {
  226. fns: []func() string{
  227. func() string {
  228. return "first"
  229. },
  230. func() string {
  231. return "second"
  232. },
  233. func() string {
  234. return "third"
  235. },
  236. },
  237. expect: "first",
  238. },
  239. {
  240. fns: []func() string{
  241. func() string {
  242. return ""
  243. },
  244. func() string {
  245. return "second"
  246. },
  247. func() string {
  248. return "third"
  249. },
  250. },
  251. expect: "second",
  252. },
  253. {
  254. fns: []func() string{
  255. func() string {
  256. return ""
  257. },
  258. func() string {
  259. return ""
  260. },
  261. func() string {
  262. return "third"
  263. },
  264. },
  265. expect: "third",
  266. },
  267. {
  268. fns: []func() string{
  269. func() string {
  270. return ""
  271. },
  272. func() string {
  273. return ""
  274. },
  275. func() string {
  276. return ""
  277. },
  278. },
  279. expect: "",
  280. },
  281. }
  282. for _, test := range tests {
  283. t.Run(RandId(), func(t *testing.T) {
  284. val := TakeWithPriority(test.fns...)
  285. assert.Equal(t, test.expect, val)
  286. })
  287. }
  288. }
  289. func TestUnion(t *testing.T) {
  290. first := []string{
  291. "one",
  292. "two",
  293. "three",
  294. }
  295. second := []string{
  296. "zero",
  297. "two",
  298. "three",
  299. "four",
  300. }
  301. union := Union(first, second)
  302. contains := func(v string) bool {
  303. for _, each := range union {
  304. if v == each {
  305. return true
  306. }
  307. }
  308. return false
  309. }
  310. assert.Equal(t, 5, len(union))
  311. assert.True(t, contains("zero"))
  312. assert.True(t, contains("one"))
  313. assert.True(t, contains("two"))
  314. assert.True(t, contains("three"))
  315. assert.True(t, contains("four"))
  316. }