strings_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 TestFirstN(t *testing.T) {
  87. tests := []struct {
  88. name string
  89. input string
  90. n int
  91. expect string
  92. }{
  93. {
  94. name: "english string",
  95. input: "anything that we use",
  96. n: 8,
  97. expect: "anything",
  98. },
  99. {
  100. name: "english string more",
  101. input: "anything that we use",
  102. n: 80,
  103. expect: "anything that we use",
  104. },
  105. {
  106. name: "chinese string",
  107. input: "我是中国人",
  108. n: 2,
  109. expect: "我是",
  110. },
  111. {
  112. name: "chinese string",
  113. input: "我是中国人",
  114. n: 10,
  115. expect: "我是中国人",
  116. },
  117. }
  118. for _, test := range tests {
  119. t.Run(test.name, func(t *testing.T) {
  120. assert.Equal(t, test.expect, FirstN(test.input, test.n))
  121. })
  122. }
  123. }
  124. func TestRemove(t *testing.T) {
  125. cases := []struct {
  126. input []string
  127. remove []string
  128. expect []string
  129. }{
  130. {
  131. input: []string{"a", "b", "a", "c"},
  132. remove: []string{"a", "b"},
  133. expect: []string{"c"},
  134. },
  135. {
  136. input: []string{"b", "c"},
  137. remove: []string{"a"},
  138. expect: []string{"b", "c"},
  139. },
  140. {
  141. input: []string{"b", "a", "c"},
  142. remove: []string{"a"},
  143. expect: []string{"b", "c"},
  144. },
  145. {
  146. input: []string{},
  147. remove: []string{"a"},
  148. expect: []string{},
  149. },
  150. }
  151. for _, each := range cases {
  152. t.Run(path.Join(each.input...), func(t *testing.T) {
  153. assert.ElementsMatch(t, each.expect, Remove(each.input, each.remove...))
  154. })
  155. }
  156. }
  157. func TestReverse(t *testing.T) {
  158. cases := []struct {
  159. input string
  160. expect string
  161. }{
  162. {
  163. input: "abcd",
  164. expect: "dcba",
  165. },
  166. {
  167. input: "",
  168. expect: "",
  169. },
  170. {
  171. input: "我爱中国",
  172. expect: "国中爱我",
  173. },
  174. }
  175. for _, each := range cases {
  176. t.Run(each.input, func(t *testing.T) {
  177. assert.Equal(t, each.expect, Reverse(each.input))
  178. })
  179. }
  180. }
  181. func TestSubstr(t *testing.T) {
  182. cases := []struct {
  183. input string
  184. start int
  185. stop int
  186. err error
  187. expect string
  188. }{
  189. {
  190. input: "abcdefg",
  191. start: 1,
  192. stop: 4,
  193. expect: "bcd",
  194. },
  195. {
  196. input: "我爱中国3000遍,even more",
  197. start: 1,
  198. stop: 9,
  199. expect: "爱中国3000遍",
  200. },
  201. {
  202. input: "abcdefg",
  203. start: -1,
  204. stop: 4,
  205. err: ErrInvalidStartPosition,
  206. expect: "",
  207. },
  208. {
  209. input: "abcdefg",
  210. start: 100,
  211. stop: 4,
  212. err: ErrInvalidStartPosition,
  213. expect: "",
  214. },
  215. {
  216. input: "abcdefg",
  217. start: 1,
  218. stop: -1,
  219. err: ErrInvalidStopPosition,
  220. expect: "",
  221. },
  222. {
  223. input: "abcdefg",
  224. start: 1,
  225. stop: 100,
  226. err: ErrInvalidStopPosition,
  227. expect: "",
  228. },
  229. }
  230. for _, each := range cases {
  231. t.Run(each.input, func(t *testing.T) {
  232. val, err := Substr(each.input, each.start, each.stop)
  233. assert.Equal(t, each.err, err)
  234. if err == nil {
  235. assert.Equal(t, each.expect, val)
  236. }
  237. })
  238. }
  239. }
  240. func TestTakeOne(t *testing.T) {
  241. cases := []struct {
  242. valid string
  243. or string
  244. expect string
  245. }{
  246. {"", "", ""},
  247. {"", "1", "1"},
  248. {"1", "", "1"},
  249. {"1", "2", "1"},
  250. }
  251. for _, each := range cases {
  252. t.Run(each.valid, func(t *testing.T) {
  253. actual := TakeOne(each.valid, each.or)
  254. assert.Equal(t, each.expect, actual)
  255. })
  256. }
  257. }
  258. func TestTakeWithPriority(t *testing.T) {
  259. tests := []struct {
  260. fns []func() string
  261. expect string
  262. }{
  263. {
  264. fns: []func() string{
  265. func() string {
  266. return "first"
  267. },
  268. func() string {
  269. return "second"
  270. },
  271. func() string {
  272. return "third"
  273. },
  274. },
  275. expect: "first",
  276. },
  277. {
  278. fns: []func() string{
  279. func() string {
  280. return ""
  281. },
  282. func() string {
  283. return "second"
  284. },
  285. func() string {
  286. return "third"
  287. },
  288. },
  289. expect: "second",
  290. },
  291. {
  292. fns: []func() string{
  293. func() string {
  294. return ""
  295. },
  296. func() string {
  297. return ""
  298. },
  299. func() string {
  300. return "third"
  301. },
  302. },
  303. expect: "third",
  304. },
  305. {
  306. fns: []func() string{
  307. func() string {
  308. return ""
  309. },
  310. func() string {
  311. return ""
  312. },
  313. func() string {
  314. return ""
  315. },
  316. },
  317. expect: "",
  318. },
  319. }
  320. for _, test := range tests {
  321. t.Run(RandId(), func(t *testing.T) {
  322. val := TakeWithPriority(test.fns...)
  323. assert.Equal(t, test.expect, val)
  324. })
  325. }
  326. }
  327. func TestUnion(t *testing.T) {
  328. first := []string{
  329. "one",
  330. "two",
  331. "three",
  332. }
  333. second := []string{
  334. "zero",
  335. "two",
  336. "three",
  337. "four",
  338. }
  339. union := Union(first, second)
  340. contains := func(v string) bool {
  341. for _, each := range union {
  342. if v == each {
  343. return true
  344. }
  345. }
  346. return false
  347. }
  348. assert.Equal(t, 5, len(union))
  349. assert.True(t, contains("zero"))
  350. assert.True(t, contains("one"))
  351. assert.True(t, contains("two"))
  352. assert.True(t, contains("three"))
  353. assert.True(t, contains("four"))
  354. }