OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [text/] [template/] [parse/] [parse_test.go] - Blame information for rev 854

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
        "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
        {"073", true, true, true, false, 073, 073, 073, 0},
33
        {"0x73", true, true, true, false, 0x73, 0x73, 0x73, 0},
34
        {"-73", true, false, true, false, -73, 0, -73, 0},
35
        {"+73", true, false, true, false, 73, 0, 73, 0},
36
        {"100", true, true, true, false, 100, 100, 100, 0},
37
        {"1e9", true, true, true, false, 1e9, 1e9, 1e9, 0},
38
        {"-1e9", true, false, true, false, -1e9, 0, -1e9, 0},
39
        {"-1.2", false, false, true, false, 0, 0, -1.2, 0},
40
        {"1e19", false, true, true, false, 0, 1e19, 1e19, 0},
41
        {"-1e19", false, false, true, false, 0, 0, -1e19, 0},
42
        {"4i", false, false, false, true, 0, 0, 0, 4i},
43
        {"-1.2+4.2i", false, false, false, true, 0, 0, 0, -1.2 + 4.2i},
44
        {"073i", false, false, false, true, 0, 0, 0, 73i}, // not octal!
45
        // complex with 0 imaginary are float (and maybe integer)
46
        {"0i", true, true, true, true, 0, 0, 0, 0},
47
        {"-1.2+0i", false, false, true, true, 0, 0, -1.2, -1.2},
48
        {"-12+0i", true, false, true, true, -12, 0, -12, -12},
49
        {"13+0i", true, true, true, true, 13, 13, 13, 13},
50
        // funny bases
51
        {"0123", true, true, true, false, 0123, 0123, 0123, 0},
52
        {"-0x0", true, true, true, false, 0, 0, 0, 0},
53
        {"0xdeadbeef", true, true, true, false, 0xdeadbeef, 0xdeadbeef, 0xdeadbeef, 0},
54
        // character constants
55
        {`'a'`, true, true, true, false, 'a', 'a', 'a', 0},
56
        {`'\n'`, true, true, true, false, '\n', '\n', '\n', 0},
57
        {`'\\'`, true, true, true, false, '\\', '\\', '\\', 0},
58
        {`'\''`, true, true, true, false, '\'', '\'', '\'', 0},
59
        {`'\xFF'`, true, true, true, false, 0xFF, 0xFF, 0xFF, 0},
60
        {`'パ'`, true, true, true, false, 0x30d1, 0x30d1, 0x30d1, 0},
61
        {`'\u30d1'`, true, true, true, false, 0x30d1, 0x30d1, 0x30d1, 0},
62
        {`'\U000030d1'`, true, true, true, false, 0x30d1, 0x30d1, 0x30d1, 0},
63
        // some broken syntax
64
        {text: "+-2"},
65
        {text: "0x123."},
66
        {text: "1e."},
67
        {text: "0xi."},
68
        {text: "1+2."},
69
        {text: "'x"},
70
        {text: "'xx'"},
71
}
72
 
