| |
| |
| |
|
|
| package cryptotest |
|
|
| import ( |
| "fmt" |
| "reflect" |
| "slices" |
| "testing" |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| func NoExtraMethods(t *testing.T, ms any, allowed ...string) { |
| t.Helper() |
| extraMethods, err := extraMethods(ms) |
| if err != nil { |
| t.Fatal(err) |
| } |
| for _, m := range extraMethods { |
| if slices.Contains(allowed, m) { |
| continue |
| } |
| t.Errorf("unexpected method %q", m) |
| } |
| } |
|
|
| func extraMethods(ip any) ([]string, error) { |
| v := reflect.ValueOf(ip) |
| if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Interface || v.Elem().IsNil() { |
| return nil, fmt.Errorf("argument must be a pointer to a non-nil interface") |
| } |
|
|
| interfaceType := v.Elem().Type() |
| concreteType := v.Elem().Elem().Type() |
|
|
| interfaceMethods := make(map[string]bool) |
| for i := range interfaceType.NumMethod() { |
| interfaceMethods[interfaceType.Method(i).Name] = true |
| } |
|
|
| var extraMethods []string |
| for i := range concreteType.NumMethod() { |
| m := concreteType.Method(i) |
| if !m.IsExported() { |
| continue |
| } |
| if !interfaceMethods[m.Name] { |
| extraMethods = append(extraMethods, m.Name) |
| } |
| } |
|
|
| return extraMethods, nil |
| } |
|
|