You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
type B interface {
Bar()
}
type A interface {
Get() B
}
Running mockery -name A -print on this does not yield a mock which checks for nil! The problem is that the B return type is parsed to an *ast.Ident, not an *ast.InterfaceType (and therefore isNillable returns false).
You can use the type of the identifier to handle this case, e.g.:
func (g *Generator) isNillable(typ ast.Expr) bool {
switch t := typ.(type) {
case *ast.StarExpr, *ast.ArrayType, *ast.MapType, *ast.InterfaceType, *ast.FuncType, *ast.ChanType:
return true
case *ast.Ident:
switch d := t.Obj.Decl.(type) {
case *ast.TypeSpec:
switch d.Type.(type) {
case *ast.StarExpr, *ast.ArrayType, *ast.MapType, *ast.InterfaceType, *ast.FuncType, *ast.ChanType:
return true
}
}
}
return false
}
The text was updated successfully, but these errors were encountered:
@spaowi Your example works for the case where the interface return type is in the same package as the interface being mocked. However, when the interface is an exported type, the returned interface is incorrectly parsed as a *ast.SelectorExpr. With the code above, generating a mock for bar.A will not yield a mock that checks for nil:
Example:
Running mockery -name A -print on this does not yield a mock which checks for nil! The problem is that the B return type is parsed to an *ast.Ident, not an *ast.InterfaceType (and therefore isNillable returns false).
You can use the type of the identifier to handle this case, e.g.:
The text was updated successfully, but these errors were encountered: