@@ -19,6 +19,7 @@ import (
19
19
"math/rand"
20
20
"net"
21
21
"os"
22
+ "reflect"
22
23
. "reflect"
23
24
"reflect/internal/example1"
24
25
"reflect/internal/example2"
@@ -8681,3 +8682,83 @@ func TestMapOfKeyPanic(t *testing.T) {
8681
8682
var slice []int
8682
8683
m .MapIndex (ValueOf (slice ))
8683
8684
}
8685
+
8686
+ func testTypeAssert [T comparable ](t * testing.T , val T ) {
8687
+ t .Helper ()
8688
+ v , ok := TypeAssert [T ](ValueOf (val ))
8689
+ if v != val || ! ok {
8690
+ t .Errorf ("TypeAssert[%T](%v) = (%v, %v); want = (%v, true)" , * new (T ), val , v , ok , val )
8691
+ }
8692
+ }
8693
+
8694
+ func testTypeAssertDifferentType [T , T2 comparable ](t * testing.T , val T2 ) {
8695
+ t .Helper ()
8696
+ if v , ok := TypeAssert [T ](ValueOf (val )); ok {
8697
+ t .Errorf ("TypeAssert[%T](%v) = (%v, %v); want = (%v, false)" , * new (T ), val , v , ok , * new (T ))
8698
+ }
8699
+ }
8700
+
8701
+ func newPtr [T any ](t T ) * T {
8702
+ return & t
8703
+ }
8704
+
8705
+ func TestTypeAssert (t * testing.T ) {
8706
+ testTypeAssert (t , int (1111 ))
8707
+ testTypeAssert (t , int (111111111 ))
8708
+ testTypeAssert (t , int (- 111111111 ))
8709
+ testTypeAssert (t , int32 (111111111 ))
8710
+ testTypeAssert (t , int32 (- 111111111 ))
8711
+ testTypeAssert (t , uint32 (111111111 ))
8712
+ testTypeAssert (t , [2 ]int {111111111 , 22222222 })
8713
+ testTypeAssert (t , [2 ]int {- 111111111 , - 22222222 })
8714
+ testTypeAssert (t , newPtr (1111 ))
8715
+ testTypeAssert (t , newPtr (111111111 ))
8716
+ testTypeAssert (t , newPtr (- 111111111 ))
8717
+ testTypeAssert (t , newPtr ([2 ]int {- 111111111 , - 22222222 }))
8718
+
8719
+ testTypeAssert (t , newPtr (time .Now ()))
8720
+
8721
+ testTypeAssertDifferentType [uint ](t , int (111111111 ))
8722
+ testTypeAssertDifferentType [uint ](t , int (- 111111111 ))
8723
+ }
8724
+
8725
+ type testTypeWithMethod struct {
8726
+ val string
8727
+ }
8728
+
8729
+ func (v testTypeWithMethod ) String () string { return v .val }
8730
+
8731
+ func TestTypeAssertMethod (t * testing.T ) {
8732
+ method := ValueOf (& testTypeWithMethod {val : "test value" }).MethodByName ("String" )
8733
+ f , ok := TypeAssert [func () string ](method )
8734
+ if ! ok {
8735
+ t .Fatalf (`TypeAssert[func() string](method) = (,false); want = (,true)` )
8736
+ }
8737
+
8738
+ out := f ()
8739
+ if out != "test value" {
8740
+ t .Fatalf (`TypeAssert[func() string](method)() = %q; want "test value"` , out )
8741
+ }
8742
+ }
8743
+
8744
+ func TestTypeAssertZeroValPanic (t * testing.T ) {
8745
+ defer func () { recover () }()
8746
+ TypeAssert [int ](Value {})
8747
+ t .Fatalf ("TypeAssert did not panic" )
8748
+ }
8749
+
8750
+ func TestTypeAssertReadOnlyPanic (t * testing.T ) {
8751
+ defer func () { recover () }()
8752
+ TypeAssert [int ](reflect .ValueOf (& testTypeWithMethod {}).FieldByName ("val" ))
8753
+ t .Fatalf ("TypeAssert did not panic" )
8754
+ }
8755
+
8756
+ func TestTypeAssertAllocs (t * testing.T ) {
8757
+ val := ValueOf (new (time.Time )).Elem ()
8758
+ allocs := testing .AllocsPerRun (100 , func () {
8759
+ TypeAssert [time.Time ](val )
8760
+ })
8761
+ if allocs != 0 {
8762
+ t .Errorf ("unexpected amount of allocations = %v; want = 0" , allocs )
8763
+ }
8764
+ }
0 commit comments