strings_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 TestJoin(t *testing.T) {
  140. tests := []struct {
  141. name string
  142. input []string
  143. expect string
  144. }{
  145. {
  146. name: "all blanks",
  147. input: []string{"", ""},
  148. expect: "",
  149. },
  150. {
  151. name: "two values",
  152. input: []string{"012", "abc"},
  153. expect: "012.abc",
  154. },
  155. {
  156. name: "last blank",
  157. input: []string{"abc", ""},
  158. expect: "abc",
  159. },
  160. {
  161. name: "first blank",
  162. input: []string{"", "abc"},
  163. expect: "abc",
  164. },
  165. }
  166. for _, test := range tests {
  167. test := test
  168. t.Run(test.name, func(t *testing.T) {
  169. assert.Equal(t, test.expect, Join('.', test.input...))
  170. })
  171. }
  172. }
  173. func TestRemove(t *testing.T) {
  174. cases := []struct {
  175. input []string
  176. remove []string
  177. expect []string
  178. }{
  179. {
  180. input: []string{"a", "b", "a", "c"},
  181. remove: []string{"a", "b"},
  182. expect: []string{"c"},
  183. },
  184. {
  185. input: []string{"b", "c"},
  186. remove: []string{"a"},
  187. expect: []string{"b", "c"},
  188. },
  189. {
  190. input: []string{"b", "a", "c"},
  191. remove: []string{"a"},
  192. expect: []string{"b", "c"},
  193. },
  194. {
  195. input: []string{},
  196. remove: []string{"a"},
  197. expect: []string{},
  198. },
  199. }
  200. for _, each := range cases {
  201. t.Run(path.Join(each.input...), func(t *testing.T) {
  202. assert.ElementsMatch(t, each.expect, Remove(each.input, each.remove...))
  203. })
  204. }
  205. }
  206. func TestReverse(t *testing.T) {
  207. cases := []struct {
  208. input string
  209. expect string
  210. }{
  211. {
  212. input: "abcd",
  213. expect: "dcba",
  214. },
  215. {
  216. input: "",
  217. expect: "",
  218. },
  219. {
  220. input: "我爱中国",
  221. expect: "国中爱我",
  222. },
  223. }
  224. for _, each := range cases {
  225. t.Run(each.input, func(t *testing.T) {
  226. assert.Equal(t, each.expect, Reverse(each.input))
  227. })
  228. }
  229. }
  230. func TestSubstr(t *testing.T) {
  231. cases := []struct {
  232. input string
  233. start int
  234. stop int
  235. err error
  236. expect string
  237. }{
  238. {
  239. input: "abcdefg",
  240. start: 1,
  241. stop: 4,
  242. expect: "bcd",
  243. },
  244. {
  245. input: "我爱中国3000遍,even more",
  246. start: 1,
  247. stop: 9,
  248. expect: "爱中国3000遍",
  249. },
  250. {
  251. input: "abcdefg",
  252. start: -1,
  253. stop: 4,
  254. err: ErrInvalidStartPosition,
  255. expect: "",
  256. },
  257. {
  258. input: "abcdefg",
  259. start: 100,
  260. stop: 4,
  261. err: ErrInvalidStartPosition,
  262. expect: "",
  263. },
  264. {
  265. input: "abcdefg",
  266. start: 1,
  267. stop: -1,
  268. err: ErrInvalidStopPosition,
  269. expect: "",
  270. },
  271. {
  272. input: "abcdefg",
  273. start: 1,
  274. stop: 100,
  275. err: ErrInvalidStopPosition,
  276. expect: "",
  277. },
  278. }
  279. for _, each := range cases {
  280. t.Run(each.input, func(t *testing.T) {
  281. val, err := Substr(each.input, each.start, each.stop)
  282. assert.Equal(t, each.err, err)
  283. if err == nil {
  284. assert.Equal(t, each.expect, val)
  285. }
  286. })
  287. }
  288. }
  289. func TestTakeOne(t *testing.T) {
  290. cases := []struct {
  291. valid string
  292. or string
  293. expect string
  294. }{
  295. {"", "", ""},
  296. {"", "1", "1"},
  297. {"1", "", "1"},
  298. {"1", "2", "1"},
  299. }
  300. for _, each := range cases {
  301. t.Run(each.valid, func(t *testing.T) {
  302. actual := TakeOne(each.valid, each.or)
  303. assert.Equal(t, each.expect, actual)
  304. })
  305. }
  306. }
  307. func TestTakeWithPriority(t *testing.T) {
  308. tests := []struct {
  309. fns []func() string
  310. expect string
  311. }{
  312. {
  313. fns: []func() string{
  314. func() string {
  315. return "first"
  316. },
  317. func() string {
  318. return "second"
  319. },
  320. func() string {
  321. return "third"
  322. },
  323. },
  324. expect: "first",
  325. },
  326. {
  327. fns: []func() string{
  328. func() string {
  329. return ""
  330. },
  331. func() string {
  332. return "second"
  333. },
  334. func() string {
  335. return "third"
  336. },
  337. },
  338. expect: "second",
  339. },
  340. {
  341. fns: []func() string{
  342. func() string {
  343. return ""
  344. },
  345. func() string {
  346. return ""
  347. },
  348. func() string {
  349. return "third"
  350. },
  351. },
  352. expect: "third",
  353. },
  354. {
  355. fns: []func() string{
  356. func() string {
  357. return ""
  358. },
  359. func() string {
  360. return ""
  361. },
  362. func() string {
  363. return ""
  364. },
  365. },
  366. expect: "",
  367. },
  368. }
  369. for _, test := range tests {
  370. t.Run(RandId(), func(t *testing.T) {
  371. val := TakeWithPriority(test.fns...)
  372. assert.Equal(t, test.expect, val)
  373. })
  374. }
  375. }
  376. func TestToCamelCase(t *testing.T) {
  377. tests := []struct {
  378. input string
  379. expect string
  380. }{
  381. {
  382. input: "",
  383. expect: "",
  384. },
  385. {
  386. input: "A",
  387. expect: "a",
  388. },
  389. {
  390. input: "a",
  391. expect: "a",
  392. },
  393. {
  394. input: "hello_world",
  395. expect: "hello_world",
  396. },
  397. {
  398. input: "Hello_world",
  399. expect: "hello_world",
  400. },
  401. {
  402. input: "hello_World",
  403. expect: "hello_World",
  404. },
  405. {
  406. input: "helloWorld",
  407. expect: "helloWorld",
  408. },
  409. {
  410. input: "HelloWorld",
  411. expect: "helloWorld",
  412. },
  413. {
  414. input: "hello World",
  415. expect: "hello World",
  416. },
  417. {
  418. input: "Hello World",
  419. expect: "hello World",
  420. },
  421. }
  422. for _, test := range tests {
  423. test := test
  424. t.Run(test.input, func(t *testing.T) {
  425. assert.Equal(t, test.expect, ToCamelCase(test.input))
  426. })
  427. }
  428. }
  429. func TestUnion(t *testing.T) {
  430. first := []string{
  431. "one",
  432. "two",
  433. "three",
  434. }
  435. second := []string{
  436. "zero",
  437. "two",
  438. "three",
  439. "four",
  440. }
  441. union := Union(first, second)
  442. contains := func(v string) bool {
  443. for _, each := range union {
  444. if v == each {
  445. return true
  446. }
  447. }
  448. return false
  449. }
  450. assert.Equal(t, 5, len(union))
  451. assert.True(t, contains("zero"))
  452. assert.True(t, contains("one"))
  453. assert.True(t, contains("two"))
  454. assert.True(t, contains("three"))
  455. assert.True(t, contains("four"))
  456. }