github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/compiler/internal/typeparams/utils_test.go (about) 1 package typeparams 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/gopherjs/gopherjs/internal/srctesting" 8 ) 9 10 func TestRequiresGenericsSupport(t *testing.T) { 11 t.Run("generic func", func(t *testing.T) { 12 f := srctesting.New(t) 13 src := `package foo 14 func foo[T any](t T) {}` 15 info, _ := f.Check("pkg/foo", f.Parse("foo.go", src)) 16 17 err := RequiresGenericsSupport(info) 18 if !errors.Is(err, errDefinesGenerics) { 19 t.Errorf("Got: RequiresGenericsSupport() = %v. Want: %v", err, errDefinesGenerics) 20 } 21 }) 22 23 t.Run("generic type", func(t *testing.T) { 24 f := srctesting.New(t) 25 src := `package foo 26 type Foo[T any] struct{t T}` 27 info, _ := f.Check("pkg/foo", f.Parse("foo.go", src)) 28 29 err := RequiresGenericsSupport(info) 30 if !errors.Is(err, errDefinesGenerics) { 31 t.Errorf("Got: RequiresGenericsSupport() = %v. Want: %v", err, errDefinesGenerics) 32 } 33 }) 34 35 t.Run("imported generic instance", func(t *testing.T) { 36 f := srctesting.New(t) 37 f.Info = nil // Do not combine type checking info from different packages. 38 src1 := `package foo 39 type Foo[T any] struct{t T}` 40 f.Check("pkg/foo", f.Parse("foo.go", src1)) 41 42 src2 := `package bar 43 import "pkg/foo" 44 func bar() { _ = foo.Foo[int]{} }` 45 info, _ := f.Check("pkg/bar", f.Parse("bar.go", src2)) 46 47 err := RequiresGenericsSupport(info) 48 if !errors.Is(err, errInstantiatesGenerics) { 49 t.Errorf("Got: RequiresGenericsSupport() = %v. Want: %v", err, errInstantiatesGenerics) 50 } 51 }) 52 53 t.Run("no generic usage", func(t *testing.T) { 54 f := srctesting.New(t) 55 src := `package foo 56 type Foo struct{} 57 func foo() { _ = Foo{} }` 58 info, _ := f.Check("pkg/foo", f.Parse("foo.go", src)) 59 60 err := RequiresGenericsSupport(info) 61 if err != nil { 62 t.Errorf("Got: RequiresGenericsSupport() = %v. Want: nil", err) 63 } 64 }) 65 }