Skip to content

Instantly share code, notes, and snippets.

@IronSavior
Created May 4, 2022 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IronSavior/a4da8cac3627426d79ba00b129277ba8 to your computer and use it in GitHub Desktop.
Save IronSavior/a4da8cac3627426d79ba00b129277ba8 to your computer and use it in GitHub Desktop.
Use Gomega matchers with gomock
package matcher_test
import (
"fmt"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type Value struct {
X, Y int
}
type HasX int
type HasY int
func (hx HasX) Match(x interface{}) (bool, error) {
match := hx.Matches(x)
if !match {
return match, fmt.Errorf("%v expected to have X == %v", x, int(hx))
}
return true, nil
}
func (hx HasX) FailureMessage(actual interface{}) (message string) { return "" }
func (hx HasX) NegatedFailureMessage(actual interface{}) (message string) { return "" }
func (hy HasY) FailureMessage(actual interface{}) (message string) { return "" }
func (hy HasY) NegatedFailureMessage(actual interface{}) (message string) { return "" }
func (hx HasX) Matches(x interface{}) bool {
actual, ok := x.(Value)
if !ok {
return false
}
return actual.X == int(hx)
}
func (hx HasX) String() string {
return ""
}
func (hy HasY) Match(x interface{}) (bool, error) {
match := hy.Matches(x)
if !match {
return match, fmt.Errorf("%v expected to have Y == %v", x, int(hy))
}
return true, nil
}
func (hy HasY) Matches(x interface{}) bool {
actual, ok := x.(Value)
if !ok {
return false
}
return actual.Y == int(hy)
}
func (hy HasY) String() string {
return ""
}
type GomegaMatcher interface {
Match(actual interface{}) (success bool, err error)
FailureMessage(actual interface{}) (message string)
}
type matcher struct {
GomegaMatcher
x interface{}
}
func (m matcher) Matches(x interface{}) bool {
m.x = x
result, _ := m.Match(x)
return result
}
func (m matcher) String() string {
return m.FailureMessage(m.x)
}
func wrap(given GomegaMatcher) gomock.Matcher {
return matcher{given, nil}
}
var _ = Describe("stuff", func() {
actual := []Value{
{0, 0},
{0, 1},
}
It("has X == 0", func() {
Expect(HasX(0).Matches(actual[0])).To(BeTrue())
})
It("does not have X == 1", func() {
Expect(HasX(1).Matches(actual[0])).To(BeFalse())
})
It("contains element {0, 0}", func() {
m := wrap(ContainElement(Value{0, 0}))
Expect(m.Matches(actual)).To(BeTrue())
})
It("contains element HasX(0)", func() {
m := wrap(ContainElement(HasX(0)))
Expect(m.Matches(actual)).To(BeTrue())
})
It("does not contain element HasX(1)", func() {
m := wrap(ContainElement(HasX(1)))
Expect(m.Matches(actual)).To(BeFalse())
})
It("contain element HasX(0), HasY(1)", func() {
m := wrap(ContainElement(And(HasX(0), HasY(1))))
Expect(m.Matches(actual)).To(BeTrue())
})
It("does not contain element HasX(0), HasY(2)", func() {
m := wrap(ContainElement(And(HasX(0), HasY(2))))
Expect(m.Matches(actual)).To(BeFalse())
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment