Skip to content

Commit 0b6ba6c

Browse files
author
Mattias Öhrn
committed
Fixing default matcher generation
1 parent ca5972e commit 0b6ba6c

File tree

2 files changed

+162
-87
lines changed

2 files changed

+162
-87
lines changed

internal/generate_matchers/matcher_generation.go

Lines changed: 126 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,148 @@ import (
1212
// go generate github.com/petergtz/pegomock/internal/generate_matchers
1313

1414
//go:generate go run matcher_generation.go
15+
//go:generate go fmt ../../matcher_factories.go
16+
//go:generate go fmt ../../ginkgo_compatible/matchers.go
1517

1618
func main() {
17-
err := ioutil.WriteFile(
18-
"../../matcher_factories.go",
19-
[]byte(GenerateDefaultMatchersFile()),
20-
0644)
19+
mustWriteFile("../../matcher_factories.go", GenerateDefaultMatchersFile())
20+
mustWriteFile("../../ginkgo_compatible/matchers.go", GenerateGinkgoMatchersFile())
21+
}
22+
23+
func mustWriteFile(path string, contents string) {
24+
err := ioutil.WriteFile(path, []byte(contents), 0644)
2125
if err != nil {
2226
panic(err)
2327
}
2428
}
2529

2630
func GenerateDefaultMatchersFile() string {
27-
return fmt.Sprintf(`package pegomock
31+
contents := `package pegomock
2832
2933
import (
3034
"reflect"
3135
)
36+
`
3237

33-
%s
34-
`, GenerateDefaultMatchers())
38+
for _, kind := range primitiveKinds {
39+
contents += fmt.Sprintf(`
40+
func Eq%[1]s(value %[2]s) %[2]s {
41+
RegisterMatcher(&EqMatcher{Value: value})
42+
return %[4]s
3543
}
3644
37-
func GenerateDefaultMatchers() string {
38-
result := ""
39-
for _, kind := range primitiveKinds {
40-
result += GenerateEqMatcherFactory(kind) +
41-
GenerateAnyMatcherFactory(kind) +
42-
GenerateAnySliceMatcherFactory(kind)
45+
func NotEq%[1]s(value %[2]s) %[2]s {
46+
RegisterMatcher(&NotEqMatcher{Value: value})
47+
return %[4]s
48+
}
49+
50+
func Any%[1]s() %[2]s {
51+
RegisterMatcher(NewAnyMatcher(reflect.TypeOf(%[3]s)))
52+
return %[4]s
53+
}
54+
55+
func %[1]sThat(matcher ArgumentMatcher) %[2]s {
56+
RegisterMatcher(matcher)
57+
return %[4]s
58+
}
59+
60+
func Eq%[1]sSlice(value []%[2]s) []%[2]s {
61+
RegisterMatcher(&EqMatcher{Value: value})
62+
return nil
63+
}
64+
65+
func NotEq%[1]sSlice(value []%[2]s) []%[2]s {
66+
RegisterMatcher(&NotEqMatcher{Value: value})
67+
return nil
68+
}
69+
70+
func Any%[1]sSlice() []%[2]s {
71+
RegisterMatcher(NewAnyMatcher(reflect.SliceOf(reflect.TypeOf(%[3]s))))
72+
return nil
73+
}
74+
75+
func %[1]sSliceThat(matcher ArgumentMatcher) []%[2]s {
76+
RegisterMatcher(matcher)
77+
return nil
78+
}
79+
`, strings.Title(kind.String()), kind.String(), exampleValue(kind), zeroValue(kind))
4380
}
44-
// hard-coding this for now as interface{} overall works slighly different than other types.
45-
result += `func EqInterface(value interface{}) interface{} {
81+
82+
// hard-coding this for now as interface{} overall works slightly different than other types.
83+
return contents + `
84+
func EqInterface(value interface{}) interface{} {
4685
RegisterMatcher(&EqMatcher{Value: value})
4786
return nil
4887
}
4988
89+
func NotEqInterface(value interface{}) interface{} {
90+
RegisterMatcher(&NotEqMatcher{Value: value})
91+
return nil
92+
}
93+
5094
func AnyInterface() interface{} {
51-
RegisterMatcher(NewAnyMatcher(reflect.TypeOf((*(interface{}))(nil)).Elem()))
95+
RegisterMatcher(NewAnyMatcher(reflect.TypeOf((*interface{})(nil)).Elem()))
96+
return nil
97+
}
98+
99+
func InterfaceThat(matcher ArgumentMatcher) interface{} {
100+
RegisterMatcher(matcher)
101+
return nil
102+
}
103+
104+
func EqInterfaceSlice(value []interface{}) []interface{} {
105+
RegisterMatcher(&EqMatcher{Value: value})
106+
return nil
107+
}
108+
109+
func NotEqInterfaceSlice(value []interface{}) []interface{} {
110+
RegisterMatcher(&NotEqMatcher{Value: value})
52111
return nil
53112
}
54113
55114
func AnyInterfaceSlice() []interface{} {
56-
RegisterMatcher(NewAnyMatcher(reflect.SliceOf(reflect.TypeOf((*(interface{}))(nil)).Elem())))
115+
RegisterMatcher(NewAnyMatcher(reflect.SliceOf(reflect.TypeOf((*interface{})(nil)).Elem())))
57116
return nil
58-
}`
59-
return result
117+
}
118+
119+
func InterfaceSliceThat(matcher ArgumentMatcher) []interface{} {
120+
RegisterMatcher(matcher)
121+
return nil
122+
}
123+
`
124+
}
125+
126+
func GenerateGinkgoMatchersFile() string {
127+
contents := `package mock
128+
129+
import (
130+
"github.com/petergtz/pegomock"
131+
)
132+
133+
var (`
134+
135+
for _, kind := range append(primitiveKinds, reflect.Interface) {
136+
contents += fmt.Sprintf(`
137+
Eq%[1]s = pegomock.Eq%[1]s
138+
NotEq%[1]s = pegomock.NotEq%[1]s
139+
Any%[1]s = pegomock.Any%[1]s
140+
%[1]sThat = pegomock.%[1]sThat
141+
Eq%[1]sSlice = pegomock.Eq%[1]sSlice
142+
NotEq%[1]sSlice = pegomock.NotEq%[1]sSlice
143+
Any%[1]sSlice = pegomock.Any%[1]sSlice
144+
%[1]sSliceThat = pegomock.%[1]sSliceThat
145+
`, strings.Title(kind.String()))
146+
}
147+
148+
return contents + `
149+
Times = pegomock.Times
150+
AtLeast = pegomock.AtLeast
151+
AtMost = pegomock.AtMost
152+
Never = pegomock.Never
153+
Once = pegomock.Once
154+
Twice = pegomock.Twice
155+
)
156+
`
60157
}
61158

62159
var primitiveKinds = []reflect.Kind{
@@ -79,38 +176,9 @@ var primitiveKinds = []reflect.Kind{
79176
reflect.String,
80177
}
81178

82-
func GenerateEqMatcherFactory(kind reflect.Kind) string {
83-
return fmt.Sprintf(`func Eq%s(value %s) %s {
84-
RegisterMatcher(&EqMatcher{Value: value})
85-
return %s
86-
}
87-
88-
`, strings.Title(kind.String()), kind, kind, nullOf(kind))
89-
}
90-
91-
func GenerateAnyMatcherFactory(kind reflect.Kind) string {
92-
return fmt.Sprintf(`func Any%s() %s {
93-
RegisterMatcher(NewAnyMatcher(reflect.TypeOf((%s)(%s))))
94-
return %s
95-
}
96-
97-
`, strings.Title(kind.String()), kind, kind.String(), nullOf(kind), nullOf(kind))
98-
}
99-
100-
func GenerateAnySliceMatcherFactory(kind reflect.Kind) string {
101-
return fmt.Sprintf(`func Any%sSlice() []%s {
102-
RegisterMatcher(NewAnyMatcher(reflect.SliceOf(reflect.TypeOf((%s)(%s)))))
103-
return nil
104-
}
105-
106-
`, strings.Title(kind.String()), kind.String(), kind.String(), nullOf(kind))
107-
}
179+
// TODO generate: chan, func matchers
108180

109-
// TODO generate:
110-
// Eq Slice matchers
111-
// generate chan, func matchers
112-
113-
func nullOf(kind reflect.Kind) string {
181+
func zeroValue(kind reflect.Kind) string {
114182
switch {
115183
case kind == reflect.Bool:
116184
return `false`
@@ -119,6 +187,13 @@ func nullOf(kind reflect.Kind) string {
119187
case kind == reflect.String:
120188
return `""`
121189
default:
122-
return "nil"
190+
return `nil`
191+
}
192+
}
193+
194+
func exampleValue(kind reflect.Kind) string {
195+
if kind == reflect.Bool || kind == reflect.Int || kind == reflect.String {
196+
return zeroValue(kind)
123197
}
198+
return fmt.Sprintf("(%s)(%s)", kind.String(), zeroValue(kind))
124199
}

0 commit comments

Comments
 (0)