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 |
Rob Pike | 5fea8c0 | 2013-07-31 13:04:57 +1000 | [diff] [blame] | 27 | ComplexZero complex128 |
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 |
Rob Pike | 1f661fc | 2013-08-27 13:29:07 +1000 | [diff] [blame^] | 60 | PS *string |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 61 | PSI *[]int |
| 62 | NIL *int |
Rob Pike | aca8071 | 2012-02-15 16:05:34 +1100 | [diff] [blame] | 63 | // Function (not method) |
Rob Pike | f1d3ff1 | 2012-03-03 23:14:20 +1100 | [diff] [blame] | 64 | BinaryFunc func(string, string) string |
| 65 | VariadicFunc func(...string) string |
| 66 | VariadicFuncInt func(int, ...string) string |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 67 | NilOKFunc func(*int) bool |
Elias Naur | 83348a1 | 2013-08-13 11:11:05 +1000 | [diff] [blame] | 68 | ErrFunc func() (string, error) |
Rob Pike | 3987b91 | 2011-07-10 07:32:01 +1000 | [diff] [blame] | 69 | // Template to test evaluation of templates. |
| 70 | Tmpl *Template |
Rob Pike | a8098cb | 2012-04-23 15:39:02 +1000 | [diff] [blame] | 71 | // Unexported field; cannot be accessed by template. |
| 72 | unexported int |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 73 | } |
| 74 | |
Rob Pike | a852981 | 2011-07-08 15:22:05 +1000 | [diff] [blame] | 75 | type U struct { |
| 76 | V string |
| 77 | } |
| 78 | |
Rob Pike | 6ca968c | 2011-08-11 14:36:51 +1000 | [diff] [blame] | 79 | type V struct { |
| 80 | j int |
| 81 | } |
| 82 | |
| 83 | func (v *V) String() string { |
| 84 | if v == nil { |
| 85 | return "nilV" |
| 86 | } |
| 87 | return fmt.Sprintf("<%d>", v.j) |
| 88 | } |
| 89 | |
David Symonds | 39fcca6 | 2011-11-04 23:45:38 +1100 | [diff] [blame] | 90 | type W struct { |
| 91 | k int |
| 92 | } |
| 93 | |
| 94 | func (w *W) Error() string { |
| 95 | if w == nil { |
| 96 | return "nilW" |
| 97 | } |
| 98 | return fmt.Sprintf("[%d]", w.k) |
| 99 | } |
| 100 | |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 101 | var tVal = &T{ |
Rob Pike | c3344d6 | 2011-07-14 07:52:07 +1000 | [diff] [blame] | 102 | True: true, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 103 | I: 17, |
| 104 | U16: 16, |
| 105 | X: "x", |
| 106 | U: &U{"v"}, |
Rob Pike | 6ca968c | 2011-08-11 14:36:51 +1000 | [diff] [blame] | 107 | V0: V{6666}, |
| 108 | V1: &V{7777}, // leave V2 as nil |
David Symonds | 39fcca6 | 2011-11-04 23:45:38 +1100 | [diff] [blame] | 109 | W0: W{888}, |
| 110 | W1: &W{999}, // leave W2 as nil |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 111 | SI: []int{3, 4, 5}, |
| 112 | SB: []bool{true, false}, |
| 113 | MSI: map[string]int{"one": 1, "two": 2, "three": 3}, |
| 114 | MSIone: map[string]int{"one": 1}, |
Gustavo Niemeyer | e3df71f | 2011-08-15 00:56:01 -0300 | [diff] [blame] | 115 | MXI: map[interface{}]int{"one": 1}, |
| 116 | MII: map[int]int{1: 1}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 117 | SMSI: []map[string]int{ |
| 118 | {"one": 1, "two": 2}, |
| 119 | {"eleven": 11, "twelve": 12}, |
| 120 | }, |
Rob Pike | 1adda46 | 2011-08-09 17:07:29 +1000 | [diff] [blame] | 121 | Empty1: 3, |
| 122 | Empty2: "empty2", |
| 123 | Empty3: []int{7, 8}, |
| 124 | Empty4: &U{"UinEmpty"}, |
| 125 | NonEmptyInterface: new(T), |
Russ Cox | 853c846 | 2011-10-27 21:17:47 -0700 | [diff] [blame] | 126 | Str: bytes.NewBuffer([]byte("foozle")), |
Russ Cox | e73680a | 2011-11-04 07:33:55 -0400 | [diff] [blame] | 127 | Err: errors.New("erroozle"), |
Rob Pike | 1adda46 | 2011-08-09 17:07:29 +1000 | [diff] [blame] | 128 | PI: newInt(23), |
Rob Pike | 1f661fc | 2013-08-27 13:29:07 +1000 | [diff] [blame^] | 129 | PS: newString("a string"), |
Rob Pike | 1adda46 | 2011-08-09 17:07:29 +1000 | [diff] [blame] | 130 | PSI: newIntSlice(21, 22, 23), |
Rob Pike | f1d3ff1 | 2012-03-03 23:14:20 +1100 | [diff] [blame] | 131 | BinaryFunc: func(a, b string) string { return fmt.Sprintf("[%s=%s]", a, b) }, |
| 132 | VariadicFunc: func(s ...string) string { return fmt.Sprint("<", strings.Join(s, "+"), ">") }, |
| 133 | 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] | 134 | NilOKFunc: func(s *int) bool { return s == nil }, |
Elias Naur | 83348a1 | 2013-08-13 11:11:05 +1000 | [diff] [blame] | 135 | ErrFunc: func() (string, error) { return "bla", nil }, |
Rob Pike | 1adda46 | 2011-08-09 17:07:29 +1000 | [diff] [blame] | 136 | 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] | 137 | } |
| 138 | |
Robert Griesemer | 7162f39 | 2011-07-30 17:11:52 -0700 | [diff] [blame] | 139 | // A non-empty interface. |
| 140 | type I interface { |
| 141 | Method0() string |
| 142 | } |
| 143 | |
| 144 | var iVal I = tVal |
| 145 | |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 146 | // Helpers for creation. |
| 147 | func newInt(n int) *int { |
Rob Pike | 1f661fc | 2013-08-27 13:29:07 +1000 | [diff] [blame^] | 148 | return &n |
| 149 | } |
| 150 | |
| 151 | func newString(s string) *string { |
| 152 | return &s |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | func newIntSlice(n ...int) *[]int { |
| 156 | p := new([]int) |
| 157 | *p = make([]int, len(n)) |
| 158 | copy(*p, n) |
| 159 | return p |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // Simple methods with and without arguments. |
| 163 | func (t *T) Method0() string { |
Rob Pike | a852981 | 2011-07-08 15:22:05 +1000 | [diff] [blame] | 164 | return "M0" |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | func (t *T) Method1(a int) int { |
| 168 | return a |
| 169 | } |
| 170 | |
| 171 | func (t *T) Method2(a uint16, b string) string { |
| 172 | return fmt.Sprintf("Method2: %d %s", a, b) |
| 173 | } |
| 174 | |
Gustavo Niemeyer | 12f473f | 2011-12-18 22:14:11 -0200 | [diff] [blame] | 175 | func (t *T) Method3(v interface{}) string { |
| 176 | return fmt.Sprintf("Method3: %v", v) |
| 177 | } |
| 178 | |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 179 | func (t *T) MAdd(a int, b []int) []int { |
| 180 | v := make([]int, len(b)) |
| 181 | for i, x := range b { |
| 182 | v[i] = x + a |
| 183 | } |
| 184 | return v |
| 185 | } |
| 186 | |
Rob Pike | 47424d9 | 2012-02-14 07:11:39 +1100 | [diff] [blame] | 187 | var myError = errors.New("my error") |
| 188 | |
| 189 | // MyError returns a value and an error according to its argument. |
| 190 | func (t *T) MyError(error bool) (bool, error) { |
Rob Pike | c756a19 | 2011-06-29 15:02:04 +1000 | [diff] [blame] | 191 | if error { |
Rob Pike | 47424d9 | 2012-02-14 07:11:39 +1100 | [diff] [blame] | 192 | return true, myError |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 193 | } |
Rob Pike | c756a19 | 2011-06-29 15:02:04 +1000 | [diff] [blame] | 194 | return false, nil |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 195 | } |
| 196 | |
Rob Pike | c3344d6 | 2011-07-14 07:52:07 +1000 | [diff] [blame] | 197 | // A few methods to test chaining. |
| 198 | func (t *T) GetU() *U { |
| 199 | return t.U |
| 200 | } |
| 201 | |
| 202 | func (u *U) TrueFalse(b bool) string { |
| 203 | if b { |
| 204 | return "true" |
| 205 | } |
| 206 | return "" |
| 207 | } |
| 208 | |
Rob Pike | 7c47741 | 2011-07-12 13:15:26 +1000 | [diff] [blame] | 209 | func typeOf(arg interface{}) string { |
| 210 | return fmt.Sprintf("%T", arg) |
| 211 | } |
| 212 | |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 213 | type execTest struct { |
| 214 | name string |
| 215 | input string |
| 216 | output string |
| 217 | data interface{} |
| 218 | ok bool |
| 219 | } |
| 220 | |
Rob Pike | 7c47741 | 2011-07-12 13:15:26 +1000 | [diff] [blame] | 221 | // bigInt and bigUint are hex string representing numbers either side |
| 222 | // of the max int boundary. |
| 223 | // We do it this way so the test doesn't depend on ints being 32 bits. |
| 224 | var ( |
| 225 | bigInt = fmt.Sprintf("0x%x", int(1<<uint(reflect.TypeOf(0).Bits()-1)-1)) |
| 226 | bigUint = fmt.Sprintf("0x%x", uint(1<<uint(reflect.TypeOf(0).Bits()-1))) |
| 227 | ) |
| 228 | |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 229 | var execTests = []execTest{ |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 230 | // Trivial cases. |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 231 | {"empty", "", "", nil, true}, |
| 232 | {"text", "some text", "some text", nil, true}, |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 233 | {"nil action", "{{nil}}", "", nil, false}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 234 | |
Rob Pike | 7c47741 | 2011-07-12 13:15:26 +1000 | [diff] [blame] | 235 | // Ideal constants. |
| 236 | {"ideal int", "{{typeOf 3}}", "int", 0, true}, |
| 237 | {"ideal float", "{{typeOf 1.0}}", "float64", 0, true}, |
| 238 | {"ideal exp float", "{{typeOf 1e1}}", "float64", 0, true}, |
| 239 | {"ideal complex", "{{typeOf 1i}}", "complex128", 0, true}, |
| 240 | {"ideal int", "{{typeOf " + bigInt + "}}", "int", 0, true}, |
| 241 | {"ideal too big", "{{typeOf " + bigUint + "}}", "", 0, false}, |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 242 | {"ideal nil without type", "{{nil}}", "", 0, false}, |
Rob Pike | 7c47741 | 2011-07-12 13:15:26 +1000 | [diff] [blame] | 243 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 244 | // Fields of structs. |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 245 | {".X", "-{{.X}}-", "-x-", tVal, true}, |
| 246 | {".U.V", "-{{.U.V}}-", "-v-", tVal, true}, |
Rob Pike | a8098cb | 2012-04-23 15:39:02 +1000 | [diff] [blame] | 247 | {".unexported", "{{.unexported}}", "", tVal, false}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 248 | |
Gustavo Niemeyer | e3df71f | 2011-08-15 00:56:01 -0300 | [diff] [blame] | 249 | // Fields on maps. |
| 250 | {"map .one", "{{.MSI.one}}", "1", tVal, true}, |
| 251 | {"map .two", "{{.MSI.two}}", "2", tVal, true}, |
| 252 | {"map .NO", "{{.MSI.NO}}", "<no value>", tVal, true}, |
| 253 | {"map .one interface", "{{.MXI.one}}", "1", tVal, true}, |
| 254 | {"map .WRONG args", "{{.MSI.one 1}}", "", tVal, false}, |
| 255 | {"map .WRONG type", "{{.MII.one}}", "", tVal, false}, |
| 256 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 257 | // Dots of all kinds to test basic evaluation. |
| 258 | {"dot int", "<{{.}}>", "<13>", 13, true}, |
| 259 | {"dot uint", "<{{.}}>", "<14>", uint(14), true}, |
| 260 | {"dot float", "<{{.}}>", "<15.1>", 15.1, true}, |
| 261 | {"dot bool", "<{{.}}>", "<true>", true, true}, |
| 262 | {"dot complex", "<{{.}}>", "<(16.2-17i)>", 16.2 - 17i, true}, |
| 263 | {"dot string", "<{{.}}>", "<hello>", "hello", true}, |
| 264 | {"dot slice", "<{{.}}>", "<[-1 -2 -3]>", []int{-1, -2, -3}, true}, |
Russ Cox | 9a7dd71 | 2011-10-17 14:51:45 -0400 | [diff] [blame] | 265 | {"dot map", "<{{.}}>", "<map[two:22]>", map[string]int{"two": 22}, true}, |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 266 | {"dot struct", "<{{.}}>", "<{7 seven}>", struct { |
| 267 | a int |
| 268 | b string |
| 269 | }{7, "seven"}, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 270 | |
Rob Pike | 58baf64 | 2011-07-09 12:05:39 +1000 | [diff] [blame] | 271 | // Variables. |
| 272 | {"$ int", "{{$}}", "123", 123, true}, |
Rob Pike | 7b79b3b | 2011-07-11 15:23:38 +1000 | [diff] [blame] | 273 | {"$.I", "{{$.I}}", "17", tVal, true}, |
| 274 | {"$.U.V", "{{$.U.V}}", "v", tVal, true}, |
Rob Pike | 3e79c95 | 2011-07-22 10:51:40 +1000 | [diff] [blame] | 275 | {"declare in action", "{{$x := $.U.V}}{{$x}}", "v", tVal, true}, |
Rob Pike | 58baf64 | 2011-07-09 12:05:39 +1000 | [diff] [blame] | 276 | |
Rob Pike | 6ca968c | 2011-08-11 14:36:51 +1000 | [diff] [blame] | 277 | // Type with String method. |
| 278 | {"V{6666}.String()", "-{{.V0}}-", "-<6666>-", tVal, true}, |
| 279 | {"&V{7777}.String()", "-{{.V1}}-", "-<7777>-", tVal, true}, |
| 280 | {"(*V)(nil).String()", "-{{.V2}}-", "-nilV-", tVal, true}, |
| 281 | |
David Symonds | 39fcca6 | 2011-11-04 23:45:38 +1100 | [diff] [blame] | 282 | // Type with Error method. |
| 283 | {"W{888}.Error()", "-{{.W0}}-", "-[888]-", tVal, true}, |
| 284 | {"&W{999}.Error()", "-{{.W1}}-", "-[999]-", tVal, true}, |
| 285 | {"(*W)(nil).Error()", "-{{.W2}}-", "-nilW-", tVal, true}, |
| 286 | |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 287 | // Pointers. |
| 288 | {"*int", "{{.PI}}", "23", tVal, true}, |
Rob Pike | 1f661fc | 2013-08-27 13:29:07 +1000 | [diff] [blame^] | 289 | {"*string", "{{.PS}}", "a string", tVal, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 290 | {"*[]int", "{{.PSI}}", "[21 22 23]", tVal, true}, |
| 291 | {"*[]int[1]", "{{index .PSI 1}}", "22", tVal, true}, |
| 292 | {"NIL", "{{.NIL}}", "<nil>", tVal, true}, |
| 293 | |
Rob Pike | d3d08e1 | 2011-07-08 18:25:46 +1000 | [diff] [blame] | 294 | // Empty interfaces holding values. |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 295 | {"empty nil", "{{.Empty0}}", "<no value>", tVal, true}, |
| 296 | {"empty with int", "{{.Empty1}}", "3", tVal, true}, |
| 297 | {"empty with string", "{{.Empty2}}", "empty2", tVal, true}, |
| 298 | {"empty with slice", "{{.Empty3}}", "[7 8]", tVal, true}, |
Rob Pike | 9a5bb28 | 2011-07-18 17:34:42 +1000 | [diff] [blame] | 299 | {"empty with struct", "{{.Empty4}}", "{UinEmpty}", tVal, true}, |
| 300 | {"empty with struct, field", "{{.Empty4.V}}", "UinEmpty", tVal, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 301 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 302 | // Method calls. |
Rob Pike | a852981 | 2011-07-08 15:22:05 +1000 | [diff] [blame] | 303 | {".Method0", "-{{.Method0}}-", "-M0-", tVal, true}, |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 304 | {".Method1(1234)", "-{{.Method1 1234}}-", "-1234-", tVal, true}, |
| 305 | {".Method1(.I)", "-{{.Method1 .I}}-", "-17-", tVal, true}, |
| 306 | {".Method2(3, .X)", "-{{.Method2 3 .X}}-", "-Method2: 3 x-", tVal, true}, |
| 307 | {".Method2(.U16, `str`)", "-{{.Method2 .U16 `str`}}-", "-Method2: 16 str-", tVal, true}, |
Rob Pike | e86d727 | 2011-07-09 17:11:35 +1000 | [diff] [blame] | 308 | {".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] | 309 | {".Method3(nil constant)", "-{{.Method3 nil}}-", "-Method3: <nil>-", tVal, true}, |
| 310 | {".Method3(nil value)", "-{{.Method3 .MXI.unset}}-", "-Method3: <nil>-", tVal, true}, |
Rob Pike | 7b79b3b | 2011-07-11 15:23:38 +1000 | [diff] [blame] | 311 | {"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] | 312 | {"method on chained var", |
| 313 | "{{range .MSIone}}{{if $.U.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}", |
| 314 | "true", tVal, true}, |
| 315 | {"chained method", |
| 316 | "{{range .MSIone}}{{if $.GetU.TrueFalse $.True}}{{$.U.TrueFalse $.True}}{{else}}WRONG{{end}}{{end}}", |
| 317 | "true", tVal, true}, |
| 318 | {"chained method on variable", |
| 319 | "{{with $x := .}}{{with .SI}}{{$.GetU.TrueFalse $.True}}{{end}}{{end}}", |
| 320 | "true", tVal, true}, |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 321 | {".NilOKFunc not nil", "{{call .NilOKFunc .PI}}", "false", tVal, true}, |
| 322 | {".NilOKFunc nil", "{{call .NilOKFunc nil}}", "true", tVal, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 323 | |
Rob Pike | f1d3ff1 | 2012-03-03 23:14:20 +1100 | [diff] [blame] | 324 | // Function call builtin. |
| 325 | {".BinaryFunc", "{{call .BinaryFunc `1` `2`}}", "[1=2]", tVal, true}, |
| 326 | {".VariadicFunc0", "{{call .VariadicFunc}}", "<>", tVal, true}, |
| 327 | {".VariadicFunc2", "{{call .VariadicFunc `he` `llo`}}", "<he+llo>", tVal, true}, |
| 328 | {".VariadicFuncInt", "{{call .VariadicFuncInt 33 `he` `llo`}}", "33=<he+llo>", tVal, true}, |
| 329 | {"if .BinaryFunc call", "{{ if .BinaryFunc}}{{call .BinaryFunc `1` `2`}}{{end}}", "[1=2]", tVal, true}, |
| 330 | {"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] | 331 | {"Interface Call", `{{stringer .S}}`, "foozle", map[string]interface{}{"S": bytes.NewBufferString("foozle")}, true}, |
Elias Naur | 83348a1 | 2013-08-13 11:11:05 +1000 | [diff] [blame] | 332 | {".ErrFunc", "{{call .ErrFunc}}", "bla", tVal, true}, |
Rob Pike | f1d3ff1 | 2012-03-03 23:14:20 +1100 | [diff] [blame] | 333 | |
| 334 | // Erroneous function calls (check args). |
| 335 | {".BinaryFuncTooFew", "{{call .BinaryFunc `1`}}", "", tVal, false}, |
| 336 | {".BinaryFuncTooMany", "{{call .BinaryFunc `1` `2` `3`}}", "", tVal, false}, |
| 337 | {".BinaryFuncBad0", "{{call .BinaryFunc 1 3}}", "", tVal, false}, |
| 338 | {".BinaryFuncBad1", "{{call .BinaryFunc `1` 3}}", "", tVal, false}, |
| 339 | {".VariadicFuncBad0", "{{call .VariadicFunc 3}}", "", tVal, false}, |
Rob Pike | 227a04d | 2012-03-04 08:06:26 +1100 | [diff] [blame] | 340 | {".VariadicFuncIntBad0", "{{call .VariadicFuncInt}}", "", tVal, false}, |
| 341 | {".VariadicFuncIntBad`", "{{call .VariadicFuncInt `x`}}", "", tVal, false}, |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 342 | {".VariadicFuncNilBad", "{{call .VariadicFunc nil}}", "", tVal, false}, |
Rob Pike | aca8071 | 2012-02-15 16:05:34 +1100 | [diff] [blame] | 343 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 344 | // Pipelines. |
Rob Pike | a852981 | 2011-07-08 15:22:05 +1000 | [diff] [blame] | 345 | {"pipeline", "-{{.Method0 | .Method2 .U16}}-", "-Method2: 16 M0-", tVal, true}, |
Rob Pike | f1d3ff1 | 2012-03-03 23:14:20 +1100 | [diff] [blame] | 346 | {"pipeline func", "-{{call .VariadicFunc `llo` | call .VariadicFunc `he` }}-", "-<he+<llo>>-", tVal, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 347 | |
Rob Pike | cc842c7 | 2012-08-24 12:37:23 -0700 | [diff] [blame] | 348 | // Parenthesized expressions |
| 349 | {"parens in pipeline", "{{printf `%d %d %d` (1) (2 | add 3) (add 4 (add 5 6))}}", "1 5 15", tVal, true}, |
| 350 | |
Rob Pike | 9050550 | 2012-09-24 13:23:15 +1000 | [diff] [blame] | 351 | // Parenthesized expressions with field accesses |
| 352 | {"parens: $ in paren", "{{($).X}}", "x", tVal, true}, |
| 353 | {"parens: $.GetU in paren", "{{($.GetU).V}}", "v", tVal, true}, |
| 354 | {"parens: $ in paren in pipe", "{{($ | echo).X}}", "x", tVal, true}, |
| 355 | {"parens: spaces and args", `{{(makemap "up" "down" "left" "right").left}}`, "right", tVal, true}, |
| 356 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 357 | // If. |
| 358 | {"if true", "{{if true}}TRUE{{end}}", "TRUE", tVal, true}, |
| 359 | {"if false", "{{if false}}TRUE{{else}}FALSE{{end}}", "FALSE", tVal, true}, |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 360 | {"if nil", "{{if nil}}TRUE{{end}}", "", tVal, false}, |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 361 | {"if 1", "{{if 1}}NON-ZERO{{else}}ZERO{{end}}", "NON-ZERO", tVal, true}, |
| 362 | {"if 0", "{{if 0}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true}, |
| 363 | {"if 1.5", "{{if 1.5}}NON-ZERO{{else}}ZERO{{end}}", "NON-ZERO", tVal, true}, |
| 364 | {"if 0.0", "{{if .FloatZero}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true}, |
| 365 | {"if 1.5i", "{{if 1.5i}}NON-ZERO{{else}}ZERO{{end}}", "NON-ZERO", tVal, true}, |
| 366 | {"if 0.0i", "{{if .ComplexZero}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true}, |
| 367 | {"if emptystring", "{{if ``}}NON-EMPTY{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, |
| 368 | {"if string", "{{if `notempty`}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true}, |
| 369 | {"if emptyslice", "{{if .SIEmpty}}NON-EMPTY{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, |
| 370 | {"if slice", "{{if .SI}}NON-EMPTY{{else}}EMPTY{{end}}", "NON-EMPTY", tVal, true}, |
| 371 | {"if emptymap", "{{if .MSIEmpty}}NON-EMPTY{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, |
| 372 | {"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] | 373 | {"if map unset", "{{if .MXI.none}}NON-ZERO{{else}}ZERO{{end}}", "ZERO", tVal, true}, |
| 374 | {"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] | 375 | {"if $x with $y int", "{{if $x := true}}{{with $y := .I}}{{$x}},{{$y}}{{end}}{{end}}", "true,17", tVal, true}, |
| 376 | {"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] | 377 | |
Rob Pike | abae847 | 2011-07-11 09:19:18 +1000 | [diff] [blame] | 378 | // Print etc. |
| 379 | {"print", `{{print "hello, print"}}`, "hello, print", tVal, true}, |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 380 | {"print 123", `{{print 1 2 3}}`, "1 2 3", tVal, true}, |
| 381 | {"print nil", `{{print nil}}`, "<nil>", tVal, true}, |
Rob Pike | abae847 | 2011-07-11 09:19:18 +1000 | [diff] [blame] | 382 | {"println", `{{println 1 2 3}}`, "1 2 3\n", tVal, true}, |
Rob Pike | b177c97 | 2011-07-05 14:23:51 +1000 | [diff] [blame] | 383 | {"printf int", `{{printf "%04x" 127}}`, "007f", tVal, true}, |
| 384 | {"printf float", `{{printf "%g" 3.5}}`, "3.5", tVal, true}, |
| 385 | {"printf complex", `{{printf "%g" 1+7i}}`, "(1+7i)", tVal, true}, |
| 386 | {"printf string", `{{printf "%s" "hello"}}`, "hello", tVal, true}, |
Rob Pike | d3d08e1 | 2011-07-08 18:25:46 +1000 | [diff] [blame] | 387 | {"printf function", `{{printf "%#q" zeroArgs}}`, "`zeroArgs`", tVal, true}, |
Rob Pike | b177c97 | 2011-07-05 14:23:51 +1000 | [diff] [blame] | 388 | {"printf field", `{{printf "%s" .U.V}}`, "v", tVal, true}, |
Rob Pike | a852981 | 2011-07-08 15:22:05 +1000 | [diff] [blame] | 389 | {"printf method", `{{printf "%s" .Method0}}`, "M0", tVal, true}, |
Rob Pike | e86d727 | 2011-07-09 17:11:35 +1000 | [diff] [blame] | 390 | {"printf dot", `{{with .I}}{{printf "%d" .}}{{end}}`, "17", tVal, true}, |
| 391 | {"printf var", `{{with $x := .I}}{{printf "%d" $x}}{{end}}`, "17", tVal, true}, |
Rob Pike | a852981 | 2011-07-08 15:22:05 +1000 | [diff] [blame] | 392 | {"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] | 393 | |
Rob Pike | eea5443 | 2011-07-05 17:05:15 +1000 | [diff] [blame] | 394 | // HTML. |
Rob Pike | cc9fed7 | 2011-07-05 15:58:54 +1000 | [diff] [blame] | 395 | {"html", `{{html "<script>alert(\"XSS\");</script>"}}`, |
Rob Pike | eea5443 | 2011-07-05 17:05:15 +1000 | [diff] [blame] | 396 | "<script>alert("XSS");</script>", nil, true}, |
Rob Pike | cc9fed7 | 2011-07-05 15:58:54 +1000 | [diff] [blame] | 397 | {"html pipeline", `{{printf "<script>alert(\"XSS\");</script>" | html}}`, |
Rob Pike | eea5443 | 2011-07-05 17:05:15 +1000 | [diff] [blame] | 398 | "<script>alert("XSS");</script>", nil, true}, |
Rob Pike | 1f661fc | 2013-08-27 13:29:07 +1000 | [diff] [blame^] | 399 | {"html", `{{html .PS}}`, "a string", tVal, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 400 | |
| 401 | // JavaScript. |
David Symonds | 33705dd | 2011-07-06 16:51:49 +1000 | [diff] [blame] | 402 | {"js", `{{js .}}`, `It\'d be nice.`, `It'd be nice.`, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 403 | |
David Symonds | 5e48e64 | 2011-08-13 14:00:16 +1000 | [diff] [blame] | 404 | // URL query. |
| 405 | {"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] | 406 | |
Rob Pike | eea5443 | 2011-07-05 17:05:15 +1000 | [diff] [blame] | 407 | // Booleans |
| 408 | {"not", "{{not true}} {{not false}}", "false true", nil, true}, |
Rob Pike | 469e333 | 2011-07-14 07:59:04 +1000 | [diff] [blame] | 409 | {"and", "{{and false 0}} {{and 1 0}} {{and 0 true}} {{and 1 1}}", "false 0 0 1", nil, true}, |
| 410 | {"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] | 411 | {"boolean if", "{{if and true 1 `hi`}}TRUE{{else}}FALSE{{end}}", "TRUE", tVal, true}, |
| 412 | {"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] | 413 | |
Rob Pike | 6732bd3 | 2011-07-06 22:27:06 +1000 | [diff] [blame] | 414 | // Indexing. |
| 415 | {"slice[0]", "{{index .SI 0}}", "3", tVal, true}, |
| 416 | {"slice[1]", "{{index .SI 1}}", "4", tVal, true}, |
| 417 | {"slice[HUGE]", "{{index .SI 10}}", "", tVal, false}, |
| 418 | {"slice[WRONG]", "{{index .SI `hello`}}", "", tVal, false}, |
| 419 | {"map[one]", "{{index .MSI `one`}}", "1", tVal, true}, |
| 420 | {"map[two]", "{{index .MSI `two`}}", "2", tVal, true}, |
Rob Pike | ce27433 | 2012-07-23 16:19:12 -0700 | [diff] [blame] | 421 | {"map[NO]", "{{index .MSI `XXX`}}", "0", tVal, true}, |
Rob Pike | 18c378c | 2012-08-08 20:02:19 -0700 | [diff] [blame] | 422 | {"map[nil]", "{{index .MSI nil}}", "0", tVal, true}, |
Rob Pike | 6732bd3 | 2011-07-06 22:27:06 +1000 | [diff] [blame] | 423 | {"map[WRONG]", "{{index .MSI 10}}", "", tVal, false}, |
| 424 | {"double index", "{{index .SMSI 1 `eleven`}}", "11", tVal, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 425 | |
Rob Pike | b1d1da4 | 2011-08-12 11:47:44 +1000 | [diff] [blame] | 426 | // Len. |
| 427 | {"slice", "{{len .SI}}", "3", tVal, true}, |
| 428 | {"map", "{{len .MSI }}", "3", tVal, true}, |
| 429 | {"len of int", "{{len 3}}", "", tVal, false}, |
| 430 | {"len of nothing", "{{len .Empty0}}", "", tVal, false}, |
| 431 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 432 | // With. |
| 433 | {"with true", "{{with true}}{{.}}{{end}}", "true", tVal, true}, |
| 434 | {"with false", "{{with false}}{{.}}{{else}}FALSE{{end}}", "FALSE", tVal, true}, |
| 435 | {"with 1", "{{with 1}}{{.}}{{else}}ZERO{{end}}", "1", tVal, true}, |
| 436 | {"with 0", "{{with 0}}{{.}}{{else}}ZERO{{end}}", "ZERO", tVal, true}, |
| 437 | {"with 1.5", "{{with 1.5}}{{.}}{{else}}ZERO{{end}}", "1.5", tVal, true}, |
| 438 | {"with 0.0", "{{with .FloatZero}}{{.}}{{else}}ZERO{{end}}", "ZERO", tVal, true}, |
| 439 | {"with 1.5i", "{{with 1.5i}}{{.}}{{else}}ZERO{{end}}", "(0+1.5i)", tVal, true}, |
| 440 | {"with 0.0i", "{{with .ComplexZero}}{{.}}{{else}}ZERO{{end}}", "ZERO", tVal, true}, |
| 441 | {"with emptystring", "{{with ``}}{{.}}{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, |
| 442 | {"with string", "{{with `notempty`}}{{.}}{{else}}EMPTY{{end}}", "notempty", tVal, true}, |
| 443 | {"with emptyslice", "{{with .SIEmpty}}{{.}}{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, |
| 444 | {"with slice", "{{with .SI}}{{.}}{{else}}EMPTY{{end}}", "[3 4 5]", tVal, true}, |
| 445 | {"with emptymap", "{{with .MSIEmpty}}{{.}}{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, |
| 446 | {"with map", "{{with .MSIone}}{{.}}{{else}}EMPTY{{end}}", "map[one:1]", tVal, true}, |
Rob Pike | 9a5bb28 | 2011-07-18 17:34:42 +1000 | [diff] [blame] | 447 | {"with empty interface, struct field", "{{with .Empty4}}{{.V}}{{end}}", "UinEmpty", tVal, true}, |
Rob Pike | fc1f0bd | 2011-07-14 13:15:55 +1000 | [diff] [blame] | 448 | {"with $x int", "{{with $x := .I}}{{$x}}{{end}}", "17", tVal, true}, |
Rob Pike | c705701 | 2011-07-17 13:31:59 +1000 | [diff] [blame] | 449 | {"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] | 450 | {"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] | 451 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 452 | // Range. |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 453 | {"range []int", "{{range .SI}}-{{.}}-{{end}}", "-3--4--5-", tVal, true}, |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 454 | {"range empty no else", "{{range .SIEmpty}}-{{.}}-{{end}}", "", tVal, true}, |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 455 | {"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] | 456 | {"range empty else", "{{range .SIEmpty}}-{{.}}-{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, |
Rob Pike | c756a19 | 2011-06-29 15:02:04 +1000 | [diff] [blame] | 457 | {"range []bool", "{{range .SB}}-{{.}}-{{end}}", "-true--false-", tVal, true}, |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 458 | {"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] | 459 | {"range map", "{{range .MSI}}-{{.}}-{{end}}", "-1--3--2-", tVal, true}, |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 460 | {"range empty map no else", "{{range .MSIEmpty}}-{{.}}-{{end}}", "", tVal, true}, |
Rob Pike | a5950df | 2012-01-13 14:09:13 -0800 | [diff] [blame] | 461 | {"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] | 462 | {"range empty map else", "{{range .MSIEmpty}}-{{.}}-{{else}}EMPTY{{end}}", "EMPTY", tVal, true}, |
Rob Pike | 238274e | 2011-07-07 14:51:35 +1000 | [diff] [blame] | 463 | {"range empty interface", "{{range .Empty3}}-{{.}}-{{else}}EMPTY{{end}}", "-7--8-", tVal, true}, |
Gustavo Niemeyer | e3f3a54 | 2011-08-15 00:22:28 -0300 | [diff] [blame] | 464 | {"range empty nil", "{{range .Empty0}}-{{.}}-{{end}}", "", tVal, true}, |
Rob Pike | fc1f0bd | 2011-07-14 13:15:55 +1000 | [diff] [blame] | 465 | {"range $x SI", "{{range $x := .SI}}<{{$x}}>{{end}}", "<3><4><5>", tVal, true}, |
| 466 | {"range $x $y SI", "{{range $x, $y := .SI}}<{{$x}}={{$y}}>{{end}}", "<0=3><1=4><2=5>", tVal, true}, |
| 467 | {"range $x MSIone", "{{range $x := .MSIone}}<{{$x}}>{{end}}", "<1>", tVal, true}, |
| 468 | {"range $x $y MSIone", "{{range $x, $y := .MSIone}}<{{$x}}={{$y}}>{{end}}", "<one=1>", tVal, true}, |
| 469 | {"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] | 470 | {"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] | 471 | {"range count", `{{range $i, $x := count 5}}[{{$i}}]{{$x}}{{end}}`, "[0]a[1]b[2]c[3]d[4]e", tVal, true}, |
| 472 | {"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] | 473 | |
Rob Pike | 469e333 | 2011-07-14 07:59:04 +1000 | [diff] [blame] | 474 | // Cute examples. |
| 475 | {"or as if true", `{{or .SI "slice is empty"}}`, "[3 4 5]", tVal, true}, |
| 476 | {"or as if false", `{{or .SIEmpty "slice is empty"}}`, "slice is empty", tVal, true}, |
| 477 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 478 | // Error handling. |
Rob Pike | 47424d9 | 2012-02-14 07:11:39 +1100 | [diff] [blame] | 479 | {"error method, error", "{{.MyError true}}", "", tVal, false}, |
| 480 | {"error method, no error", "{{.MyError false}}", "false", tVal, true}, |
Rob Pike | d366c36 | 2011-07-11 18:06:24 +1000 | [diff] [blame] | 481 | |
| 482 | // Fixed bugs. |
| 483 | // 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] | 484 | {"bug0", "{{range .MSIone}}{{if $.Method1 .}}X{{end}}{{end}}", "X", tVal, true}, |
Robert Griesemer | 7162f39 | 2011-07-30 17:11:52 -0700 | [diff] [blame] | 485 | // Do not loop endlessly in indirect for non-empty interfaces. |
Rob Pike | 1adda46 | 2011-08-09 17:07:29 +1000 | [diff] [blame] | 486 | // The bug appears with *interface only; looped forever. |
| 487 | {"bug1", "{{.Method0}}", "M0", &iVal, true}, |
| 488 | // Was taking address of interface field, so method set was empty. |
| 489 | {"bug2", "{{$.NonEmptyInterface.Method0}}", "M0", tVal, true}, |
| 490 | // Struct values were not legal in with - mere oversight. |
Rob Pike | 39fa2a5 | 2011-08-11 08:27:16 +1000 | [diff] [blame] | 491 | {"bug3", "{{with $}}{{.Method0}}{{end}}", "M0", tVal, true}, |
| 492 | // Nil interface values in if. |
| 493 | {"bug4", "{{if .Empty0}}non-nil{{else}}nil{{end}}", "nil", tVal, true}, |
David Symonds | fe59d86 | 2011-08-12 13:29:56 +1000 | [diff] [blame] | 494 | // Stringer. |
| 495 | {"bug5", "{{.Str}}", "foozle", tVal, true}, |
Russ Cox | e73680a | 2011-11-04 07:33:55 -0400 | [diff] [blame] | 496 | {"bug5a", "{{.Err}}", "erroozle", tVal, true}, |
Rob Pike | d45e808c | 2011-09-06 15:34:38 -0700 | [diff] [blame] | 497 | // Args need to be indirected and dereferenced sometimes. |
| 498 | {"bug6a", "{{vfunc .V0 .V1}}", "vfunc", tVal, true}, |
| 499 | {"bug6b", "{{vfunc .V0 .V0}}", "vfunc", tVal, true}, |
| 500 | {"bug6c", "{{vfunc .V1 .V0}}", "vfunc", tVal, true}, |
| 501 | {"bug6d", "{{vfunc .V1 .V1}}", "vfunc", tVal, true}, |
Rob Pike | d6ad6f0 | 2012-03-14 10:46:21 +1100 | [diff] [blame] | 502 | // Legal parse but illegal execution: non-function should have no arguments. |
| 503 | {"bug7a", "{{3 2}}", "", tVal, false}, |
| 504 | {"bug7b", "{{$x := 1}}{{$x 2}}", "", tVal, false}, |
| 505 | {"bug7c", "{{$x := 1}}{{3 | $x}}", "", tVal, false}, |
Rob Pike | 065db4e | 2012-04-03 11:44:52 +1000 | [diff] [blame] | 506 | // Pipelined arg was not being type-checked. |
| 507 | {"bug8a", "{{3|oneArg}}", "", tVal, false}, |
| 508 | {"bug8b", "{{4|dddArg 3}}", "", tVal, false}, |
Rob Pike | a8098cb | 2012-04-23 15:39:02 +1000 | [diff] [blame] | 509 | // A bug was introduced that broke map lookups for lower-case names. |
| 510 | {"bug9", "{{.cause}}", "neglect", map[string]string{"cause": "neglect"}, true}, |
Rob Pike | 99645db | 2013-03-27 16:31:14 -0700 | [diff] [blame] | 511 | // Field chain starting with function did not work. |
| 512 | {"bug10", "{{mapOfThree.three}}-{{(mapOfThree).three}}", "3-3", 0, true}, |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 513 | } |
| 514 | |
Rob Pike | d3d08e1 | 2011-07-08 18:25:46 +1000 | [diff] [blame] | 515 | func zeroArgs() string { |
| 516 | return "zeroArgs" |
| 517 | } |
| 518 | |
| 519 | func oneArg(a string) string { |
| 520 | return "oneArg=" + a |
Rob Pike | b177c97 | 2011-07-05 14:23:51 +1000 | [diff] [blame] | 521 | } |
| 522 | |
Rob Pike | 065db4e | 2012-04-03 11:44:52 +1000 | [diff] [blame] | 523 | func dddArg(a int, b ...string) string { |
| 524 | return fmt.Sprintln(a, b) |
| 525 | } |
| 526 | |
Rob Pike | 361c5ac | 2011-08-29 15:56:52 +1000 | [diff] [blame] | 527 | // count returns a channel that will deliver n sequential 1-letter strings starting at "a" |
| 528 | func count(n int) chan string { |
| 529 | if n == 0 { |
| 530 | return nil |
| 531 | } |
| 532 | c := make(chan string) |
| 533 | go func() { |
| 534 | for i := 0; i < n; i++ { |
| 535 | c <- "abcdefghijklmnop"[i : i+1] |
| 536 | } |
| 537 | close(c) |
| 538 | }() |
| 539 | return c |
| 540 | } |
| 541 | |
Rob Pike | d45e808c | 2011-09-06 15:34:38 -0700 | [diff] [blame] | 542 | // vfunc takes a *V and a V |
| 543 | func vfunc(V, *V) string { |
| 544 | return "vfunc" |
| 545 | } |
| 546 | |
Rob Pike | cc842c7 | 2012-08-24 12:37:23 -0700 | [diff] [blame] | 547 | func add(args ...int) int { |
| 548 | sum := 0 |
| 549 | for _, x := range args { |
| 550 | sum += x |
| 551 | } |
| 552 | return sum |
| 553 | } |
| 554 | |
Rob Pike | 9050550 | 2012-09-24 13:23:15 +1000 | [diff] [blame] | 555 | func echo(arg interface{}) interface{} { |
| 556 | return arg |
| 557 | } |
| 558 | |
| 559 | func makemap(arg ...string) map[string]string { |
| 560 | if len(arg)%2 != 0 { |
| 561 | panic("bad makemap") |
| 562 | } |
| 563 | m := make(map[string]string) |
| 564 | for i := 0; i < len(arg); i += 2 { |
| 565 | m[arg[i]] = arg[i+1] |
| 566 | } |
| 567 | return m |
| 568 | } |
| 569 | |
Ugorji Nwoke | 4f7c33c | 2012-05-22 15:21:35 -0700 | [diff] [blame] | 570 | func stringer(s fmt.Stringer) string { |
| 571 | return s.String() |
| 572 | } |
| 573 | |
Rob Pike | 99645db | 2013-03-27 16:31:14 -0700 | [diff] [blame] | 574 | func mapOfThree() interface{} { |
| 575 | return map[string]int{"three": 3} |
| 576 | } |
| 577 | |
Rob Pike | f56db6f | 2011-11-23 20:17:22 -0800 | [diff] [blame] | 578 | func testExecute(execTests []execTest, template *Template, t *testing.T) { |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 579 | b := new(bytes.Buffer) |
Rob Pike | 361c5ac | 2011-08-29 15:56:52 +1000 | [diff] [blame] | 580 | funcs := FuncMap{ |
Rob Pike | 99645db | 2013-03-27 16:31:14 -0700 | [diff] [blame] | 581 | "add": add, |
| 582 | "count": count, |
| 583 | "dddArg": dddArg, |
| 584 | "echo": echo, |
| 585 | "makemap": makemap, |
| 586 | "mapOfThree": mapOfThree, |
| 587 | "oneArg": oneArg, |
| 588 | "stringer": stringer, |
| 589 | "typeOf": typeOf, |
| 590 | "vfunc": vfunc, |
| 591 | "zeroArgs": zeroArgs, |
Rob Pike | 361c5ac | 2011-08-29 15:56:52 +1000 | [diff] [blame] | 592 | } |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 593 | for _, test := range execTests { |
Rob Pike | f56db6f | 2011-11-23 20:17:22 -0800 | [diff] [blame] | 594 | var tmpl *Template |
| 595 | var err error |
| 596 | if template == nil { |
| 597 | tmpl, err = New(test.name).Funcs(funcs).Parse(test.input) |
| 598 | } else { |
| 599 | tmpl, err = template.New(test.name).Funcs(funcs).Parse(test.input) |
Rob Pike | 25d2987 | 2011-11-17 22:53:23 -0800 | [diff] [blame] | 600 | } |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 601 | if err != nil { |
| 602 | t.Errorf("%s: parse error: %s", test.name, err) |
| 603 | continue |
| 604 | } |
| 605 | b.Reset() |
Rob Pike | 0f7a195 | 2011-07-21 14:22:01 +1000 | [diff] [blame] | 606 | err = tmpl.Execute(b, test.data) |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 607 | switch { |
| 608 | case !test.ok && err == nil: |
| 609 | t.Errorf("%s: expected error; got none", test.name) |
| 610 | continue |
| 611 | case test.ok && err != nil: |
| 612 | t.Errorf("%s: unexpected execute error: %s", test.name, err) |
| 613 | continue |
| 614 | case !test.ok && err != nil: |
Rob Pike | 6732bd3 | 2011-07-06 22:27:06 +1000 | [diff] [blame] | 615 | // expected error, got one |
| 616 | if *debug { |
| 617 | fmt.Printf("%s: %s\n\t%s\n", test.name, test.input, err) |
| 618 | } |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 619 | } |
| 620 | result := b.String() |
| 621 | if result != test.output { |
| 622 | t.Errorf("%s: expected\n\t%q\ngot\n\t%q", test.name, test.output, result) |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
Rob Pike | 13f8897 | 2011-07-04 15:15:47 +1000 | [diff] [blame] | 627 | func TestExecute(t *testing.T) { |
| 628 | testExecute(execTests, nil, t) |
| 629 | } |
| 630 | |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 631 | var delimPairs = []string{ |
| 632 | "", "", // default |
| 633 | "{{", "}}", // same as default |
| 634 | "<<", ">>", // distinct |
| 635 | "|", "|", // same |
| 636 | "(日)", "(本)", // peculiar |
| 637 | } |
| 638 | |
| 639 | func TestDelims(t *testing.T) { |
| 640 | const hello = "Hello, world" |
| 641 | var value = struct{ Str string }{hello} |
| 642 | for i := 0; i < len(delimPairs); i += 2 { |
| 643 | text := ".Str" |
| 644 | left := delimPairs[i+0] |
Rob Pike | b3dd327 | 2011-10-06 15:21:56 -0700 | [diff] [blame] | 645 | trueLeft := left |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 646 | right := delimPairs[i+1] |
Rob Pike | b3dd327 | 2011-10-06 15:21:56 -0700 | [diff] [blame] | 647 | trueRight := right |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 648 | if left == "" { // default case |
Rob Pike | b3dd327 | 2011-10-06 15:21:56 -0700 | [diff] [blame] | 649 | trueLeft = "{{" |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 650 | } |
| 651 | if right == "" { // default case |
Rob Pike | b3dd327 | 2011-10-06 15:21:56 -0700 | [diff] [blame] | 652 | trueRight = "}}" |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 653 | } |
Rob Pike | b3dd327 | 2011-10-06 15:21:56 -0700 | [diff] [blame] | 654 | text = trueLeft + text + trueRight |
| 655 | // Now add a comment |
| 656 | text += trueLeft + "/*comment*/" + trueRight |
| 657 | // Now add an action containing a string. |
| 658 | text += trueLeft + `"` + trueLeft + `"` + trueRight |
| 659 | // At this point text looks like `{{.Str}}{{/*comment*/}}{{"{{"}}`. |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 660 | tmpl, err := New("delims").Delims(left, right).Parse(text) |
| 661 | if err != nil { |
| 662 | t.Fatalf("delim %q text %q parse err %s", left, text, err) |
| 663 | } |
| 664 | var b = new(bytes.Buffer) |
| 665 | err = tmpl.Execute(b, value) |
| 666 | if err != nil { |
| 667 | t.Fatalf("delim %q exec err %s", left, err) |
| 668 | } |
Rob Pike | b3dd327 | 2011-10-06 15:21:56 -0700 | [diff] [blame] | 669 | if b.String() != hello+trueLeft { |
Robert Hencke | c501824 | 2011-10-13 13:34:01 +1100 | [diff] [blame] | 670 | t.Errorf("expected %q got %q", hello+trueLeft, b.String()) |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 675 | // Check that an error from a method flows back to the top. |
| 676 | func TestExecuteError(t *testing.T) { |
| 677 | b := new(bytes.Buffer) |
| 678 | tmpl := New("error") |
Rob Pike | 47424d9 | 2012-02-14 07:11:39 +1100 | [diff] [blame] | 679 | _, err := tmpl.Parse("{{.MyError true}}") |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 680 | if err != nil { |
| 681 | t.Fatalf("parse error: %s", err) |
| 682 | } |
| 683 | err = tmpl.Execute(b, tVal) |
| 684 | if err == nil { |
| 685 | t.Errorf("expected error; got none") |
Rob Pike | 47424d9 | 2012-02-14 07:11:39 +1100 | [diff] [blame] | 686 | } else if !strings.Contains(err.Error(), myError.Error()) { |
Rob Pike | dfffc7a | 2011-07-14 11:32:06 +1000 | [diff] [blame] | 687 | if *debug { |
| 688 | fmt.Printf("test execute error: %s\n", err) |
| 689 | } |
Rob Pike | 47424d9 | 2012-02-14 07:11:39 +1100 | [diff] [blame] | 690 | t.Errorf("expected myError; got %s", err) |
Rob Pike | 81592c2 | 2011-06-28 23:04:08 +1000 | [diff] [blame] | 691 | } |
| 692 | } |
David Symonds | 33705dd | 2011-07-06 16:51:49 +1000 | [diff] [blame] | 693 | |
Rob Pike | 7f4b4c0 | 2012-10-03 12:02:13 +1000 | [diff] [blame] | 694 | const execErrorText = `line 1 |
| 695 | line 2 |
| 696 | line 3 |
| 697 | {{template "one" .}} |
| 698 | {{define "one"}}{{template "two" .}}{{end}} |
| 699 | {{define "two"}}{{template "three" .}}{{end}} |
| 700 | {{define "three"}}{{index "hi" $}}{{end}}` |
| 701 | |
| 702 | // Check that an error from a nested template contains all the relevant information. |
| 703 | func TestExecError(t *testing.T) { |
| 704 | tmpl, err := New("top").Parse(execErrorText) |
| 705 | if err != nil { |
| 706 | t.Fatal("parse error:", err) |
| 707 | } |
| 708 | var b bytes.Buffer |
| 709 | err = tmpl.Execute(&b, 5) // 5 is out of range indexing "hi" |
| 710 | if err == nil { |
| 711 | t.Fatal("expected error") |
| 712 | } |
| 713 | const want = `template: top:7:20: executing "three" at <index "hi" $>: error calling index: index out of range: 5` |
| 714 | got := err.Error() |
| 715 | if got != want { |
| 716 | t.Errorf("expected\n%q\ngot\n%q", want, got) |
| 717 | } |
| 718 | } |
| 719 | |
David Symonds | 33705dd | 2011-07-06 16:51:49 +1000 | [diff] [blame] | 720 | func TestJSEscaping(t *testing.T) { |
| 721 | testCases := []struct { |
| 722 | in, exp string |
| 723 | }{ |
| 724 | {`a`, `a`}, |
| 725 | {`'foo`, `\'foo`}, |
| 726 | {`Go "jump" \`, `Go \"jump\" \\`}, |
| 727 | {`Yukihiro says "今日は世界"`, `Yukihiro says \"今日は世界\"`}, |
| 728 | {"unprintable \uFDFF", `unprintable \uFDFF`}, |
David Symonds | a16ad6f | 2011-07-14 12:02:58 +1000 | [diff] [blame] | 729 | {`<html>`, `\x3Chtml\x3E`}, |
David Symonds | 33705dd | 2011-07-06 16:51:49 +1000 | [diff] [blame] | 730 | } |
| 731 | for _, tc := range testCases { |
| 732 | s := JSEscapeString(tc.in) |
| 733 | if s != tc.exp { |
| 734 | t.Errorf("JS escaping [%s] got [%s] want [%s]", tc.in, s, tc.exp) |
| 735 | } |
| 736 | } |
| 737 | } |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 738 | |
| 739 | // A nice example: walk a binary tree. |
| 740 | |
| 741 | type Tree struct { |
| 742 | Val int |
| 743 | Left, Right *Tree |
| 744 | } |
| 745 | |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 746 | // Use different delimiters to test Set.Delims. |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 747 | const treeTemplate = ` |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 748 | (define "tree") |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 749 | [ |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 750 | (.Val) |
| 751 | (with .Left) |
| 752 | (template "tree" .) |
| 753 | (end) |
| 754 | (with .Right) |
| 755 | (template "tree" .) |
| 756 | (end) |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 757 | ] |
Rob Pike | dcf5318 | 2011-10-06 13:30:50 -0700 | [diff] [blame] | 758 | (end) |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 759 | ` |
| 760 | |
| 761 | func TestTree(t *testing.T) { |
| 762 | var tree = &Tree{ |
| 763 | 1, |
| 764 | &Tree{ |
| 765 | 2, &Tree{ |
| 766 | 3, |
| 767 | &Tree{ |
| 768 | 4, nil, nil, |
| 769 | }, |
| 770 | nil, |
| 771 | }, |
| 772 | &Tree{ |
| 773 | 5, |
| 774 | &Tree{ |
| 775 | 6, nil, nil, |
| 776 | }, |
| 777 | nil, |
| 778 | }, |
| 779 | }, |
| 780 | &Tree{ |
| 781 | 7, |
| 782 | &Tree{ |
| 783 | 8, |
| 784 | &Tree{ |
| 785 | 9, nil, nil, |
| 786 | }, |
| 787 | nil, |
| 788 | }, |
| 789 | &Tree{ |
| 790 | 10, |
| 791 | &Tree{ |
| 792 | 11, nil, nil, |
| 793 | }, |
| 794 | nil, |
| 795 | }, |
| 796 | }, |
| 797 | } |
Rob Pike | f56db6f | 2011-11-23 20:17:22 -0800 | [diff] [blame] | 798 | tmpl, err := New("root").Delims("(", ")").Parse(treeTemplate) |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 799 | if err != nil { |
| 800 | t.Fatal("parse error:", err) |
| 801 | } |
| 802 | var b bytes.Buffer |
Russ Cox | cfa036a | 2011-10-25 22:22:42 -0700 | [diff] [blame] | 803 | stripSpace := func(r rune) rune { |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 804 | if r == '\t' || r == '\n' { |
| 805 | return -1 |
| 806 | } |
| 807 | return r |
| 808 | } |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 809 | 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] | 810 | // First by looking up the template. |
Rob Pike | e9025df | 2011-11-26 08:32:55 -0800 | [diff] [blame] | 811 | err = tmpl.Lookup("tree").Execute(&b, tree) |
Rob Pike | f56db6f | 2011-11-23 20:17:22 -0800 | [diff] [blame] | 812 | if err != nil { |
| 813 | t.Fatal("exec error:", err) |
| 814 | } |
| 815 | result := strings.Map(stripSpace, b.String()) |
| 816 | if result != expect { |
| 817 | t.Errorf("expected %q got %q", expect, result) |
| 818 | } |
| 819 | // Then direct to execution. |
| 820 | b.Reset() |
| 821 | err = tmpl.ExecuteTemplate(&b, "tree", tree) |
| 822 | if err != nil { |
| 823 | t.Fatal("exec error:", err) |
| 824 | } |
| 825 | result = strings.Map(stripSpace, b.String()) |
Rob Pike | 02039b6 | 2011-07-08 16:49:06 +1000 | [diff] [blame] | 826 | if result != expect { |
| 827 | t.Errorf("expected %q got %q", expect, result) |
| 828 | } |
| 829 | } |
Rob Pike | bcccad4 | 2012-10-07 09:26:59 +1100 | [diff] [blame] | 830 | |
| 831 | func TestExecuteOnNewTemplate(t *testing.T) { |
| 832 | // This is issue 3872. |
| 833 | _ = New("Name").Templates() |
| 834 | } |
Rob Pike | c763799 | 2013-03-06 12:34:19 -0800 | [diff] [blame] | 835 | |
| 836 | const testTemplates = `{{define "one"}}one{{end}}{{define "two"}}two{{end}}` |
| 837 | |
| 838 | func TestMessageForExecuteEmpty(t *testing.T) { |
| 839 | // Test a truly empty template. |
| 840 | tmpl := New("empty") |
| 841 | var b bytes.Buffer |
| 842 | err := tmpl.Execute(&b, 0) |
| 843 | if err == nil { |
| 844 | t.Fatal("expected initial error") |
| 845 | } |
| 846 | got := err.Error() |
| 847 | want := `template: empty: "empty" is an incomplete or empty template` |
| 848 | if got != want { |
| 849 | t.Errorf("expected error %s got %s", want, got) |
| 850 | } |
| 851 | // Add a non-empty template to check that the error is helpful. |
| 852 | tests, err := New("").Parse(testTemplates) |
| 853 | if err != nil { |
| 854 | t.Fatal(err) |
| 855 | } |
| 856 | tmpl.AddParseTree("secondary", tests.Tree) |
| 857 | err = tmpl.Execute(&b, 0) |
| 858 | if err == nil { |
| 859 | t.Fatal("expected second error") |
| 860 | } |
| 861 | got = err.Error() |
| 862 | want = `template: empty: "empty" is an incomplete or empty template; defined templates are: "secondary"` |
| 863 | if got != want { |
| 864 | t.Errorf("expected error %s got %s", want, got) |
| 865 | } |
| 866 | // Make sure we can execute the secondary. |
| 867 | err = tmpl.ExecuteTemplate(&b, "secondary", 0) |
| 868 | if err != nil { |
| 869 | t.Fatal(err) |
| 870 | } |
| 871 | } |
Rob Pike | 7bbe320 | 2013-08-21 11:27:27 +1000 | [diff] [blame] | 872 | |
| 873 | type cmpTest struct { |
| 874 | expr string |
| 875 | truth string |
| 876 | ok bool |
| 877 | } |
| 878 | |
| 879 | var cmpTests = []cmpTest{ |
| 880 | {"eq true true", "true", true}, |
| 881 | {"eq true false", "false", true}, |
| 882 | {"eq 1+2i 1+2i", "true", true}, |
| 883 | {"eq 1+2i 1+3i", "false", true}, |
| 884 | {"eq 1.5 1.5", "true", true}, |
| 885 | {"eq 1.5 2.5", "false", true}, |
| 886 | {"eq 1 1", "true", true}, |
| 887 | {"eq 1 2", "false", true}, |
| 888 | {"eq `xy` `xy`", "true", true}, |
| 889 | {"eq `xy` `xyz`", "false", true}, |
| 890 | {"eq .Xuint .Xuint", "true", true}, |
| 891 | {"eq .Xuint .Yuint", "false", true}, |
| 892 | {"ne true true", "false", true}, |
| 893 | {"ne true false", "true", true}, |
| 894 | {"ne 1+2i 1+2i", "false", true}, |
| 895 | {"ne 1+2i 1+3i", "true", true}, |
| 896 | {"ne 1.5 1.5", "false", true}, |
| 897 | {"ne 1.5 2.5", "true", true}, |
| 898 | {"ne 1 1", "false", true}, |
| 899 | {"ne 1 2", "true", true}, |
| 900 | {"ne `xy` `xy`", "false", true}, |
| 901 | {"ne `xy` `xyz`", "true", true}, |
| 902 | {"ne .Xuint .Xuint", "false", true}, |
| 903 | {"ne .Xuint .Yuint", "true", true}, |
| 904 | {"lt 1.5 1.5", "false", true}, |
| 905 | {"lt 1.5 2.5", "true", true}, |
| 906 | {"lt 1 1", "false", true}, |
| 907 | {"lt 1 2", "true", true}, |
| 908 | {"lt `xy` `xy`", "false", true}, |
| 909 | {"lt `xy` `xyz`", "true", true}, |
| 910 | {"lt .Xuint .Xuint", "false", true}, |
| 911 | {"lt .Xuint .Yuint", "true", true}, |
| 912 | {"le 1.5 1.5", "true", true}, |
| 913 | {"le 1.5 2.5", "true", true}, |
| 914 | {"le 2.5 1.5", "false", true}, |
| 915 | {"le 1 1", "true", true}, |
| 916 | {"le 1 2", "true", true}, |
| 917 | {"le 2 1", "false", true}, |
| 918 | {"le `xy` `xy`", "true", true}, |
| 919 | {"le `xy` `xyz`", "true", true}, |
| 920 | {"le `xyz` `xy`", "false", true}, |
| 921 | {"le .Xuint .Xuint", "true", true}, |
| 922 | {"le .Xuint .Yuint", "true", true}, |
| 923 | {"le .Yuint .Xuint", "false", true}, |
| 924 | {"gt 1.5 1.5", "false", true}, |
| 925 | {"gt 1.5 2.5", "false", true}, |
| 926 | {"gt 1 1", "false", true}, |
| 927 | {"gt 2 1", "true", true}, |
| 928 | {"gt 1 2", "false", true}, |
| 929 | {"gt `xy` `xy`", "false", true}, |
| 930 | {"gt `xy` `xyz`", "false", true}, |
| 931 | {"gt .Xuint .Xuint", "false", true}, |
| 932 | {"gt .Xuint .Yuint", "false", true}, |
| 933 | {"gt .Yuint .Xuint", "true", true}, |
| 934 | {"ge 1.5 1.5", "true", true}, |
| 935 | {"ge 1.5 2.5", "false", true}, |
| 936 | {"ge 2.5 1.5", "true", true}, |
| 937 | {"ge 1 1", "true", true}, |
| 938 | {"ge 1 2", "false", true}, |
| 939 | {"ge 2 1", "true", true}, |
| 940 | {"ge `xy` `xy`", "true", true}, |
| 941 | {"ge `xy` `xyz`", "false", true}, |
| 942 | {"ge `xyz` `xy`", "true", true}, |
| 943 | {"ge .Xuint .Xuint", "true", true}, |
| 944 | {"ge .Xuint .Yuint", "false", true}, |
| 945 | {"ge .Yuint .Xuint", "true", true}, |
| 946 | // Errors |
| 947 | {"eq 3 4 5", "", false}, // Too many arguments. |
| 948 | {"eq `xy` 1", "", false}, // Different types. |
| 949 | {"lt true true", "", false}, // Unordered types. |
| 950 | {"lt 1+0i 1+0i", "", false}, // Unordered types. |
| 951 | } |
| 952 | |
| 953 | func TestComparison(t *testing.T) { |
| 954 | b := new(bytes.Buffer) |
| 955 | var cmpStruct = struct { |
| 956 | Xuint, Yuint uint |
| 957 | }{3, 4} |
| 958 | for _, test := range cmpTests { |
| 959 | text := fmt.Sprintf("{{if %s}}true{{else}}false{{end}}", test.expr) |
| 960 | tmpl, err := New("empty").Parse(text) |
| 961 | if err != nil { |
| 962 | t.Fatal(err) |
| 963 | } |
| 964 | b.Reset() |
| 965 | err = tmpl.Execute(b, &cmpStruct) |
| 966 | if test.ok && err != nil { |
| 967 | t.Errorf("%s errored incorrectly: %s", test.expr, err) |
| 968 | continue |
| 969 | } |
| 970 | if !test.ok && err == nil { |
| 971 | t.Errorf("%s did not error") |
| 972 | continue |
| 973 | } |
| 974 | if b.String() != test.truth { |
| 975 | t.Errorf("%s: want %s; got %s", test.expr, test.truth, b.String()) |
| 976 | } |
| 977 | } |
| 978 | } |