73
func TestNumberParse(t *testing.T) {
74
        for _, test := range numberTests {
75
                // If fmt.Sscan thinks it's complex, it's complex.  We can't trust the output
76
                // because imaginary comes out as a number.
77
                var c complex128
78
                typ := itemNumber
79
                if test.text[0] == '\'' {
80
                        typ = itemCharConstant
81
                } else {
82
                        _, err := fmt.Sscan(test.text, &c)
83
                        if err == nil {
84
                                typ = itemComplex
85
                        }
86
                }
87
                n, err := newNumber(test.text, typ)
88
                ok := test.isInt || test.isUint || test.isFloat || test.isComplex
89
                if ok && err != nil {
90
                        t.Errorf("unexpected error for %q: %s", test.text, err)
91
                        continue
92
                }
93
                if !ok && err == nil {
94
                        t.Errorf("expected error for %q", test.text)
95
                        continue
96
                }
97
                if !ok {
98
                        if *debug {
99
                                fmt.Printf("%s\n\t%s\n", test.text, err)
100
                        }
101
                        continue
102
                }
103
                if n.IsComplex != test.isComplex {
104
                        t.Errorf("complex incorrect for %q; should be %t", test.text, test.isComplex)
105
                }
106
                if test.isInt {
107
                        if !n.IsInt {
108
                                t.Errorf("expected integer for %q", test.text)
109
                        }
110
                        if n.Int64 != test.int64 {
111
                                t.Errorf("int64 for %q should be %d Is %d", test.text, test.int64, n.Int64)
112
                        }
113
                } else if n.IsInt {
114
                        t.Errorf("did not expect integer for %q", test.text)
115
                }
116
                if test.isUint {
117
                        if !n.IsUint {
118
                                t.Errorf("expected unsigned integer for %q", test.text)
119
                        }
120
                        if n.Uint64 != test.uint64 {
121
                                t.Errorf("uint64 for %q should be %d Is %d", test.text, test.uint64, n.Uint64)
122
                        }
123
                } else if n.IsUint {
124
                        t.Errorf("did not expect unsigned integer for %q", test.text)
125
                }
126
                if test.isFloat {
127
                        if !n.IsFloat {
128
                                t.Errorf("expected float for %q", test.text)
129
                        }
130
                        if n.Float64 != test.float64 {
131
                                t.Errorf("float64 for %q should be %g Is %g", test.text, test.float64, n.Float64)
132
                        }
133
                } else if n.IsFloat {
134
                        t.Errorf("did not expect float for %q", test.text)
135
                }
136
                if test.isComplex {
137
                        if !n.IsComplex {
138
                                t.Errorf("expected complex for %q", test.text)
139
                        }
140
                        if n.Complex128 != test.complex128 {
141
                                t.Errorf("complex128 for %q should be %g Is %g", test.text, test.complex128, n.Complex128)
142
                        }
143
                } else if n.IsComplex {
144
                        t.Errorf("did not expect complex for %q", test.text)
145
                }
146
        }
147
}
148
 
149
type parseTest struct {
150
        name   string
151
        input  string
152
        ok     bool
153
        result string // what the user would see in an error message.
154
}
155
 
156
const (
157
        noError  = true
158
        hasError = false
159
)
160
 
161
var parseTests = []parseTest{
162
        {"empty", "", noError,
163
                ``},
164
        {"comment", "{{/*\n\n\n*/}}", noError,
165
                ``},
166
        {"spaces", " \t\n", noError,
167
                `" \t\n"`},
168
        {"text", "some text", noError,
169
                `"some text"`},
170
        {"emptyAction", "{{}}", hasError,
171
                `{{}}`},
172
        {"field", "{{.X}}", noError,
173
                `{{.X}}`},
174
        {"simple command", "{{printf}}", noError,
175
                `{{printf}}`},
176
        {"$ invocation", "{{$}}", noError,
177
                "{{$}}"},
178
        {"variable invocation", "{{with $x := 3}}{{$x 23}}{{end}}", noError,
179
                "{{with $x := 3}}{{$x 23}}{{end}}"},
180
        {"variable with fields", "{{$.I}}", noError,
181
                "{{$.I}}"},
182
        {"multi-word command", "{{printf `%d` 23}}", noError,
183
                "{{printf `%d` 23}}"},
184
        {"pipeline", "{{.X|.Y}}", noError,
185
                `{{.X | .Y}}`},
186
        {"pipeline with decl", "{{$x := .X|.Y}}", noError,
187
                `{{$x := .X | .Y}}`},
188
        {"simple if", "{{if .X}}hello{{end}}", noError,
189
                `{{if .X}}"hello"{{end}}`},
190
        {"if with else", "{{if .X}}true{{else}}false{{end}}", noError,
191
                `{{if .X}}"true"{{else}}"false"{{end}}`},
192
        {"simple range", "{{range .X}}hello{{end}}", noError,
193
                `{{range .X}}"hello"{{end}}`},
194
        {"chained field range", "{{range .X.Y.Z}}hello{{end}}", noError,
195
                `{{range .X.Y.Z}}"hello"{{end}}`},
196
        {"nested range", "{{range .X}}hello{{range .Y}}goodbye{{end}}{{end}}", noError,
197
                `{{range .X}}"hello"{{range .Y}}"goodbye"{{end}}{{end}}`},
198
        {"range with else", "{{range .X}}true{{else}}false{{end}}", noError,
199
                `{{range .X}}"true"{{else}}"false"{{end}}`},
200
        {"range over pipeline", "{{range .X|.M}}true{{else}}false{{end}}", noError,
201
                `{{range .X | .M}}"true"{{else}}"false"{{end}}`},
202
        {"range []int", "{{range .SI}}{{.}}{{end}}", noError,
203
                `{{range .SI}}{{.}}{{end}}`},
204
        {"constants", "{{range .SI 1 -3.2i true false 'a'}}{{end}}", noError,
205
                `{{range .SI 1 -3.2i true false 'a'}}{{end}}`},
206
        {"template", "{{template `x`}}", noError,
207
                `{{template "x"}}`},
208
        {"template with arg", "{{template `x` .Y}}", noError,
209
                `{{template "x" .Y}}`},
210
        {"with", "{{with .X}}hello{{end}}", noError,
211
                `{{with .X}}"hello"{{end}}`},
212
        {"with with else", "{{with .X}}hello{{else}}goodbye{{end}}", noError,
213
                `{{with .X}}"hello"{{else}}"goodbye"{{end}}`},
214
        // Errors.
215
        {"unclosed action", "hello{{range", hasError, ""},
216
        {"unmatched end", "{{end}}", hasError, ""},
217
        {"missing end", "hello{{range .x}}", hasError, ""},
218
        {"missing end after else", "hello{{range .x}}{{else}}", hasError, ""},
219
        {"undefined function", "hello{{undefined}}", hasError, ""},
220
        {"undefined variable", "{{$x}}", hasError, ""},
221
        {"variable undefined after end", "{{with $x := 4}}{{end}}{{$x}}", hasError, ""},
222
        {"variable undefined in template", "{{template $v}}", hasError, ""},
223
        {"declare with field", "{{with $x.Y := 4}}{{end}}", hasError, ""},
224
        {"template with field ref", "{{template .X}}", hasError, ""},
225
        {"template with var", "{{template $v}}", hasError, ""},
226
        {"invalid punctuation", "{{printf 3, 4}}", hasError, ""},
227
        {"multidecl outside range", "{{with $v, $u := 3}}{{end}}", hasError, ""},
228
        {"too many decls in range", "{{range $u, $v, $w := 3}}{{end}}", hasError, ""},
229
}
230
 
