list_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package sprig
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestTuple(t *testing.T) {
  7. tpl := `{{$t := tuple 1 "a" "foo"}}{{index $t 2}}{{index $t 0 }}{{index $t 1}}`
  8. if err := runt(tpl, "foo1a"); err != nil {
  9. t.Error(err)
  10. }
  11. }
  12. func TestList(t *testing.T) {
  13. tpl := `{{$t := list 1 "a" "foo"}}{{index $t 2}}{{index $t 0 }}{{index $t 1}}`
  14. if err := runt(tpl, "foo1a"); err != nil {
  15. t.Error(err)
  16. }
  17. }
  18. func TestPush(t *testing.T) {
  19. // Named `append` in the function map
  20. tests := map[string]string{
  21. `{{ $t := tuple 1 2 3 }}{{ append $t 4 | len }}`: "4",
  22. `{{ $t := tuple 1 2 3 4 }}{{ append $t 5 | join "-" }}`: "1-2-3-4-5",
  23. `{{ $t := regexSplit "/" "foo/bar/baz" -1 }}{{ append $t "qux" | join "-" }}`: "foo-bar-baz-qux",
  24. }
  25. for tpl, expect := range tests {
  26. assert.NoError(t, runt(tpl, expect))
  27. }
  28. }
  29. func TestMustPush(t *testing.T) {
  30. // Named `append` in the function map
  31. tests := map[string]string{
  32. `{{ $t := tuple 1 2 3 }}{{ mustAppend $t 4 | len }}`: "4",
  33. `{{ $t := tuple 1 2 3 4 }}{{ mustAppend $t 5 | join "-" }}`: "1-2-3-4-5",
  34. `{{ $t := regexSplit "/" "foo/bar/baz" -1 }}{{ mustPush $t "qux" | join "-" }}`: "foo-bar-baz-qux",
  35. }
  36. for tpl, expect := range tests {
  37. assert.NoError(t, runt(tpl, expect))
  38. }
  39. }
  40. func TestChunk(t *testing.T) {
  41. tests := map[string]string{
  42. `{{ tuple 1 2 3 4 5 6 7 | chunk 3 | len }}`: "3",
  43. `{{ tuple | chunk 3 | len }}`: "0",
  44. `{{ range ( tuple 1 2 3 4 5 6 7 8 9 | chunk 3 ) }}{{. | join "-"}}|{{end}}`: "1-2-3|4-5-6|7-8-9|",
  45. `{{ range ( tuple 1 2 3 4 5 6 7 8 | chunk 3 ) }}{{. | join "-"}}|{{end}}`: "1-2-3|4-5-6|7-8|",
  46. `{{ range ( tuple 1 2 | chunk 3 ) }}{{. | join "-"}}|{{end}}`: "1-2|",
  47. }
  48. for tpl, expect := range tests {
  49. assert.NoError(t, runt(tpl, expect))
  50. }
  51. }
  52. func TestMustChunk(t *testing.T) {
  53. tests := map[string]string{
  54. `{{ tuple 1 2 3 4 5 6 7 | mustChunk 3 | len }}`: "3",
  55. `{{ tuple | mustChunk 3 | len }}`: "0",
  56. `{{ range ( tuple 1 2 3 4 5 6 7 8 9 | mustChunk 3 ) }}{{. | join "-"}}|{{end}}`: "1-2-3|4-5-6|7-8-9|",
  57. `{{ range ( tuple 1 2 3 4 5 6 7 8 | mustChunk 3 ) }}{{. | join "-"}}|{{end}}`: "1-2-3|4-5-6|7-8|",
  58. `{{ range ( tuple 1 2 | mustChunk 3 ) }}{{. | join "-"}}|{{end}}`: "1-2|",
  59. }
  60. for tpl, expect := range tests {
  61. assert.NoError(t, runt(tpl, expect))
  62. }
  63. }
  64. func TestPrepend(t *testing.T) {
  65. tests := map[string]string{
  66. `{{ $t := tuple 1 2 3 }}{{ prepend $t 0 | len }}`: "4",
  67. `{{ $t := tuple 1 2 3 4 }}{{ prepend $t 0 | join "-" }}`: "0-1-2-3-4",
  68. `{{ $t := regexSplit "/" "foo/bar/baz" -1 }}{{ prepend $t "qux" | join "-" }}`: "qux-foo-bar-baz",
  69. }
  70. for tpl, expect := range tests {
  71. assert.NoError(t, runt(tpl, expect))
  72. }
  73. }
  74. func TestMustPrepend(t *testing.T) {
  75. tests := map[string]string{
  76. `{{ $t := tuple 1 2 3 }}{{ mustPrepend $t 0 | len }}`: "4",
  77. `{{ $t := tuple 1 2 3 4 }}{{ mustPrepend $t 0 | join "-" }}`: "0-1-2-3-4",
  78. `{{ $t := regexSplit "/" "foo/bar/baz" -1 }}{{ mustPrepend $t "qux" | join "-" }}`: "qux-foo-bar-baz",
  79. }
  80. for tpl, expect := range tests {
  81. assert.NoError(t, runt(tpl, expect))
  82. }
  83. }
  84. func TestFirst(t *testing.T) {
  85. tests := map[string]string{
  86. `{{ list 1 2 3 | first }}`: "1",
  87. `{{ list | first }}`: "<no value>",
  88. `{{ regexSplit "/src/" "foo/src/bar" -1 | first }}`: "foo",
  89. }
  90. for tpl, expect := range tests {
  91. assert.NoError(t, runt(tpl, expect))
  92. }
  93. }
  94. func TestMustFirst(t *testing.T) {
  95. tests := map[string]string{
  96. `{{ list 1 2 3 | mustFirst }}`: "1",
  97. `{{ list | mustFirst }}`: "<no value>",
  98. `{{ regexSplit "/src/" "foo/src/bar" -1 | mustFirst }}`: "foo",
  99. }
  100. for tpl, expect := range tests {
  101. assert.NoError(t, runt(tpl, expect))
  102. }
  103. }
  104. func TestLast(t *testing.T) {
  105. tests := map[string]string{
  106. `{{ list 1 2 3 | last }}`: "3",
  107. `{{ list | last }}`: "<no value>",
  108. `{{ regexSplit "/src/" "foo/src/bar" -1 | last }}`: "bar",
  109. }
  110. for tpl, expect := range tests {
  111. assert.NoError(t, runt(tpl, expect))
  112. }
  113. }
  114. func TestMustLast(t *testing.T) {
  115. tests := map[string]string{
  116. `{{ list 1 2 3 | mustLast }}`: "3",
  117. `{{ list | mustLast }}`: "<no value>",
  118. `{{ regexSplit "/src/" "foo/src/bar" -1 | mustLast }}`: "bar",
  119. }
  120. for tpl, expect := range tests {
  121. assert.NoError(t, runt(tpl, expect))
  122. }
  123. }
  124. func TestInitial(t *testing.T) {
  125. tests := map[string]string{
  126. `{{ list 1 2 3 | initial | len }}`: "2",
  127. `{{ list 1 2 3 | initial | last }}`: "2",
  128. `{{ list 1 2 3 | initial | first }}`: "1",
  129. `{{ list | initial }}`: "[]",
  130. `{{ regexSplit "/" "foo/bar/baz" -1 | initial }}`: "[foo bar]",
  131. }
  132. for tpl, expect := range tests {
  133. assert.NoError(t, runt(tpl, expect))
  134. }
  135. }
  136. func TestMustInitial(t *testing.T) {
  137. tests := map[string]string{
  138. `{{ list 1 2 3 | mustInitial | len }}`: "2",
  139. `{{ list 1 2 3 | mustInitial | last }}`: "2",
  140. `{{ list 1 2 3 | mustInitial | first }}`: "1",
  141. `{{ list | mustInitial }}`: "[]",
  142. `{{ regexSplit "/" "foo/bar/baz" -1 | mustInitial }}`: "[foo bar]",
  143. }
  144. for tpl, expect := range tests {
  145. assert.NoError(t, runt(tpl, expect))
  146. }
  147. }
  148. func TestRest(t *testing.T) {
  149. tests := map[string]string{
  150. `{{ list 1 2 3 | rest | len }}`: "2",
  151. `{{ list 1 2 3 | rest | last }}`: "3",
  152. `{{ list 1 2 3 | rest | first }}`: "2",
  153. `{{ list | rest }}`: "[]",
  154. `{{ regexSplit "/" "foo/bar/baz" -1 | rest }}`: "[bar baz]",
  155. }
  156. for tpl, expect := range tests {
  157. assert.NoError(t, runt(tpl, expect))
  158. }
  159. }
  160. func TestMustRest(t *testing.T) {
  161. tests := map[string]string{
  162. `{{ list 1 2 3 | mustRest | len }}`: "2",
  163. `{{ list 1 2 3 | mustRest | last }}`: "3",
  164. `{{ list 1 2 3 | mustRest | first }}`: "2",
  165. `{{ list | mustRest }}`: "[]",
  166. `{{ regexSplit "/" "foo/bar/baz" -1 | mustRest }}`: "[bar baz]",
  167. }
  168. for tpl, expect := range tests {
  169. assert.NoError(t, runt(tpl, expect))
  170. }
  171. }
  172. func TestReverse(t *testing.T) {
  173. tests := map[string]string{
  174. `{{ list 1 2 3 | reverse | first }}`: "3",
  175. `{{ list 1 2 3 | reverse | rest | first }}`: "2",
  176. `{{ list 1 2 3 | reverse | last }}`: "1",
  177. `{{ list 1 2 3 4 | reverse }}`: "[4 3 2 1]",
  178. `{{ list 1 | reverse }}`: "[1]",
  179. `{{ list | reverse }}`: "[]",
  180. `{{ regexSplit "/" "foo/bar/baz" -1 | reverse }}`: "[baz bar foo]",
  181. }
  182. for tpl, expect := range tests {
  183. assert.NoError(t, runt(tpl, expect))
  184. }
  185. }
  186. func TestMustReverse(t *testing.T) {
  187. tests := map[string]string{
  188. `{{ list 1 2 3 | mustReverse | first }}`: "3",
  189. `{{ list 1 2 3 | mustReverse | rest | first }}`: "2",
  190. `{{ list 1 2 3 | mustReverse | last }}`: "1",
  191. `{{ list 1 2 3 4 | mustReverse }}`: "[4 3 2 1]",
  192. `{{ list 1 | mustReverse }}`: "[1]",
  193. `{{ list | mustReverse }}`: "[]",
  194. `{{ regexSplit "/" "foo/bar/baz" -1 | mustReverse }}`: "[baz bar foo]",
  195. }
  196. for tpl, expect := range tests {
  197. assert.NoError(t, runt(tpl, expect))
  198. }
  199. }
  200. func TestCompact(t *testing.T) {
  201. tests := map[string]string{
  202. `{{ list 1 0 "" "hello" | compact }}`: `[1 hello]`,
  203. `{{ list "" "" | compact }}`: `[]`,
  204. `{{ list | compact }}`: `[]`,
  205. `{{ regexSplit "/" "foo//bar" -1 | compact }}`: "[foo bar]",
  206. }
  207. for tpl, expect := range tests {
  208. assert.NoError(t, runt(tpl, expect))
  209. }
  210. }
  211. func TestMustCompact(t *testing.T) {
  212. tests := map[string]string{
  213. `{{ list 1 0 "" "hello" | mustCompact }}`: `[1 hello]`,
  214. `{{ list "" "" | mustCompact }}`: `[]`,
  215. `{{ list | mustCompact }}`: `[]`,
  216. `{{ regexSplit "/" "foo//bar" -1 | mustCompact }}`: "[foo bar]",
  217. }
  218. for tpl, expect := range tests {
  219. assert.NoError(t, runt(tpl, expect))
  220. }
  221. }
  222. func TestUniq(t *testing.T) {
  223. tests := map[string]string{
  224. `{{ list 1 2 3 4 | uniq }}`: `[1 2 3 4]`,
  225. `{{ list "a" "b" "c" "d" | uniq }}`: `[a b c d]`,
  226. `{{ list 1 1 1 1 2 2 2 2 | uniq }}`: `[1 2]`,
  227. `{{ list "foo" 1 1 1 1 "foo" "foo" | uniq }}`: `[foo 1]`,
  228. `{{ list | uniq }}`: `[]`,
  229. `{{ regexSplit "/" "foo/foo/bar" -1 | uniq }}`: "[foo bar]",
  230. }
  231. for tpl, expect := range tests {
  232. assert.NoError(t, runt(tpl, expect))
  233. }
  234. }
  235. func TestMustUniq(t *testing.T) {
  236. tests := map[string]string{
  237. `{{ list 1 2 3 4 | mustUniq }}`: `[1 2 3 4]`,
  238. `{{ list "a" "b" "c" "d" | mustUniq }}`: `[a b c d]`,
  239. `{{ list 1 1 1 1 2 2 2 2 | mustUniq }}`: `[1 2]`,
  240. `{{ list "foo" 1 1 1 1 "foo" "foo" | mustUniq }}`: `[foo 1]`,
  241. `{{ list | mustUniq }}`: `[]`,
  242. `{{ regexSplit "/" "foo/foo/bar" -1 | mustUniq }}`: "[foo bar]",
  243. }
  244. for tpl, expect := range tests {
  245. assert.NoError(t, runt(tpl, expect))
  246. }
  247. }
  248. func TestWithout(t *testing.T) {
  249. tests := map[string]string{
  250. `{{ without (list 1 2 3 4) 1 }}`: `[2 3 4]`,
  251. `{{ without (list "a" "b" "c" "d") "a" }}`: `[b c d]`,
  252. `{{ without (list 1 1 1 1 2) 1 }}`: `[2]`,
  253. `{{ without (list) 1 }}`: `[]`,
  254. `{{ without (list 1 2 3) }}`: `[1 2 3]`,
  255. `{{ without list }}`: `[]`,
  256. `{{ without (regexSplit "/" "foo/bar/baz" -1 ) "foo" }}`: "[bar baz]",
  257. }
  258. for tpl, expect := range tests {
  259. assert.NoError(t, runt(tpl, expect))
  260. }
  261. }
  262. func TestMustWithout(t *testing.T) {
  263. tests := map[string]string{
  264. `{{ mustWithout (list 1 2 3 4) 1 }}`: `[2 3 4]`,
  265. `{{ mustWithout (list "a" "b" "c" "d") "a" }}`: `[b c d]`,
  266. `{{ mustWithout (list 1 1 1 1 2) 1 }}`: `[2]`,
  267. `{{ mustWithout (list) 1 }}`: `[]`,
  268. `{{ mustWithout (list 1 2 3) }}`: `[1 2 3]`,
  269. `{{ mustWithout list }}`: `[]`,
  270. `{{ mustWithout (regexSplit "/" "foo/bar/baz" -1 ) "foo" }}`: "[bar baz]",
  271. }
  272. for tpl, expect := range tests {
  273. assert.NoError(t, runt(tpl, expect))
  274. }
  275. }
  276. func TestHas(t *testing.T) {
  277. tests := map[string]string{
  278. `{{ list 1 2 3 | has 1 }}`: `true`,
  279. `{{ list 1 2 3 | has 4 }}`: `false`,
  280. `{{ regexSplit "/" "foo/bar/baz" -1 | has "bar" }}`: `true`,
  281. `{{ has "bar" nil }}`: `false`,
  282. }
  283. for tpl, expect := range tests {
  284. assert.NoError(t, runt(tpl, expect))
  285. }
  286. }
  287. func TestMustHas(t *testing.T) {
  288. tests := map[string]string{
  289. `{{ list 1 2 3 | mustHas 1 }}`: `true`,
  290. `{{ list 1 2 3 | mustHas 4 }}`: `false`,
  291. `{{ regexSplit "/" "foo/bar/baz" -1 | mustHas "bar" }}`: `true`,
  292. `{{ mustHas "bar" nil }}`: `false`,
  293. }
  294. for tpl, expect := range tests {
  295. assert.NoError(t, runt(tpl, expect))
  296. }
  297. }
  298. func TestSlice(t *testing.T) {
  299. tests := map[string]string{
  300. `{{ slice (list 1 2 3) }}`: "[1 2 3]",
  301. `{{ slice (list 1 2 3) 0 1 }}`: "[1]",
  302. `{{ slice (list 1 2 3) 1 3 }}`: "[2 3]",
  303. `{{ slice (list 1 2 3) 1 }}`: "[2 3]",
  304. `{{ slice (regexSplit "/" "foo/bar/baz" -1) 1 2 }}`: "[bar]",
  305. }
  306. for tpl, expect := range tests {
  307. assert.NoError(t, runt(tpl, expect))
  308. }
  309. }
  310. func TestMustSlice(t *testing.T) {
  311. tests := map[string]string{
  312. `{{ mustSlice (list 1 2 3) }}`: "[1 2 3]",
  313. `{{ mustSlice (list 1 2 3) 0 1 }}`: "[1]",
  314. `{{ mustSlice (list 1 2 3) 1 3 }}`: "[2 3]",
  315. `{{ mustSlice (list 1 2 3) 1 }}`: "[2 3]",
  316. `{{ mustSlice (regexSplit "/" "foo/bar/baz" -1) 1 2 }}`: "[bar]",
  317. }
  318. for tpl, expect := range tests {
  319. assert.NoError(t, runt(tpl, expect))
  320. }
  321. }
  322. func TestConcat(t *testing.T) {
  323. tests := map[string]string{
  324. `{{ concat (list 1 2 3) }}`: "[1 2 3]",
  325. `{{ concat (list 1 2 3) (list 4 5) }}`: "[1 2 3 4 5]",
  326. `{{ concat (list 1 2 3) (list 4 5) (list) }}`: "[1 2 3 4 5]",
  327. `{{ concat (list 1 2 3) (list 4 5) (list nil) }}`: "[1 2 3 4 5 <nil>]",
  328. `{{ concat (list 1 2 3) (list 4 5) (list ( list "foo" ) ) }}`: "[1 2 3 4 5 [foo]]",
  329. }
  330. for tpl, expect := range tests {
  331. assert.NoError(t, runt(tpl, expect))
  332. }
  333. }