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 file. 4 5package function 6 7import ( 8 "bytes" 9 "reflect" 10 "testing" 11) 12 13type myType struct{ bytes.Buffer } 14 15func (myType) valueMethod() {} 16func (myType) ValueMethod() {} 17 18func (*myType) pointerMethod() {} 19func (*myType) PointerMethod() {} 20 21func TestNameOf(t *testing.T) { 22 tests := []struct { 23 fnc interface{} 24 want string 25 }{ 26 {TestNameOf, "function.TestNameOf"}, 27 {func() {}, "function.TestNameOf.func1"}, 28 {(myType).valueMethod, "function.myType.valueMethod"}, 29 {(myType).ValueMethod, "function.myType.ValueMethod"}, 30 {(myType{}).valueMethod, "function.myType.valueMethod"}, 31 {(myType{}).ValueMethod, "function.myType.ValueMethod"}, 32 {(*myType).valueMethod, "function.myType.valueMethod"}, 33 {(*myType).ValueMethod, "function.myType.ValueMethod"}, 34 {(&myType{}).valueMethod, "function.myType.valueMethod"}, 35 {(&myType{}).ValueMethod, "function.myType.ValueMethod"}, 36 {(*myType).pointerMethod, "function.myType.pointerMethod"}, 37 {(*myType).PointerMethod, "function.myType.PointerMethod"}, 38 {(&myType{}).pointerMethod, "function.myType.pointerMethod"}, 39 {(&myType{}).PointerMethod, "function.myType.PointerMethod"}, 40 {(*myType).Write, "function.myType.Write"}, 41 {(&myType{}).Write, "bytes.Buffer.Write"}, 42 } 43 for _, tt := range tests { 44 t.Run("", func(t *testing.T) { 45 got := NameOf(reflect.ValueOf(tt.fnc)) 46 if got != tt.want { 47 t.Errorf("NameOf() = %v, want %v", got, tt.want) 48 } 49 }) 50 } 51} 52