Skip to content

Commit 2848ab8

Browse files
timothy-kinggopherbot
authored andcommitted
internal/gcimporter: clean up test expectations
Use (types.Object).String() to write expectations now that the formatting for type parameterized aliases was fixed. This resolves a TODO in TestIExportDataTypeParameterizedAliases. Change-Id: I5b5d55cb7c190686c89dec59bdad30d83de595a5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/616995 Auto-Submit: Tim King <taking@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com>
1 parent ee66134 commit 2848ab8

File tree

1 file changed

+20
-56
lines changed

1 file changed

+20
-56
lines changed

internal/gcimporter/iexport_test.go

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
"golang.org/x/tools/go/gcexportdata"
2626
"golang.org/x/tools/go/packages"
27-
"golang.org/x/tools/internal/aliases"
2827
"golang.org/x/tools/internal/gcimporter"
2928
"golang.org/x/tools/internal/testenv"
3029
)
@@ -440,7 +439,7 @@ func TestIExportDataTypeParameterizedAliases(t *testing.T) {
440439
// * import the data (via either x/tools or GOROOT's gcimporter), and
441440
// * check the imported types.
442441

443-
const src = `package a
442+
const src = `package pkg
444443
445444
type A[T any] = *T
446445
type B[R any, S *R] = []S
@@ -452,12 +451,12 @@ type Chained = C[Named] // B[Named, A[Named]] = B[Named, *Named] = []*Named
452451

453452
// parse and typecheck
454453
fset1 := token.NewFileSet()
455-
f, err := parser.ParseFile(fset1, "a", src, 0)
454+
f, err := parser.ParseFile(fset1, "pkg", src, 0)
456455
if err != nil {
457456
t.Fatal(err)
458457
}
459458
var conf types.Config
460-
pkg1, err := conf.Check("a", fset1, []*ast.File{f}, nil)
459+
pkg1, err := conf.Check("pkg", fset1, []*ast.File{f}, nil)
461460
if err != nil {
462461
t.Fatal(err)
463462
}
@@ -499,7 +498,7 @@ type Chained = C[Named] // B[Named, A[Named]] = B[Named, *Named] = []*Named
499498

500499
// Write export data to temporary file
501500
out := t.TempDir()
502-
name := filepath.Join(out, "a.out")
501+
name := filepath.Join(out, "pkg.out")
503502
if err := os.WriteFile(name+".a", buf.Bytes(), 0644); err != nil {
504503
t.Fatal(err)
505504
}
@@ -514,58 +513,23 @@ type Chained = C[Named] // B[Named, A[Named]] = B[Named, *Named] = []*Named
514513
for name, importer := range testcases {
515514
t.Run(name, func(t *testing.T) {
516515
pkg := importer(t)
516+
for name, want := range map[string]string{
517+
"A": "type pkg.A[T any] = *T",
518+
"B": "type pkg.B[R any, S *R] = []S",
519+
"C": "type pkg.C[U any] = pkg.B[U, pkg.A[U]]",
520+
"Named": "type pkg.Named int",
521+
"Chained": "type pkg.Chained = pkg.C[pkg.Named]",
522+
} {
523+
obj := pkg.Scope().Lookup(name)
524+
if obj == nil {
525+
t.Errorf("failed to find %q in package %s", name, pkg)
526+
continue
527+
}
517528

518-
obj := pkg.Scope().Lookup("A")
519-
if obj == nil {
520-
t.Fatalf("failed to find %q in package %s", "A", pkg)
521-
}
522-
523-
// Check that A is type A[T any] = *T.
524-
// TODO(taking): fix how go/types prints parameterized aliases to simplify tests.
525-
alias, ok := obj.Type().(*types.Alias)
526-
if !ok {
527-
t.Fatalf("Obj %s is not an Alias", obj)
528-
}
529-
530-
targs := aliases.TypeArgs(alias)
531-
if targs.Len() != 0 {
532-
t.Errorf("%s has %d type arguments. expected 0", alias, targs.Len())
533-
}
534-
535-
tparams := aliases.TypeParams(alias)
536-
if tparams.Len() != 1 {
537-
t.Fatalf("%s has %d type arguments. expected 1", alias, targs.Len())
538-
}
539-
tparam := tparams.At(0)
540-
if got, want := tparam.String(), "T"; got != want {
541-
t.Errorf("(%q).TypeParams().At(0)=%q. want %q", alias, got, want)
542-
}
543-
544-
anyt := types.Universe.Lookup("any").Type()
545-
if c := tparam.Constraint(); !types.Identical(anyt, c) {
546-
t.Errorf("(%q).Constraint()=%q. expected %q", tparam, c, anyt)
547-
}
548-
549-
ptparam := types.NewPointer(tparam)
550-
if rhs := aliases.Rhs(alias); !types.Identical(ptparam, rhs) {
551-
t.Errorf("(%q).Rhs()=%q. expected %q", alias, rhs, ptparam)
552-
}
553-
554-
// TODO(taking): add tests for B and C once it is simpler to write tests.
555-
556-
chained := pkg.Scope().Lookup("Chained")
557-
if chained == nil {
558-
t.Fatalf("failed to find %q in package %s", "Chained", pkg)
559-
}
560-
561-
named, _ := pkg.Scope().Lookup("Named").(*types.TypeName)
562-
if named == nil {
563-
t.Fatalf("failed to find %q in package %s", "Named", pkg)
564-
}
565-
566-
want := types.NewSlice(types.NewPointer(named.Type()))
567-
if got := chained.Type(); !types.Identical(got, want) {
568-
t.Errorf("(%q).Type()=%q which should be identical to %q", chained, got, want)
529+
got := strings.ReplaceAll(obj.String(), pkg.Path(), "pkg")
530+
if got != want {
531+
t.Errorf("(%q).String()=%q. wanted %q", name, got, want)
532+
}
569533
}
570534
})
571535
}

0 commit comments

Comments
 (0)