-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctxgen.go
191 lines (167 loc) · 3.68 KB
/
ctxgen.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"html/template"
"log"
"os"
"strings"
)
type typeDef struct {
Name string
Value string
Zero string
}
func noescape(str string) template.HTML {
return template.HTML(str)
}
func main() {
funcMap := template.FuncMap{
"Title": strings.Title,
"noescape": noescape,
}
types := []typeDef{
{
Name: "string",
Value: `"example text"`,
Zero: `""`,
},
{
Name: "int",
Value: "123",
Zero: "0",
},
{
Name: "int8",
Value: "123",
Zero: "0",
},
{
Name: "int16",
Value: "123",
Zero: "0",
},
{
Name: "int32",
Value: "123",
Zero: "0",
},
{
Name: "int64",
Value: "123",
Zero: "0",
},
{
Name: "float32",
Value: "123.5",
Zero: "0",
},
{
Name: "float64",
Value: "123.5",
Zero: "0",
},
{
Name: "bool",
Value: "false",
Zero: "false",
},
}
f, err := os.Create("context_get.go")
die(err)
var tmpl = template.Must(template.New("").Funcs(funcMap).Parse(getTmpl))
err = tmpl.Execute(f, struct {
Types []typeDef
}{Types: types})
die(err)
f.Close()
f, err = os.Create("context_get_test.go")
die(err)
tmpl = template.Must(template.New("").Funcs(funcMap).Parse(testTmpl))
err = tmpl.Execute(f, struct {
Types []typeDef
}{Types: types})
die(err)
f.Close()
}
func die(err error) {
if err != nil {
log.Fatal(err)
}
}
const testTmpl = `// Code generated .* DO NOT EDIT.
package gobdd
import "testing"
import "errors"
func TestContext_GetError(t *testing.T) {
ctx := NewContext()
expected := errors.New("new err")
ctx.Set("test", expected)
received, err := ctx.GetError("test")
if err != nil {
t.Error(err)
}
if received != expected {
t.Errorf("expected %+v but received %+v", expected, received)
}
}
{{ range .Types }}
func TestContext_Get{{ .Name | Title }}(t *testing.T) {
ctx := NewContext()
expected := {{ .Name }}({{ .Value | noescape }})
ctx.Set("test", expected)
received, err := ctx.Get{{ .Name | Title }}("test")
if err != nil {
t.Error(err)
}
if received != expected {
t.Errorf("expected %+v but received %+v", expected, received)
}
}
func TestContext_Get{{ .Name | Title }}_WithDefaultValue(t *testing.T) {
ctx := NewContext()
defaultValue := {{ .Name }}({{ .Value | noescape }})
received, err := ctx.Get{{ .Name | Title }}("test", defaultValue)
if err != nil {
t.Error(err)
}
if received != defaultValue {
t.Errorf("expected %+v but received %+v", defaultValue, received)
}
}
func TestContext_Get{{ .Name | Title }}_ShouldReturnErrorWhenMoreThanOneDefaultValue(t *testing.T) {
ctx := NewContext()
_, err := ctx.Get{{ .Name | Title }}("test", {{ .Value | noescape }}, {{ .Value | noescape }})
if err == nil {
t.Error("the Get{{ .Name | Title }} should return an error")
}
}
func TestContext_Get{{ .Name | Title }}_ErrorOnNotFound(t *testing.T) {
ctx := NewContext()
_, err := ctx.Get{{ .Name | Title }}("test")
if err == nil {
t.Error("the Get{{ .Name | Title }} should return an error")
}
}
{{ end }}
`
const getTmpl = `// Code generated .* DO NOT EDIT.
package gobdd
import "fmt"
{{ range .Types }}
func (ctx Context) Get{{ .Name | Title }}(key interface{}, defaultValue ...{{ .Name }}) ({{ .Name }}, error) {
if len(defaultValue) > 1 {
return {{.Zero|noescape}}, fmt.Errorf("allowed to pass only 1 default value but %d got", len(defaultValue))
}
if _, ok := ctx.values[key]; !ok {
if len(defaultValue) == 1 {
return defaultValue[0], nil
}
return {{.Zero|noescape}}, fmt.Errorf("the key %+v does not exist", key)
}
value, ok := ctx.values[key].({{ .Name }})
if !ok {
return {{.Zero|noescape}}, fmt.Errorf("the expected value is not {{ .Name }} (%T)", key)
}
return value, nil
}
{{ end }}
`