strings_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package sprig
  2. import (
  3. "encoding/base32"
  4. "encoding/base64"
  5. "fmt"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestSubstr(t *testing.T) {
  10. tpl := `{{"fooo" | substr 0 3 }}`
  11. if err := runt(tpl, "foo"); err != nil {
  12. t.Error(err)
  13. }
  14. }
  15. func TestSubstr_shorterString(t *testing.T) {
  16. tpl := `{{"foo" | substr 0 10 }}`
  17. if err := runt(tpl, "foo"); err != nil {
  18. t.Error(err)
  19. }
  20. }
  21. func TestTrunc(t *testing.T) {
  22. tpl := `{{ "foooooo" | trunc 3 }}`
  23. if err := runt(tpl, "foo"); err != nil {
  24. t.Error(err)
  25. }
  26. tpl = `{{ "baaaaaar" | trunc -3 }}`
  27. if err := runt(tpl, "aar"); err != nil {
  28. t.Error(err)
  29. }
  30. tpl = `{{ "baaaaaar" | trunc -999 }}`
  31. if err := runt(tpl, "baaaaaar"); err != nil {
  32. t.Error(err)
  33. }
  34. tpl = `{{ "baaaaaz" | trunc 0 }}`
  35. if err := runt(tpl, ""); err != nil {
  36. t.Error(err)
  37. }
  38. }
  39. func TestQuote(t *testing.T) {
  40. tpl := `{{quote "a" "b" "c"}}`
  41. if err := runt(tpl, `"a" "b" "c"`); err != nil {
  42. t.Error(err)
  43. }
  44. tpl = `{{quote "\"a\"" "b" "c"}}`
  45. if err := runt(tpl, `"\"a\"" "b" "c"`); err != nil {
  46. t.Error(err)
  47. }
  48. tpl = `{{quote 1 2 3 }}`
  49. if err := runt(tpl, `"1" "2" "3"`); err != nil {
  50. t.Error(err)
  51. }
  52. tpl = `{{ .value | quote }}`
  53. values := map[string]any{"value": nil}
  54. if err := runtv(tpl, ``, values); err != nil {
  55. t.Error(err)
  56. }
  57. }
  58. func TestSquote(t *testing.T) {
  59. tpl := `{{squote "a" "b" "c"}}`
  60. if err := runt(tpl, `'a' 'b' 'c'`); err != nil {
  61. t.Error(err)
  62. }
  63. tpl = `{{squote 1 2 3 }}`
  64. if err := runt(tpl, `'1' '2' '3'`); err != nil {
  65. t.Error(err)
  66. }
  67. tpl = `{{ .value | squote }}`
  68. values := map[string]any{"value": nil}
  69. if err := runtv(tpl, ``, values); err != nil {
  70. t.Error(err)
  71. }
  72. }
  73. func TestContains(t *testing.T) {
  74. // Mainly, we're just verifying the paramater order swap.
  75. tests := []string{
  76. `{{if contains "cat" "fair catch"}}1{{end}}`,
  77. `{{if hasPrefix "cat" "catch"}}1{{end}}`,
  78. `{{if hasSuffix "cat" "ducat"}}1{{end}}`,
  79. }
  80. for _, tt := range tests {
  81. if err := runt(tt, "1"); err != nil {
  82. t.Error(err)
  83. }
  84. }
  85. }
  86. func TestTrim(t *testing.T) {
  87. tests := []string{
  88. `{{trim " 5.00 "}}`,
  89. `{{trimAll "$" "$5.00$"}}`,
  90. `{{trimPrefix "$" "$5.00"}}`,
  91. `{{trimSuffix "$" "5.00$"}}`,
  92. }
  93. for _, tt := range tests {
  94. if err := runt(tt, "5.00"); err != nil {
  95. t.Error(err)
  96. }
  97. }
  98. }
  99. func TestSplit(t *testing.T) {
  100. tpl := `{{$v := "foo$bar$baz" | split "$"}}{{$v._0}}`
  101. if err := runt(tpl, "foo"); err != nil {
  102. t.Error(err)
  103. }
  104. }
  105. func TestSplitn(t *testing.T) {
  106. tpl := `{{$v := "foo$bar$baz" | splitn "$" 2}}{{$v._0}}`
  107. if err := runt(tpl, "foo"); err != nil {
  108. t.Error(err)
  109. }
  110. }
  111. func TestToString(t *testing.T) {
  112. tpl := `{{ toString 1 | kindOf }}`
  113. assert.NoError(t, runt(tpl, "string"))
  114. }
  115. func TestToStrings(t *testing.T) {
  116. tpl := `{{ $s := list 1 2 3 | toStrings }}{{ index $s 1 | kindOf }}`
  117. assert.NoError(t, runt(tpl, "string"))
  118. tpl = `{{ list 1 .value 2 | toStrings }}`
  119. values := map[string]any{"value": nil}
  120. if err := runtv(tpl, `[1 2]`, values); err != nil {
  121. t.Error(err)
  122. }
  123. }
  124. func TestJoin(t *testing.T) {
  125. assert.NoError(t, runt(`{{ tuple "a" "b" "c" | join "-" }}`, "a-b-c"))
  126. assert.NoError(t, runt(`{{ tuple 1 2 3 | join "-" }}`, "1-2-3"))
  127. assert.NoError(t, runtv(`{{ join "-" .V }}`, "a-b-c", map[string]any{"V": []string{"a", "b", "c"}}))
  128. assert.NoError(t, runtv(`{{ join "-" .V }}`, "abc", map[string]any{"V": "abc"}))
  129. assert.NoError(t, runtv(`{{ join "-" .V }}`, "1-2-3", map[string]any{"V": []int{1, 2, 3}}))
  130. assert.NoError(t, runtv(`{{ join "-" .value }}`, "1-2", map[string]any{"value": []any{"1", nil, "2"}}))
  131. }
  132. func TestSortAlpha(t *testing.T) {
  133. // Named `append` in the function map
  134. tests := map[string]string{
  135. `{{ list "c" "a" "b" | sortAlpha | join "" }}`: "abc",
  136. `{{ list 2 1 4 3 | sortAlpha | join "" }}`: "1234",
  137. }
  138. for tpl, expect := range tests {
  139. assert.NoError(t, runt(tpl, expect))
  140. }
  141. }
  142. func TestBase64EncodeDecode(t *testing.T) {
  143. magicWord := "coffee"
  144. expect := base64.StdEncoding.EncodeToString([]byte(magicWord))
  145. if expect == magicWord {
  146. t.Fatal("Encoder doesn't work.")
  147. }
  148. tpl := `{{b64enc "coffee"}}`
  149. if err := runt(tpl, expect); err != nil {
  150. t.Error(err)
  151. }
  152. tpl = fmt.Sprintf("{{b64dec %q}}", expect)
  153. if err := runt(tpl, magicWord); err != nil {
  154. t.Error(err)
  155. }
  156. }
  157. func TestBase32EncodeDecode(t *testing.T) {
  158. magicWord := "coffee"
  159. expect := base32.StdEncoding.EncodeToString([]byte(magicWord))
  160. if expect == magicWord {
  161. t.Fatal("Encoder doesn't work.")
  162. }
  163. tpl := `{{b32enc "coffee"}}`
  164. if err := runt(tpl, expect); err != nil {
  165. t.Error(err)
  166. }
  167. tpl = fmt.Sprintf("{{b32dec %q}}", expect)
  168. if err := runt(tpl, magicWord); err != nil {
  169. t.Error(err)
  170. }
  171. }
  172. func TestCat(t *testing.T) {
  173. tpl := `{{$b := "b"}}{{"c" | cat "a" $b}}`
  174. if err := runt(tpl, "a b c"); err != nil {
  175. t.Error(err)
  176. }
  177. tpl = `{{ .value | cat "a" "b"}}`
  178. values := map[string]any{"value": nil}
  179. if err := runtv(tpl, "a b", values); err != nil {
  180. t.Error(err)
  181. }
  182. }
  183. func TestIndent(t *testing.T) {
  184. tpl := `{{indent 4 "a\nb\nc"}}`
  185. if err := runt(tpl, " a\n b\n c"); err != nil {
  186. t.Error(err)
  187. }
  188. }
  189. func TestNindent(t *testing.T) {
  190. tpl := `{{nindent 4 "a\nb\nc"}}`
  191. if err := runt(tpl, "\n a\n b\n c"); err != nil {
  192. t.Error(err)
  193. }
  194. }
  195. func TestReplace(t *testing.T) {
  196. tpl := `{{"I Am Henry VIII" | replace " " "-"}}`
  197. if err := runt(tpl, "I-Am-Henry-VIII"); err != nil {
  198. t.Error(err)
  199. }
  200. }
  201. func TestPlural(t *testing.T) {
  202. tpl := `{{$num := len "two"}}{{$num}} {{$num | plural "1 char" "chars"}}`
  203. if err := runt(tpl, "3 chars"); err != nil {
  204. t.Error(err)
  205. }
  206. tpl = `{{len "t" | plural "cheese" "%d chars"}}`
  207. if err := runt(tpl, "cheese"); err != nil {
  208. t.Error(err)
  209. }
  210. }