OLD | NEW |
(Empty) | |
| 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 html |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 "exp/template" |
| 10 "testing" |
| 11 ) |
| 12 |
| 13 type data struct { |
| 14 World string |
| 15 Coming bool |
| 16 } |
| 17 |
| 18 func TestReverse(t *testing.T) { |
| 19 templateSource := |
| 20 "{{if .Coming}}Hello{{else}}Goodbye{{end}}, {{.World}}!" |
| 21 templateData := data{ |
| 22 World: "Cincinatti", |
| 23 Coming: true, |
| 24 } |
| 25 |
| 26 tmpl := template.New("test") |
| 27 tmpl, err := tmpl.Parse(templateSource) |
| 28 if err != nil { |
| 29 t.Errorf("failed to parse template: %s", err) |
| 30 return |
| 31 } |
| 32 |
| 33 Reverse(tmpl) |
| 34 |
| 35 buffer := new(bytes.Buffer) |
| 36 |
| 37 err = tmpl.Execute(buffer, templateData) |
| 38 if err != nil { |
| 39 t.Errorf("failed to execute reversed template: %s", err) |
| 40 return |
| 41 } |
| 42 |
| 43 golden := "!ittanicniC ,olleH" |
| 44 actual := buffer.String() |
| 45 if golden != actual { |
| 46 t.Errorf("reversed output: %q != %q", golden, actual) |
| 47 } |
| 48 } |
OLD | NEW |