You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

52 lines
1.5 KiB

  1. // Copyright 2019, The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE.md file.
  4. package function
  5. import (
  6. "bytes"
  7. "reflect"
  8. "testing"
  9. )
  10. type myType struct{ bytes.Buffer }
  11. func (myType) valueMethod() {}
  12. func (myType) ValueMethod() {}
  13. func (*myType) pointerMethod() {}
  14. func (*myType) PointerMethod() {}
  15. func TestNameOf(t *testing.T) {
  16. tests := []struct {
  17. fnc interface{}
  18. want string
  19. }{
  20. {TestNameOf, "function.TestNameOf"},
  21. {func() {}, "function.TestNameOf.func1"},
  22. {(myType).valueMethod, "function.myType.valueMethod"},
  23. {(myType).ValueMethod, "function.myType.ValueMethod"},
  24. {(myType{}).valueMethod, "function.myType.valueMethod"},
  25. {(myType{}).ValueMethod, "function.myType.ValueMethod"},
  26. {(*myType).valueMethod, "function.myType.valueMethod"},
  27. {(*myType).ValueMethod, "function.myType.ValueMethod"},
  28. {(&myType{}).valueMethod, "function.myType.valueMethod"},
  29. {(&myType{}).ValueMethod, "function.myType.ValueMethod"},
  30. {(*myType).pointerMethod, "function.myType.pointerMethod"},
  31. {(*myType).PointerMethod, "function.myType.PointerMethod"},
  32. {(&myType{}).pointerMethod, "function.myType.pointerMethod"},
  33. {(&myType{}).PointerMethod, "function.myType.PointerMethod"},
  34. {(*myType).Write, "function.myType.Write"},
  35. {(&myType{}).Write, "bytes.Buffer.Write"},
  36. }
  37. for _, tt := range tests {
  38. t.Run("", func(t *testing.T) {
  39. got := NameOf(reflect.ValueOf(tt.fnc))
  40. if got != tt.want {
  41. t.Errorf("NameOf() = %v, want %v", got, tt.want)
  42. }
  43. })
  44. }
  45. }