github.com/wuhuizuo/gomplate@v3.5.0+incompatible/tests/integration/collection_test.go (about)

     1  //+build integration
     2  
     3  package integration
     4  
     5  import (
     6  	. "gopkg.in/check.v1"
     7  
     8  	"github.com/gotestyourself/gotestyourself/fs"
     9  	"github.com/gotestyourself/gotestyourself/icmd"
    10  )
    11  
    12  type CollSuite struct {
    13  	tmpDir *fs.Dir
    14  }
    15  
    16  var _ = Suite(&CollSuite{})
    17  
    18  func (s *CollSuite) SetUpTest(c *C) {
    19  	s.tmpDir = fs.NewDir(c, "gomplate-inttests",
    20  		fs.WithFiles(map[string]string{
    21  			"defaults.yaml": `values:
    22    one: 1
    23    two: 2
    24    three:
    25      - 4
    26    four:
    27      a: a
    28      b: b
    29  `,
    30  			"config.json": `{
    31  				"values": {
    32  					"one": "uno",
    33  					"three": [ 5, 6, 7 ],
    34  					"four": { "a": "eh?" }
    35  				}
    36  			}`,
    37  		}))
    38  }
    39  
    40  func (s *CollSuite) TearDownTest(c *C) {
    41  	s.tmpDir.Remove()
    42  }
    43  
    44  func (s *CollSuite) TestMerge(c *C) {
    45  	result := icmd.RunCmd(icmd.Command(GomplateBin,
    46  		"-d", "defaults="+s.tmpDir.Join("defaults.yaml"),
    47  		"-d", "config="+s.tmpDir.Join("config.json"),
    48  		"-i", `{{ $defaults := ds "defaults" -}}
    49  		{{ $config := ds "config" -}}
    50  		{{ $merged := coll.Merge $config $defaults -}}
    51  		{{ $merged | data.ToYAML }}
    52  `))
    53  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: `values:
    54    four:
    55      a: eh?
    56      b: b
    57    one: uno
    58    three:
    59    - 5
    60    - 6
    61    - 7
    62    two: 2
    63  `})
    64  }
    65  
    66  func (s *CollSuite) TestSort(c *C) {
    67  	result := icmd.RunCmd(icmd.Command(GomplateBin,
    68  		"-i", `{{ $maps := jsonArray "[{\"a\": \"foo\", \"b\": 1}, {\"a\": \"bar\", \"b\": 8}, {\"a\": \"baz\", \"b\": 3}]" -}}
    69  {{ range coll.Sort "b" $maps -}}
    70  {{ .a }}
    71  {{ end -}}
    72  `))
    73  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "foo\nbaz\nbar\n"})
    74  
    75  	result = icmd.RunCmd(icmd.Command(GomplateBin,
    76  		"-i", `
    77  {{- coll.Sort (slice "b" "a" "c" "aa") }}
    78  {{ coll.Sort (slice "b" 14 "c" "aa") }}
    79  {{ coll.Sort (slice 3.14 3.0 4.0) }}
    80  {{ coll.Sort "Scheme" (coll.Slice (conv.URL "zzz:///") (conv.URL "https:///") (conv.URL "http:///")) }}
    81  `))
    82  	result.Assert(c, icmd.Expected{ExitCode: 0,
    83  		Out: `[a aa b c]
    84  [b 14 c aa]
    85  [3 3.14 4]
    86  [http:/// https:/// zzz:///]
    87  `,
    88  	})
    89  }
    90  
    91  func (s *CollSuite) TestJSONPath(c *C) {
    92  	result := icmd.RunCmd(icmd.Command(GomplateBin,
    93  		"-c", "config="+s.tmpDir.Join("config.json"),
    94  		"-i", `{{ .config | jsonpath ".*.three" }}`))
    95  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: `[5 6 7]`})
    96  
    97  	result = icmd.RunCmd(icmd.Command(GomplateBin,
    98  		"-c", "config="+s.tmpDir.Join("config.json"),
    99  		"-i", `{{ .config | coll.JSONPath ".values..a" }}`))
   100  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: `eh?`})
   101  }