-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolden_test.go
250 lines (212 loc) · 6.56 KB
/
golden_test.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
// Golden represents a test case
type Golden struct {
name string // The test case name, also used for test file name.
typeName string // '-type' parameter
data map[string]string // '-data' parameter
execArgsStr string
inputStr string
inputTmplStr string
outputStr string
}
func TestGolden(t *testing.T) {
testCases := []Golden{
{
name: "os_file_mode",
typeName: "os.FileMode",
data: map[string]string{"typename":"OSFileModeStr"},
execArgsStr: "",
inputStr: os_file_mode_in,
inputTmplStr: os_file_mode_tmpl,
outputStr: os_file_mode_out,
},
{
name: "dayofweek",
typeName: "DayOfWeek",
data: map[string]string{"package":"dayofweek","typename":"DayOfWeek"},
execArgsStr: "",
inputStr: dayofweek_in,
inputTmplStr: dayofweek_tmpl,
outputStr: dayofweek_out,
},
{
name: "const_kind",
typeName: "constant.Kind",
data: map[string]string{},
execArgsStr: "-type=constant.Kind -template=const_kind.tmpl const_kind.go",
inputStr: const_kind_in,
inputTmplStr: const_kind_tmpl,
outputStr: const_kind_out,
},
}
dir, err := ioutil.TempDir("", "constconv")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(dir)
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T){
inputFileName := tc.name + ".go"
inputFilePath := filepath.Join(dir, inputFileName)
tmplFileName := tc.name + ".tmpl"
tmplFilePath := filepath.Join(dir, tmplFileName)
if err := ioutil.WriteFile(inputFilePath, []byte(tc.inputStr), 0644); err != nil {
t.Error(err)
}
if err := ioutil.WriteFile(tmplFilePath, []byte(tc.inputTmplStr), 0644); err != nil {
t.Error(err)
}
config := &Config{
types: []string{tc.typeName},
tags: nil,
extraData: tc.data,
execArgsStr: tc.execArgsStr,
dirOrFiles: []string{inputFilePath},
baseDir: dir,
templateFile: tmplFilePath,
outputFile: "", // don't output in this tests
}
gen := NewGenerator(config)
if err := gen.LoadTemplate(); err != nil {
t.Errorf("loading template output: %s", err)
}
parser := NewParser(config)
if err := parser.Parse(); err != nil {
t.Errorf("parsing output: %s", err)
}
src, err := gen.Generate(parser)
if err != nil {
t.Errorf("generating output: %s", err)
}
gotSrc := string(src)
if gotSrc != tc.outputStr {
t.Errorf("%s: got(%d)\n====\n%q====\nexpected(%d)\n====%q", tc.name, len(gotSrc), gotSrc, len(tc.outputStr), tc.outputStr)
}
})
}
}
const os_file_mode_in = `package testdata
import(
_ "os"
)
`
const os_file_mode_tmpl = `{{ $.DoNotEdit }}
package {{ $.BasePackageName }}
type {{ $.Extra.typename }} string
const (
{{ range $v := $.Result.Values -}}
{{ $.Extra.typename }}{{ $v.Name }} {{ $.Extra.typename }} = "{{ $v.Name | UpperSnakeCase }}"
{{ end }}
)`
const os_file_mode_out = `// DO NOT EDIT.; Code generated by "constconv "
package testdata
type OSFileModeStr string
const (
OSFileModeStrModeDir OSFileModeStr = "MODE_DIR"
OSFileModeStrModeAppend OSFileModeStr = "MODE_APPEND"
OSFileModeStrModeExclusive OSFileModeStr = "MODE_EXCLUSIVE"
OSFileModeStrModeTemporary OSFileModeStr = "MODE_TEMPORARY"
OSFileModeStrModeSymlink OSFileModeStr = "MODE_SYMLINK"
OSFileModeStrModeDevice OSFileModeStr = "MODE_DEVICE"
OSFileModeStrModeNamedPipe OSFileModeStr = "MODE_NAMED_PIPE"
OSFileModeStrModeSocket OSFileModeStr = "MODE_SOCKET"
OSFileModeStrModeSetuid OSFileModeStr = "MODE_SETUID"
OSFileModeStrModeSetgid OSFileModeStr = "MODE_SETGID"
OSFileModeStrModeCharDevice OSFileModeStr = "MODE_CHAR_DEVICE"
OSFileModeStrModeSticky OSFileModeStr = "MODE_STICKY"
OSFileModeStrModeIrregular OSFileModeStr = "MODE_IRREGULAR"
OSFileModeStrModePerm OSFileModeStr = "MODE_PERM"
)
`
const dayofweek_in = `package testdata
// This constant definitions are the part of googleapis' type
// see more: https://github.com/googleapis/go-genproto/blob/master/googleapis/type/dayofweek/dayofweek.pb.go
// Represents a day of the week.
type DayOfWeek int32
const (
// The day of the week is unspecified.
DayOfWeek_DAY_OF_WEEK_UNSPECIFIED DayOfWeek = 0
// Monday
DayOfWeek_MONDAY DayOfWeek = 1
// Tuesday
DayOfWeek_TUESDAY DayOfWeek = 2
// Wednesday
DayOfWeek_WEDNESDAY DayOfWeek = 3
// Thursday
DayOfWeek_THURSDAY DayOfWeek = 4
// Friday
DayOfWeek_FRIDAY DayOfWeek = 5
// Saturday
DayOfWeek_SATURDAY DayOfWeek = 6
// Sunday
DayOfWeek_SUNDAY DayOfWeek = 7
)`
const dayofweek_tmpl = `{{ $.DoNotEdit }}
package {{ $.Extra.package }}
type {{ $.Extra.typename }} string
const (
{{ range $v := $.Result.Values -}}
{{ $v.Name | UpperCamelCase }} {{ $.Extra.typename }} = "{{ TrimPrefix (TrimPrefix $v.Name $.Result.RepTypeName) "_" | UpperSnakeCase }}" // {{ $v.Str }}
{{ end }}
)`
const dayofweek_out = `// DO NOT EDIT.; Code generated by "constconv "
package dayofweek
type DayOfWeek string
const (
DayOfWeekDayOfWeekUnspecified DayOfWeek = "DAY_OF_WEEK_UNSPECIFIED" // 0
DayOfWeekMonday DayOfWeek = "MONDAY" // 1
DayOfWeekTuesday DayOfWeek = "TUESDAY" // 2
DayOfWeekWednesday DayOfWeek = "WEDNESDAY" // 3
DayOfWeekThursday DayOfWeek = "THURSDAY" // 4
DayOfWeekFriday DayOfWeek = "FRIDAY" // 5
DayOfWeekSaturday DayOfWeek = "SATURDAY" // 6
DayOfWeekSunday DayOfWeek = "SUNDAY" // 7
)
`
const const_kind_in = `package testdata
import (
_ "go/constant"
)
`
const const_kind_tmpl = `{{ $.DoNotEdit }}
package {{ $.BasePackageName }}
import(
{{ range $v := $.Result.Imports -}}
{{ $v.Path }}
{{ end }}
)
func {{ $.Result.RepTypeName | DropDot | UpperCamelCase }}ToString({{ $.Result.RepTypeName | DropDot | LowerCamelCase }} {{ $.Result.RepTypeName }}) string {
switch {{ $.Result.RepTypeName | DropDot | LowerCamelCase }} {
{{ range $v := $.Result.Values -}}
case {{ $v.Str }}: return {{ $v.Name | Quote }}
{{ end }}
}
}`
const const_kind_out = `// DO NOT EDIT.; Code generated by "constconv -type=constant.Kind -template=const_kind.tmpl const_kind.go"
package testdata
import (
"go/constant"
)
func ConstantKindToString(constantKind constant.Kind) string {
switch constantKind {
case 0:
return "Unknown"
case 1:
return "Bool"
case 2:
return "String"
case 3:
return "Int"
case 4:
return "Float"
case 5:
return "Complex"
}
}
`