~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/exp/template/parse_test.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        "flag"
 
9
        "fmt"
 
10
        "testing"
 
11
)
 
12
 
 
13
var debug = flag.Bool("debug", false, "show the errors produced by the tests")
 
14
 
 
15
type numberTest struct {
 
16
        text      string
 
17
        isInt     bool
 
18
        isUint    bool
 
19
        isFloat   bool
 
20
        isComplex bool
 
21
        int64
 
22
        uint64
 
23
        float64
 
24
        complex128
 
25
}
 
26
 
 
27
var numberTests = []numberTest{
 
28
        // basics
 
29
        {"0", true, true, true, false, 0, 0, 0, 0},
 
30
        {"-0", true, true, true, false, 0, 0, 0, 0}, // check that -0 is a uint.
 
31
        {"73", true, true, true, false, 73, 73, 73, 0},
 
32
        {"-73", true, false, true, false, -73, 0, -73, 0},
 
33
        {"+73", true, false, true, false, 73, 0, 73, 0},
 
34
        {"100", true, true, true, false, 100, 100, 100, 0},
 
35
        {"1e9", true, true, true, false, 1e9, 1e9, 1e9, 0},
 
36
        {"-1e9", true, false, true, false, -1e9, 0, -1e9, 0},
 
37
        {"-1.2", false, false, true, false, 0, 0, -1.2, 0},
 
38
        {"1e19", false, true, true, false, 0, 1e19, 1e19, 0},
 
39
        {"-1e19", false, false, true, false, 0, 0, -1e19, 0},
 
40
        {"4i", false, false, false, true, 0, 0, 0, 4i},
 
41
        {"-1.2+4.2i", false, false, false, true, 0, 0, 0, -1.2 + 4.2i},
 
42
        // complex with 0 imaginary are float (and maybe integer)
 
43
        {"0i", true, true, true, true, 0, 0, 0, 0},
 
44
        {"-1.2+0i", false, false, true, true, 0, 0, -1.2, -1.2},
 
45
        {"-12+0i", true, false, true, true, -12, 0, -12, -12},
 
46
        {"13+0i", true, true, true, true, 13, 13, 13, 13},
 
47
        // funny bases
 
48
        {"0123", true, true, true, false, 0123, 0123, 0123, 0},
 
49
        {"-0x0", true, true, true, false, 0, 0, 0, 0},
 
50
        {"0xdeadbeef", true, true, true, false, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 0},
 
51
        // some broken syntax
 
52
        {text: "+-2"},
 
53
        {text: "0x123."},
 
54
        {text: "1e."},
 
55
        {text: "0xi."},
 
56
        {text: "1+2."},
 
57
}
 
58
 
 
59
func TestNumberParse(t *testing.T) {
 
60
        for _, test := range numberTests {
 
61
                // If fmt.Sscan thinks it's complex, it's complex.  We can't trust the output
 
62
                // because imaginary comes out as a number.
 
63
                var c complex128
 
64
                _, err := fmt.Sscan(test.text, &c)
 
65
                n, err := newNumber(test.text, err == nil)
 
66
                ok := test.isInt || test.isUint || test.isFloat || test.isComplex
 
67
                if ok && err != nil {
 
68
                        t.Errorf("unexpected error for %q", test.text)
 
69
                        continue
 
70
                }
 
71
                if !ok && err == nil {
 
72
                        t.Errorf("expected error for %q", test.text)
 
73
                        continue
 
74
                }
 
75
                if !ok {
 
76
                        continue
 
77
                }
 
78
                if n.isComplex != test.isComplex {
 
79
                        t.Errorf("complex incorrect for %q; should be %t", test.text, test.isComplex)
 
80
                }
 
81
                if test.isInt {
 
82
                        if !n.isInt {
 
83
                                t.Errorf("expected integer for %q", test.text)
 
84
                        }
 
85
                        if n.int64 != test.int64 {
 
86
                                t.Errorf("int64 for %q should be %d is %d", test.text, test.int64, n.int64)
 
87
                        }
 
88
                } else if n.isInt {
 
89
                        t.Errorf("did not expect integer for %q", test.text)
 
90
                }
 
91
                if test.isUint {
 
92
                        if !n.isUint {
 
93
                                t.Errorf("expected unsigned integer for %q", test.text)
 
94
                        }
 
95
                        if n.uint64 != test.uint64 {
 
96
                                t.Errorf("uint64 for %q should be %d is %d", test.text, test.uint64, n.uint64)
 
97
                        }
 
98
                } else if n.isUint {
 
99
                        t.Errorf("did not expect unsigned integer for %q", test.text)
 
100
                }
 
101
                if test.isFloat {
 
102
                        if !n.isFloat {
 
103
                                t.Errorf("expected float for %q", test.text)
 
104
                        }
 
105
                        if n.float64 != test.float64 {
 
106
                                t.Errorf("float64 for %q should be %g is %g", test.text, test.float64, n.float64)
 
107
                        }
 
108
                } else if n.isFloat {
 
109
                        t.Errorf("did not expect float for %q", test.text)
 
110
                }
 
111
                if test.isComplex {
 
112
                        if !n.isComplex {
 
113
                                t.Errorf("expected complex for %q", test.text)
 
114
                        }
 
115
                        if n.complex128 != test.complex128 {
 
116
                                t.Errorf("complex128 for %q should be %g is %g", test.text, test.complex128, n.complex128)
 
117
                        }
 
118
                } else if n.isComplex {
 
119
                        t.Errorf("did not expect complex for %q", test.text)
 
120
                }
 
121
        }
 
122
}
 
