defaults_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package sprig
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestDefault(t *testing.T) {
  7. tpl := `{{"" | default "foo"}}`
  8. if err := runt(tpl, "foo"); err != nil {
  9. t.Error(err)
  10. }
  11. tpl = `{{default "foo" 234}}`
  12. if err := runt(tpl, "234"); err != nil {
  13. t.Error(err)
  14. }
  15. tpl = `{{default "foo" 2.34}}`
  16. if err := runt(tpl, "2.34"); err != nil {
  17. t.Error(err)
  18. }
  19. tpl = `{{ .Nothing | default "123" }}`
  20. if err := runt(tpl, "123"); err != nil {
  21. t.Error(err)
  22. }
  23. tpl = `{{ default "123" }}`
  24. if err := runt(tpl, "123"); err != nil {
  25. t.Error(err)
  26. }
  27. }
  28. func TestEmpty(t *testing.T) {
  29. tpl := `{{if empty 1}}1{{else}}0{{end}}`
  30. if err := runt(tpl, "0"); err != nil {
  31. t.Error(err)
  32. }
  33. tpl = `{{if empty 0}}1{{else}}0{{end}}`
  34. if err := runt(tpl, "1"); err != nil {
  35. t.Error(err)
  36. }
  37. tpl = `{{if empty ""}}1{{else}}0{{end}}`
  38. if err := runt(tpl, "1"); err != nil {
  39. t.Error(err)
  40. }
  41. tpl = `{{if empty 0.0}}1{{else}}0{{end}}`
  42. if err := runt(tpl, "1"); err != nil {
  43. t.Error(err)
  44. }
  45. tpl = `{{if empty false}}1{{else}}0{{end}}`
  46. if err := runt(tpl, "1"); err != nil {
  47. t.Error(err)
  48. }
  49. dict := map[string]any{"top": map[string]any{}}
  50. tpl = `{{if empty .top.NoSuchThing}}1{{else}}0{{end}}`
  51. if err := runtv(tpl, "1", dict); err != nil {
  52. t.Error(err)
  53. }
  54. tpl = `{{if empty .bottom.NoSuchThing}}1{{else}}0{{end}}`
  55. if err := runtv(tpl, "1", dict); err != nil {
  56. t.Error(err)
  57. }
  58. }
  59. func TestCoalesce(t *testing.T) {
  60. tests := map[string]string{
  61. `{{ coalesce 1 }}`: "1",
  62. `{{ coalesce "" 0 nil 2 }}`: "2",
  63. `{{ $two := 2 }}{{ coalesce "" 0 nil $two }}`: "2",
  64. `{{ $two := 2 }}{{ coalesce "" $two 0 0 0 }}`: "2",
  65. `{{ $two := 2 }}{{ coalesce "" $two 3 4 5 }}`: "2",
  66. `{{ coalesce }}`: "<no value>",
  67. }
  68. for tpl, expect := range tests {
  69. assert.NoError(t, runt(tpl, expect))
  70. }
  71. dict := map[string]any{"top": map[string]any{}}
  72. tpl := `{{ coalesce .top.NoSuchThing .bottom .bottom.dollar "airplane"}}`
  73. if err := runtv(tpl, "airplane", dict); err != nil {
  74. t.Error(err)
  75. }
  76. }
  77. func TestAll(t *testing.T) {
  78. tests := map[string]string{
  79. `{{ all 1 }}`: "true",
  80. `{{ all "" 0 nil 2 }}`: "false",
  81. `{{ $two := 2 }}{{ all "" 0 nil $two }}`: "false",
  82. `{{ $two := 2 }}{{ all "" $two 0 0 0 }}`: "false",
  83. `{{ $two := 2 }}{{ all "" $two 3 4 5 }}`: "false",
  84. `{{ all }}`: "true",
  85. }
  86. for tpl, expect := range tests {
  87. assert.NoError(t, runt(tpl, expect))
  88. }
  89. dict := map[string]any{"top": map[string]any{}}
  90. tpl := `{{ all .top.NoSuchThing .bottom .bottom.dollar "airplane"}}`
  91. if err := runtv(tpl, "false", dict); err != nil {
  92. t.Error(err)
  93. }
  94. }
  95. func TestAny(t *testing.T) {
  96. tests := map[string]string{
  97. `{{ any 1 }}`: "true",
  98. `{{ any "" 0 nil 2 }}`: "true",
  99. `{{ $two := 2 }}{{ any "" 0 nil $two }}`: "true",
  100. `{{ $two := 2 }}{{ any "" $two 3 4 5 }}`: "true",
  101. `{{ $zero := 0 }}{{ any "" $zero 0 0 0 }}`: "false",
  102. `{{ any }}`: "false",
  103. }
  104. for tpl, expect := range tests {
  105. assert.NoError(t, runt(tpl, expect))
  106. }
  107. dict := map[string]any{"top": map[string]any{}}
  108. tpl := `{{ any .top.NoSuchThing .bottom .bottom.dollar "airplane"}}`
  109. if err := runtv(tpl, "true", dict); err != nil {
  110. t.Error(err)
  111. }
  112. }
  113. func TestFromJSON(t *testing.T) {
  114. dict := map[string]any{"Input": `{"foo": 55}`}
  115. tpl := `{{.Input | fromJSON}}`
  116. expected := `map[foo:55]`
  117. if err := runtv(tpl, expected, dict); err != nil {
  118. t.Error(err)
  119. }
  120. tpl = `{{(.Input | fromJSON).foo}}`
  121. expected = `55`
  122. if err := runtv(tpl, expected, dict); err != nil {
  123. t.Error(err)
  124. }
  125. }
  126. func TestToJSON(t *testing.T) {
  127. dict := map[string]any{"Top": map[string]any{"bool": true, "string": "test", "number": 42}}
  128. tpl := `{{.Top | toJSON}}`
  129. expected := `{"bool":true,"number":42,"string":"test"}`
  130. if err := runtv(tpl, expected, dict); err != nil {
  131. t.Error(err)
  132. }
  133. }
  134. func TestToPrettyJSON(t *testing.T) {
  135. dict := map[string]any{"Top": map[string]any{"bool": true, "string": "test", "number": 42}}
  136. tpl := `{{.Top | toPrettyJSON}}`
  137. expected := `{
  138. "bool": true,
  139. "number": 42,
  140. "string": "test"
  141. }`
  142. if err := runtv(tpl, expected, dict); err != nil {
  143. t.Error(err)
  144. }
  145. }
  146. func TestToRawJSON(t *testing.T) {
  147. dict := map[string]any{"Top": map[string]any{"bool": true, "string": "test", "number": 42, "html": "<HEAD>"}}
  148. tpl := `{{.Top | toRawJSON}}`
  149. expected := `{"bool":true,"html":"<HEAD>","number":42,"string":"test"}`
  150. if err := runtv(tpl, expected, dict); err != nil {
  151. t.Error(err)
  152. }
  153. }
  154. func TestTernary(t *testing.T) {
  155. tpl := `{{true | ternary "foo" "bar"}}`
  156. if err := runt(tpl, "foo"); err != nil {
  157. t.Error(err)
  158. }
  159. tpl = `{{ternary "foo" "bar" true}}`
  160. if err := runt(tpl, "foo"); err != nil {
  161. t.Error(err)
  162. }
  163. tpl = `{{false | ternary "foo" "bar"}}`
  164. if err := runt(tpl, "bar"); err != nil {
  165. t.Error(err)
  166. }
  167. tpl = `{{ternary "foo" "bar" false}}`
  168. if err := runt(tpl, "bar"); err != nil {
  169. t.Error(err)
  170. }
  171. }