231
var builtins = map[string]interface{}{
232
        "printf": fmt.Sprintf,
233
}
234
 
235
func TestParse(t *testing.T) {
236
        for _, test := range parseTests {
237
                tmpl, err := New(test.name).Parse(test.input, "", "", make(map[string]*Tree), builtins)
238
                switch {
239
                case err == nil && !test.ok:
240
                        t.Errorf("%q: expected error; got none", test.name)
241
                        continue
242
                case err != nil && test.ok:
243
                        t.Errorf("%q: unexpected error: %v", test.name, err)
244
                        continue
245
                case err != nil && !test.ok:
246
                        // expected error, got one
247
                        if *debug {
248
                                fmt.Printf("%s: %s\n\t%s\n", test.name, test.input, err)
249
                        }
250
                        continue
251
                }
252
                result := tmpl.Root.String()
253
                if result != test.result {
254
                        t.Errorf("%s=(%q): got\n\t%v\nexpected\n\t%v", test.name, test.input, result, test.result)
255
                }
256
        }
257
}
258
 
259
type isEmptyTest struct {
260
        name  string
261
        input string
262
        empty bool
263
}
264
 
265
var isEmptyTests = []isEmptyTest{
266
        {"empty", ``, true},
267
        {"nonempty", `hello`, false},
268
        {"spaces only", " \t\n \t\n", true},
269
        {"definition", `{{define "x"}}something{{end}}`, true},
270
        {"definitions and space", "{{define `x`}}something{{end}}\n\n{{define `y`}}something{{end}}\n\n", true},
271
        {"definitions and text", "{{define `x`}}something{{end}}\nx\n{{define `y`}}something{{end}}\ny\n}}", false},
272
        {"definition and action", "{{define `x`}}something{{end}}{{if 3}}foo{{end}}", false},
273
}
274
 
275
func TestIsEmpty(t *testing.T) {
276
        for _, test := range isEmptyTests {
277
                tree, err := New("root").Parse(test.input, "", "", make(map[string]*Tree), nil)
278
                if err != nil {
279
                        t.Errorf("%q: unexpected error: %v", test.name, err)
280
                        continue
281
                }
282
                if empty := IsEmptyTree(tree.Root); empty != test.empty {
283
                        t.Errorf("%q: expected %t got %t", test.name, test.empty, empty)
284
                }
285
        }
286
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.