Skip to content

Commit

Permalink
test: Add text/template smoke test
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy authored and niaow committed Feb 24, 2022
1 parent cfed3f0 commit b406422
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ test-corpus-wasi: wasi-libc
.PHONY: smoketest
smoketest:
$(TINYGO) version
# compile-only platform-independent examples
cd tests/text/template/smoke && $(TINYGO) test -c && rm -f smoke.test
# regression test for #2563
cd tests/os/smoke && $(TINYGO) test -c -target=pybadge && rm smoke.test
# test all examples (except pwm)
Expand Down
4 changes: 4 additions & 0 deletions tests/text/template/smoke/empty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package template_smoke

func Function() {
}
112 changes: 112 additions & 0 deletions tests/text/template/smoke/smoke_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit b406422

Please sign in to comment.