aboutsummaryrefslogtreecommitdiff
path: root/src/text/template/exec_test.go
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2015-04-03 13:10:47 -0700
committerRob Pike <r@golang.org>2015-04-03 23:40:59 +0000
commit4e5ac45ec5c15dba0f57f80e923a0de6f9f4f511 (patch)
tree93c3b557f655573d1692d9542e17ab384e95805b /src/text/template/exec_test.go
parent4f2a73011f2d076d97a2b46492ab21f26a33222b (diff)
downloadgo-4e5ac45ec5c15dba0f57f80e923a0de6f9f4f511.tar.xz
text/template: provide a mechanism for options
Add one option, which is the motivating example, a way to control what happens when a map is indexed with a key that is not in the map. Rather than do something specific for that case, we provide a simple general option mechanism to avoid adding API if something else comes up. This general approach also makes it easy for html/template to track (and adapt, should that become important). New method: Option(option string...). The option strings are key=value pairs or just simple strings (no =). New option: missingkey: Control the behavior during execution if a map is indexed with a key that is not present in the map. "missingkey=default" or "missingkey=invalid" The default behavior: Do nothing and continue execution. If printed, the result of the index operation is the string "<no value>". "missingkey=zero" The operation returns the zero value for the map type's element. "missingkey=error" Execution stops immediately with an error. Fixes #6288. Change-Id: Id811e2b99dc05aff324d517faac113ef3c25293a Reviewed-on: https://go-review.googlesource.com/8462 Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/text/template/exec_test.go')
-rw-r--r--src/text/template/exec_test.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/text/template/exec_test.go b/src/text/template/exec_test.go
index b1f778797b..8c4e165f2f 100644
--- a/src/text/template/exec_test.go
+++ b/src/text/template/exec_test.go
@@ -1044,3 +1044,54 @@ func TestComparison(t *testing.T) {
}
}
}
+
+func TestMissingMapKey(t *testing.T) {
+ data := map[string]int{
+ "x": 99,
+ }
+ tmpl, err := New("t1").Parse("{{.x}} {{.y}}")
+ if err != nil {
+ t.Fatal(err)
+ }
+ var b bytes.Buffer
+ // By default, just get "<no value>"
+ err = tmpl.Execute(&b, data)
+ if err != nil {
+ t.Fatal(err)
+ }
+ want := "99 <no value>"
+ got := b.String()
+ if got != want {
+ t.Errorf("got %q; expected %q", got, want)
+ }
+ // Same if we set the option explicitly to the default.
+ tmpl.Option("missingkey=default")
+ b.Reset()
+ err = tmpl.Execute(&b, data)
+ if err != nil {
+ t.Fatal("default:", err)
+ }
+ want = "99 <no value>"
+ got = b.String()
+ if got != want {
+ t.Errorf("got %q; expected %q", got, want)
+ }
+ // Next we ask for a zero value
+ tmpl.Option("missingkey=zero")
+ b.Reset()
+ err = tmpl.Execute(&b, data)
+ if err != nil {
+ t.Fatal("zero:", err)
+ }
+ want = "99 0"
+ got = b.String()
+ if got != want {
+ t.Errorf("got %q; expected %q", got, want)
+ }
+ // Now we ask for an error.
+ tmpl.Option("missingkey=error")
+ err = tmpl.Execute(&b, data)
+ if err == nil {
+ t.Errorf("expected error; got none")
+ }
+}