how to compare funcs in golang

huangapple go评论82阅读模式
英文:

how to compare funcs in golang

问题

我正在尝试为我的包编写一个测试,但在比较函数时失败了。以下是我实际在做的事情:

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!")
}

retrievedhandler进行比较会导致以下错误:

invalid operation: (func(interface {}))(retrieved) != handler (func can only be compared to nil)

如何正确测试我检索到的函数与之前添加的函数是相同的?

英文:

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?

答案1

得分: 4

鉴于您无法比较函数,您可以以不同的方式编写测试。您可以让handler在测试中设置一个布尔值,并通过调用它并检查布尔值是否更改来检查是否获得了正确的函数。

以下是一个示例:

func main() {
    bus := New()
    called := false
    handler := func (cmd interface{}) { called = true }

    bus.RegisterHandler(&RegisterUserCommand{}, handler)
    bus.GetHandler(&RegisterUserCommand{})(nil)

    if called {
        fmt.Println("我们得到了正确的处理程序!")
        return
    }

    fmt.Println("我们没有得到正确的处理程序")
}
英文:

Given that you can't compare functions, you can write your test in a different way. You can make handler set a boolean value in your test and check that you've got the right function by calling it and seeing if the boolean changes.

Here's an example:

func main() {
    bus := New()
    called := false
    handler := func (cmd interface{}) { called = true }

    bus.RegisterHandler(&RegisterUserCommand{}, handler)
    bus.GetHandler(&RegisterUserCommand{})(nil)

    if called {
        fmt.Println("We got the right handler!")
        return
    }

    fmt.Println("We didn't get the right handler")
}

huangapple
  • 本文由 发表于 2015年4月29日 10:59:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/29933547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定