strings_test.go 6.6 KB

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