aboutsummaryrefslogtreecommitdiff
path: root/src/reflect/all_test.go
diff options
context:
space:
mode:
authorJoe Tsai <joetsai@digital-static.net>2021-09-04 13:02:09 -0700
committerJoseph Tsai <joetsai@digital-static.net>2022-03-02 18:06:42 +0000
commita5b8b56d1d05d186999e4abf1e2147b6aa203ec9 (patch)
tree78a3ef01c71bcf0a173efd7087208cebee92039b /src/reflect/all_test.go
parentec4687f337465b719efdeef72b357fa0b05879bb (diff)
downloadgo-a5b8b56d1d05d186999e4abf1e2147b6aa203ec9.tar.gz
go-a5b8b56d1d05d186999e4abf1e2147b6aa203ec9.zip
reflect: allow Value.Bytes on addressable byte arrays
Modify Value.Bytes to be callable addressable byte arrays. While related, the behavior of Value.SetBytes was not modified. Fixes #47066 Change-Id: Ic3ba4432353b8da5f33b3188e20034a33b2f6ee8 Reviewed-on: https://go-review.googlesource.com/c/go/+/357331 Trust: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Keith Randall <khr@golang.org> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/reflect/all_test.go')
-rw-r--r--src/reflect/all_test.go29
1 files changed, 27 insertions, 2 deletions
diff --git a/src/reflect/all_test.go b/src/reflect/all_test.go
index 866f38e687..5364166eab 100644
--- a/src/reflect/all_test.go
+++ b/src/reflect/all_test.go
@@ -3682,8 +3682,11 @@ func TestTagGet(t *testing.T) {
}
func TestBytes(t *testing.T) {
- type B []byte
- x := B{1, 2, 3, 4}
+ shouldPanic("on int Value", func() { ValueOf(0).Bytes() })
+ shouldPanic("of non-byte slice", func() { ValueOf([]string{}).Bytes() })
+
+ type S []byte
+ x := S{1, 2, 3, 4}
y := ValueOf(x).Bytes()
if !bytes.Equal(x, y) {
t.Fatalf("ValueOf(%v).Bytes() = %v", x, y)
@@ -3691,6 +3694,28 @@ func TestBytes(t *testing.T) {
if &x[0] != &y[0] {
t.Errorf("ValueOf(%p).Bytes() = %p", &x[0], &y[0])
}
+
+ type A [4]byte
+ a := A{1, 2, 3, 4}
+ shouldPanic("unaddressable", func() { ValueOf(a).Bytes() })
+ shouldPanic("on ptr Value", func() { ValueOf(&a).Bytes() })
+ b := ValueOf(&a).Elem().Bytes()
+ if !bytes.Equal(a[:], y) {
+ t.Fatalf("ValueOf(%v).Bytes() = %v", a, b)
+ }
+ if &a[0] != &b[0] {
+ t.Errorf("ValueOf(%p).Bytes() = %p", &a[0], &b[0])
+ }
+
+ // Per issue #24746, it was decided that Bytes can be called on byte slices
+ // that normally cannot be converted from per Go language semantics.
+ type B byte
+ type SB []B
+ type AB [4]B
+ ValueOf([]B{1, 2, 3, 4}).Bytes() // should not panic
+ ValueOf(new([4]B)).Elem().Bytes() // should not panic
+ ValueOf(SB{1, 2, 3, 4}).Bytes() // should not panic
+ ValueOf(new(AB)).Elem().Bytes() // should not panic
}
func TestSetBytes(t *testing.T) {