Skip to content

Commit

Permalink
Adding tests for FinalizeRemovals
Browse files Browse the repository at this point in the history
  • Loading branch information
grantnelson-wf committed Jan 14, 2025
1 parent 437309a commit eb95f4d
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/astutil/astutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ func ConcatenateFiles(file *ast.File, tails ...*ast.File) error {
for _, imp := range tail.Imports {
path := imp.Path.Value
if oldImp, ok := imports[path]; ok {
// Import is in both files so check if the import name is different.
// Import is in both files so check if the import name is not different.
if oldName, newName := ImportName(oldImp), ImportName(imp); oldName != newName {
return fmt.Errorf("import from of %q can not be concatenated with different name: %q != %q", path, oldName, newName)
}
Expand Down
197 changes: 197 additions & 0 deletions compiler/astutil/astutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,200 @@ func TestSqueezeIdents(t *testing.T) {
})
}
}

func TestFinalizeRemovals(t *testing.T) {
tests := []struct {
name string
src string
perforator func(f *ast.File)
want string
}{
{
name: `no removals`,
src: `package testpackage
// foo took a journey
func foo() {}
// bar went home
func bar[T any](v T) T { return v }
// baz is a mystery
var baz int = 42`,
perforator: func(f *ast.File) {},
want: `package testpackage
// foo took a journey
func foo() {}
// bar went home
func bar[T any](v T) T { return v }
// baz is a mystery
var baz int = 42`,
},
{
name: `removal first decl`,
src: `package testpackage
// foo took a journey
func foo() {}
// bar went home
func bar[T any](v T) T { return v }
// baz is a mystery
var baz int = 42`,
perforator: func(f *ast.File) {
f.Decls[0] = nil
},
want: `package testpackage
// bar went home
func bar[T any](v T) T { return v }
// baz is a mystery
var baz int = 42`,
},
{
name: `removal middle decl`,
src: `package testpackage
// foo took a journey
func foo() {}
// bar went home
func bar[T any](v T) T { return v }
// baz is a mystery
var baz int = 42`,
perforator: func(f *ast.File) {
f.Decls[1] = nil
},
want: `package testpackage
// foo took a journey
func foo() {}
// baz is a mystery
var baz int = 42`,
},
{
name: `removal last decl`,
src: `package testpackage
// foo took a journey
func foo() {}
// bar went home
func bar[T any](v T) T { return v }
// baz is a mystery
var baz int = 42`,
perforator: func(f *ast.File) {
f.Decls[len(f.Decls)-1] = nil
},
want: `package testpackage
// foo took a journey
func foo() {}
// bar went home
func bar[T any](v T) T { return v }`,
},
{
name: `removal one whole value spec`,
src: `package testpackage
var (
foo string = "foo"
bar, baz int = 42, 36
)`,
perforator: func(f *ast.File) {
f.Decls[0].(*ast.GenDecl).Specs[1] = nil
},
want: `package testpackage
var (
foo string = "foo"
)`,
},
{
name: `removal part of one value spec`,
src: `package testpackage
var (
foo string = "foo"
bar, baz int = 42, 36
)`,
perforator: func(f *ast.File) {
spec := f.Decls[0].(*ast.GenDecl).Specs[1].(*ast.ValueSpec)
spec.Names[1] = nil
spec.Values[1] = nil
},
want: `package testpackage
var (
foo string = "foo"
bar int = 42
)`,
},
{
name: `removal all parts of one value spec`,
src: `package testpackage
var (
foo string = "foo"
bar, baz int = 42, 36
)`,
perforator: func(f *ast.File) {
spec := f.Decls[0].(*ast.GenDecl).Specs[1].(*ast.ValueSpec)
spec.Names[0] = nil
spec.Values[0] = nil
spec.Names[1] = nil
spec.Values[1] = nil
},
want: `package testpackage
var (
foo string = "foo"
)`,
},
{
name: `removal all value specs`,
src: `package testpackage
var (
foo string = "foo"
bar, baz int = 42, 36
)`,
perforator: func(f *ast.File) {
decl := f.Decls[0].(*ast.GenDecl)
decl.Specs[0] = nil
decl.Specs[1] = nil
},
want: `package testpackage`,
},
{
name: `removal one type spec`,
src: `package testpackage
type (
foo interface{ String() string }
bar struct{ baz int }
)`,
perforator: func(f *ast.File) {
decl := f.Decls[0].(*ast.GenDecl)
decl.Specs[0] = nil
},
want: `package testpackage
type (
bar struct{ baz int }
)`,
},
{
name: `removal all type specs`,
src: `package testpackage
type (
foo interface{ String() string }
bar struct{ baz int }
)`,
perforator: func(f *ast.File) {
decl := f.Decls[0].(*ast.GenDecl)
decl.Specs[0] = nil
decl.Specs[1] = nil
},
want: `package testpackage`,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
st := srctesting.New(t)

srcFile := st.Parse("testSrc.go", test.src)
test.perforator(srcFile)
FinalizeRemovals(srcFile)
got := srctesting.Format(t, st.FileSet, srcFile)

// parse and format the expected result so that formatting matches
wantFile := st.Parse("testWant.go", test.want)
want := srctesting.Format(t, st.FileSet, wantFile)

if got != want {
t.Errorf("Unexpected resulting AST:\n\tgot: %q\n\twant: %q", got, want)
}
})
}
}

0 comments on commit eb95f4d

Please sign in to comment.