1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:04:44 -07:00

go/packages/packagestest: allow expectations to have *regexp.Regexp and interface{} parameters

Change-Id: I86312ae2f30ba04290e9b7a001e0284439f4bfe6
Reviewed-on: https://go-review.googlesource.com/c/151346
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Ian Cottrell 2018-11-27 15:31:57 -05:00
parent b4e97c0ed8
commit e782529d0d

View File

@ -48,8 +48,10 @@ const (
// expect.Comment : passed the Comment instance being evaluated. // expect.Comment : passed the Comment instance being evaluated.
// string : can be supplied either a string literal or an identifier. // string : can be supplied either a string literal or an identifier.
// int : can only be supplied an integer literal. // int : can only be supplied an integer literal.
// *regexp.Regexp : can only be supplied a regular expression literal
// token.Pos : has a file position calculated as described below. // token.Pos : has a file position calculated as described below.
// token.Position : has a file position calculated as described below. // token.Position : has a file position calculated as described below.
// interface{} : will be passed any value
// //
// Position calculation // Position calculation
// //
@ -173,6 +175,7 @@ var (
positionType = reflect.TypeOf(token.Position{}) positionType = reflect.TypeOf(token.Position{})
rangeType = reflect.TypeOf(Range{}) rangeType = reflect.TypeOf(Range{})
fsetType = reflect.TypeOf((*token.FileSet)(nil)) fsetType = reflect.TypeOf((*token.FileSet)(nil))
regexType = reflect.TypeOf((*regexp.Regexp)(nil))
) )
// converter converts from a marker's argument parsed from the comment to // converter converts from a marker's argument parsed from the comment to
@ -238,6 +241,17 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) {
return reflect.Value{}, nil, fmt.Errorf("cannot convert %v to string", arg) return reflect.Value{}, nil, fmt.Errorf("cannot convert %v to string", arg)
} }
}, nil }, nil
case pt == regexType:
return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) {
arg := args[0]
args = args[1:]
if _, ok := arg.(*regexp.Regexp); !ok {
return reflect.Value{}, nil, fmt.Errorf("cannot convert %v to *regexp.Regexp", arg)
}
return reflect.ValueOf(arg), args, nil
}, nil
case pt.Kind() == reflect.String: case pt.Kind() == reflect.String:
return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) {
arg := args[0] arg := args[0]
@ -290,7 +304,12 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) {
return result, args, nil return result, args, nil
}, nil }, nil
default: default:
return nil, fmt.Errorf("param has invalid type %v", pt) if pt.Kind() == reflect.Interface && pt.NumMethod() == 0 {
return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) {
return reflect.ValueOf(args[0]), args[1:], nil
}, nil
}
return nil, fmt.Errorf("param has unexpected type %v (kind %v)", pt, pt.Kind())
} }
} }