-
Notifications
You must be signed in to change notification settings - Fork 7
/
checkstarexpr_gen.go
92 lines (76 loc) · 1.62 KB
/
checkstarexpr_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
package main
import (
"fmt"
"io"
"text/template"
"github.com/0xfaded/go-testgen"
)
type Test struct{}
var comment = template.Must(template.New("Comment").Parse(
`// Test {{ .Star.Value }}
`))
var defs =
`
a := 1
b := &a
_ = b
`
var body = template.Must(template.New("Body").Parse(defs +
` env := MakeSimpleEnv()
env.Vars["a"] = reflect.ValueOf(&a)
env.Vars["b"] = reflect.ValueOf(&b)
{{ if .Errors }}
expectCheckError(t, `+"`{{ .Expr }}`"+`, env,{{ range .Errors }}
`+"`{{ . }}`"+`,{{ end }}
)
{{ else }}
expectType(t, `+"`{{ .Expr }}`"+`, env, reflect.TypeOf({{ .Expr }})){{ end }}
`))
func (*Test) Package() string {
return "eval"
}
func (*Test) Prefix() string {
return "CheckStarExpr"
}
func (*Test) Imports() map[string]string {
return map[string]string { "reflect": "" }
}
func (*Test) Dimensions() []testgen.Dimension {
stars := []testgen.Element{
{"A", "a"},
{"B", "b"},
{"AtA", "&a"},
{"AtB", "&b"},
{"Int", "int(1)"},
{"Number", "1.4"},
{"Rune", "'a'"},
{"Bool", "true"},
{"String", `"a"`},
{"Nil", "nil"},
{"StarB", "*b"},
}
return []testgen.Dimension{
stars,
}
}
func (*Test) Globals(w io.Writer) error {
return nil
}
func (*Test) Comment(w io.Writer, elts ...testgen.Element) error {
vars := map[string] interface{} {
"Star": elts[0],
}
return comment.Execute(w, vars)
}
func (*Test) Body(w io.Writer, elts ...testgen.Element) error {
expr := fmt.Sprintf("*%v", elts[0].Value)
compileErrs, err := compileExprWithDefs(expr, defs)
if err != nil {
return err
}
vars := map[string] interface{} {
"Expr": expr,
"Errors": compileErrs,
}
return body.Execute(w, &vars)
}