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.
13
var debug = flag.Bool("debug", false, "show the errors produced by the tests")
15
type numberTest struct {
27
var numberTests = []numberTest{
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},
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},
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.
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
68
t.Errorf("unexpected error for %q", test.text)
71
if !ok && err == nil {
72
t.Errorf("expected error for %q", test.text)
78
if n.isComplex != test.isComplex {
79
t.Errorf("complex incorrect for %q; should be %t", test.text, test.isComplex)
83
t.Errorf("expected integer for %q", test.text)
85
if n.int64 != test.int64 {
86
t.Errorf("int64 for %q should be %d is %d", test.text, test.int64, n.int64)
89
t.Errorf("did not expect integer for %q", test.text)
93
t.Errorf("expected unsigned integer for %q", test.text)
95
if n.uint64 != test.uint64 {
96
t.Errorf("uint64 for %q should be %d is %d", test.text, test.uint64, n.uint64)
99
t.Errorf("did not expect unsigned integer for %q", test.text)
103
t.Errorf("expected float for %q", test.text)
105
if n.float64 != test.float64 {
106
t.Errorf("float64 for %q should be %g is %g", test.text, test.float64, n.float64)
108
} else if n.isFloat {
109
t.Errorf("did not expect float for %q", test.text)
113
t.Errorf("expected complex for %q", test.text)
115
if n.complex128 != test.complex128 {
116
t.Errorf("complex128 for %q should be %g is %g", test.text, test.complex128, n.complex128)
118
} else if n.isComplex {
119
t.Errorf("did not expect complex for %q", test.text)
124
type parseTest struct {
136
var parseTests = []parseTest{
137
{"empty", "", noError,
139
{"spaces", " \t\n", noError,
140
`[(text: " \t\n")]`},
141
{"text", "some text", noError,
142
`[(text: "some text")]`},
143
{"emptyAction", "{{}}", hasError,
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")])]`},
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, ""},
184
func TestParse(t *testing.T) {
185
for _, test := range parseTests {
186
tmpl := New(test.name)
187
err := tmpl.Parse(test.input)
189
case err == nil && !test.ok:
190
t.Errorf("%q: expected error; got none", test.name)
192
case err != nil && test.ok:
193
t.Errorf("%q: unexpected error: %v", test.name, err)
195
case err != nil && !test.ok:
196
// expected error, got one
198
fmt.Printf("%s: %s\n\t%s\n", test.name, test.input, err)
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)