123
 
 
124
type parseTest struct {
 
125
        name   string
 
126
        input  string
 
127
        ok     bool
 
128
        result string
 
129
}
 
130
 
 
131
const (
 
132
        noError  = true
 
133
        hasError = false
 
134
)
 
135
 
 
136
var parseTests = []parseTest{
 
137
        {"empty", "", noError,
 
138
                `[]`},
 
139
        {"spaces", " \t\n", noError,
 
140
                `[(text: " \t\n")]`},
 
141
        {"text", "some text", noError,
 
142
                `[(text: "some text")]`},
 
143
        {"emptyAction", "{{}}", hasError,
 
144
                `[(action: [])]`},
 
145
        {"field", "{{.X}}", noError,
 
146
                `[(action: [(command: [F=[X]])])]`},
 
147
        {"simple command", "{{printf}}", noError,
 
148
                `[(action: [(command: [I=printf])])]`},
 
149
        {"multi-word command", "{{printf `%d` 23}}", noError,
 
150
                "[(action: [(command: [I=printf S=`%d` N=23])])]"},
 
151
        {"pipeline", "{{.X|.Y}}", noError,
 
152
                `[(action: [(command: [F=[X]]) (command: [F=[Y]])])]`},
 
153
        {"simple if", "{{if .X}}hello{{end}}", noError,
 
154
                `[({{if [(command: [F=[X]])]}} [(text: "hello")])]`},
 
155
        {"if with else", "{{if .X}}true{{else}}false{{end}}", noError,
 
156
                `[({{if [(command: [F=[X]])]}} [(text: "true")] {{else}} [(text: "false")])]`},
 
157
        {"simple range", "{{range .X}}hello{{end}}", noError,
 
158
                `[({{range [(command: [F=[X]])]}} [(text: "hello")])]`},
 
159
        {"chained field range", "{{range .X.Y.Z}}hello{{end}}", noError,
 
160
                `[({{range [(command: [F=[X Y Z]])]}} [(text: "hello")])]`},
 
161
        {"nested range", "{{range .X}}hello{{range .Y}}goodbye{{end}}{{end}}", noError,
 
162
                `[({{range [(command: [F=[X]])]}} [(text: "hello")({{range [(command: [F=[Y]])]}} [(text: "goodbye")])])]`},
 
163
        {"range with else", "{{range .X}}true{{else}}false{{end}}", noError,
 
164
                `[({{range [(command: [F=[X]])]}} [(text: "true")] {{else}} [(text: "false")])]`},
 
165
        {"range over pipeline", "{{range .X|.M}}true{{else}}false{{end}}", noError,
 
166
                `[({{range [(command: [F=[X]]) (command: [F=[M]])]}} [(text: "true")] {{else}} [(text: "false")])]`},
 
167
        {"range []int", "{{range .SI}}{{.}}{{end}}", noError,
 
168
                `[({{range [(command: [F=[SI]])]}} [(action: [(command: [{{<.>}}])])])]`},
 
169
        {"constants", "{{range .SI 1 -3.2i true false }}{{end}}", noError,
 
170
                `[({{range [(command: [F=[SI] N=1 N=-3.2i B=true B=false])]}} [])]`},
 
171
        {"template", "{{template `x` .Y}}", noError,
 
172
                "[{{template S=`x` [(command: [F=[Y]])]}}]"},
 
173
        {"with", "{{with .X}}hello{{end}}", noError,
 
174
                `[({{with [(command: [F=[X]])]}} [(text: "hello")])]`},
 
175
        {"with with else", "{{with .X}}hello{{else}}goodbye{{end}}", noError,
 
176
                `[({{with [(command: [F=[X]])]}} [(text: "hello")] {{else}} [(text: "goodbye")])]`},
 
177
        // Errors.
 
178
        {"unclosed action", "hello{{range", hasError, ""},
 
179
        {"missing end", "hello{{range .x}}", hasError, ""},
 
180
        {"missing end after else", "hello{{range .x}}{{else}}", hasError, ""},
 
181
        {"undefined function", "hello{{undefined}}", hasError, ""},
 
182
}
 
183
 
 
184
func TestParse(t *testing.T) {
 
185
        for _, test := range parseTests {
 
186
                tmpl := New(test.name)
 
187
                err := tmpl.Parse(test.input)
 
188
                switch {
 
189
                case err == nil && !test.ok:
 
190
                        t.Errorf("%q: expected error; got none", test.name)
 
191
                        continue
 
192
                case err != nil && test.ok:
 
193
                        t.Errorf("%q: unexpected error: %v", test.name, err)
 
194
                        continue
 
195
                case err != nil && !test.ok:
 
196
                        // expected error, got one
 
197
                        if *debug {
 
198
                                fmt.Printf("%s: %s\n\t%s\n", test.name, test.input, err)
 
199
                        }
 
200
                        continue
 
201
                }
 
202
                result := tmpl.root.String()
 
203
                if result != test.result {
 
204
                        t.Errorf("%s=(%q): got\n\t%v\nexpected\n\t%v", test.name, test.input, result, test.result)
 
205
                }
 
206
        }
 
207
}