Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 1 | // Copyright 2011 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package template |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
Russ Cox | e73680a | 2011-11-04 07:33:55 -0400 | [diff] [blame] | 9 | "errors" |
Rob Pike | c66917d | 2011-08-09 15:42:53 +1000 | [diff] [blame] | 10 | "flag" |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 11 | "fmt" |
Rob Pike | 7c47741 | 2011-07-12 13:15:26 +1000 | [diff] [blame] | 12 | "reflect" |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 13 | "strings" |
| 14 | "testing" |
| 15 | ) |
| 16 | |
Rob Pike | c66917d | 2011-08-09 15:42:53 +1000 | [diff] [blame] | 17 | var debug = flag.Bool("debug", false, "show the errors produced by the tests") |
| 18 | |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 19 | // T has lots of interesting pieces to use to test execution. |
| 20 | type T struct { |
| 21 | // Basics |
Rob Pike | c3344d6 | 2011-07-14 07:52:07 +1000 | [diff] [blame] | 22 | True bool |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 23 | I int |
| 24 | U16 uint16 |
| 25 | X string |
| 26 | FloatZero float64 |
| 27 | ComplexZero float64 |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 28 | // Nested structs. |
| 29 | U *U |
Rob Pike | 6ca968c | 2011-08-11 14:36:51 +1000 | [diff] [blame] | 30 | // Struct with String method. |
| 31 | V0 V |
| 32 | V1, V2 *V |
David Symonds | 39fcca6 | 2011-11-04 23:45:38 +1100 | [diff] [blame] | 33 | // Struct with Error method. |
| 34 | W0 W |
| 35 | W1, W2 *W |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 36 | // Slices |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 37 | SI []int |
| 38 | SIEmpty []int |
| 39 | SB []bool |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 40 | // Maps |
| 41 | MSI map[string]int |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 42 | MSIone map[string]int // one element, for deterministic output |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 43 | MSIEmpty map[string]int |
Gustavo Niemeyer | e3df71f | 2011-08-15 00:56:01 -0300 | [diff] [blame] | 44 | MXI map[interface{}]int |
| 45 | MII map[int]int |
Rob Pike | 6732bd3 | 2011-07-06 22:27:06 +1000 | [diff] [blame] | 46 | SMSI []map[string]int |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 47 | // Empty interfaces; used to see if we can dig inside one. |
| 48 | Empty0 interface{} // nil |
| 49 | Empty1 interface{} |
| 50 | Empty2 interface{} |
| 51 | Empty3 interface{} |
| 52 | Empty4 interface{} |
Rob Pike | 1adda46 | 2011-08-09 17:07:29 +1000 | [diff] [blame] | 53 | // Non-empty interface. |
| 54 | NonEmptyInterface I |
David Symonds | fe59d86 | 2011-08-12 13:29:56 +1000 | [diff] [blame] | 55 | // Stringer. |
| 56 | Str fmt.Stringer |
Russ Cox | e73680a | 2011-11-04 07:33:55 -0400 | [diff] [blame] | 57 | Err error |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 58 | // Pointers |
| 59 | PI *int |
| 60 | PSI *[]int |
| 61 | NIL *int |
Rob Pike | aca8071 | 2012-02-15 16:05:34 +1100 | [diff] [blame] | 62 | // Function (not method) |
Rob Pike | f1d3ff1 | 2012-03-03 23:14:20 +1100 | [diff] [blame] | 63 | BinaryFunc func(string, string) string |
| 64 | VariadicFunc func(...string) string |
| 65 | VariadicFuncInt func(int, ...string) string |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 66 | NilOKFunc func(*int) bool |
Rob Pike | 3987b91 | 2011-07-10 07:32:01 +1000 | [diff] [blame] | 67 | // Template to test evaluation of templates. |
| 68 | Tmpl *Template |
Rob Pike | a8098cb | 2012-04-23 15:39:02 +1000 | [diff] [blame] | 69 | // Unexported field; cannot be accessed by template. |
| 70 | unexported int |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 71 | } |
| 72 | |
Rob Pike | a852981 | 2011-07-08 15:22:05 +1000 | [diff] [blame] | 73 | type U struct { |
| 74 | V string |
| 75 | } |
| 76 | |
Rob Pike | 6ca968c | 2011-08-11 14:36:51 +1000 | [diff] [blame] | 77 | type V struct { |
| 78 | j int |
| 79 | } |
| 80 | |
| 81 | func (v *V) String() string { |
| 82 | if v == nil { |
| 83 | return "nilV" |
| 84 | } |
| 85 | return fmt.Sprintf("<%d>", v.j) |
| 86 | } |
| 87 | |
David Symonds | 39fcca6 | 2011-11-04 23:45:38 +1100 | [diff] [blame] | 88 | type W struct { |
| 89 | k int |
| 90 | } |
| 91 | |
| 92 | func (w *W) Error() string { |
| 93 | if w == nil { |
| 94 | return "nilW" |
| 95 | } |
| 96 | return fmt.Sprintf("[%d]", w.k) |
| 97 | } |
| 98 | |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 99 | var tVal = &T{ |
Rob Pike | c3344d6 | 2011-07-14 07:52:07 +1000 | [diff] [blame] | 100 | True: true, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 101 | I: 17, |
| 102 | U16: 16, |
| 103 | X: "x", |
| 104 | U: &U{"v"}, |
Rob Pike | 6ca968c | 2011-08-11 14:36:51 +1000 | [diff] [blame] | 105 | V0: V{6666}, |
| 106 | V1: &V{7777}, // leave V2 as nil |
David Symonds | 39fcca6 | 2011-11-04 23:45:38 +1100 | [diff] [blame] | 107 | W0: W{888}, |
| 108 | W1: &W{999}, // leave W2 as nil |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 109 | SI: []int{3, 4, 5}, |
| 110 | SB: []bool{true, false}, |
| 111 | MSI: map[string]int{"one": 1, "two": 2, "three": 3}, |
| 112 | MSIone: map[string]int{"one": 1}, |
Gustavo Niemeyer | e3df71f | 2011-08-15 00:56:01 -0300 | [diff] [blame] | 113 | MXI: map[interface{}]int{"one": 1}, |
| 114 | MII: map[int]int{1: 1}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 115 | SMSI: []map[string]int{ |
| 116 | {"one": 1, "two": 2}, |
| 117 | {"eleven": 11, "twelve": 12}, |
| 118 | }, |
Rob Pike | 1adda46 | 2011-08-09 17:07:29 +1000 | [diff] [blame] | 119 | Empty1: 3, |
| 120 | Empty2: "empty2", |
| 121 | Empty3: []int{7, 8}, |
| 122 | Empty4: &U{"UinEmpty"}, |
| 123 | NonEmptyInterface: new(T), |
Russ Cox | 853c846 | 2011-10-27 21:17:47 -0700 | [diff] [blame] | 124 | Str: bytes.NewBuffer([]byte("foozle")), |
Russ Cox | e73680a | 2011-11-04 07:33:55 -0400 | [diff] [blame] | 125 | Err: errors.New("erroozle"), |
Rob Pike | 1adda46 | 2011-08-09 17:07:29 +1000 | [diff] [blame] | 126 | PI: newInt(23), |
| 127 | PSI: newIntSlice(21, 22, 23), |
Rob Pike | f1d3ff1 | 2012-03-03 23:14:20 +1100 | [diff] [blame] | 128 | BinaryFunc: func(a, b string) string { return fmt.Sprintf("[%s=%s]", a, b) }, |
| 129 | VariadicFunc: func(s ...string) string { return fmt.Sprint("<", strings.Join(s, "+"), ">") }, |
| 130 | VariadicFuncInt: func(a int, s ...string) string { return fmt.Sprint(a, "=<", strings.Join(s, "+"), ">") }, |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 131 | NilOKFunc: func(s *int) bool { return s == nil }, |
Rob Pike | 1adda46 | 2011-08-09 17:07:29 +1000 | [diff] [blame] | 132 | Tmpl: Must(New("x").Parse("test template")), // "x" is the value of .X |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 133 | } |
| 134 | |
Robert Griesemer | 7162f39 | 2011-07-30 17:11:52 -0700 | [diff] [blame] | 135 | // A non-empty interface. |
| 136 | type I interface { |
| 137 | Method0() string |
| 138 | } |
| 139 | |
| 140 | var iVal I = tVal |
| 141 | |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 142 | // Helpers for creation. |
| 143 | func newInt(n int) *int { |
| 144 | p := new(int) |
| 145 | *p = n |
| 146 | return p |
| 147 | } |
| 148 | |
| 149 | func newIntSlice(n ...int) *[]int { |
| 150 | p := new([]int) |
| 151 | *p = make([]int, len(n)) |
| 152 | copy(*p, n) |
| 153 | return p |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Simple methods with and without arguments. |
| 157 | func (t *T) Method0() string { |
Rob Pike | a852981 | 2011-07-08 15:22:05 +1000 | [diff] [blame] | 158 | return "M0" |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | func (t *T) Method1(a int) int { |
| 162 | return a |
| 163 | } |
| 164 | |
| 165 | func (t *T) Method2(a uint16, b string) string { |
| 166 | return fmt.Sprintf("Method2: %d %s", a, b) |
| 167 | } |
| 168 | |
Gustavo Niemeyer | 12f473f | 2011-12-18 22:14:11 -0200 | [diff] [blame] | 169 | func (t *T) Method3(v interface{}) string { |
| 170 | return fmt.Sprintf("Method3: %v", v) |
| 171 | } |
| 172 | |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 173 | func (t *T) MAdd(a int, b []int) []int { |
| 174 | v := make([]int, len(b)) |
| 175 | for i, x := range b { |
| 176 | v[i] = x + a |
| 177 | } |
| 178 | return v |
| 179 | } |
| 180 | |
Rob Pike | 47424d9 | 2012-02-14 07:11:39 +1100 | [diff] [blame] | 181 | var myError = errors.New("my error") |
| 182 | |
| 183 | // MyError returns a value and an error according to its argument. |
| 184 | func (t *T) MyError(error bool) (bool, error) { |
Rob Pike | c756a19 | 2011-06-29 15:02:04 +1000 | [diff] [blame] | 185 | if error { |
Rob Pike | 47424d9 | 2012-02-14 07:11:39 +1100 | [diff] [blame] | 186 | return true, myError |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 187 | } |
Rob Pike | c756a19 | 2011-06-29 15:02:04 +1000 | [diff] [blame] | 188 | return false, nil |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 189 | } |
| 190 | |
Rob Pike | c3344d6 | 2011-07-14 07:52:07 +1000 | [diff] [blame] | 191 | // A few methods to test chaining. |
| 192 | func (t *T) GetU() *U { |
| 193 | return t.U |
| 194 | } |
| 195 | |
| 196 | func (u *U) TrueFalse(b bool) string { |
| 197 | if b { |
| 198 | return "true" |
| 199 | } |
| 200 | return "" |
| 201 | } |
| 202 | |
Rob Pike | 7c47741 | 2011-07-12 13:15:26 +1000 | [diff] [blame] | 203 | func typeOf(arg interface{}) string { |
| 204 | return fmt.Sprintf("%T", arg) |
| 205 | } |
| 206 | |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 207 | type execTest struct { |
| 208 | name string |
| 209 | input string |
| 210 | output string |
| 211 | data interface{} |
| 212 | ok bool |
| 213 | } |
| 214 | |
Rob Pike | 7c47741 | 2011-07-12 13:15:26 +1000 | [diff] [blame] | 215 | // bigInt and bigUint are hex string representing numbers either side |
| 216 | // of the max int boundary. |
| 217 | // We do it this way so the test doesn't depend on ints being 32 bits. |
| 218 | var ( |
| 219 | bigInt = fmt.Sprintf("0x%x", int(1<<uint(reflect.TypeOf(0).Bits()-1)-1)) |
| 220 | bigUint = fmt.Sprintf("0x%x", uint(1<<uint(reflect.TypeOf(0).Bits()-1))) |
| 221 | ) |
| 222 | |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 223 | var execTests = []execTest{ |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 224 | // Trivial cases. |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 225 | {"empty", "", "", nil, true}, |
| 226 | {"text", "some text", "some text", nil, true}, |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 227 | {"nil action", "{{nil}}", "", nil, false}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 228 | |
Rob Pike | 7c47741 | 2011-07-12 13:15:26 +1000 | [diff] [blame] | 229 | // Ideal constants. |
| 230 | {"ideal int", "{{typeOf 3}}", "int", 0, true}, |
| 231 | {"ideal float", "{{typeOf 1.0}}", "float64", 0, true}, |
| 232 | {"ideal exp float", "{{typeOf 1e1}}", "float64", 0, true}, |
| 233 | {"ideal complex", "{{typeOf 1i}}", "complex128", 0, true}, |
| 234 | {"ideal int", "{{typeOf " + bigInt + "}}", "int", 0, true}, |
| 235 | {"ideal too big", "{{typeOf " + bigUint + "}}", "", 0, false}, |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 236 | {"ideal nil without type", "{{nil}}", "", 0, false}, |
Rob Pike | 7c47741 | 2011-07-12 13:15:26 +1000 | [diff] [blame] | 237 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 238 | // Fields of structs. |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 239 | {".X", "-{{.X}}-", "-x-", tVal, true}, |
| 240 | {".U.V", "-{{.U.V}}-", "-v-", tVal, true}, |
Rob Pike | a8098cb | 2012-04-23 15:39:02 +1000 | [diff] [blame] | 241 | {".unexported", "{{.unexported}}", "", tVal, false}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 242 | |
Gustavo Niemeyer | e3df71f | 2011-08-15 00:56:01 -0300 | [diff] [blame] | 243 | // Fields on maps. |
| 244 | {"map .one", "{{.MSI.one}}", "1", tVal, true}, |
| 245 | {"map .two", "{{.MSI.two}}", "2", tVal, true}, |
| 246 | {"map .NO", "{{.MSI.NO}}", "<no value>", tVal, true}, |
| 247 | {"map .one interface", "{{.MXI.one}}", "1", tVal, true}, |
| 248 | {"map .WRONG args", "{{.MSI.one 1}}", "", tVal, false}, |
| 249 | {"map .WRONG type", "{{.MII.one}}", "", tVal, false}, |
| 250 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 251 | // Dots of all kinds to test basic evaluation. |
| 252 | {"dot int", "<{{.}}>", "<13>", 13, true}, |
| 253 | {"dot uint", "<{{.}}>", "<14>", uint(14), true}, |
| 254 | {"dot float", "<{{.}}>", "<15.1>", 15.1, true}, |
| 255 | {"dot bool", "<{{.}}>", "<true>", true, true}, |
| 256 | {"dot complex", "<{{.}}>", "<(16.2-17i)>", 16.2 - 17i, true}, |
| 257 | {"dot string", "<{{.}}>", "<hello>", "hello", true}, |
| 258 | {"dot slice", "<{{.}}>", "<[-1 -2 -3]>", []int{-1, -2, -3}, true}, |
Russ Cox | 9a7dd71 | 2011-10-17 14:51:45 -0400 | [diff] [blame] | 259 | {"dot map", "<{{.}}>", "<map[two:22]>", map[string]int{"two": 22}, true}, |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 260 | {"dot struct", "<{{.}}>", "<{7 seven}>", struct { |
| 261 | a int |
| 262 | b string |
| 263 | }{7, "seven"}, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 264 | |
Rob Pike | 58baf64 | 2011-07-09 12:05:39 +1000 | [diff] [blame] | 265 | // Variables. |
| 266 | {"$ int", "{{$}}", "123", 123, true}, |
Rob Pike | 7b79b3b | 2011-07-11 15:23:38 +1000 | [diff] [blame] | 267 | {"$.I", "{{$.I}}", "17", tVal, true}, |
| 268 | {"$.U.V", "{{$.U.V}}", "v", tVal, true}, |
Rob Pike | 3e79c95 | 2011-07-22 10:51:40 +1000 | [diff] [blame] | 269 | {"declare in action", "{{$x := $.U.V}}{{$x}}", "v", tVal, true}, |
Rob Pike | 58baf64 | 2011-07-09 12:05:39 +1000 | [diff] [blame] | 270 | |
Rob Pike | 6ca968c | 2011-08-11 14:36:51 +1000 | [diff] [blame] | 271 | // Type with String method. |
| 272 | {"V{6666}.String()", "-{{.V0}}-", "-<6666>-", tVal, true}, |
| 273 | {"&V{7777}.String()", "-{{.V1}}-", "-<7777>-", tVal, true}, |
| 274 | {"(*V)(nil).String()", "-{{.V2}}-", "-nilV-", tVal, true}, |
| 275 | |
David Symonds | 39fcca6 | 2011-11-04 23:45:38 +1100 | [diff] [blame] | 276 | // Type with Error method. |
| 277 | {"W{888}.Error()", "-{{.W0}}-", "-[888]-", tVal, true}, |
| 278 | {"&W{999}.Error()", "-{{.W1}}-", "-[999]-", tVal, true}, |
| 279 | {"(*W)(nil).Error()", "-{{.W2}}-", "-nilW-", tVal, true}, |
| 280 | |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 281 | // Pointers. |
| 282 | {"*int", "{{.PI}}", "23", tVal, true}, |
| 283 | {"*[]int", "{{.PSI}}", "[21 22 23]", tVal, true}, |
| 284 | {"*[]int[1]", "{{index .PSI 1}}", "22", tVal, true}, |
| 285 | {"NIL", "{{.NIL}}", "<nil>", tVal, true}, |
| 286 | |
Rob Pike | d3d08e1 | 2011-07-08 18:25:46 +1000 | [diff] [blame] | 287 | // Empty interfaces holding values. |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 288 | {"empty nil", "{{.Empty0}}", "<no value>", tVal, true}, |
| 289 | {"empty with int", "{{.Empty1}}", "3", tVal, true}, |
| 290 | {"empty with string", "{{.Empty2}}", "empty2", tVal, true}, |
| 291 | {"empty with slice", "{{.Empty3}}", "[7 8]", tVal, true}, |
Rob Pike | 9a5bb28 | 2011-07-18 17:34:42 +1000 | [diff] [blame] | 292 | {"empty with struct", "{{.Empty4}}", "{UinEmpty}", tVal, true}, |
| 293 | {"empty with struct, field", "{{.Empty4.V}}", "UinEmpty", tVal, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 294 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 295 | // Method calls. |
Rob Pike | a852981 | 2011-07-08 15:22:05 +1000 | [diff] [blame] | 296 | {".Method0", "-{{.Method0}}-", "-M0-", tVal, true}, |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 297 | {".Method1(1234)", "-{{.Method1 1234}}-", "-1234-", tVal, true}, |
| 298 | {".Method1(.I)", "-{{.Method1 .I}}-", "-17-", tVal, true}, |
| 299 | {".Method2(3, .X)", "-{{.Method2 3 .X}}-", "-Method2: 3 x-", tVal, true}, |
| 300 | {".Method2(.U16, `str`)", "-{{.Method2 .U16 `str`}}-", "-Method2: 16 str-", tVal, true}, |
Rob Pike | e86d727 | 2011-07-09 17:11:35 +1000 | [diff] [blame] | 301 | {".Method2(.U16, $x)", "{{if $x := .X}}-{{.Method2 .U16 $x}}{{end}}-", "-Method2: 16 x-", tVal, true}, |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 302 | {".Method3(nil constant)", "-{{.Method3 nil}}-", "-Method3: <nil>-", tVal, true}, |
| 303 | {".Method3(nil value)", "-{{.Method3 .MXI.unset}}-", "-Method3: <nil>-", tVal, true}, |
Rob Pike | 7b79b3b | 2011-07-11 15:23:38 +1000 | [diff] [blame] | 304 | {"method on var", "{{if $x := .}}-{{$x.Method2 .U16 $x.X}}{{end}}-", "-Method2: 16 x-", tVal, true}, |
Rob Pike | c3344d6 | 2011-07-14 07:52:07 +1000 | [diff] [blame] | 305 | {"method on chained var", |
| 306 | "{{range .MSIone}}{{if $.U.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}", |
| 307 | "true", tVal, true}, |
| 308 | {"chained method", |
| 309 | "{{range .MSIone}}{{if $.GetU.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}", |
| 310 | "true", tVal, true}, |
| 311 | {"chained method on variable", |
| 312 | "{{with $x := .}}{{with .SI}}{{$.GetU.TrueFalse $.True}}{{end}}{{end}}", |
| 313 | "true", tVal, true}, |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 314 | {".NilOKFunc not nil", "{{call .NilOKFunc .PI}}", "false", tVal, true}, |
| 315 | {".NilOKFunc nil", "{{call .NilOKFunc nil}}", "true", tVal, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 316 | |
Rob Pike | f1d3ff1 | 2012-03-03 23:14:20 +1100 | [diff] [blame] | 317 | // Function call builtin. |
| 318 | {".BinaryFunc", "{{call .BinaryFunc `1` `2`}}", "[1=2]", tVal, true}, |
| 319 | {".VariadicFunc0", "{{call .VariadicFunc}}", "<>", tVal, true}, |
| 320 | {".VariadicFunc2", "{{call .VariadicFunc `he` `llo`}}", "<he+llo>", tVal, true}, |
| 321 | {".VariadicFuncInt", "{{call .VariadicFuncInt 33 `he` `llo`}}", "33=<he+llo>", tVal, true}, |
| 322 | {"if .BinaryFunc call", "{{ if .BinaryFunc}}{{call .BinaryFunc `1` `2`}}{{end}}", "[1=2]", tVal, true}, |
| 323 | {"if not .BinaryFunc call", "{{ if not .BinaryFunc}}{{call .BinaryFunc `1` `2`}}{{else}}No{{end}}", "No", tVal, true}, |
Ugorji Nwoke | 4f7c33c | 2012-05-22 15:21:35 -0700 | [diff] [blame] | 324 | {"Interface Call", `{{stringer .S}}`, "foozle", map[string]interface{}{"S": bytes.NewBufferString("foozle")}, true}, |
Rob Pike | f1d3ff1 | 2012-03-03 23:14:20 +1100 | [diff] [blame] | 325 | |
| 326 | // Erroneous function calls (check args). |
| 327 | {".BinaryFuncTooFew", "{{call .BinaryFunc `1`}}", "", tVal, false}, |
| 328 | {".BinaryFuncTooMany", "{{call .BinaryFunc `1` `2` `3`}}", "", tVal, false}, |
| 329 | {".BinaryFuncBad0", "{{call .BinaryFunc 1 3}}", "", tVal, false}, |
| 330 | {".BinaryFuncBad1", "{{call .BinaryFunc `1` 3}}", "", tVal, false}, |
| 331 | {".VariadicFuncBad0", "{{call .VariadicFunc 3}}", "", tVal, false}, |
Rob Pike | 227a04d | 2012-03-04 08:06:26 +1100 | [diff] [blame] | 332 | {".VariadicFuncIntBad0", "{{call .VariadicFuncInt}}", "", tVal, false}, |
| 333 | {".VariadicFuncIntBad`", "{{call .VariadicFuncInt `x`}}", "", tVal, false}, |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 334 | {".VariadicFuncNilBad", "{{call .VariadicFunc nil}}", "", tVal, false}, |
Rob Pike | aca8071 | 2012-02-15 16:05:34 +1100 | [diff] [blame] | 335 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 336 | // Pipelines. |
Rob Pike | a852981 | 2011-07-08 15:22:05 +1000 | [diff] [blame] | 337 | {"pipeline", "-{{.Method0 | .Method2 .U16}}-", "-Method2: 16 M0-", tVal, true}, |
Rob Pike | f1d3ff1 | 2012-03-03 23:14:20 +1100 | [diff] [blame] | 338 | {"pipeline func", "-{{call .VariadicFunc `llo` | call .VariadicFunc `he` }}-", "-<he+<llo>>-", tVal, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 339 | |
Rob Pike | cc842c7 | 2012-08-24 12:37:23 -0700 | [diff] [blame^] | 340 | // Parenthesized expressions |
| 341 | {"parens in pipeline", "{{printf `%d %d %d` (1) (2 | add 3) (add 4 (add 5 6))}}", "1 5 15", tVal, true}, |
| 342 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 343 | // If. |
| 344 | {"if true", "{{if true}}TRUE{{end}}", "TRUE", tVal, true}, |
| 345 | {"if false", "{{if false}}TRUE{{else}}FALSE{{end}}", "FALSE", tVal, true}, |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 346 | {"if nil", "{{if nil}}TRUE{{end}}", "", tVal, false}, |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 347 | {"if 1", "{{if 1}}NON-ZERO{{else}}ZERO{{end}}", "NON-ZERO", tVal, true}, |
| 348 | {"if 0", "{{if 0}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true}, |
| 349 | {"if 1.5", "{{if 1.5}}NON-ZERO{{else}}ZERO{{end}}", "NON-ZERO", tVal, true}, |
| 350 | {"if 0.0", "{{if .FloatZero}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true}, |
| 351 | {"if 1.5i", "{{if 1.5i}}NON-ZERO{{else}}ZERO{{end}}", "NON-ZERO", tVal, true}, |
| 352 | {"if 0.0i", "{{if .ComplexZero}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true}, |
| 353 | {"if emptystring", "{{if ``}}NON-EMPTY{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, |
| 354 | {"if string", "{{if `notempty`}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true}, |
| 355 | {"if emptyslice", "{{if .SIEmpty}}NON-EMPTY{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, |
| 356 | {"if slice", "{{if .SI}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true}, |
| 357 | {"if emptymap", "{{if .MSIEmpty}}NON-EMPTY{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, |
| 358 | {"if map", "{{if .MSI}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true}, |
Gustavo Niemeyer | 12f473f | 2011-12-18 22:14:11 -0200 | [diff] [blame] | 359 | {"if map unset", "{{if .MXI.none}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true}, |
| 360 | {"if map not unset", "{{if not .MXI.none}}ZERO{{else}}NON-ZERO{{end}}", "ZERO", tVal, true}, |
Rob Pike | fc1f0bd | 2011-07-14 13:15:55 +1000 | [diff] [blame] | 361 | {"if $x with $y int", "{{if $x := true}}{{with $y := .I}}{{$x}},{{$y}}{{end}}{{end}}", "true,17", tVal, true}, |
| 362 | {"if $x with $x int", "{{if $x := true}}{{with $x := .I}}{{$x}},{{end}}{{$x}}{{end}}", "17,true", tVal, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 363 | |
Rob Pike | abae847 | 2011-07-11 09:19:18 +1000 | [diff] [blame] | 364 | // Print etc. |
| 365 | {"print", `{{print "hello, print"}}`, "hello, print", tVal, true}, |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 366 | {"print 123", `{{print 1 2 3}}`, "1 2 3", tVal, true}, |
| 367 | {"print nil", `{{print nil}}`, "<nil>", tVal, true}, |
Rob Pike | abae847 | 2011-07-11 09:19:18 +1000 | [diff] [blame] | 368 | {"println", `{{println 1 2 3}}`, "1 2 3\n", tVal, true}, |
Rob Pike | b177c97 | 2011-07-05 14:23:51 +1000 | [diff] [blame] | 369 | {"printf int", `{{printf "%04x" 127}}`, "007f", tVal, true}, |
| 370 | {"printf float", `{{printf "%g" 3.5}}`, "3.5", tVal, true}, |
| 371 | {"printf complex", `{{printf "%g" 1+7i}}`, "(1+7i)", tVal, true}, |
| 372 | {"printf string", `{{printf "%s" "hello"}}`, "hello", tVal, true}, |
Rob Pike | d3d08e1 | 2011-07-08 18:25:46 +1000 | [diff] [blame] | 373 | {"printf function", `{{printf "%#q" zeroArgs}}`, "`zeroArgs`", tVal, true}, |
Rob Pike | b177c97 | 2011-07-05 14:23:51 +1000 | [diff] [blame] | 374 | {"printf field", `{{printf "%s" .U.V}}`, "v", tVal, true}, |
Rob Pike | a852981 | 2011-07-08 15:22:05 +1000 | [diff] [blame] | 375 | {"printf method", `{{printf "%s" .Method0}}`, "M0", tVal, true}, |
Rob Pike | e86d727 | 2011-07-09 17:11:35 +1000 | [diff] [blame] | 376 | {"printf dot", `{{with .I}}{{printf "%d" .}}{{end}}`, "17", tVal, true}, |
| 377 | {"printf var", `{{with $x := .I}}{{printf "%d" $x}}{{end}}`, "17", tVal, true}, |
Rob Pike | a852981 | 2011-07-08 15:22:05 +1000 | [diff] [blame] | 378 | {"printf lots", `{{printf "%d %s %g %s" 127 "hello" 7-3i .Method0}}`, "127 hello (7-3i) M0", tVal, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 379 | |
Rob Pike | eea5443 | 2011-07-05 17:05:15 +1000 | [diff] [blame] | 380 | // HTML. |
Rob Pike | cc9fed7 | 2011-07-05 15:58:54 +1000 | [diff] [blame] | 381 | {"html", `{{html "<script>alert(\"XSS\");</script>"}}`, |
Rob Pike | eea5443 | 2011-07-05 17:05:15 +1000 | [diff] [blame] | 382 | "<script>alert("XSS");</script>", nil, true}, |
Rob Pike | cc9fed7 | 2011-07-05 15:58:54 +1000 | [diff] [blame] | 383 | {"html pipeline", `{{printf "<script>alert(\"XSS\");</script>" | html}}`, |
Rob Pike | eea5443 | 2011-07-05 17:05:15 +1000 | [diff] [blame] | 384 | "<script>alert("XSS");</script>", nil, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 385 | |
| 386 | // JavaScript. |
David Symonds | 33705dd | 2011-07-06 16:51:49 +1000 | [diff] [blame] | 387 | {"js", `{{js .}}`, `It\'d be nice.`, `It'd be nice.`, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 388 | |
David Symonds | 5e48e64 | 2011-08-13 14:00:16 +1000 | [diff] [blame] | 389 | // URL query. |
| 390 | {"urlquery", `{{"http://www.example.org/"|urlquery}}`, "http%3A%2F%2Fwww.example.org%2F", nil, true}, |
David Symonds | a09ba8b | 2011-08-08 16:29:57 +1000 | [diff] [blame] | 391 | |
Rob Pike | eea5443 | 2011-07-05 17:05:15 +1000 | [diff] [blame] | 392 | // Booleans |
| 393 | {"not", "{{not true}} {{not false}}", "false true", nil, true}, |
Rob Pike | 469e333 | 2011-07-14 07:59:04 +1000 | [diff] [blame] | 394 | {"and", "{{and false 0}} {{and 1 0}} {{and 0 true}} {{and 1 1}}", "false 0 0 1", nil, true}, |
| 395 | {"or", "{{or 0 0}} {{or 1 0}} {{or 0 true}} {{or 1 1}}", "0 1 true 1", nil, true}, |
Rob Pike | eea5443 | 2011-07-05 17:05:15 +1000 | [diff] [blame] | 396 | {"boolean if", "{{if and true 1 `hi`}}TRUE{{else}}FALSE{{end}}", "TRUE", tVal, true}, |
| 397 | {"boolean if not", "{{if and true 1 `hi` | not}}TRUE{{else}}FALSE{{end}}", "FALSE", nil, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 398 | |
Rob Pike | 6732bd3 | 2011-07-06 22:27:06 +1000 | [diff] [blame] | 399 | // Indexing. |
| 400 | {"slice[0]", "{{index .SI 0}}", "3", tVal, true}, |
| 401 | {"slice[1]", "{{index .SI 1}}", "4", tVal, true}, |
| 402 | {"slice[HUGE]", "{{index .SI 10}}", "", tVal, false}, |
| 403 | {"slice[WRONG]", "{{index .SI `hello`}}", "", tVal, false}, |
| 404 | {"map[one]", "{{index .MSI `one`}}", "1", tVal, true}, |
| 405 | {"map[two]", "{{index .MSI `two`}}", "2", tVal, true}, |
Rob Pike | ce27433 | 2012-07-23 16:19:12 -0700 | [diff] [blame] | 406 | {"map[NO]", "{{index .MSI `XXX`}}", "0", tVal, true}, |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 407 | {"map[nil]", "{{index .MSI nil}}", "0", tVal, true}, |
Rob Pike | 6732bd3 | 2011-07-06 22:27:06 +1000 | [diff] [blame] | 408 | {"map[WRONG]", "{{index .MSI 10}}", "", tVal, false}, |
| 409 | {"double index", "{{index .SMSI 1 `eleven`}}", "11", tVal, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 410 | |
Rob Pike | b1d1da4 | 2011-08-12 11:47:44 +1000 | [diff] [blame] | 411 | // Len. |
| 412 | {"slice", "{{len .SI}}", "3", tVal, true}, |
| 413 | {"map", "{{len .MSI }}", "3", tVal, true}, |
| 414 | {"len of int", "{{len 3}}", "", tVal, false}, |
| 415 | {"len of nothing", "{{len .Empty0}}", "", tVal, false}, |
| 416 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 417 | // With. |
| 418 | {"with true", "{{with true}}{{.}}{{end}}", "true", tVal, true}, |
| 419 | {"with false", "{{with false}}{{.}}{{else}}FALSE{{end}}", "FALSE", tVal, true}, |
| 420 | {"with 1", "{{with 1}}{{.}}{{else}}ZERO{{end}}", "1", tVal, true}, |
| 421 | {"with 0", "{{with 0}}{{.}}{{else}}ZERO{{end}}", "ZERO", tVal, true}, |
| 422 | {"with 1.5", "{{with 1.5}}{{.}}{{else}}ZERO{{end}}", "1.5", tVal, true}, |
| 423 | {"with 0.0", "{{with .FloatZero}}{{.}}{{else}}ZERO{{end}}", "ZERO", tVal, true}, |
| 424 | {"with 1.5i", "{{with 1.5i}}{{.}}{{else}}ZERO{{end}}", "(0+1.5i)", tVal, true}, |
| 425 | {"with 0.0i", "{{with .ComplexZero}}{{.}}{{else}}ZERO{{end}}", "ZERO", tVal, true}, |
| 426 | {"with emptystring", "{{with ``}}{{.}}{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, |
| 427 | {"with string", "{{with `notempty`}}{{.}}{{else}}EMPTY{{end}}", "notempty", tVal, true}, |
| 428 | {"with emptyslice", "{{with .SIEmpty}}{{.}}{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, |
| 429 | {"with slice", "{{with .SI}}{{.}}{{else}}EMPTY{{end}}", "[3 4 5]", tVal, true}, |
| 430 | {"with emptymap", "{{with .MSIEmpty}}{{.}}{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, |
| 431 | {"with map", "{{with .MSIone}}{{.}}{{else}}EMPTY{{end}}", "map[one:1]", tVal, true}, |
Rob Pike | 9a5bb28 | 2011-07-18 17:34:42 +1000 | [diff] [blame] | 432 | {"with empty interface, struct field", "{{with .Empty4}}{{.V}}{{end}}", "UinEmpty", tVal, true}, |
Rob Pike | fc1f0bd | 2011-07-14 13:15:55 +1000 | [diff] [blame] | 433 | {"with $x int", "{{with $x := .I}}{{$x}}{{end}}", "17", tVal, true}, |
Rob Pike | c705701 | 2011-07-17 13:31:59 +1000 | [diff] [blame] | 434 | {"with $x struct.U.V", "{{with $x := $}}{{$x.U.V}}{{end}}", "v", tVal, true}, |
Rob Pike | 3e79c95 | 2011-07-22 10:51:40 +1000 | [diff] [blame] | 435 | {"with variable and action", "{{with $x := $}}{{$y := $.U.V}}{{$y}}{{end}}", "v", tVal, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 436 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 437 | // Range. |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 438 | {"range []int", "{{range .SI}}-{{.}}-{{end}}", "-3--4--5-", tVal, true}, |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 439 | {"range empty no else", "{{range .SIEmpty}}-{{.}}-{{end}}", "", tVal, true}, |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 440 | {"range []int else", "{{range .SI}}-{{.}}-{{else}}EMPTY{{end}}", "-3--4--5-", tVal, true}, |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 441 | {"range empty else", "{{range .SIEmpty}}-{{.}}-{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, |
Rob Pike | c756a19 | 2011-06-29 15:02:04 +1000 | [diff] [blame] | 442 | {"range []bool", "{{range .SB}}-{{.}}-{{end}}", "-true--false-", tVal, true}, |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 443 | {"range []int method", "{{range .SI | .MAdd .I}}-{{.}}-{{end}}", "-20--21--22-", tVal, true}, |
Rob Pike | a5950df | 2012-01-13 14:09:13 -0800 | [diff] [blame] | 444 | {"range map", "{{range .MSI}}-{{.}}-{{end}}", "-1--3--2-", tVal, true}, |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 445 | {"range empty map no else", "{{range .MSIEmpty}}-{{.}}-{{end}}", "", tVal, true}, |
Rob Pike | a5950df | 2012-01-13 14:09:13 -0800 | [diff] [blame] | 446 | {"range map else", "{{range .MSI}}-{{.}}-{{else}}EMPTY{{end}}", "-1--3--2-", tVal, true}, |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 447 | {"range empty map else", "{{range .MSIEmpty}}-{{.}}-{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 448 | {"range empty interface", "{{range .Empty3}}-{{.}}-{{else}}EMPTY{{end}}", "-7--8-", tVal, true}, |
Gustavo Niemeyer | e3f3a54 | 2011-08-15 00:22:28 -0300 | [diff] [blame] | 449 | {"range empty nil", "{{range .Empty0}}-{{.}}-{{end}}", "", tVal, true}, |
Rob Pike | fc1f0bd | 2011-07-14 13:15:55 +1000 | [diff] [blame] | 450 | {"range $x SI", "{{range $x := .SI}}<{{$x}}>{{end}}", "<3><4><5>", tVal, true}, |
| 451 | {"range $x $y SI", "{{range $x, $y := .SI}}<{{$x}}={{$y}}>{{end}}", "<0=3><1=4><2=5>", tVal, true}, |
| 452 | {"range $x MSIone", "{{range $x := .MSIone}}<{{$x}}>{{end}}", "<1>", tVal, true}, |
| 453 | {"range $x $y MSIone", "{{range $x, $y := .MSIone}}<{{$x}}={{$y}}>{{end}}", "<one=1>", tVal, true}, |
| 454 | {"range $x PSI", "{{range $x := .PSI}}<{{$x}}>{{end}}", "<21><22><23>", tVal, true}, |
Rob Pike | 3e79c95 | 2011-07-22 10:51:40 +1000 | [diff] [blame] | 455 | {"declare in range", "{{range $x := .PSI}}<{{$foo:=$x}}{{$x}}>{{end}}", "<21><22><23>", tVal, true}, |
Rob Pike | 361c5ac | 2011-08-29 15:56:52 +1000 | [diff] [blame] | 456 | {"range count", `{{range $i, $x := count 5}}[{{$i}}]{{$x}}{{end}}`, "[0]a[1]b[2]c[3]d[4]e", tVal, true}, |
| 457 | {"range nil count", `{{range $i, $x := count 0}}{{else}}empty{{end}}`, "empty", tVal, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 458 | |
Rob Pike | 469e333 | 2011-07-14 07:59:04 +1000 | [diff] [blame] | 459 | // Cute examples. |
| 460 | {"or as if true", `{{or .SI "slice is empty"}}`, "[3 4 5]", tVal, true}, |
| 461 | {"or as if false", `{{or .SIEmpty "slice is empty"}}`, "slice is empty", tVal, true}, |
| 462 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 463 | // Error handling. |
Rob Pike | 47424d9 | 2012-02-14 07:11:39 +1100 | [diff] [blame] | 464 | {"error method, error", "{{.MyError true}}", "", tVal, false}, |
| 465 | {"error method, no error", "{{.MyError false}}", "false", tVal, true}, |
Rob Pike | d366c36 | 2011-07-11 18:06:24 +1000 | [diff] [blame] | 466 | |
| 467 | // Fixed bugs. |
| 468 | // Must separate dot and receiver; otherwise args are evaluated with dot set to variable. |
Rob Pike | c3344d6 | 2011-07-14 07:52:07 +1000 | [diff] [blame] | 469 | {"bug0", "{{range .MSIone}}{{if $.Method1 .}}X{{end}}{{end}}", "X", tVal, true}, |
Robert Griesemer | 7162f39 | 2011-07-30 17:11:52 -0700 | [diff] [blame] | 470 | // Do not loop endlessly in indirect for non-empty interfaces. |
Rob Pike | 1adda46 | 2011-08-09 17:07:29 +1000 | [diff] [blame] | 471 | // The bug appears with *interface only; looped forever. |
| 472 | {"bug1", "{{.Method0}}", "M0", &iVal, true}, |
| 473 | // Was taking address of interface field, so method set was empty. |
| 474 | {"bug2", "{{$.NonEmptyInterface.Method0}}", "M0", tVal, true}, |
| 475 | // Struct values were not legal in with - mere oversight. |
Rob Pike | 39fa2a5 | 2011-08-11 08:27:16 +1000 | [diff] [blame] | 476 | {"bug3", "{{with $}}{{.Method0}}{{end}}", "M0", tVal, true}, |
| 477 | // Nil interface values in if. |
| 478 | {"bug4", "{{if .Empty0}}non-nil{{else}}nil{{end}}", "nil", tVal, true}, |
David Symonds | fe59d86 | 2011-08-12 13:29:56 +1000 | [diff] [blame] | 479 | // Stringer. |
| 480 | {"bug5", "{{.Str}}", "foozle", tVal, true}, |
Russ Cox | e73680a | 2011-11-04 07:33:55 -0400 | [diff] [blame] | 481 | {"bug5a", "{{.Err}}", "erroozle", tVal, true}, |
Rob Pike | d45e808c | 2011-09-06 15:34:38 -0700 | [diff] [blame] | 482 | // Args need to be indirected and dereferenced sometimes. |
| 483 | {"bug6a", "{{vfunc .V0 .V1}}", "vfunc", tVal, true}, |
| 484 | {"bug6b", "{{vfunc .V0 .V0}}", "vfunc", tVal, true}, |
| 485 | {"bug6c", "{{vfunc .V1 .V0}}", "vfunc", tVal, true}, |
| 486 | {"bug6d", "{{vfunc .V1 .V1}}", "vfunc", tVal, true}, |
Rob Pike | d6ad6f0 | 2012-03-14 10:46:21 +1100 | [diff] [blame] | 487 | // Legal parse but illegal execution: non-function should have no arguments. |
| 488 | {"bug7a", "{{3 2}}", "", tVal, false}, |
| 489 | {"bug7b", "{{$x := 1}}{{$x 2}}", "", tVal, false}, |
| 490 | {"bug7c", "{{$x := 1}}{{3 | $x}}", "", tVal, false}, |
Rob Pike | 065db4e | 2012-04-03 11:44:52 +1000 | [diff] [blame] | 491 | // Pipelined arg was not being type-checked. |
| 492 | {"bug8a", "{{3|oneArg}}", "", tVal, false}, |
| 493 | {"bug8b", "{{4|dddArg 3}}", "", tVal, false}, |
Rob Pike | a8098cb | 2012-04-23 15:39:02 +1000 | [diff] [blame] | 494 | // A bug was introduced that broke map lookups for lower-case names. |
| 495 | {"bug9", "{{.cause}}", "neglect", map[string]string{"cause": "neglect"}, true}, |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 496 | } |
| 497 | |
Rob Pike | d3d08e1 | 2011-07-08 18:25:46 +1000 | [diff] [blame] | 498 | func zeroArgs() string { |
| 499 | return "zeroArgs" |
| 500 | } |
| 501 | |
| 502 | func oneArg(a string) string { |
| 503 | return "oneArg=" + a |
Rob Pike | b177c97 | 2011-07-05 14:23:51 +1000 | [diff] [blame] | 504 | } |
| 505 | |
Rob Pike | 065db4e | 2012-04-03 11:44:52 +1000 | [diff] [blame] | 506 | func dddArg(a int, b ...string) string { |
| 507 | return fmt.Sprintln(a, b) |
| 508 | } |
| 509 | |
Rob Pike | 361c5ac | 2011-08-29 15:56:52 +1000 | [diff] [blame] | 510 | // count returns a channel that will deliver n sequential 1-letter strings starting at "a" |
| 511 | func count(n int) chan string { |
| 512 | if n == 0 { |
| 513 | return nil |
| 514 | } |
| 515 | c := make(chan string) |
| 516 | go func() { |
| 517 | for i := 0; i < n; i++ { |
| 518 | c <- "abcdefghijklmnop"[i : i+1] |
| 519 | } |
| 520 | close(c) |
| 521 | }() |
| 522 | return c |
| 523 | } |
| 524 | |
Rob Pike | d45e808c | 2011-09-06 15:34:38 -0700 | [diff] [blame] | 525 | // vfunc takes a *V and a V |
| 526 | func vfunc(V, *V) string { |
| 527 | return "vfunc" |
| 528 | } |
| 529 | |
Rob Pike | cc842c7 | 2012-08-24 12:37:23 -0700 | [diff] [blame^] | 530 | func add(args ...int) int { |
| 531 | sum := 0 |
| 532 | for _, x := range args { |
| 533 | sum += x |
| 534 | } |
| 535 | return sum |
| 536 | } |
| 537 | |
Ugorji Nwoke | 4f7c33c | 2012-05-22 15:21:35 -0700 | [diff] [blame] | 538 | func stringer(s fmt.Stringer) string { |
| 539 | return s.String() |
| 540 | } |
| 541 | |
Rob Pike | f56db6f | 2011-11-23 20:17:22 -0800 | [diff] [blame] | 542 | func testExecute(execTests []execTest, template *Template, t *testing.T) { |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 543 | b := new(bytes.Buffer) |
Rob Pike | 361c5ac | 2011-08-29 15:56:52 +1000 | [diff] [blame] | 544 | funcs := FuncMap{ |
Rob Pike | cc842c7 | 2012-08-24 12:37:23 -0700 | [diff] [blame^] | 545 | "add": add, |
Rob Pike | 361c5ac | 2011-08-29 15:56:52 +1000 | [diff] [blame] | 546 | "count": count, |
Rob Pike | 065db4e | 2012-04-03 11:44:52 +1000 | [diff] [blame] | 547 | "dddArg": dddArg, |
Rob Pike | 361c5ac | 2011-08-29 15:56:52 +1000 | [diff] [blame] | 548 | "oneArg": oneArg, |
| 549 | "typeOf": typeOf, |
Rob Pike | d45e808c | 2011-09-06 15:34:38 -0700 | [diff] [blame] | 550 | "vfunc": vfunc, |
Rob Pike | 361c5ac | 2011-08-29 15:56:52 +1000 | [diff] [blame] | 551 | "zeroArgs": zeroArgs, |
Ugorji Nwoke | 4f7c33c | 2012-05-22 15:21:35 -0700 | [diff] [blame] | 552 | "stringer": stringer, |
Rob Pike | 361c5ac | 2011-08-29 15:56:52 +1000 | [diff] [blame] | 553 | } |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 554 | for _, test := range execTests { |
Rob Pike | f56db6f | 2011-11-23 20:17:22 -0800 | [diff] [blame] | 555 | var tmpl *Template |
| 556 | var err error |
| 557 | if template == nil { |
| 558 | tmpl, err = New(test.name).Funcs(funcs).Parse(test.input) |
| 559 | } else { |
| 560 | tmpl, err = template.New(test.name).Funcs(funcs).Parse(test.input) |
Rob Pike | 25d2987 | 2011-11-17 22:53:23 -0800 | [diff] [blame] | 561 | } |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 562 | if err != nil { |
| 563 | t.Errorf("%s: parse error: %s", test.name, err) |
| 564 | continue |
| 565 | } |
| 566 | b.Reset() |
Rob Pike | 0f7a195 | 2011-07-21 14:22:01 +1000 | [diff] [blame] | 567 | err = tmpl.Execute(b, test.data) |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 568 | switch { |
| 569 | case !test.ok && err == nil: |
| 570 | t.Errorf("%s: expected error; got none", test.name) |
| 571 | continue |
| 572 | case test.ok && err != nil: |
| 573 | t.Errorf("%s: unexpected execute error: %s", test.name, err) |
| 574 | continue |
| 575 | case !test.ok && err != nil: |
Rob Pike | 6732bd3 | 2011-07-06 22:27:06 +1000 | [diff] [blame] | 576 | // expected error, got one |
| 577 | if *debug { |
| 578 | fmt.Printf("%s: %s\n\t%s\n", test.name, test.input, err) |
| 579 | } |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 580 | } |
| 581 | result := b.String() |
| 582 | if result != test.output { |
| 583 | t.Errorf("%s: expected\n\t%q\ngot\n\t%q", test.name, test.output, result) |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 588 | func TestExecute(t *testing.T) { |
| 589 | testExecute(execTests, nil, t) |
| 590 | } |
| 591 | |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 592 | var delimPairs = []string{ |
| 593 | "", "", // default |
| 594 | "{{", "}}", // same as default |
| 595 | "<<", ">>", // distinct |
| 596 | "|", "|", // same |
| 597 | "(日)", "(本)", // peculiar |
| 598 | } |
| 599 | |
| 600 | func TestDelims(t *testing.T) { |
| 601 | const hello = "Hello, world" |
| 602 | var value = struct{ Str string }{hello} |
| 603 | for i := 0; i < len(delimPairs); i += 2 { |
| 604 | text := ".Str" |
| 605 | left := delimPairs[i+0] |
Rob Pike | b3dd327 | 2011-10-06 15:21:56 -0700 | [diff] [blame] | 606 | trueLeft := left |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 607 | right := delimPairs[i+1] |
Rob Pike | b3dd327 | 2011-10-06 15:21:56 -0700 | [diff] [blame] | 608 | trueRight := right |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 609 | if left == "" { // default case |
Rob Pike | b3dd327 | 2011-10-06 15:21:56 -0700 | [diff] [blame] | 610 | trueLeft = "{{" |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 611 | } |
| 612 | if right == "" { // default case |
Rob Pike | b3dd327 | 2011-10-06 15:21:56 -0700 | [diff] [blame] | 613 | trueRight = "}}" |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 614 | } |
Rob Pike | b3dd327 | 2011-10-06 15:21:56 -0700 | [diff] [blame] | 615 | text = trueLeft + text + trueRight |
| 616 | // Now add a comment |
| 617 | text += trueLeft + "/*comment*/" + trueRight |
| 618 | // Now add an action containing a string. |
| 619 | text += trueLeft + `"` + trueLeft + `"` + trueRight |
| 620 | // At this point text looks like `{{.Str}}{{/*comment*/}}{{"{{"}}`. |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 621 | tmpl, err := New("delims").Delims(left, right).Parse(text) |
| 622 | if err != nil { |
| 623 | t.Fatalf("delim %q text %q parse err %s", left, text, err) |
| 624 | } |
| 625 | var b = new(bytes.Buffer) |
| 626 | err = tmpl.Execute(b, value) |
| 627 | if err != nil { |
| 628 | t.Fatalf("delim %q exec err %s", left, err) |
| 629 | } |
Rob Pike | b3dd327 | 2011-10-06 15:21:56 -0700 | [diff] [blame] | 630 | if b.String() != hello+trueLeft { |
Robert Hencke | c501824 | 2011-10-13 13:34:01 +1100 | [diff] [blame] | 631 | t.Errorf("expected %q got %q", hello+trueLeft, b.String()) |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 636 | // Check that an error from a method flows back to the top. |
| 637 | func TestExecuteError(t *testing.T) { |
| 638 | b := new(bytes.Buffer) |
| 639 | tmpl := New("error") |
Rob Pike | 47424d9 | 2012-02-14 07:11:39 +1100 | [diff] [blame] | 640 | _, err := tmpl.Parse("{{.MyError true}}") |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 641 | if err != nil { |
| 642 | t.Fatalf("parse error: %s", err) |
| 643 | } |
| 644 | err = tmpl.Execute(b, tVal) |
| 645 | if err == nil { |
| 646 | t.Errorf("expected error; got none") |
Rob Pike | 47424d9 | 2012-02-14 07:11:39 +1100 | [diff] [blame] | 647 | } else if !strings.Contains(err.Error(), myError.Error()) { |
Rob Pike | dfffc7a | 2011-07-14 11:32:06 +1000 | [diff] [blame] | 648 | if *debug { |
| 649 | fmt.Printf("test execute error: %s\n", err) |
| 650 | } |
Rob Pike | 47424d9 | 2012-02-14 07:11:39 +1100 | [diff] [blame] | 651 | t.Errorf("expected myError; got %s", err) |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 652 | } |
| 653 | } |
David Symonds | 33705dd | 2011-07-06 16:51:49 +1000 | [diff] [blame] | 654 | |
| 655 | func TestJSEscaping(t *testing.T) { |
| 656 | testCases := []struct { |
| 657 | in, exp string |
| 658 | }{ |
| 659 | {`a`, `a`}, |
| 660 | {`'foo`, `\'foo`}, |
| 661 | {`Go "jump" \`, `Go \"jump\" \\`}, |
| 662 | {`Yukihiro says "今日は世界"`, `Yukihiro says \"今日は世界\"`}, |
| 663 | {"unprintable \uFDFF", `unprintable \uFDFF`}, |
David Symonds | a16ad6f | 2011-07-14 12:02:58 +1000 | [diff] [blame] | 664 | {`<html>`, `\x3Chtml\x3E`}, |
David Symonds | 33705dd | 2011-07-06 16:51:49 +1000 | [diff] [blame] | 665 | } |
| 666 | for _, tc := range testCases { |
| 667 | s := JSEscapeString(tc.in) |
| 668 | if s != tc.exp { |
| 669 | t.Errorf("JS escaping [%s] got [%s] want [%s]", tc.in, s, tc.exp) |
| 670 | } |
| 671 | } |
| 672 | } |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 673 | |
| 674 | // A nice example: walk a binary tree. |
| 675 | |
| 676 | type Tree struct { |
| 677 | Val int |
| 678 | Left, Right *Tree |
| 679 | } |
| 680 | |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 681 | // Use different delimiters to test Set.Delims. |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 682 | const treeTemplate = ` |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 683 | (define "tree") |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 684 | [ |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 685 | (.Val) |
| 686 | (with .Left) |
| 687 | (template "tree" .) |
| 688 | (end) |
| 689 | (with .Right) |
| 690 | (template "tree" .) |
| 691 | (end) |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 692 | ] |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 693 | (end) |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 694 | ` |
| 695 | |
| 696 | func TestTree(t *testing.T) { |
| 697 | var tree = &Tree{ |
| 698 | 1, |
| 699 | &Tree{ |
| 700 | 2, &Tree{ |
| 701 | 3, |
| 702 | &Tree{ |
| 703 | 4, nil, nil, |
| 704 | }, |
| 705 | nil, |
| 706 | }, |
| 707 | &Tree{ |
| 708 | 5, |
| 709 | &Tree{ |
| 710 | 6, nil, nil, |
| 711 | }, |
| 712 | nil, |
| 713 | }, |
| 714 | }, |
| 715 | &Tree{ |
| 716 | 7, |
| 717 | &Tree{ |
| 718 | 8, |
| 719 | &Tree{ |
| 720 | 9, nil, nil, |
| 721 | }, |
| 722 | nil, |
| 723 | }, |
| 724 | &Tree{ |
| 725 | 10, |
| 726 | &Tree{ |
| 727 | 11, nil, nil, |
| 728 | }, |
| 729 | nil, |
| 730 | }, |
| 731 | }, |
| 732 | } |
Rob Pike | f56db6f | 2011-11-23 20:17:22 -0800 | [diff] [blame] | 733 | tmpl, err := New("root").Delims("(", ")").Parse(treeTemplate) |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 734 | if err != nil { |
| 735 | t.Fatal("parse error:", err) |
| 736 | } |
| 737 | var b bytes.Buffer |
Russ Cox | cfa036a | 2011-10-25 22:22:42 -0700 | [diff] [blame] | 738 | stripSpace := func(r rune) rune { |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 739 | if r == '\t' || r == '\n' { |
| 740 | return -1 |
| 741 | } |
| 742 | return r |
| 743 | } |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 744 | const expect = "[1[2[3[4]][5[6]]][7[8[9]][10[11]]]]" |
Rob Pike | f56db6f | 2011-11-23 20:17:22 -0800 | [diff] [blame] | 745 | // First by looking up the template. |
Rob Pike | e9025df | 2011-11-26 08:32:55 -0800 | [diff] [blame] | 746 | err = tmpl.Lookup("tree").Execute(&b, tree) |
Rob Pike | f56db6f | 2011-11-23 20:17:22 -0800 | [diff] [blame] | 747 | if err != nil { |
| 748 | t.Fatal("exec error:", err) |
| 749 | } |
| 750 | result := strings.Map(stripSpace, b.String()) |
| 751 | if result != expect { |
| 752 | t.Errorf("expected %q got %q", expect, result) |
| 753 | } |
| 754 | // Then direct to execution. |
| 755 | b.Reset() |
| 756 | err = tmpl.ExecuteTemplate(&b, "tree", tree) |
| 757 | if err != nil { |
| 758 | t.Fatal("exec error:", err) |
| 759 | } |
| 760 | result = strings.Map(stripSpace, b.String()) |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 761 | if result != expect { |
| 762 | t.Errorf("expected %q got %q", expect, result) |
| 763 | } |
| 764 | } |