diff options
author | Kyle Conroy <[email protected]> | 2022-02-08 09:53:56 -0800 |
---|---|---|
committer | Nia <[email protected]> | 2022-02-24 12:32:40 -0500 |
commit | b406422277bea9821798cfc776cbb1b4c6917ef5 (patch) | |
tree | 08ecf2cadafea2d97d0b36e9202c281df49a8aef /tests | |
parent | cfed3f02136485796b59e59050115d6139fbab6f (diff) | |
download | tinygo-b406422277bea9821798cfc776cbb1b4c6917ef5.tar.gz tinygo-b406422277bea9821798cfc776cbb1b4c6917ef5.zip |
test: Add text/template smoke test
Diffstat (limited to 'tests')
-rw-r--r-- | tests/text/template/smoke/empty.go | 4 | ||||
-rw-r--r-- | tests/text/template/smoke/smoke_test.go | 112 |
2 files changed, 116 insertions, 0 deletions
diff --git a/tests/text/template/smoke/empty.go b/tests/text/template/smoke/empty.go new file mode 100644 index 000000000..cbde21c39 --- /dev/null +++ b/tests/text/template/smoke/empty.go @@ -0,0 +1,4 @@ +package template_smoke + +func Function() { +} diff --git a/tests/text/template/smoke/smoke_test.go b/tests/text/template/smoke/smoke_test.go new file mode 100644 index 000000000..c097f9e9a --- /dev/null +++ b/tests/text/template/smoke/smoke_test.go @@ -0,0 +1,112 @@ +//go:build go1.16 +// +build go1.16 + +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +package template_smoke_test + +import ( + "os" + "strings" + "testing" + "text/template" +) + +func TestExampleTemplate(tt *testing.T) { + // Define a template. + const letter = ` +Dear {{.Name}}, +{{if .Attended}} +It was a pleasure to see you at the wedding. +{{- else}} +It is a shame you couldn't make it to the wedding. +{{- end}} +{{with .Gift -}} +Thank you for the lovely {{.}}. +{{end}} +Best wishes, +Josie +` + + // Prepare some data to insert into the template. + type Recipient struct { + Name, Gift string + Attended bool + } + var recipients = []Recipient{ + {"Aunt Mildred", "bone china tea set", true}, + {"Uncle John", "moleskin pants", false}, + {"Cousin Rodney", "", false}, + } + + // Create a new template and parse the letter into it. + t := template.Must(template.New("letter").Parse(letter)) + + // Execute the template for each recipient. + for _, r := range recipients { + err := t.Execute(os.Stdout, r) + if err != nil { + tt.Log("executing template:", err) + } + } + + // Output: + // Dear Aunt Mildred, + // + // It was a pleasure to see you at the wedding. + // Thank you for the lovely bone china tea set. + // + // Best wishes, + // Josie + // + // Dear Uncle John, + // + // It is a shame you couldn't make it to the wedding. + // Thank you for the lovely moleskin pants. + // + // Best wishes, + // Josie + // + // Dear Cousin Rodney, + // + // It is a shame you couldn't make it to the wedding. + // + // Best wishes, + // Josie +} + +// The following example is duplicated in html/template; keep them in sync. + +func TestExampleTemplate_block(tt *testing.T) { + const ( + master = `Names:{{block "list" .}}{{"\n"}}{{range .}}{{println "-" .}}{{end}}{{end}}` + overlay = `{{define "list"}} {{join . ", "}}{{end}} ` + ) + var ( + funcs = template.FuncMap{"join": strings.Join} + guardians = []string{"Gamora", "Groot", "Nebula", "Rocket", "Star-Lord"} + ) + masterTmpl, err := template.New("master").Funcs(funcs).Parse(master) + if err != nil { + tt.Fatal(err) + } + overlayTmpl, err := template.Must(masterTmpl.Clone()).Parse(overlay) + if err != nil { + tt.Fatal(err) + } + if err := masterTmpl.Execute(os.Stdout, guardians); err != nil { + tt.Fatal(err) + } + if err := overlayTmpl.Execute(os.Stdout, guardians); err != nil { + tt.Fatal(err) + } + // Output: + // Names: + // - Gamora + // - Groot + // - Nebula + // - Rocket + // - Star-Lord + // Names: Gamora, Groot, Nebula, Rocket, Star-Lord +} |