diff --git a/go/packages/packagestest/expect.go b/go/packages/packagestest/expect.go index 3566388694..14e1fc013c 100644 --- a/go/packages/packagestest/expect.go +++ b/go/packages/packagestest/expect.go @@ -48,8 +48,10 @@ const ( // expect.Comment : passed the Comment instance being evaluated. // string : can be supplied either a string literal or an identifier. // 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.Position : has a file position calculated as described below. +// interface{} : will be passed any value // // Position calculation // @@ -173,6 +175,7 @@ var ( positionType = reflect.TypeOf(token.Position{}) rangeType = reflect.TypeOf(Range{}) fsetType = reflect.TypeOf((*token.FileSet)(nil)) + regexType = reflect.TypeOf((*regexp.Regexp)(nil)) ) // 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) } }, 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: return func(n *expect.Note, args []interface{}) (reflect.Value, []interface{}, error) { arg := args[0] @@ -290,7 +304,12 @@ func (e *Exported) buildConverter(pt reflect.Type) (converter, error) { return result, args, nil }, nil 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()) } }