1
0
mirror of https://github.com/golang/go synced 2024-11-19 02:44:44 -07:00
go/internal/imports/imports_test.go
Suzy Mueller 4cb0cfd181 internal/imports: test exported functions with nil args
Test the api of the internal imports package to make sure that it does
not crash when given nil as opts or Env.

Change-Id: I0127d550a49f63040efb16c07e8cff8b599bbe3c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/190000
Reviewed-by: Heschi Kreinick <heschi@google.com>
2019-08-14 23:42:26 +00:00

75 lines
1.3 KiB
Go

package imports
import (
"go/build"
"testing"
)
// TestNilOpts tests that process does not crash with nil opts.
func TestNilOpts(t *testing.T) {
var testOpts = []struct {
name string
opt *Options
}{
{
name: "nil",
opt: nil,
},
{
name: "nil env",
opt: &Options{Comments: true, TabIndent: true, TabWidth: 8},
},
{
name: "default",
opt: &Options{
Env: &ProcessEnv{
GOPATH: build.Default.GOPATH,
GOROOT: build.Default.GOROOT,
},
Comments: true,
TabIndent: true,
TabWidth: 8,
},
},
}
input := `package p
func _() {
fmt.Println()
}
`
want := `package p
import "fmt"
func _() {
fmt.Println()
}
`
for _, test := range testOpts {
// Test Process
got, err := Process("", []byte(input), test.opt)
if err != nil {
t.Errorf("%s: %s", test.name, err.Error())
}
if string(got) != want {
t.Errorf("%s: Process: Got:\n%s\nWant:\n%s\n", test.name, string(got), want)
}
// Test FixImports and ApplyFixes
fixes, err := FixImports("", []byte(input), test.opt)
if err != nil {
t.Errorf("%s: %s", test.name, err.Error())
}
got, err = ApplyFixes(fixes, "", []byte(input), test.opt)
if err != nil {
t.Errorf("%s: %s", test.name, err.Error())
}
if string(got) != want {
t.Errorf("%s: ApplyFix: Got:\n%s\nWant:\n%s\n", test.name, string(got), want)
}
}
}