Allow more flexible callback return error type.

This commit is contained in:
Mitar
2021-12-13 00:02:56 +01:00
committed by Alec Thomas
parent deebf0b09b
commit ece1f6d8cb
2 changed files with 47 additions and 2 deletions
+45
View File
@@ -42,6 +42,51 @@ func TestBindTo(t *testing.T) {
require.Equal(t, "foo", saw)
}
func TestInvalidCallback(t *testing.T) {
type iface interface {
Method()
}
saw := ""
method := func(i iface) string {
saw = string(i.(impl))
return saw
}
var cli struct{}
p, err := New(&cli, BindTo(impl("foo"), (*iface)(nil)))
require.NoError(t, err)
err = callMethod("method", reflect.ValueOf(impl("??")), reflect.ValueOf(method), p.bindings)
require.EqualError(t, err, `return value of *reflect.rtype.method() must implement "error"`)
}
type zrror struct{}
func (*zrror) Error() string {
return "error"
}
func TestCallbackCustomError(t *testing.T) {
type iface interface {
Method()
}
saw := ""
method := func(i iface) *zrror {
saw = string(i.(impl))
return nil
}
var cli struct{}
p, err := New(&cli, BindTo(impl("foo"), (*iface)(nil)))
require.NoError(t, err)
err = callMethod("method", reflect.ValueOf(impl("??")), reflect.ValueOf(method), p.bindings)
require.NoError(t, err)
require.Equal(t, "foo", saw)
}
type bindToProviderCLI struct {
Called bool
Cmd bindToProviderCmd `cmd:""`