I'm attempting write a test for my package and failing at comparing funcs. Here's essentially what i'm doing.
package main
import (
"fmt"
"reflect"
)
type HandlerFunc func(cmd interface{})
type Bus struct {
handlers map[reflect.Type]HandlerFunc
}
func (bus *Bus) RegisterHandler(cmd interface{}, handler HandlerFunc) {
bus.handlers[reflect.TypeOf(cmd)] = handler
}
func (bus *Bus) GetHandler(cmd interface{}) HandlerFunc {
t := reflect.TypeOf(cmd)
for kind, handler := range bus.handlers {
if t == kind {
return handler
}
}
return nil
}
func New() *Bus {
return &Bus{
handlers: make(map[reflect.Type]HandlerFunc),
}
}
type RegisterUserCommand struct {}
func main() {
bus := New()
handler := func (cmd interface{}) {}
bus.RegisterHandler(&RegisterUserCommand{}, handler)
retrieved := bus.GetHandler(&RegisterUserCommand{})
if retrieved != handler {
fmt.Println("Not the same!")
return
}
fmt.Println("Same!")
}
Comparing retrieved
with handler
causes the following error
invalid operation: (func(interface {}))(retrieved) != handler (func can only be compared to nil)
How can i properly test the function i'm retrieving is the same one added previously?