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

     1  //+build integration
     2  
     3  package integration
     4  
     5  import (
     6  	. "gopkg.in/check.v1"
     7  )
     8  
     9  type TypeconvSuite struct{}
    10  
    11  var _ = Suite(&TypeconvSuite{})
    12  
    13  const (
    14  	testYAML = "foo:\\n bar:\\n  baz: qux"
    15  	testJSON = `{"foo":{"bar":{"baz":"qux"}}}`
    16  	testCsv  = `lang,keywords
    17  C,32
    18  Go,25
    19  COBOL,357`
    20  	testTsv = `lang	keywords
    21  C	32
    22  Go	25
    23  COBOL	357`
    24  )
    25  
    26  func (s *TypeconvSuite) TestTypeconvFuncs(c *C) {
    27  	//@test "'has' can handle sub-maps in nested maps" {
    28  	inOutTest(c, `{{ has ("`+testYAML+`" | yaml).foo.bar "baz"}}`,
    29  		"true")
    30  }
    31  func (s *TypeconvSuite) TestJSON(c *C) {
    32  	inOutTest(c, `{{ "`+testYAML+`" | yaml | toJSON }}`, testJSON)
    33  
    34  	inOutTest(c, `{{ `+"`"+testJSON+"`"+` | json | toJSONPretty "   " }}
    35  {{ toJSONPretty "" (`+"`"+testJSON+"`"+` | json) }}`,
    36  		`{
    37     "foo": {
    38        "bar": {
    39           "baz": "qux"
    40        }
    41     }
    42  }
    43  {
    44  "foo": {
    45  "bar": {
    46  "baz": "qux"
    47  }
    48  }
    49  }`)
    50  }
    51  
    52  func (s *TypeconvSuite) TestJoin(c *C) {
    53  	inOutTest(c, `{{ $a := "[1, 2, 3]" | jsonArray }}{{ join $a "-" }}`,
    54  		"1-2-3")
    55  }
    56  
    57  func (s *TypeconvSuite) TestCSV(c *C) {
    58  	inOutTest(c, `{{ $c := `+"`"+testCsv+"`"+` | csv -}}
    59  {{ index (index $c 0) 1 }}`,
    60  		"keywords")
    61  
    62  	inOutTest(c, `{{ $c := `+"`"+testCsv+"`"+` | csvByRow -}}
    63  {{ range $c }}{{ .lang }} has {{ .keywords }} keywords.
    64  {{end}}`,
    65  		`C has 32 keywords.
    66  Go has 25 keywords.
    67  COBOL has 357 keywords.`)
    68  
    69  	inOutTest(c, `{{ $c := `+"`"+testTsv+"`"+` | csvByColumn "\t" -}}
    70  Languages are: {{ join $c.lang " and " }}`,
    71  		"Languages are: C and Go and COBOL")
    72  }
    73  
    74  func (s *TypeconvSuite) TestTOML(c *C) {
    75  	inOutTest(c, `{{ $t := `+"`"+`# comment
    76  foo = "bar"
    77  
    78  [baz]
    79  qux = "quux"`+"`"+` | toml -}}
    80  {{ $t.baz.qux }}`, "quux")
    81  
    82  	inOutTest(c, `{{ "foo:\n bar:\n  baz: qux" | yaml | toTOML }}`,
    83  		`[foo]
    84    [foo.bar]
    85      baz = "qux"`)
    86  }
    87  
    88  func (s *TypeconvSuite) TestDict(c *C) {
    89  	inOutTest(c, `{{ $d := dict true false "foo" "bar" }}{{ data.ToJSON $d }}`,
    90  		`{"foo":"bar","true":false}`)
    91  }