github.com/hairyhenderson/gomplate/v3@v3.11.7/internal/tests/integration/typeconv_test.go (about) 1 package integration 2 3 import ( 4 "fmt" 5 "testing" 6 ) 7 8 const ( 9 testYAML = "foo:\\n bar:\\n baz: qux" 10 testJSON = `{"foo":{"bar":{"baz":"qux"}}}` 11 testCsv = `lang,keywords 12 C,32 13 Go,25 14 COBOL,357` 15 testTsv = `lang keywords 16 C 32 17 Go 25 18 COBOL 357` 19 ) 20 21 func TestTypeconv_TypeconvFuncs(t *testing.T) { 22 inOutTest(t, `{{ has ("`+testYAML+`" | yaml).foo.bar "baz"}}`, 23 "true") 24 } 25 26 func TestTypeconv_JSON(t *testing.T) { 27 inOutTest(t, `{{ "`+testYAML+`" | yaml | toJSON }}`, testJSON) 28 29 inOutTest(t, `{{ `+"`"+testJSON+"`"+` | json | toJSONPretty " " }} 30 {{ toJSONPretty "" (`+"`"+testJSON+"`"+` | json) }}`, 31 `{ 32 "foo": { 33 "bar": { 34 "baz": "qux" 35 } 36 } 37 } 38 { 39 "foo": { 40 "bar": { 41 "baz": "qux" 42 } 43 } 44 }`) 45 } 46 47 func TestTypeconv_Join(t *testing.T) { 48 inOutTest(t, `{{ $a := "[1, 2, 3]" | jsonArray }}{{ join $a "-" }}`, 49 "1-2-3") 50 } 51 52 func TestTypeconv_CSV(t *testing.T) { 53 inOutTest(t, fmt.Sprintf(`{{ $c := %q | csv -}} 54 {{ index (index $c 0) 1 }}`, testCsv), 55 "keywords") 56 57 inOutTest(t, fmt.Sprintf(`{{ $c := %q | csvByRow -}} 58 {{ range $c }}{{ .lang }} has {{ .keywords }} keywords. 59 {{end}}`, testCsv), 60 `C has 32 keywords. 61 Go has 25 keywords. 62 COBOL has 357 keywords. 63 `) 64 65 inOutTest(t, fmt.Sprintf(`{{ $c := %q | csvByColumn "\t" -}} 66 Languages are: {{ join $c.lang " and " }}`, testTsv), 67 "Languages are: C and Go and COBOL") 68 } 69 70 func TestTypeconv_TOML(t *testing.T) { 71 tomlIn := `# comment 72 foo = "bar" 73 74 [baz] 75 qux = "quux"` 76 inOutTest(t, fmt.Sprintf(`{{ $t := %q | toml }}{{ $t.baz.qux }}`, tomlIn), "quux") 77 78 inOutTest(t, `{{ "foo:\n bar:\n baz: qux" | yaml | toTOML }}`, 79 `[foo] 80 [foo.bar] 81 baz = "qux" 82 `) 83 } 84 85 func TestTypeconv_Dict(t *testing.T) { 86 inOutTest(t, `{{ $d := dict true false "foo" "bar" }}{{ data.ToJSON $d }}`, 87 `{"foo":"bar","true":false}`) 88 }