github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/compiler/generics_test.go (about) 1 package compiler_test 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/nspcc-dev/neo-go/pkg/compiler" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestGenericMethodReceiver(t *testing.T) { 12 t.Run("star expression", func(t *testing.T) { 13 src := ` 14 package receiver 15 type Pointer[T any] struct { 16 value T 17 } 18 func (x *Pointer[T]) Load() *T { 19 return &x.value 20 } 21 ` 22 _, _, err := compiler.CompileWithOptions("foo.go", strings.NewReader(src), nil) 23 require.ErrorIs(t, err, compiler.ErrGenericsUnsuppored) 24 }) 25 t.Run("ident expression", func(t *testing.T) { 26 src := ` 27 package receiver 28 type Pointer[T any] struct { 29 value T 30 } 31 func (x Pointer[T]) Load() *T { 32 return &x.value 33 } 34 ` 35 _, _, err := compiler.CompileWithOptions("foo.go", strings.NewReader(src), nil) 36 require.ErrorIs(t, err, compiler.ErrGenericsUnsuppored) 37 }) 38 } 39 40 func TestGenericFuncArgument(t *testing.T) { 41 src := ` 42 package sum 43 func SumInts[V int64 | int32 | int16](vals []V) V { // doesn't make sense with NeoVM, but still it's a valid go code. 44 var s V 45 for i := range vals { 46 s += vals[i] 47 } 48 return s 49 } 50 ` 51 _, _, err := compiler.CompileWithOptions("foo.go", strings.NewReader(src), nil) 52 require.ErrorIs(t, err, compiler.ErrGenericsUnsuppored) 53 } 54 55 func TestGenericTypeDecl(t *testing.T) { 56 t.Run("global scope", func(t *testing.T) { 57 src := ` 58 package sum 59 type List[T any] struct { 60 next *List[T] 61 val T 62 } 63 64 func Main() any { 65 l := List[int]{} 66 return l 67 } 68 ` 69 _, _, err := compiler.CompileWithOptions("foo.go", strings.NewReader(src), nil) 70 require.ErrorIs(t, err, compiler.ErrGenericsUnsuppored) 71 }) 72 t.Run("local scope", func(t *testing.T) { 73 src := ` 74 package sum 75 func Main() any { 76 type ( 77 SomeGoodType int 78 79 List[T any] struct { 80 next *List[T] 81 val T 82 } 83 ) 84 l := List[int]{} 85 return l 86 } 87 ` 88 _, _, err := compiler.CompileWithOptions("foo.go", strings.NewReader(src), nil) 89 require.ErrorIs(t, err, compiler.ErrGenericsUnsuppored) 90 }) 91 }