aboutsummaryrefslogtreecommitdiff
path: root/src/reflect/all_test.go
diff options
context:
space:
mode:
authorJoe Tsai <joetsai@digital-static.net>2020-10-14 18:41:16 -0700
committerJoe Tsai <thebrokentoaster@gmail.com>2021-02-25 21:21:51 +0000
commitb83d073e9eb4cbd0cd5ca530f576668c49f6d0f1 (patch)
treef706562328a6ff09eec8117618bc5edb100b2466 /src/reflect/all_test.go
parent7fcf9893f71c75f6b2fd53bea326d5061f705208 (diff)
downloadgo-b83d073e9eb4cbd0cd5ca530f576668c49f6d0f1.tar.gz
go-b83d073e9eb4cbd0cd5ca530f576668c49f6d0f1.zip
reflect: add Method.IsExported and StructField.IsExported methods
The IsExported method is a more intuitive helper for checking whether the method or field is exported than checking whether PkgPath is empty. In the same CL, modify the standard library to make use of this helper. Fixes #41563 Change-Id: Iaacfb3b74449501f98e2707aa32095a32bd3c3c1 Reviewed-on: https://go-review.googlesource.com/c/go/+/266197 Trust: Joe Tsai <joetsai@digital-static.net> Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/reflect/all_test.go')
-rw-r--r--src/reflect/all_test.go55
1 files changed, 46 insertions, 9 deletions
diff --git a/src/reflect/all_test.go b/src/reflect/all_test.go
index 1225d6177d..35cc469d74 100644
--- a/src/reflect/all_test.go
+++ b/src/reflect/all_test.go
@@ -2900,6 +2900,7 @@ func TestFieldPkgPath(t *testing.T) {
index []int
pkgPath string
embedded bool
+ exported bool
}
checkPkgPath := func(name string, s []pkgpathTest) {
@@ -2911,27 +2912,63 @@ func TestFieldPkgPath(t *testing.T) {
if got, want := f.Anonymous, test.embedded; got != want {
t.Errorf("%s: Field(%d).Anonymous = %v, want %v", name, test.index, got, want)
}
+ if got, want := f.IsExported(), test.exported; got != want {
+ t.Errorf("%s: Field(%d).IsExported = %v, want %v", name, test.index, got, want)
+ }
}
}
checkPkgPath("testStruct", []pkgpathTest{
- {[]int{0}, "", false}, // Exported
- {[]int{1}, "reflect_test", false}, // unexported
- {[]int{2}, "", true}, // OtherPkgFields
- {[]int{2, 0}, "", false}, // OtherExported
- {[]int{2, 1}, "reflect", false}, // otherUnexported
- {[]int{3}, "reflect_test", true}, // int
- {[]int{4}, "reflect_test", true}, // *x
+ {[]int{0}, "", false, true}, // Exported
+ {[]int{1}, "reflect_test", false, false}, // unexported
+ {[]int{2}, "", true, true}, // OtherPkgFields
+ {[]int{2, 0}, "", false, true}, // OtherExported
+ {[]int{2, 1}, "reflect", false, false}, // otherUnexported
+ {[]int{3}, "reflect_test", true, false}, // int
+ {[]int{4}, "reflect_test", true, false}, // *x
})
type localOtherPkgFields OtherPkgFields
typ = TypeOf(localOtherPkgFields{})
checkPkgPath("localOtherPkgFields", []pkgpathTest{
- {[]int{0}, "", false}, // OtherExported
- {[]int{1}, "reflect", false}, // otherUnexported
+ {[]int{0}, "", false, true}, // OtherExported
+ {[]int{1}, "reflect", false, false}, // otherUnexported
})
}
+func TestMethodPkgPath(t *testing.T) {
+ type I interface {
+ x()
+ X()
+ }
+ typ := TypeOf((*interface {
+ I
+ y()
+ Y()
+ })(nil)).Elem()
+
+ tests := []struct {
+ name string
+ pkgPath string
+ exported bool
+ }{
+ {"X", "", true},
+ {"Y", "", true},
+ {"x", "reflect_test", false},
+ {"y", "reflect_test", false},
+ }
+
+ for _, test := range tests {
+ m, _ := typ.MethodByName(test.name)
+ if got, want := m.PkgPath, test.pkgPath; got != want {
+ t.Errorf("MethodByName(%q).PkgPath = %q, want %q", test.name, got, want)
+ }
+ if got, want := m.IsExported(), test.exported; got != want {
+ t.Errorf("MethodByName(%q).IsExported = %v, want %v", test.name, got, want)
+ }
+ }
+}
+
func TestVariadicType(t *testing.T) {
// Test example from Type documentation.
var f func(x int, y ...float64)