-
Notifications
You must be signed in to change notification settings - Fork 7
/
checkunaryexpr_typed_gen.go
93 lines (76 loc) · 1.86 KB
/
checkunaryexpr_typed_gen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"io"
"text/template"
"go/token"
"github.com/0xfaded/go-testgen"
)
type Test struct{}
var comment = template.Must(template.New("Comment").Parse(
`// Test {{ .Op.Value }} {{ .Rhs.Name }}
`))
var body = template.Must(template.New("Body").Parse(
` env := MakeSimpleEnv()
{{ if .Errors }}
expectCheckError(t, `+"`{{ .Expr }}`"+`, env,{{ range .Errors }}
`+"`{{ . }}`"+`,{{ end }}
)
{{ else }}
expectConst(t, `+"`{{ .Expr }}`"+`, env, {{ .Expr }}, reflect.TypeOf({{ .Expr }})){{ end }}
`))
func (*Test) Package() string {
return "eval"
}
func (*Test) Prefix() string {
return "CheckUnaryTypedExpr"
}
func (*Test) Imports() map[string]string {
return map[string]string {"reflect": ""}
}
func (*Test) Dimensions() []testgen.Dimension {
// All numeric values have been chosen to be a power of two, for good reason :)
types := []testgen.Element{
{"Int32", "int32(4)"},
//TODO[crc] go1.1 doesnt pick this up. Add after upgrade to 1.2
// {"int32Overflow", "int32(-0x80000000)"},
{"Float64", "float64(2)"},
{"Complex128", "complex128(8i)"},
{"Bool", "bool(true)"},
{"String", `string("abc")`},
}
ops := []testgen.Element{
{"Add", token.ADD},
{"Sub", token.SUB},
{"Xor", token.XOR},
{"Not", token.NOT},
}
return []testgen.Dimension{
ops,
types,
}
}
func (*Test) Globals(w io.Writer) error {
return nil
}
func (*Test) Comment(w io.Writer, elts ...testgen.Element) error {
vars := map[string] interface{} {
"Op": elts[0],
"Rhs": elts[1],
}
return comment.Execute(w, vars)
}
func (*Test) Body(w io.Writer, elts ...testgen.Element) error {
op := elts[0].Value.(token.Token)
expr := fmt.Sprintf("%v %v", op, elts[1].Value)
compileErrs, err := compileExpr(expr)
if err != nil {
return err
}
vars := map[string] interface{} {
"Expr": expr,
"Errors": compileErrs,
"Op": elts[1],
}
return body.Execute(w, &vars)
}