Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Last active January 10, 2024 01:33
Show Gist options
  • Save mikeschinkel/9de3f399ab43698dc37af86f9a5627bd to your computer and use it in GitHub Desktop.
Save mikeschinkel/9de3f399ab43698dc37af86f9a5627bd to your computer and use it in GitHub Desktop.
package vermock
import (
"testing"
)
//goland:noinspection GoSnakeCaseUsage
type MockGetter interface {
Get_Vermock() *Mock
}
type Mock struct {
times map[any]int
// Add any other properties that are needed
}
func Apply[T any](obj *T) *T {
// Setup whatever is needed to keep track of mock
return obj
}
type Option[T any] func(*Mock, *T)
func Expect[T any](t *testing.T, obj *T, fn any, opt Option[T]) {
// Implement expect here
}
// Called is an example options function for Expect
func Called[T any](times int) func(*Mock, *T) {
return func(m *Mock, obj *T) {
m.times[obj] = times
}
}
func AssertExpectedCalls[T any](t *testing.T, obj *T) {
// Implement AssertExpectedCalls
}
func Call0[T any](obj *T, fn any, in ...any) {
// Implement Call0
}
func Call1[T1, T any](obj *T, fn any, in ...any) (v T1) {
// Implement Call1
return
}
func Call2[T1, T2, T any](obj *T, fn any, in ...any) (v1 T1, v2 T2) {
// Implement Call2
return
}
package vermock_test
import (
"testing"
"github.com/Versent/go-mock/vermock"
)
var _ vermock.MockGetter = (*mockCache)(nil)
type mockCache struct {
mock *vermock.Mock
getFunc func(string) (any, bool)
putFunc func(string, any) error
deleteFunc func(string)
}
//goland:noinspection GoSnakeCaseUsage
func (m *mockCache) Get_Vermock() *vermock.Mock {
return m.mock
}
func (m *mockCache) Get(obj string) (value any, ok bool) {
return vermock.Call2[any, bool](m, m.getFunc, obj)
}
func (m *mockCache) Put(obj string, value any) error {
return vermock.Call1[error](m, m.putFunc, obj, value)
}
func (m *mockCache) Delete(obj string) {
vermock.Call0(m, m.deleteFunc, obj)
}
func TestObject(t *testing.T) {
// Apply your required methods here
m := vermock.Apply(&mockCache{
getFunc: func(key string) (any, bool) {
// Define your mock's behaviour
return nil, false
},
putFunc: func(key string, value any) error {
// Define your mock's behaviour
return nil
},
})
// Do all your expecting here
vermock.Expect(t, m, m.getFunc,
vermock.Called[mockCache](2),
)
vermock.Expect(t, m, m.putFunc,
vermock.Called[mockCache](1),
)
// Use the mock instance in your test
// Assert that all expected methods were called
vermock.AssertExpectedCalls(t, m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment