aboutsummaryrefslogtreecommitdiff
path: root/src/text/template/exec_test.go
diff options
context:
space:
mode:
authorAriel Mashraki <ariel@mashraki.co.il>2019-02-10 22:44:03 +0200
committerRob Pike <r@golang.org>2019-05-23 08:01:24 +0000
commite2970a4591084d25c8bbd3e0648f8228f9defa2e (patch)
treee91f640aac9ffaa80f04fcd8b1108992bdf78e6e /src/text/template/exec_test.go
parent6f51082da77a1d4cafd5b7af0db69293943f4066 (diff)
downloadgo-e2970a4591084d25c8bbd3e0648f8228f9defa2e.tar.xz
text/template: add a slice function to the predefined global functions
The new slice function returns the result of slicing its first argument by the following arguments. Thus {{slice x 1 3}} is, in Go syntax, x[1:3]. Each sliced item must be a string, slice, or array. Closed #30153 RELNOTE=yes Change-Id: I63188c422848cee3d383a64dc4d046e3a1767c63 Reviewed-on: https://go-review.googlesource.com/c/go/+/161762 Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/text/template/exec_test.go')
-rw-r--r--src/text/template/exec_test.go33
1 files changed, 32 insertions, 1 deletions
diff --git a/src/text/template/exec_test.go b/src/text/template/exec_test.go
index 63ccd5c3c0..81f9e04476 100644
--- a/src/text/template/exec_test.go
+++ b/src/text/template/exec_test.go
@@ -23,7 +23,7 @@ type T struct {
True bool
I int
U16 uint16
- X string
+ X, S string
FloatZero float64
ComplexZero complex128
// Nested structs.
@@ -36,8 +36,11 @@ type T struct {
W1, W2 *W
// Slices
SI []int
+ SICap []int
SIEmpty []int
SB []bool
+ // Arrays
+ AI [3]int
// Maps
MSI map[string]int
MSIone map[string]int // one element, for deterministic output
@@ -122,12 +125,15 @@ var tVal = &T{
I: 17,
U16: 16,
X: "x",
+ S: "xyz",
U: &U{"v"},
V0: V{6666},
V1: &V{7777}, // leave V2 as nil
W0: W{888},
W1: &W{999}, // leave W2 as nil
SI: []int{3, 4, 5},
+ SICap: make([]int, 5, 10),
+ AI: [3]int{3, 4, 5},
SB: []bool{true, false},
MSI: map[string]int{"one": 1, "two": 2, "three": 3},
MSIone: map[string]int{"one": 1},
@@ -491,6 +497,31 @@ var execTests = []execTest{
{"map MI8S", "{{index .MI8S 3}}", "i83", tVal, true},
{"map MUI8S", "{{index .MUI8S 2}}", "u82", tVal, true},
+ // Slicing.
+ {"slice[:]", "{{slice .SI}}", "[3 4 5]", tVal, true},
+ {"slice[1:]", "{{slice .SI 1}}", "[4 5]", tVal, true},
+ {"slice[1:2]", "{{slice .SI 1 2}}", "[4]", tVal, true},
+ {"slice[-1:]", "{{slice .SI -1}}", "", tVal, false},
+ {"slice[1:-2]", "{{slice .SI 1 -2}}", "", tVal, false},
+ {"slice[1:2:-1]", "{{slice .SI 1 2 -1}}", "", tVal, false},
+ {"slice[2:1]", "{{slice .SI 2 1}}", "", tVal, false},
+ {"slice[2:2:1]", "{{slice .SI 2 2 1}}", "", tVal, false},
+ {"out of range", "{{slice .SI 4 5}}", "", tVal, false},
+ {"out of range", "{{slice .SI 2 2 5}}", "", tVal, false},
+ {"len(s) < indexes < cap(s)", "{{slice .SICap 6 10}}", "[0 0 0 0]", tVal, true},
+ {"len(s) < indexes < cap(s)", "{{slice .SICap 6 10 10}}", "[0 0 0 0]", tVal, true},
+ {"indexes > cap(s)", "{{slice .SICap 10 11}}", "", tVal, false},
+ {"indexes > cap(s)", "{{slice .SICap 6 10 11}}", "", tVal, false},
+ {"array[:]", "{{slice .AI}}", "[3 4 5]", tVal, true},
+ {"array[1:]", "{{slice .AI 1}}", "[4 5]", tVal, true},
+ {"array[1:2]", "{{slice .AI 1 2}}", "[4]", tVal, true},
+ {"string[:]", "{{slice .S}}", "xyz", tVal, true},
+ {"string[0:1]", "{{slice .S 0 1}}", "x", tVal, true},
+ {"string[1:]", "{{slice .S 1}}", "yz", tVal, true},
+ {"string[1:2]", "{{slice .S 1 2}}", "y", tVal, true},
+ {"out of range", "{{slice .S 1 5}}", "", tVal, false},
+ {"3-index slice of string", "{{slice .S 1 2 2}}", "", tVal, false},
+
// Len.
{"slice", "{{len .SI}}", "3", tVal, true},
{"map", "{{len .MSI }}", "3", tVal, true},