github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/text/template/parse/parse_test.go (about) 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 parse 6 7 import ( 8 "flag" 9 "fmt" 10 "strings" 11 "testing" 12 ) 13 14 var debug = flag.Bool("debug", false, "show the errors produced by the main tests") 15 16 type numberTest struct { 17 text string 18 isInt bool 19 isUint bool 20 isFloat bool 21 isComplex bool 22 int64 23 uint64 24 float64 25 complex128 26 } 27 28 var numberTests = []numberTest{ 29 // basics 30 {"0", true, true, true, false, 0, 0, 0, 0}, 31 {"-0", true, true, true, false, 0, 0, 0, 0}, // check that -0 is a uint. 32 {"73", true, true, true, false, 73, 73, 73, 0}, 33 {"073", true, true, true, false, 073, 073, 073, 0}, 34 {"0x73", true, true, true, false, 0x73, 0x73, 0x73, 0}, 35 {"-73", true, false, true, false, -73, 0, -73, 0}, 36 {"+73", true, false, true, false, 73, 0, 73, 0}, 37 {"100", true, true, true, false, 100, 100, 100, 0}, 38 {"1e9", true, true, true, false, 1e9, 1e9, 1e9, 0}, 39 {"-1e9", true, false, true, false, -1e9, 0, -1e9, 0}, 40 {"-1.2", false, false, true, false, 0, 0, -1.2, 0}, 41 {"1e19", false, true, true, false, 0, 1e19, 1e19, 0}, 42 {"-1e19", false, false, true, false, 0, 0, -1e19, 0}, 43 {"4i", false, false, false, true, 0, 0, 0, 4i}, 44 {"-1.2+4.2i", false, false, false, true, 0, 0, 0, -1.2 + 4.2i}, 45 {"073i", false, false, false, true, 0, 0, 0, 73i}, // not octal! 46 // complex with 0 imaginary are float (and maybe integer) 47 {"0i", true, true, true, true, 0, 0, 0, 0}, 48 {"-1.2+0i", false, false, true, true, 0, 0, -1.2, -1.2}, 49 {"-12+0i", true, false, true, true, -12, 0, -12, -12}, 50 {"13+0i", true, true, true, true, 13, 13, 13, 13}, 51 // funny bases 52 {"0123", true, true, true, false, 0123, 0123, 0123, 0}, 53 {"-0x0", true, true, true, false, 0, 0, 0, 0}, 54 {"0xdeadbeef", true, true, true, false, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 0}, 55 // character constants 56 {`'a'`, true, true, true, false, 'a', 'a', 'a', 0}, 57 {`'\n'`, true, true, true, false, '\n', '\n', '\n', 0}, 58 {`'\\'`, true, true, true, false, '\\', '\\', '\\', 0}, 59 {`'\''`, true, true, true, false, '\'', '\'', '\'', 0}, 60 {`'\xFF'`, true, true, true, false, 0xFF, 0xFF, 0xFF, 0}, 61 {`'パ'`, true, true, true, false, 0x30d1, 0x30d1, 0x30d1, 0}, 62 {`'\u30d1'`, true, true, true, false, 0x30d1, 0x30d1, 0x30d1, 0}, 63 {`'\U000030d1'`, true, true, true, false, 0x30d1, 0x30d1, 0x30d1, 0}, 64 // some broken syntax 65 {text: "+-2"}, 66 {text: "0x123."}, 67 {text: "1e."}, 68 {text: "0xi."}, 69 {text: "1+2."}, 70 {text: "'x"}, 71 {text: "'xx'"}, 72 } 73 74 func TestNumberParse(t *testing.T) { 75 for _, test := range numberTests { 76 // If fmt.Sscan thinks it's complex, it's complex. We can't trust the output 77 // because imaginary comes out as a number. 78 var c complex128 79 typ := itemNumber 80 if test.text[0] == '\'' { 81 typ = itemCharConstant 82 } else { 83 _, err := fmt.Sscan(test.text, &c) 84 if err == nil { 85 typ = itemComplex 86 } 87 } 88 n, err := newNumber(0, test.text, typ) 89 ok := test.isInt || test.isUint || test.isFloat || test.isComplex 90 if ok && err != nil { 91 t.Errorf("unexpected error for %q: %s", test.text, err) 92 continue 93 } 94 if !ok && err == nil { 95 t.Errorf("expected error for %q", test.text) 96 continue 97 } 98 if !ok { 99 if *debug { 100 fmt.Printf("%s\n\t%s\n", test.text, err) 101 } 102 continue 103 } 104 if n.IsComplex != test.isComplex { 105 t.Errorf("complex incorrect for %q; should be %t", test.text, test.isComplex) 106 } 107 if test.isInt { 108 if !n.IsInt { 109 t.Errorf("expected integer for %q", test.text) 110 } 111 if n.Int64 != test.int64 { 112 t.Errorf("int64 for %q should be %d Is %d", test.text, test.int64, n.Int64) 113 } 114 } else if n.IsInt { 115 t.Errorf("did not expect integer for %q", test.text) 116 } 117 if test.isUint { 118 if !n.IsUint { 119 t.Errorf("expected unsigned integer for %q", test.text) 120 } 121 if n.Uint64 != test.uint64 { 122 t.Errorf("uint64 for %q should be %d Is %d", test.text, test.uint64, n.Uint64) 123 } 124 } else if n.IsUint { 125 t.Errorf("did not expect unsigned integer for %q", test.text) 126 } 127 if test.isFloat { 128 if !n.IsFloat { 129 t.Errorf("expected float for %q", test.text) 130 } 131 if n.Float64 != test.float64 { 132 t.Errorf("float64 for %q should be %g Is %g", test.text, test.float64, n.Float64) 133 } 134 } else if n.IsFloat { 135 t.Errorf("did not expect float for %q", test.text) 136 } 137 if test.isComplex { 138 if !n.IsComplex { 139 t.Errorf("expected complex for %q", test.text) 140 } 141 if n.Complex128 != test.complex128 { 142 t.Errorf("complex128 for %q should be %g Is %g", test.text, test.complex128, n.Complex128) 143 } 144 } else if n.IsComplex { 145 t.Errorf("did not expect complex for %q", test.text) 146 } 147 } 148 } 149 150 type parseTest struct { 151 name string 152 input string 153 ok bool 154 result string // what the user would see in an error message. 155 } 156 157 const ( 158 noError = true 159 hasError = false 160 ) 161 162 var parseTests = []parseTest{ 163 {"empty", "", noError, 164 ``}, 165 {"comment", "{{/*\n\n\n*/}}", noError, 166 ``}, 167 {"spaces", " \t\n", noError, 168 `" \t\n"`}, 169 {"text", "some text", noError, 170 `"some text"`}, 171 {"emptyAction", "{{}}", hasError, 172 `{{}}`}, 173 {"field", "{{.X}}", noError, 174 `{{.X}}`}, 175 {"simple command", "{{printf}}", noError, 176 `{{printf}}`}, 177 {"$ invocation", "{{$}}", noError, 178 "{{$}}"}, 179 {"variable invocation", "{{with $x := 3}}{{$x 23}}{{end}}", noError, 180 "{{with $x := 3}}{{$x 23}}{{end}}"}, 181 {"variable with fields", "{{$.I}}", noError, 182 "{{$.I}}"}, 183 {"multi-word command", "{{printf `%d` 23}}", noError, 184 "{{printf `%d` 23}}"}, 185 {"pipeline", "{{.X|.Y}}", noError, 186 `{{.X | .Y}}`}, 187 {"pipeline with decl", "{{$x := .X|.Y}}", noError, 188 `{{$x := .X | .Y}}`}, 189 {"nested pipeline", "{{.X (.Y .Z) (.A | .B .C) (.E)}}", noError, 190 `{{.X (.Y .Z) (.A | .B .C) (.E)}}`}, 191 {"field applied to parentheses", "{{(.Y .Z).Field}}", noError, 192 `{{(.Y .Z).Field}}`}, 193 {"simple if", "{{if .X}}hello{{end}}", noError, 194 `{{if .X}}"hello"{{end}}`}, 195 {"if with else", "{{if .X}}true{{else}}false{{end}}", noError, 196 `{{if .X}}"true"{{else}}"false"{{end}}`}, 197 {"if with else if", "{{if .X}}true{{else if .Y}}false{{end}}", noError, 198 `{{if .X}}"true"{{else}}{{if .Y}}"false"{{end}}{{end}}`}, 199 {"if else chain", "+{{if .X}}X{{else if .Y}}Y{{else if .Z}}Z{{end}}+", noError, 200 `"+"{{if .X}}"X"{{else}}{{if .Y}}"Y"{{else}}{{if .Z}}"Z"{{end}}{{end}}{{end}}"+"`}, 201 {"simple range", "{{range .X}}hello{{end}}", noError, 202 `{{range .X}}"hello"{{end}}`}, 203 {"chained field range", "{{range .X.Y.Z}}hello{{end}}", noError, 204 `{{range .X.Y.Z}}"hello"{{end}}`}, 205 {"nested range", "{{range .X}}hello{{range .Y}}goodbye{{end}}{{end}}", noError, 206 `{{range .X}}"hello"{{range .Y}}"goodbye"{{end}}{{end}}`}, 207 {"range with else", "{{range .X}}true{{else}}false{{end}}", noError, 208 `{{range .X}}"true"{{else}}"false"{{end}}`}, 209 {"range over pipeline", "{{range .X|.M}}true{{else}}false{{end}}", noError, 210 `{{range .X | .M}}"true"{{else}}"false"{{end}}`}, 211 {"range []int", "{{range .SI}}{{.}}{{end}}", noError, 212 `{{range .SI}}{{.}}{{end}}`}, 213 {"range 1 var", "{{range $x := .SI}}{{.}}{{end}}", noError, 214 `{{range $x := .SI}}{{.}}{{end}}`}, 215 {"range 2 vars", "{{range $x, $y := .SI}}{{.}}{{end}}", noError, 216 `{{range $x, $y := .SI}}{{.}}{{end}}`}, 217 {"constants", "{{range .SI 1 -3.2i true false 'a' nil}}{{end}}", noError, 218 `{{range .SI 1 -3.2i true false 'a' nil}}{{end}}`}, 219 {"template", "{{template `x`}}", noError, 220 `{{template "x"}}`}, 221 {"template with arg", "{{template `x` .Y}}", noError, 222 `{{template "x" .Y}}`}, 223 {"with", "{{with .X}}hello{{end}}", noError, 224 `{{with .X}}"hello"{{end}}`}, 225 {"with with else", "{{with .X}}hello{{else}}goodbye{{end}}", noError, 226 `{{with .X}}"hello"{{else}}"goodbye"{{end}}`}, 227 // Errors. 228 {"unclosed action", "hello{{range", hasError, ""}, 229 {"unmatched end", "{{end}}", hasError, ""}, 230 {"missing end", "hello{{range .x}}", hasError, ""}, 231 {"missing end after else", "hello{{range .x}}{{else}}", hasError, ""}, 232 {"undefined function", "hello{{undefined}}", hasError, ""}, 233 {"undefined variable", "{{$x}}", hasError, ""}, 234 {"variable undefined after end", "{{with $x := 4}}{{end}}{{$x}}", hasError, ""}, 235 {"variable undefined in template", "{{template $v}}", hasError, ""}, 236 {"declare with field", "{{with $x.Y := 4}}{{end}}", hasError, ""}, 237 {"template with field ref", "{{template .X}}", hasError, ""}, 238 {"template with var", "{{template $v}}", hasError, ""}, 239 {"invalid punctuation", "{{printf 3, 4}}", hasError, ""}, 240 {"multidecl outside range", "{{with $v, $u := 3}}{{end}}", hasError, ""}, 241 {"too many decls in range", "{{range $u, $v, $w := 3}}{{end}}", hasError, ""}, 242 {"dot applied to parentheses", "{{printf (printf .).}}", hasError, ""}, 243 {"adjacent args", "{{printf 3`x`}}", hasError, ""}, 244 {"adjacent args with .", "{{printf `x`.}}", hasError, ""}, 245 {"extra end after if", "{{if .X}}a{{else if .Y}}b{{end}}{{end}}", hasError, ""}, 246 // Equals (and other chars) do not assignments make (yet). 247 {"bug0a", "{{$x := 0}}{{$x}}", noError, "{{$x := 0}}{{$x}}"}, 248 {"bug0b", "{{$x = 1}}{{$x}}", hasError, ""}, 249 {"bug0c", "{{$x ! 2}}{{$x}}", hasError, ""}, 250 {"bug0d", "{{$x % 3}}{{$x}}", hasError, ""}, 251 // Check the parse fails for := rather than comma. 252 {"bug0e", "{{range $x := $y := 3}}{{end}}", hasError, ""}, 253 // Another bug: variable read must ignore following punctuation. 254 {"bug1a", "{{$x:=.}}{{$x!2}}", hasError, ""}, // ! is just illegal here. 255 {"bug1b", "{{$x:=.}}{{$x+2}}", hasError, ""}, // $x+2 should not parse as ($x) (+2). 256 {"bug1c", "{{$x:=.}}{{$x +2}}", noError, "{{$x := .}}{{$x +2}}"}, // It's OK with a space. 257 } 258 259 var builtins = map[string]interface{}{ 260 "printf": fmt.Sprintf, 261 } 262 263 func testParse(doCopy bool, t *testing.T) { 264 textFormat = "%q" 265 defer func() { textFormat = "%s" }() 266 for _, test := range parseTests { 267 tmpl, err := New(test.name).Parse(test.input, "", "", make(map[string]*Tree), builtins) 268 switch { 269 case err == nil && !test.ok: 270 t.Errorf("%q: expected error; got none", test.name) 271 continue 272 case err != nil && test.ok: 273 t.Errorf("%q: unexpected error: %v", test.name, err) 274 continue 275 case err != nil && !test.ok: 276 // expected error, got one 277 if *debug { 278 fmt.Printf("%s: %s\n\t%s\n", test.name, test.input, err) 279 } 280 continue 281 } 282 var result string 283 if doCopy { 284 result = tmpl.Root.Copy().String() 285 } else { 286 result = tmpl.Root.String() 287 } 288 if result != test.result { 289 t.Errorf("%s=(%q): got\n\t%v\nexpected\n\t%v", test.name, test.input, result, test.result) 290 } 291 } 292 } 293 294 func TestParse(t *testing.T) { 295 testParse(false, t) 296 } 297 298 // Same as TestParse, but we copy the node first 299 func TestParseCopy(t *testing.T) { 300 testParse(true, t) 301 } 302 303 type isEmptyTest struct { 304 name string 305 input string 306 empty bool 307 } 308 309 var isEmptyTests = []isEmptyTest{ 310 {"empty", ``, true}, 311 {"nonempty", `hello`, false}, 312 {"spaces only", " \t\n \t\n", true}, 313 {"definition", `{{define "x"}}something{{end}}`, true}, 314 {"definitions and space", "{{define `x`}}something{{end}}\n\n{{define `y`}}something{{end}}\n\n", true}, 315 {"definitions and text", "{{define `x`}}something{{end}}\nx\n{{define `y`}}something{{end}}\ny\n}}", false}, 316 {"definition and action", "{{define `x`}}something{{end}}{{if 3}}foo{{end}}", false}, 317 } 318 319 func TestIsEmpty(t *testing.T) { 320 if !IsEmptyTree(nil) { 321 t.Errorf("nil tree is not empty") 322 } 323 for _, test := range isEmptyTests { 324 tree, err := New("root").Parse(test.input, "", "", make(map[string]*Tree), nil) 325 if err != nil { 326 t.Errorf("%q: unexpected error: %v", test.name, err) 327 continue 328 } 329 if empty := IsEmptyTree(tree.Root); empty != test.empty { 330 t.Errorf("%q: expected %t got %t", test.name, test.empty, empty) 331 } 332 } 333 } 334 335 // All failures, and the result is a string that must appear in the error message. 336 var errorTests = []parseTest{ 337 // Check line numbers are accurate. 338 {"unclosed1", 339 "line1\n{{", 340 hasError, `unclosed1:2: unexpected unclosed action in command`}, 341 {"unclosed2", 342 "line1\n{{define `x`}}line2\n{{", 343 hasError, `unclosed2:3: unexpected unclosed action in command`}, 344 // Specific errors. 345 {"function", 346 "{{foo}}", 347 hasError, `function "foo" not defined`}, 348 {"comment", 349 "{{/*}}", 350 hasError, `unclosed comment`}, 351 {"lparen", 352 "{{.X (1 2 3}}", 353 hasError, `unclosed left paren`}, 354 {"rparen", 355 "{{.X 1 2 3)}}", 356 hasError, `unexpected ")"`}, 357 {"space", 358 "{{`x`3}}", 359 hasError, `missing space?`}, 360 {"idchar", 361 "{{a#}}", 362 hasError, `'#'`}, 363 {"charconst", 364 "{{'a}}", 365 hasError, `unterminated character constant`}, 366 {"stringconst", 367 `{{"a}}`, 368 hasError, `unterminated quoted string`}, 369 {"rawstringconst", 370 "{{`a}}", 371 hasError, `unterminated raw quoted string`}, 372 {"number", 373 "{{0xi}}", 374 hasError, `number syntax`}, 375 {"multidefine", 376 "{{define `a`}}a{{end}}{{define `a`}}b{{end}}", 377 hasError, `multiple definition of template`}, 378 {"eof", 379 "{{range .X}}", 380 hasError, `unexpected EOF`}, 381 {"variable", 382 // Declare $x so it's defined, to avoid that error, and then check we don't parse a declaration. 383 "{{$x := 23}}{{with $x.y := 3}}{{$x 23}}{{end}}", 384 hasError, `unexpected ":="`}, 385 {"multidecl", 386 "{{$a,$b,$c := 23}}", 387 hasError, `too many declarations`}, 388 {"undefvar", 389 "{{$a}}", 390 hasError, `undefined variable`}, 391 } 392 393 func TestErrors(t *testing.T) { 394 for _, test := range errorTests { 395 _, err := New(test.name).Parse(test.input, "", "", make(map[string]*Tree)) 396 if err == nil { 397 t.Errorf("%q: expected error", test.name) 398 continue 399 } 400 if !strings.Contains(err.Error(), test.result) { 401 t.Errorf("%q: error %q does not contain %q", test.name, err, test.result) 402 } 403 } 404 }