dict_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package sprig
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func TestDict(t *testing.T) {
  7. tpl := `{{$d := dict 1 2 "three" "four" 5}}{{range $k, $v := $d}}{{$k}}{{$v}}{{end}}`
  8. out, err := runRaw(tpl, nil)
  9. if err != nil {
  10. t.Error(err)
  11. }
  12. if len(out) != 12 {
  13. t.Errorf("Expected length 12, got %d", len(out))
  14. }
  15. // dict does not guarantee ordering because it is backed by a map.
  16. if !strings.Contains(out, "12") {
  17. t.Error("Expected grouping 12")
  18. }
  19. if !strings.Contains(out, "threefour") {
  20. t.Error("Expected grouping threefour")
  21. }
  22. if !strings.Contains(out, "5") {
  23. t.Error("Expected 5")
  24. }
  25. tpl = `{{$t := dict "I" "shot" "the" "albatross"}}{{$t.the}} {{$t.I}}`
  26. if err := runt(tpl, "albatross shot"); err != nil {
  27. t.Error(err)
  28. }
  29. }
  30. func TestUnset(t *testing.T) {
  31. tpl := `{{- $d := dict "one" 1 "two" 222222 -}}
  32. {{- $_ := unset $d "two" -}}
  33. {{- range $k, $v := $d}}{{$k}}{{$v}}{{- end -}}
  34. `
  35. expect := "one1"
  36. if err := runt(tpl, expect); err != nil {
  37. t.Error(err)
  38. }
  39. }
  40. func TestHasKey(t *testing.T) {
  41. tpl := `{{- $d := dict "one" 1 "two" 222222 -}}
  42. {{- if hasKey $d "one" -}}1{{- end -}}
  43. `
  44. expect := "1"
  45. if err := runt(tpl, expect); err != nil {
  46. t.Error(err)
  47. }
  48. }
  49. func TestPluck(t *testing.T) {
  50. tpl := `
  51. {{- $d := dict "one" 1 "two" 222222 -}}
  52. {{- $d2 := dict "one" 1 "two" 33333 -}}
  53. {{- $d3 := dict "one" 1 -}}
  54. {{- $d4 := dict "one" 1 "two" 4444 -}}
  55. {{- pluck "two" $d $d2 $d3 $d4 -}}
  56. `
  57. expect := "[222222 33333 4444]"
  58. if err := runt(tpl, expect); err != nil {
  59. t.Error(err)
  60. }
  61. }
  62. func TestKeys(t *testing.T) {
  63. tests := map[string]string{
  64. `{{ dict "foo" 1 "bar" 2 | keys | sortAlpha }}`: "[bar foo]",
  65. `{{ dict | keys }}`: "[]",
  66. `{{ keys (dict "foo" 1) (dict "bar" 2) (dict "bar" 3) | uniq | sortAlpha }}`: "[bar foo]",
  67. }
  68. for tpl, expect := range tests {
  69. if err := runt(tpl, expect); err != nil {
  70. t.Error(err)
  71. }
  72. }
  73. }
  74. func TestPick(t *testing.T) {
  75. tests := map[string]string{
  76. `{{- $d := dict "one" 1 "two" 222222 }}{{ pick $d "two" | len -}}`: "1",
  77. `{{- $d := dict "one" 1 "two" 222222 }}{{ pick $d "two" -}}`: "map[two:222222]",
  78. `{{- $d := dict "one" 1 "two" 222222 }}{{ pick $d "one" "two" | len -}}`: "2",
  79. `{{- $d := dict "one" 1 "two" 222222 }}{{ pick $d "one" "two" "three" | len -}}`: "2",
  80. `{{- $d := dict }}{{ pick $d "two" | len -}}`: "0",
  81. }
  82. for tpl, expect := range tests {
  83. if err := runt(tpl, expect); err != nil {
  84. t.Error(err)
  85. }
  86. }
  87. }
  88. func TestOmit(t *testing.T) {
  89. tests := map[string]string{
  90. `{{- $d := dict "one" 1 "two" 222222 }}{{ omit $d "one" | len -}}`: "1",
  91. `{{- $d := dict "one" 1 "two" 222222 }}{{ omit $d "one" -}}`: "map[two:222222]",
  92. `{{- $d := dict "one" 1 "two" 222222 }}{{ omit $d "one" "two" | len -}}`: "0",
  93. `{{- $d := dict "one" 1 "two" 222222 }}{{ omit $d "two" "three" | len -}}`: "1",
  94. `{{- $d := dict }}{{ omit $d "two" | len -}}`: "0",
  95. }
  96. for tpl, expect := range tests {
  97. if err := runt(tpl, expect); err != nil {
  98. t.Error(err)
  99. }
  100. }
  101. }
  102. func TestGet(t *testing.T) {
  103. tests := map[string]string{
  104. `{{- $d := dict "one" 1 }}{{ get $d "one" -}}`: "1",
  105. `{{- $d := dict "one" 1 "two" "2" }}{{ get $d "two" -}}`: "2",
  106. `{{- $d := dict }}{{ get $d "two" -}}`: "",
  107. }
  108. for tpl, expect := range tests {
  109. if err := runt(tpl, expect); err != nil {
  110. t.Error(err)
  111. }
  112. }
  113. }
  114. func TestSet(t *testing.T) {
  115. tpl := `{{- $d := dict "one" 1 "two" 222222 -}}
  116. {{- $_ := set $d "two" 2 -}}
  117. {{- $_ := set $d "three" 3 -}}
  118. {{- if hasKey $d "one" -}}{{$d.one}}{{- end -}}
  119. {{- if hasKey $d "two" -}}{{$d.two}}{{- end -}}
  120. {{- if hasKey $d "three" -}}{{$d.three}}{{- end -}}
  121. `
  122. expect := "123"
  123. if err := runt(tpl, expect); err != nil {
  124. t.Error(err)
  125. }
  126. }
  127. func TestValues(t *testing.T) {
  128. tests := map[string]string{
  129. `{{- $d := dict "a" 1 "b" 2 }}{{ values $d | sortAlpha | join "," }}`: "1,2",
  130. `{{- $d := dict "a" "first" "b" 2 }}{{ values $d | sortAlpha | join "," }}`: "2,first",
  131. }
  132. for tpl, expect := range tests {
  133. if err := runt(tpl, expect); err != nil {
  134. t.Error(err)
  135. }
  136. }
  137. }
  138. func TestDig(t *testing.T) {
  139. tests := map[string]string{
  140. `{{- $d := dict "a" (dict "b" (dict "c" 1)) }}{{ dig "a" "b" "c" "" $d }}`: "1",
  141. `{{- $d := dict "a" (dict "b" (dict "c" 1)) }}{{ dig "a" "b" "z" "2" $d }}`: "2",
  142. `{{ dict "a" 1 | dig "a" "" }}`: "1",
  143. `{{ dict "a" 1 | dig "z" "2" }}`: "2",
  144. }
  145. for tpl, expect := range tests {
  146. if err := runt(tpl, expect); err != nil {
  147. t.Error(err)
  148. }
  149. }
  150. }