github.com/hairyhenderson/gomplate/v3@v3.11.7/internal/tests/integration/collection_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/v3/fs"
     7  )
     8  
     9  func setupCollTest(t *testing.T) *fs.Dir {
    10  	tmpDir := fs.NewDir(t, "gomplate-inttests",
    11  		fs.WithFiles(map[string]string{
    12  			"defaults.yaml": `values:
    13    one: 1
    14    two: 2
    15    three:
    16      - 4
    17    four:
    18      a: a
    19      b: b
    20  `,
    21  			"config.json": `{
    22  				"values": {
    23  					"one": "uno",
    24  					"three": [ 5, 6, 7 ],
    25  					"four": { "a": "eh?" }
    26  				}
    27  			}`,
    28  		}))
    29  	t.Cleanup(tmpDir.Remove)
    30  
    31  	return tmpDir
    32  }
    33  
    34  func TestColl_Merge(t *testing.T) {
    35  	tmpDir := setupCollTest(t)
    36  	o, e, err := cmd(t,
    37  		"-d", "defaults="+tmpDir.Join("defaults.yaml"),
    38  		"-d", "config="+tmpDir.Join("config.json"),
    39  		"-i", `{{ $defaults := ds "defaults" -}}
    40  		{{ $config := ds "config" -}}
    41  		{{ $merged := coll.Merge $config $defaults -}}
    42  		{{ $merged | data.ToYAML }}`).run()
    43  	assertSuccess(t, o, e, err, `values:
    44    four:
    45      a: eh?
    46      b: b
    47    one: uno
    48    three:
    49      - 5
    50      - 6
    51      - 7
    52    two: 2
    53  `)
    54  }
    55  
    56  func TestColl_Sort(t *testing.T) {
    57  	inOutTest(t, `{{ $maps := jsonArray "[{\"a\": \"foo\", \"b\": 1}, {\"a\": \"bar\", \"b\": 8}, {\"a\": \"baz\", \"b\": 3}]" -}}
    58  {{ range coll.Sort "b" $maps -}}
    59  {{ .a }}
    60  {{ end -}}
    61  `, "foo\nbaz\nbar\n")
    62  
    63  	inOutTest(t, `
    64  {{- coll.Sort (slice "b" "a" "c" "aa") }}
    65  {{ coll.Sort (slice "b" 14 "c" "aa") }}
    66  {{ coll.Sort (slice 3.14 3.0 4.0) }}
    67  {{ coll.Sort "Scheme" (coll.Slice (conv.URL "zzz:///") (conv.URL "https:///") (conv.URL "http:///")) }}
    68  `, `[a aa b c]
    69  [b 14 c aa]
    70  [3 3.14 4]
    71  [http:/// https:/// zzz:///]
    72  `)
    73  }
    74  
    75  func TestColl_JSONPath(t *testing.T) {
    76  	tmpDir := setupCollTest(t)
    77  	o, e, err := cmd(t, "-c", "config="+tmpDir.Join("config.json"),
    78  		"-i", `{{ .config | jsonpath ".*.three" }}`).run()
    79  	assertSuccess(t, o, e, err, `[5 6 7]`)
    80  
    81  	o, e, err = cmd(t, "-c", "config="+tmpDir.Join("config.json"),
    82  		"-i", `{{ .config | coll.JSONPath ".values..a" }}`).run()
    83  	assertSuccess(t, o, e, err, `eh?`)
    84  }
    85  
    86  func TestColl_Flatten(t *testing.T) {
    87  	in := "[[1,2],[],[[3,4],[[[5],6],7]]]"
    88  	inOutTest(t, "{{ `"+in+"` | jsonArray | coll.Flatten | toJSON }}", "[1,2,3,4,5,6,7]")
    89  	inOutTest(t, "{{ `"+in+"` | jsonArray | flatten 0 | toJSON }}", in)
    90  	inOutTest(t, "{{ coll.Flatten 1 (`"+in+"` | jsonArray) | toJSON }}", "[1,2,[3,4],[[[5],6],7]]")
    91  	inOutTest(t, "{{ `"+in+"` | jsonArray | coll.Flatten 2 | toJSON }}", "[1,2,3,4,[[5],6],7]")
    92  }
    93  
    94  func TestColl_Pick(t *testing.T) {
    95  	inOutTest(t, `{{ $data := dict "foo" 1 "bar" 2 "baz" 3 }}{{ coll.Pick "foo" "baz" $data }}`, "map[baz:3 foo:1]")
    96  }
    97  
    98  func TestColl_Omit(t *testing.T) {
    99  	inOutTest(t, `{{ $data := dict "foo" 1 "bar" 2 "baz" 3 }}{{ coll.Omit "foo" "baz" $data }}`, "map[bar:2]")
   100  }