Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(10)

Delta Between Two Patch Sets: src/pkg/exp/template/exec_test.go

Issue 4665041: code review 4665041: Add the beginnings of the template execution code. Lot... (Closed)
Left Patch Set: diff -r b295b8bda20b https://go.googlecode.com/hg/ Created 13 years, 9 months ago
Right Patch Set: diff -r a5c1163200db https://go.googlecode.com/hg/ Created 13 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/exp/template/exec.go ('k') | src/pkg/exp/template/lex.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2011 The Go Authors. All rights reserved. 1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package template 5 package template
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "fmt" 9 "fmt"
10 "os"
11 "sort"
12 "strings"
10 "testing" 13 "testing"
11 ) 14 )
12 15
16 // T has lots of interesting pieces to use to test execution.
13 type T struct { 17 type T struct {
18 // Basics
14 I int 19 I int
15 U16 uint16 20 U16 uint16
16 X string 21 X string
17 » U *U 22 » // Nested structs.
23 » U *U
24 » // Slices
25 » SI []int
26 » SEmpty []int
27 » // Maps
28 » MSI map[string]int
29 » MSIEmpty map[string]int
18 } 30 }
19 31
32 // Simple methods with and without arguments.
20 func (t *T) Method0() string { 33 func (t *T) Method0() string {
21 return "resultOfMethod0" 34 return "resultOfMethod0"
22 } 35 }
23 36
24 func (t *T) Method1(a int) int { 37 func (t *T) Method1(a int) int {
25 return a 38 return a
26 } 39 }
27 40
28 func (t *T) Method2(a uint16, b string) string { 41 func (t *T) Method2(a uint16, b string) string {
29 return fmt.Sprintf("Method2: %d %s", a, b) 42 return fmt.Sprintf("Method2: %d %s", a, b)
30 } 43 }
31 44
45 func (t *T) MAdd(a int, b []int) []int {
46 v := make([]int, len(b))
47 for i, x := range b {
48 v[i] = x + a
49 }
50 return v
51 }
52
53 // MSort is used to sort map keys for stable output. (Nice trick!)
54 func (t *T) MSort(m map[string]int) []string {
55 keys := make([]string, len(m))
56 i := 0
57 for k := range m {
58 keys[i] = k
59 i++
60 }
61 sort.SortStrings(keys)
62 return keys
63 }
64
65 // EPERM returns a value and an os.Error according to its argument.
66 func (t *T) EPERM(a int) (int, os.Error) {
67 if a == 0 {
68 return 0, os.EPERM
69 }
70 return a, nil
71 }
72
32 type U struct { 73 type U struct {
33 V string 74 V string
34 } 75 }
35 76
36 var tVal = &T{ 77 var tVal = &T{
37 I: 17, 78 I: 17,
38 U16: 16, 79 U16: 16,
39 X: "x", 80 X: "x",
40 U: &U{"v"}, 81 U: &U{"v"},
82 SI: []int{3, 4, 5},
83 MSI: map[string]int{"one": 1, "two": 2, "three": 3},
41 } 84 }
42 85
43 type execTest struct { 86 type execTest struct {
44 name string 87 name string
45 input string 88 input string
46 output string 89 output string
47 data interface{} 90 data interface{}
48 ok bool 91 ok bool
49 } 92 }
50 93
51 var execTests = []execTest{ 94 var execTests = []execTest{
52 {"empty", "", "", nil, true}, 95 {"empty", "", "", nil, true},
53 {"text", "some text", "some text", nil, true}, 96 {"text", "some text", "some text", nil, true},
54 {".X", "-{{.X}}-", "-x-", tVal, true}, 97 {".X", "-{{.X}}-", "-x-", tVal, true},
55 {".U.V", "-{{.U.V}}-", "-v-", tVal, true}, 98 {".U.V", "-{{.U.V}}-", "-v-", tVal, true},
56 {".Method0", "-{{.Method0}}-", "-resultOfMethod0-", tVal, true}, 99 {".Method0", "-{{.Method0}}-", "-resultOfMethod0-", tVal, true},
57 {".Method1(1234)", "-{{.Method1 1234}}-", "-1234-", tVal, true}, 100 {".Method1(1234)", "-{{.Method1 1234}}-", "-1234-", tVal, true},
58 {".Method1(.I)", "-{{.Method1 .I}}-", "-17-", tVal, true}, 101 {".Method1(.I)", "-{{.Method1 .I}}-", "-17-", tVal, true},
59 {".Method2(3, .X)", "-{{.Method2 3 .X}}-", "-Method2: 3 x-", tVal, true} , 102 {".Method2(3, .X)", "-{{.Method2 3 .X}}-", "-Method2: 3 x-", tVal, true} ,
60 {".Method2(.U16, `str`)", "-{{.Method2 .U16 `str`}}-", "-Method2: 16 str -", tVal, true}, 103 {".Method2(.U16, `str`)", "-{{.Method2 .U16 `str`}}-", "-Method2: 16 str -", tVal, true},
61 {"pipeline", "-{{.Method0 | .Method2 .U16}}-", "-Method2: 16 resultOfMet hod0-", tVal, true}, 104 {"pipeline", "-{{.Method0 | .Method2 .U16}}-", "-Method2: 16 resultOfMet hod0-", tVal, true},
105 {"range []int", "{{range .SI}}-{{.}}-{{end}}", "-3--4--5-", tVal, true},
106 {"range empty no else", "{{range .SEmpty}}-{{.}}-{{end}}", "", tVal, tru e},
107 {"range []int else", "{{range .SI}}-{{.}}-{{else}}EMPTY{{end}}", "-3--4- -5-", tVal, true},
108 {"range empty else", "{{range .SEmpty}}-{{.}}-{{else}}EMPTY{{end}}", "EM PTY", tVal, true},
109 {"range []int method", "{{range .SI | .MAdd .I}}-{{.}}-{{end}}", "-20--2 1--22-", tVal, true},
110 {"range map", "{{range .MSI | .MSort}}-{{.}}-{{end}}", "-one--three--two -", tVal, true},
111 {"range empty map no else", "{{range .MSIEmpty}}-{{.}}-{{end}}", "", tVa l, true},
112 {"range map else", "{{range .MSI | .MSort}}-{{.}}-{{else}}EMPTY{{end}}", "-one--three--two-", tVal, true},
113 {"range empty map else", "{{range .MSIEmpty}}-{{.}}-{{else}}EMPTY{{end}} ", "EMPTY", tVal, true},
114 {"error method, no error", "{{.EPERM 1}}", "1", tVal, true},
115 {"error method, error", "{{.EPERM 0}}", "1", tVal, false},
62 } 116 }
63 117
64 func TestExecute(t *testing.T) { 118 func TestExecute(t *testing.T) {
65 b := new(bytes.Buffer) 119 b := new(bytes.Buffer)
66 for _, test := range execTests { 120 for _, test := range execTests {
67 tmpl := New(test.name) 121 tmpl := New(test.name)
68 err := tmpl.Parse(test.input) 122 err := tmpl.Parse(test.input)
69 if err != nil { 123 if err != nil {
70 t.Errorf("%s: parse error: %s", test.name, err) 124 t.Errorf("%s: parse error: %s", test.name, err)
71 continue 125 continue
72 } 126 }
73 b.Reset() 127 b.Reset()
74 err = tmpl.Execute(b, test.data) 128 err = tmpl.Execute(b, test.data)
75 switch { 129 switch {
76 case !test.ok && err == nil: 130 case !test.ok && err == nil:
77 t.Errorf("%s: expected error; got none", test.name) 131 t.Errorf("%s: expected error; got none", test.name)
78 continue 132 continue
79 case test.ok && err != nil: 133 case test.ok && err != nil:
80 t.Errorf("%s: unexpected execute error: %s", test.name, err) 134 t.Errorf("%s: unexpected execute error: %s", test.name, err)
81 continue 135 continue
82 case !test.ok && err != nil: 136 case !test.ok && err != nil:
83 continue 137 continue
84 } 138 }
85 result := b.String() 139 result := b.String()
86 if result != test.output { 140 if result != test.output {
87 t.Errorf("%s: expected\n\t%q\ngot\n\t%q", test.name, tes t.output, result) 141 t.Errorf("%s: expected\n\t%q\ngot\n\t%q", test.name, tes t.output, result)
88 } 142 }
89 } 143 }
90 } 144 }
145
146 // Check that an error from a method flows back to the top.
147 func TestExecuteError(t *testing.T) {
148 b := new(bytes.Buffer)
149 tmpl := New("error")
150 err := tmpl.Parse("{{.EPERM 0}}")
151 if err != nil {
152 t.Fatalf("parse error: %s", err)
153 }
154 err = tmpl.Execute(b, tVal)
155 if err == nil {
156 t.Errorf("expected error; got none")
157 } else if !strings.Contains(err.String(), os.EPERM.String()) {
158 t.Errorf("expected os.EPERM; got %s %s", err)
159 }
160 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b