-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolden_test.go
226 lines (179 loc) · 5 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
// (c) Copyright 2021, Gorror Authors.
//
// Licensed under the terms of the GNU GPL License version 3.
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
var golden = []Golden{
{"simple", false, simpleIn, simpleOut},
{"simpleCompatIs", true, simpleIn, simpleErrIsOut},
{"oneField", false, oneFieldIn, oneFieldOut},
{"multiFields", false, multiFieldsIn, multiFieldsOut},
{"complexField", false, complexFieldIn, complexFieldOut},
{"mustWrap", false, mustWrapIn, mustWrapOut},
{"noWrap", false, noWrapIn, noWrapOut},
}
// Golden represents a test case.
type Golden struct {
name string // name of the test case
compatIs bool // enables compatibility with errors.Is
input string // given input
output string // expected output
}
const simpleIn = `type Err string
const ErrOpen = Err("failed to open file")`
const simpleOut = `type errOpen struct {
_errWrap
}
func newErrOpen() *errOpen {
return &errOpen{_errWrap{nil}}
}
func (e *errOpen) Error() string {
if e.cause == nil {
return fmt.Sprintf("failed to open file")
}
return fmt.Sprintf("failed to open file: %v", e.cause)
}
func (e *errOpen) Wrap(cause error) error {
e.cause = cause
return e
}
func (*errOpen) Is(e Err) bool { return e == ErrOpen }`
const simpleErrIsOut = `type errOpen struct {
_errWrap
}
func newErrOpen() *errOpen {
return &errOpen{_errWrap{nil}}
}
func (e *errOpen) Error() string {
if e.cause == nil {
return fmt.Sprintf("failed to open file")
}
return fmt.Sprintf("failed to open file: %v", e.cause)
}
func (e *errOpen) Wrap(cause error) error {
e.cause = cause
return e
}
func (*errOpen) Is(e error) bool { return e == ErrOpen }`
const oneFieldIn = `type Err string
const ErrOpen = Err("failed to open {{filename string %q}}")`
const oneFieldOut = `type errOpen struct {
_errWrap
filename string
}
func newErrOpen(filename string) *errOpen {
return &errOpen{_errWrap{nil}, filename}
}
func (e *errOpen) Error() string {
if e.cause == nil {
return fmt.Sprintf("failed to open %q", e.filename)
}
return fmt.Sprintf("failed to open %q: %v", e.filename, e.cause)
}
func (e *errOpen) Wrap(cause error) error {
e.cause = cause
return e
}
func (*errOpen) Is(e Err) bool { return e == ErrOpen }`
const multiFieldsIn = `type Err string
const ErrFileOp = Err("failed to {{op string %s}} {{file string %q}} (code {{code int %d}})")`
const multiFieldsOut = `type errFileOp struct {
_errWrap
op string
file string
code int
}
func newErrFileOp(op string, file string, code int) *errFileOp {
return &errFileOp{_errWrap{nil}, op, file, code}
}
func (e *errFileOp) Error() string {
if e.cause == nil {
return fmt.Sprintf("failed to %s %q (code %d)", e.op, e.file, e.code)
}
return fmt.Sprintf("failed to %s %q (code %d): %v", e.op, e.file, e.code, e.cause)
}
func (e *errFileOp) Wrap(cause error) error {
e.cause = cause
return e
}
func (*errFileOp) Is(e Err) bool { return e == ErrFileOp }`
const complexFieldIn = `type Err string
const ErrSome = Err("failed for {{c.Field[0] MyStruct %s}}")`
const complexFieldOut = `type errSome struct {
_errWrap
c MyStruct
}
func newErrSome(c MyStruct) *errSome {
return &errSome{_errWrap{nil}, c}
}
func (e *errSome) Error() string {
if e.cause == nil {
return fmt.Sprintf("failed for %s", e.c.Field[0])
}
return fmt.Sprintf("failed for %s: %v", e.c.Field[0], e.cause)
}
func (e *errSome) Wrap(cause error) error {
e.cause = cause
return e
}
func (*errSome) Is(e Err) bool { return e == ErrSome }`
const mustWrapIn = `type Err string
const ErrSome = Err("wrap:some error")`
const mustWrapOut = `type errSome struct {
_errWrap
}
func newErrSome(err error) *errSome {
return &errSome{_errWrap{err}}
}
func (e *errSome) Error() string {
return fmt.Sprintf("some error: %v", e.cause)
}
func (e *errSome) Wrap(cause error) error {
e.cause = cause
return e
}
func (*errSome) Is(e Err) bool { return e == ErrSome }`
const noWrapIn = `type Err string
const ErrSome = Err("nowrap:some error")`
const noWrapOut = `type errSome struct {
}
func newErrSome() *errSome {
return &errSome{}
}
func (e *errSome) Error() string {
return fmt.Sprintf("some error")
}
func (*errSome) Is(e Err) bool { return e == ErrSome }`
func TestGolden(t *testing.T) {
for _, test := range golden {
t.Run(test.name, func(t *testing.T) {
input := "package test\n" + test.input
absFile := filepath.Join(t.TempDir(), test.name+".go")
err := os.WriteFile(absFile, []byte(input), 0644)
if err != nil {
t.Error(err)
}
// Extract type declaration name from the first line.
tokens := strings.SplitN(test.input, " ", 3)
if len(tokens) != 3 {
t.Fatalf("%s: need type declaration on first line", test.name)
}
g := Generator{typeName: tokens[1], compatIs: test.compatIs}
g.loadPackage([]string{absFile})
for _, e := range g.specs {
g.generate(e)
}
got := string(g.format())
expected := test.output + "\n\n"
if got != expected {
t.Errorf("%s: got(%d)\n====\n%q====\nexpected(%d)\n====\n%q",
test.name, len(got), got, len(expected), expected)
}
})
}
}