github.com/haraldrudell/parl@v0.4.176/preflect/predeclared-type_test.go (about) 1 /* 2 © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 3 ISC License 4 */ 5 6 package preflect 7 8 import ( 9 "strconv" 10 "testing" 11 "unsafe" 12 ) 13 14 func TestPredeclaredType(t *testing.T) { 15 var bitSize = int(unsafe.Sizeof(0) * 8) 16 type args struct { 17 value any 18 } 19 tests := []struct { 20 name string 21 args args 22 wantS string 23 }{ 24 {"nil", args{nil}, "nil"}, 25 {"bool", args{false}, "bool"}, 26 {"uint8", args{uint8(0)}, "uint8"}, 27 {"uint", args{uint(0)}, "uint(" + strconv.Itoa(bitSize) + ")"}, 28 {"byte", args{byte(0)}, "uint8"}, 29 {"complex64", args{complex64(0)}, "complex64"}, 30 {"float32", args{float32(0)}, "float32"}, 31 {"uintptr", args{uintptr(0)}, "uintptr"}, 32 {"string", args{""}, "string"}, 33 {"array", args{[1]int{0}}, "[1]int(64)"}, 34 {"slice", args{[]int{0}}, "[]int(64)"}, 35 {"struct", args{struct{ int }{}}, "struct{ int(64) }"}, 36 {"pointer", args{&bitSize}, "*int(64)"}, 37 {"function", args{func(i int) (s string) { return }}, "func(int(64)) (string)"}, 38 {"interface nil", args{error(nil)}, "nil"}, 39 {"interface non-nil", args{any(1)}, "int(64)"}, 40 {"function", args{func(i int) (s string) { return }}, "func(int(64)) (string)"}, 41 {"map", args{map[int]string{}}, "[int(64)]string"}, 42 {"chan", args{make(chan int)}, "chan int(64)"}, 43 {"chan", args{make(chan<- bool)}, "chan<- bool"}, 44 {"chan", args{make(<-chan struct{})}, "<-chan struct{ }"}, 45 {"typed nil", args{(*int)(nil)}, "*int(64)"}, 46 } 47 for _, tt := range tests { 48 t.Run(tt.name, func(t *testing.T) { 49 if gotS := PredeclaredType(tt.args.value); gotS != tt.wantS { 50 t.Errorf("PredeclaredType() = %v, want %v", gotS, tt.wantS) 51 } 52 }) 53 } 54 }